|
50 | 50 | import javax.xml.parsers.DocumentBuilderFactory; |
51 | 51 | import javax.xml.parsers.ParserConfigurationException; |
52 | 52 |
|
| 53 | +import com.cloud.network.router.CommandSetupHelper; |
| 54 | +import com.cloud.network.router.NetworkHelper; |
53 | 55 | import org.apache.cloudstack.acl.ControlledEntity; |
54 | 56 | import org.apache.cloudstack.acl.ControlledEntity.ACLType; |
55 | 57 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
|
116 | 118 | import org.apache.commons.collections.MapUtils; |
117 | 119 | import org.apache.commons.lang3.StringUtils; |
118 | 120 | import org.apache.log4j.Logger; |
| 121 | +import org.springframework.beans.factory.annotation.Autowired; |
| 122 | +import org.springframework.beans.factory.annotation.Qualifier; |
119 | 123 | import org.w3c.dom.Document; |
120 | 124 | import org.w3c.dom.Element; |
121 | 125 | import org.w3c.dom.NodeList; |
@@ -536,6 +540,11 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir |
536 | 540 | private BackupManager backupManager; |
537 | 541 | @Inject |
538 | 542 | private AnnotationDao annotationDao; |
| 543 | + @Inject |
| 544 | + protected CommandSetupHelper commandSetupHelper; |
| 545 | + @Autowired |
| 546 | + @Qualifier("networkHelper") |
| 547 | + protected NetworkHelper nwHelper; |
539 | 548 |
|
540 | 549 | private ScheduledExecutorService _executor = null; |
541 | 550 | private ScheduledExecutorService _vmIpFetchExecutor = null; |
@@ -2903,17 +2912,55 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo |
2903 | 2912 | _vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName); |
2904 | 2913 |
|
2905 | 2914 | 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); |
2912 | 2920 | } |
2913 | 2921 |
|
2914 | 2922 | return _vmDao.findById(id); |
2915 | 2923 | } |
2916 | 2924 |
|
| 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 | + |
2917 | 2964 | private boolean updateUserDataInternal(UserVm vm) throws ResourceUnavailableException, InsufficientCapacityException { |
2918 | 2965 | VMTemplateVO template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId()); |
2919 | 2966 |
|
|
0 commit comments