Skip to content

Commit f409e7a

Browse files
authored
api: Update DNS on changing VM name (#5425)
* api: Update DNS on changing VM name * refactor backend code + UI - add warning messaging to prompt users to restart VM in case particular fields are updated * address comments
1 parent 3c2360c commit f409e7a

4 files changed

Lines changed: 57 additions & 7 deletions

File tree

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import javax.xml.parsers.DocumentBuilderFactory;
5151
import javax.xml.parsers.ParserConfigurationException;
5252

53+
import com.cloud.network.router.CommandSetupHelper;
54+
import com.cloud.network.router.NetworkHelper;
5355
import org.apache.cloudstack.acl.ControlledEntity;
5456
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
5557
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -116,6 +118,8 @@
116118
import org.apache.commons.collections.MapUtils;
117119
import org.apache.commons.lang3.StringUtils;
118120
import org.apache.log4j.Logger;
121+
import org.springframework.beans.factory.annotation.Autowired;
122+
import org.springframework.beans.factory.annotation.Qualifier;
119123
import org.w3c.dom.Document;
120124
import org.w3c.dom.Element;
121125
import org.w3c.dom.NodeList;
@@ -536,6 +540,11 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
536540
private BackupManager backupManager;
537541
@Inject
538542
private AnnotationDao annotationDao;
543+
@Inject
544+
protected CommandSetupHelper commandSetupHelper;
545+
@Autowired
546+
@Qualifier("networkHelper")
547+
protected NetworkHelper nwHelper;
539548

540549
private ScheduledExecutorService _executor = null;
541550
private ScheduledExecutorService _vmIpFetchExecutor = null;
@@ -2903,17 +2912,55 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
29032912
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName);
29042913

29052914
if (updateUserdata) {
2906-
boolean result = updateUserDataInternal(_vmDao.findById(id));
2907-
if (result) {
2908-
s_logger.debug("User data successfully updated for vm id=" + id);
2909-
} else {
2910-
throw new CloudRuntimeException("Failed to reset userdata for the virtual machine ");
2911-
}
2915+
updateUserData(vm);
2916+
}
2917+
2918+
if (State.Running == vm.getState()) {
2919+
updateDns(vm, hostName);
29122920
}
29132921

29142922
return _vmDao.findById(id);
29152923
}
29162924

2925+
private void updateUserData(UserVm vm) throws ResourceUnavailableException, InsufficientCapacityException {
2926+
boolean result = updateUserDataInternal(vm);
2927+
if (result) {
2928+
s_logger.debug(String.format("User data successfully updated for vm id: %s", vm.getId()));
2929+
} else {
2930+
throw new CloudRuntimeException("Failed to reset userdata for the virtual machine ");
2931+
}
2932+
}
2933+
2934+
private void updateDns(UserVmVO vm, String hostName) throws ResourceUnavailableException, InsufficientCapacityException {
2935+
if (!StringUtils.isEmpty(hostName)) {
2936+
vm.setHostName(hostName);
2937+
try {
2938+
List<NicVO> nicVOs = _nicDao.listByVmId(vm.getId());
2939+
for (NicVO nic : nicVOs) {
2940+
List<DomainRouterVO> routers = _routerDao.findByNetwork(nic.getNetworkId());
2941+
for (DomainRouterVO router : routers) {
2942+
if (router.getState() != State.Running) {
2943+
s_logger.warn(String.format("Unable to update DNS for VM %s, as virtual router: %s is not in the right state: %s ", vm, router.getName(), router.getState()));
2944+
continue;
2945+
}
2946+
Commands commands = new Commands(Command.OnError.Stop);
2947+
commandSetupHelper.createDhcpEntryCommand(router, vm, nic, false, commands);
2948+
if (!nwHelper.sendCommandsToRouter(router, commands)) {
2949+
throw new CloudRuntimeException(String.format("Unable to send commands to virtual router: %s", router.getHostId()));
2950+
}
2951+
Answer answer = commands.getAnswer("dhcp");
2952+
if (answer == null || !answer.getResult()) {
2953+
throw new CloudRuntimeException("Failed to update hostname");
2954+
}
2955+
updateUserData(vm);
2956+
}
2957+
}
2958+
} catch (CloudRuntimeException e) {
2959+
throw new CloudRuntimeException(String.format("Failed to update hostname of VM %s to %s", vm.getInstanceName(), vm.getHostName()));
2960+
}
2961+
}
2962+
}
2963+
29172964
private boolean updateUserDataInternal(UserVm vm) throws ResourceUnavailableException, InsufficientCapacityException {
29182965
VMTemplateVO template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId());
29192966

ui/public/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,6 +3170,7 @@
31703170
"message.restart.mgmt.server": "Please restart your management server(s) for your new settings to take effect.",
31713171
"message.restart.mgmt.usage.server": "Please restart your management server(s) and usage server(s) for your new settings to take effect.",
31723172
"message.restart.network": "All services provided by this network will be interrupted. Please confirm that you want to restart this network.",
3173+
"message.restart.vm.to.update.settings": "Update in fields other than Name and Display Name will require the VM to be restarted",
31733174
"message.restart.vpc": "Please confirm that you want to restart the VPC",
31743175
"message.restart.vpc.remark": "Please confirm that you want to restart the VPC <p><i>Remark: making a non-redundant VPC redundant will force a clean up. The networks will not be available for a couple of minutes</i>.</p>",
31753176
"message.restorevm": "Do you want to restore the VM ?",

ui/src/config/section/compute.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export default {
8686
docHelp: 'adminguide/virtual_machines.html#changing-the-vm-name-os-or-group',
8787
dataView: true,
8888
popup: true,
89-
show: (record) => { return ['Stopped'].includes(record.state) },
9089
component: () => import('@/views/compute/EditVM.vue')
9190
},
9291
{

ui/src/views/compute/EditVM.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
:form="form"
2424
v-ctrl-enter="handleSubmit"
2525
@submit="handleSubmit">
26+
<a-alert style="margin-bottom: 5px" type="warning" show-icon>
27+
<span slot="message" v-html="$t('message.restart.vm.to.update.settings')" />
28+
</a-alert>
2629
<a-form-item>
2730
<tooltip-label slot="label" :title="$t('label.name')" :tooltip="apiParams.name.description"/>
2831
<a-input

0 commit comments

Comments
 (0)