195195import com .google .gson .reflect .TypeToken ;
196196
197197public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManager , AutoScaleService , Configurable {
198+ // Windows OS has a limit of 15 characters for hostname
199+ // https://learn.microsoft.com/en-us/troubleshoot/windows-server/active-directory/naming-conventions-for-computer-domain-site-ou
200+ protected static final int MAX_WINDOWS_VM_HOSTNAME_LENGTH = 15 ;
198201
199202 @ Inject
200203 protected DispatchChainFactory dispatchChainFactory = null ;
@@ -1962,7 +1965,9 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19621965
19631966 protected String getTrimmedHostNameForWindows (String name ) {
19641967 String valid = name .replaceAll ("[^a-zA-Z0-9-]" , "" );
1965- String hostName = valid .length () > 15 ? valid .substring (valid .length () - 15 ) : valid ;
1968+ String hostName = valid .length () > MAX_WINDOWS_VM_HOSTNAME_LENGTH ?
1969+ valid .substring (valid .length () - MAX_WINDOWS_VM_HOSTNAME_LENGTH ) :
1970+ valid ;
19661971 if (!hostName .isEmpty () && !Character .isLetter (hostName .charAt (0 ))) {
19671972 for (int i = 0 ; i < hostName .length (); i ++) {
19681973 if (Character .isLetter (hostName .charAt (i ))) {
@@ -1971,7 +1976,7 @@ protected String getTrimmedHostNameForWindows(String name) {
19711976 }
19721977 }
19731978 if (!Character .isLetter (hostName .charAt (0 ))) {
1974- hostName = hostName .length () < 15 ? "a" + hostName : "a" + hostName . substring ( 1 );
1979+ hostName = "a" + hostName .substring ( hostName . length () < MAX_WINDOWS_VM_HOSTNAME_LENGTH ? 0 : 1 );
19751980 }
19761981 }
19771982 return hostName ;
@@ -1996,7 +2001,7 @@ protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO as
19962001 // Truncate vm group name because max length of vm name is 63
19972002 int subStringLength = Math .min (asGroup .getName ().length (), 63 - VM_HOSTNAME_PREFIX .length () - vmHostNameSuffix .length ());
19982003 String name = VM_HOSTNAME_PREFIX + asGroup .getName ().substring (0 , subStringLength ) + vmHostNameSuffix ;
1999- if (!isWindows || name .length () <= 15 ) {
2004+ if (!isWindows || name .length () <= MAX_WINDOWS_VM_HOSTNAME_LENGTH ) {
20002005 return new Pair <>(name , name );
20012006 }
20022007 return new Pair <>(getTrimmedHostNameForWindows (name ), name );
0 commit comments