Skip to content

Commit 96897f0

Browse files
Set hypervisor hostname via userdata only when the global settings are allowed (#7360)
* Set hypervisor hostname via userdata only when the corresponding global settings are enabled * reformat code
1 parent d871729 commit 96897f0

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
import com.cloud.vm.UserVmVO;
237237
import com.cloud.vm.VMInstanceVO;
238238
import com.cloud.vm.VirtualMachine;
239+
import com.cloud.vm.VirtualMachineManager;
239240
import com.cloud.vm.VirtualMachine.Type;
240241
import com.cloud.vm.VirtualMachineProfile;
241242
import com.cloud.vm.dao.DomainRouterDao;
@@ -1811,20 +1812,32 @@ public void finalizeUpdateInSequence(Network network, boolean success) {
18111812

18121813
@Override
18131814
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);
18271838
}
1839+
} catch (ResourceUnavailableException e) {
1840+
s_logger.error(String.format("%s, error states %s", errorMsg, e));
18281841
}
18291842
}
18301843
}

0 commit comments

Comments
 (0)