3838
3939import javax .inject .Inject ;
4040
41- import com .cloud .network .NetworkModel ;
4241import org .apache .cloudstack .acl .ControlledEntity ;
4342import org .apache .cloudstack .affinity .AffinityGroupVO ;
4443import org .apache .cloudstack .affinity .dao .AffinityGroupDao ;
113112import com .cloud .network .Network ;
114113import com .cloud .network .Network .Capability ;
115114import com .cloud .network .Network .IpAddresses ;
115+ import com .cloud .network .NetworkModel ;
116116import com .cloud .network .as .AutoScaleCounter .AutoScaleCounterParam ;
117117import com .cloud .network .as .dao .AutoScalePolicyConditionMapDao ;
118118import com .cloud .network .as .dao .AutoScalePolicyDao ;
146146import com .cloud .server .ResourceTag ;
147147import com .cloud .service .ServiceOfferingVO ;
148148import com .cloud .service .dao .ServiceOfferingDao ;
149+ import com .cloud .storage .GuestOSVO ;
149150import com .cloud .storage .dao .DiskOfferingDao ;
151+ import com .cloud .storage .dao .GuestOSDao ;
150152import com .cloud .template .TemplateManager ;
151153import com .cloud .template .VirtualMachineTemplate ;
152154import com .cloud .user .Account ;
@@ -280,6 +282,8 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManage
280282 private NetworkOfferingDao networkOfferingDao ;
281283 @ Inject
282284 private VirtualMachineManager virtualMachineManager ;
285+ @ Inject
286+ GuestOSDao guestOSDao ;
283287
284288 private static final String PARAM_ROOT_DISK_SIZE = "rootdisksize" ;
285289 private static final String PARAM_DISK_OFFERING_ID = "diskofferingid" ;
@@ -296,6 +300,10 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManage
296300 protected static final String VM_HOSTNAME_PREFIX = "autoScaleVm-" ;
297301 protected static final int VM_HOSTNAME_RANDOM_SUFFIX_LENGTH = 6 ;
298302
303+ // Windows OS has a limit of 15 characters for hostname
304+ // https://learn.microsoft.com/en-us/troubleshoot/windows-server/active-directory/naming-conventions-for-computer-domain-site-ou
305+ protected static final String WINDOWS_VM_HOSTNAME_PREFIX = "as-WinVm-" ;
306+
299307 private static final Long DEFAULT_HOST_ID = -1L ;
300308
301309 ExecutorService groupExecutor ;
@@ -1818,26 +1826,28 @@ protected UserVm createNewVM(AutoScaleVmGroupVO asGroup) {
18181826 List <Long > affinityGroupIdList = getVmAffinityGroupId (deployParams );
18191827 updateVmDetails (deployParams , customParameters );
18201828
1821- String vmHostName = getNextVmHostName (asGroup );
1829+ Pair <String , String > vmHostAndDisplayName = getNextVmHostAndDisplayName (asGroup , template );
1830+ String vmHostName = vmHostAndDisplayName .first ();
1831+ String vmDisplayName = vmHostAndDisplayName .second ();
18221832 asGroup .setNextVmSeq (asGroup .getNextVmSeq () + 1 );
18231833 autoScaleVmGroupDao .persist (asGroup );
18241834
18251835 if (zone .getNetworkType () == NetworkType .Basic ) {
18261836 vm = userVmService .createBasicSecurityGroupVirtualMachine (zone , serviceOffering , template , null , owner , vmHostName ,
1827- vmHostName , diskOfferingId , dataDiskSize , null , null ,
1837+ vmDisplayName , diskOfferingId , dataDiskSize , null , null ,
18281838 hypervisorType , HTTPMethod .GET , userData , userDataId , userDataDetails , sshKeyPairs ,
18291839 null , null , true , null , affinityGroupIdList , customParameters , null , null , null ,
18301840 null , true , overrideDiskOfferingId , null , null );
18311841 } else {
18321842 if (networkModel .checkSecurityGroupSupportForNetwork (owner , zone , networkIds ,
18331843 Collections .emptyList ())) {
18341844 vm = userVmService .createAdvancedSecurityGroupVirtualMachine (zone , serviceOffering , template , networkIds , null ,
1835- owner , vmHostName ,vmHostName , diskOfferingId , dataDiskSize , null , null ,
1845+ owner , vmHostName , vmDisplayName , diskOfferingId , dataDiskSize , null , null ,
18361846 hypervisorType , HTTPMethod .GET , userData , userDataId , userDataDetails , sshKeyPairs ,
18371847 null , null , true , null , affinityGroupIdList , customParameters , null , null , null ,
18381848 null , true , overrideDiskOfferingId , null , null , null );
18391849 } else {
1840- vm = userVmService .createAdvancedVirtualMachine (zone , serviceOffering , template , networkIds , owner , vmHostName , vmHostName ,
1850+ vm = userVmService .createAdvancedVirtualMachine (zone , serviceOffering , template , networkIds , owner , vmHostName , vmDisplayName ,
18411851 diskOfferingId , dataDiskSize , null , null ,
18421852 hypervisorType , HTTPMethod .GET , userData , userDataId , userDataDetails , sshKeyPairs ,
18431853 null , addrs , true , null , affinityGroupIdList , customParameters , null , null , null ,
@@ -1962,13 +1972,29 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19621972 }
19631973 }
19641974
1965- @ Override
1966- public String getNextVmHostName (AutoScaleVmGroupVO asGroup ) {
1967- String vmHostNameSuffix = "-" + asGroup .getNextVmSeq () + "-" +
1968- RandomStringUtils .random (VM_HOSTNAME_RANDOM_SUFFIX_LENGTH , 0 , 0 , true , false , (char [])null , new SecureRandom ()).toLowerCase ();
1975+ protected boolean isWindowsOs (VirtualMachineTemplate template ) {
1976+ GuestOSVO guestOSVO = guestOSDao .findById (template .getGuestOSId ());
1977+ if (guestOSVO == null ) {
1978+ return false ;
1979+ }
1980+ String osName = StringUtils .firstNonBlank (guestOSVO .getName (), guestOSVO .getDisplayName ());
1981+ if (StringUtils .isBlank (osName )) {
1982+ return false ;
1983+ }
1984+ return osName .toLowerCase ().contains ("windows" );
1985+ }
1986+
1987+ protected Pair <String , String > getNextVmHostAndDisplayName (AutoScaleVmGroupVO asGroup , VirtualMachineTemplate template ) {
1988+ boolean isWindows = isWindowsOs (template );
1989+ String winVmHostNameSuffix = RandomStringUtils .random (VM_HOSTNAME_RANDOM_SUFFIX_LENGTH , 0 , 0 , true , false , (char [])null , new SecureRandom ()).toLowerCase ();
1990+ String vmHostNameSuffix = "-" + asGroup .getNextVmSeq () + "-" + winVmHostNameSuffix ;
19691991 // Truncate vm group name because max length of vm name is 63
19701992 int subStringLength = Math .min (asGroup .getName ().length (), 63 - VM_HOSTNAME_PREFIX .length () - vmHostNameSuffix .length ());
1971- return VM_HOSTNAME_PREFIX + asGroup .getName ().substring (0 , subStringLength ) + vmHostNameSuffix ;
1993+ String name = VM_HOSTNAME_PREFIX + asGroup .getName ().substring (0 , subStringLength ) + vmHostNameSuffix ;
1994+ if (!isWindows ) {
1995+ return new Pair <>(name , name );
1996+ }
1997+ return new Pair <>(WINDOWS_VM_HOSTNAME_PREFIX + winVmHostNameSuffix , name );
19721998 }
19731999
19742000 @ Override
0 commit comments