|
236 | 236 | import com.cloud.vm.UserVmVO; |
237 | 237 | import com.cloud.vm.VMInstanceVO; |
238 | 238 | import com.cloud.vm.VirtualMachine; |
| 239 | +import com.cloud.vm.VirtualMachineManager; |
239 | 240 | import com.cloud.vm.VirtualMachine.Type; |
240 | 241 | import com.cloud.vm.VirtualMachineProfile; |
241 | 242 | import com.cloud.vm.dao.DomainRouterDao; |
@@ -1811,20 +1812,32 @@ public void finalizeUpdateInSequence(Network network, boolean success) { |
1811 | 1812 |
|
1812 | 1813 | @Override |
1813 | 1814 | public void setHypervisorHostname(VirtualMachineProfile vm, DeployDestination dest, boolean migrationSuccessful) throws ResourceUnavailableException { |
1814 | | - final List<NicVO> nics = _nicDao.listByVmId(vm.getId()); |
1815 | | - for (final NicVO nic : nics) { |
1816 | | - final NetworkVO network = _networksDao.findById(nic.getNetworkId()); |
1817 | | - final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId()); |
1818 | | - final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), |
1819 | | - _networkModel.getNetworkTag(vm.getHypervisorType(), network)); |
1820 | | - for (final NetworkElement element : networkElements) { |
1821 | | - if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.UserData) && element instanceof UserDataServiceProvider) { |
1822 | | - if (element instanceof ConfigDriveNetworkElement && !migrationSuccessful || element instanceof VirtualRouterElement && migrationSuccessful) { |
1823 | | - final UserDataServiceProvider sp = (UserDataServiceProvider) element; |
1824 | | - if (!sp.saveHypervisorHostname(profile, network, vm, dest)) { |
1825 | | - throw new CloudRuntimeException("Failed to Add hypervisor hostname"); |
1826 | | - } |
| 1815 | + String hypervisorHostName = VirtualMachineManager.getHypervisorHostname(dest.getHost().getName()); |
| 1816 | + if (StringUtils.isNotEmpty(hypervisorHostName)) { |
| 1817 | + final List<NicVO> nics = _nicDao.listByVmId(vm.getId()); |
| 1818 | + for (final NicVO nic : nics) { |
| 1819 | + final NetworkVO network = _networksDao.findById(nic.getNetworkId()); |
| 1820 | + final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId()); |
| 1821 | + final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), |
| 1822 | + _networkModel.getNetworkTag(vm.getHypervisorType(), network)); |
| 1823 | + setHypervisorHostnameInNetwork(vm, dest, network, profile, migrationSuccessful); |
| 1824 | + } |
| 1825 | + } |
| 1826 | + } |
| 1827 | + |
| 1828 | + private void setHypervisorHostnameInNetwork(VirtualMachineProfile vm, DeployDestination dest, Network network, NicProfile profile, boolean migrationSuccessful) { |
| 1829 | + for (final NetworkElement element : networkElements) { |
| 1830 | + if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.UserData) && element instanceof UserDataServiceProvider |
| 1831 | + && (element instanceof ConfigDriveNetworkElement && !migrationSuccessful || element instanceof VirtualRouterElement && migrationSuccessful)) { |
| 1832 | + String errorMsg = String.format("Failed to add hypervisor host name while applying the userdata during the migration of VM %s, " + |
| 1833 | + "VM needs to stop and start to apply the userdata again", vm.getInstanceName()); |
| 1834 | + try { |
| 1835 | + final UserDataServiceProvider sp = (UserDataServiceProvider) element; |
| 1836 | + if (!sp.saveHypervisorHostname(profile, network, vm, dest)) { |
| 1837 | + s_logger.error(errorMsg); |
1827 | 1838 | } |
| 1839 | + } catch (ResourceUnavailableException e) { |
| 1840 | + s_logger.error(String.format("%s, error states %s", errorMsg, e)); |
1828 | 1841 | } |
1829 | 1842 | } |
1830 | 1843 | } |
|
0 commit comments