|
38 | 38 |
|
39 | 39 | import javax.inject.Inject; |
40 | 40 |
|
41 | | -import com.cloud.network.NetworkModel; |
42 | 41 | import org.apache.cloudstack.acl.ControlledEntity; |
43 | 42 | import org.apache.cloudstack.affinity.AffinityGroupVO; |
44 | 43 | import org.apache.cloudstack.affinity.dao.AffinityGroupDao; |
|
113 | 112 | import com.cloud.network.Network; |
114 | 113 | import com.cloud.network.Network.Capability; |
115 | 114 | import com.cloud.network.Network.IpAddresses; |
| 115 | +import com.cloud.network.NetworkModel; |
116 | 116 | import com.cloud.network.as.AutoScaleCounter.AutoScaleCounterParam; |
117 | 117 | import com.cloud.network.as.dao.AutoScalePolicyConditionMapDao; |
118 | 118 | import com.cloud.network.as.dao.AutoScalePolicyDao; |
|
146 | 146 | import com.cloud.server.ResourceTag; |
147 | 147 | import com.cloud.service.ServiceOfferingVO; |
148 | 148 | import com.cloud.service.dao.ServiceOfferingDao; |
| 149 | +import com.cloud.storage.GuestOSVO; |
149 | 150 | import com.cloud.storage.dao.DiskOfferingDao; |
| 151 | +import com.cloud.storage.dao.GuestOSDao; |
150 | 152 | import com.cloud.template.TemplateManager; |
151 | 153 | import com.cloud.template.VirtualMachineTemplate; |
152 | 154 | import com.cloud.user.Account; |
@@ -280,6 +282,8 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManage |
280 | 282 | private NetworkOfferingDao networkOfferingDao; |
281 | 283 | @Inject |
282 | 284 | private VirtualMachineManager virtualMachineManager; |
| 285 | + @Inject |
| 286 | + GuestOSDao guestOSDao; |
283 | 287 |
|
284 | 288 | private static final String PARAM_ROOT_DISK_SIZE = "rootdisksize"; |
285 | 289 | private static final String PARAM_DISK_OFFERING_ID = "diskofferingid"; |
@@ -1805,26 +1809,28 @@ protected UserVm createNewVM(AutoScaleVmGroupVO asGroup) { |
1805 | 1809 | List<Long> affinityGroupIdList = getVmAffinityGroupId(deployParams); |
1806 | 1810 | updateVmDetails(deployParams, customParameters); |
1807 | 1811 |
|
1808 | | - String vmHostName = getNextVmHostName(asGroup); |
| 1812 | + Pair<String, String> vmHostAndDisplayName = getNextVmHostAndDisplayName(asGroup, template); |
| 1813 | + String vmHostName = vmHostAndDisplayName.first(); |
| 1814 | + String vmDisplayName = vmHostAndDisplayName.second(); |
1809 | 1815 | asGroup.setNextVmSeq(asGroup.getNextVmSeq() + 1); |
1810 | 1816 | autoScaleVmGroupDao.persist(asGroup); |
1811 | 1817 |
|
1812 | 1818 | if (zone.getNetworkType() == NetworkType.Basic) { |
1813 | 1819 | vm = userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, null, owner, vmHostName, |
1814 | | - vmHostName, diskOfferingId, dataDiskSize, null, |
| 1820 | + vmDisplayName, diskOfferingId, dataDiskSize, null, |
1815 | 1821 | hypervisorType, HTTPMethod.GET, userData, userDataId, userDataDetails, sshKeyPairs, |
1816 | 1822 | null, null, true, null, affinityGroupIdList, customParameters, null, null, null, |
1817 | 1823 | null, true, overrideDiskOfferingId, null, null); |
1818 | 1824 | } else { |
1819 | 1825 | if (networkModel.checkSecurityGroupSupportForNetwork(owner, zone, networkIds, |
1820 | 1826 | Collections.emptyList())) { |
1821 | 1827 | vm = userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, networkIds, null, |
1822 | | - owner, vmHostName,vmHostName, diskOfferingId, dataDiskSize, null, |
| 1828 | + owner, vmHostName, vmDisplayName, diskOfferingId, dataDiskSize, null, |
1823 | 1829 | hypervisorType, HTTPMethod.GET, userData, userDataId, userDataDetails, sshKeyPairs, |
1824 | 1830 | null, null, true, null, affinityGroupIdList, customParameters, null, null, null, |
1825 | 1831 | null, true, overrideDiskOfferingId, null, null, null); |
1826 | 1832 | } else { |
1827 | | - vm = userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, networkIds, owner, vmHostName, vmHostName, |
| 1833 | + vm = userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, networkIds, owner, vmHostName, vmDisplayName, |
1828 | 1834 | diskOfferingId, dataDiskSize, null, |
1829 | 1835 | hypervisorType, HTTPMethod.GET, userData, userDataId, userDataDetails, sshKeyPairs, |
1830 | 1836 | null, addrs, true, null, affinityGroupIdList, customParameters, null, null, null, |
@@ -1949,13 +1955,20 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String |
1949 | 1955 | } |
1950 | 1956 | } |
1951 | 1957 |
|
1952 | | - @Override |
1953 | | - public String getNextVmHostName(AutoScaleVmGroupVO asGroup) { |
| 1958 | + protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO asGroup, VirtualMachineTemplate template) { |
| 1959 | + template.getGuestOSId(); |
| 1960 | + GuestOSVO guestOSVO = guestOSDao.findById(template.getGuestOSId()); |
| 1961 | + boolean isWindows = guestOSVO != null && guestOSVO.getDisplayName().toLowerCase().contains("windows"); |
1954 | 1962 | String vmHostNameSuffix = "-" + asGroup.getNextVmSeq() + "-" + |
1955 | 1963 | RandomStringUtils.random(VM_HOSTNAME_RANDOM_SUFFIX_LENGTH, 0, 0, true, false, (char[])null, new SecureRandom()).toLowerCase(); |
1956 | 1964 | // Truncate vm group name because max length of vm name is 63 |
1957 | 1965 | int subStringLength = Math.min(asGroup.getName().length(), 63 - VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length()); |
1958 | | - return VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix; |
| 1966 | + String displayName = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix; |
| 1967 | + String hostName = displayName; |
| 1968 | + if (isWindows) { |
| 1969 | + hostName = displayName.substring(Math.max(0, displayName.length() - 15)); |
| 1970 | + } |
| 1971 | + return new Pair<>(hostName, displayName); |
1959 | 1972 | } |
1960 | 1973 |
|
1961 | 1974 | @Override |
|
0 commit comments