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 ;
@@ -1973,7 +1976,9 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19731976
19741977 protected String getTrimmedHostNameForWindows (String name ) {
19751978 String valid = name .replaceAll ("[^a-zA-Z0-9-]" , "" );
1976- String hostName = valid .length () > 15 ? valid .substring (valid .length () - 15 ) : valid ;
1979+ String hostName = valid .length () > MAX_WINDOWS_VM_HOSTNAME_LENGTH ?
1980+ valid .substring (valid .length () - MAX_WINDOWS_VM_HOSTNAME_LENGTH ) :
1981+ valid ;
19771982 if (!hostName .isEmpty () && !Character .isLetter (hostName .charAt (0 ))) {
19781983 for (int i = 0 ; i < hostName .length (); i ++) {
19791984 if (Character .isLetter (hostName .charAt (i ))) {
@@ -1982,7 +1987,7 @@ protected String getTrimmedHostNameForWindows(String name) {
19821987 }
19831988 }
19841989 if (!Character .isLetter (hostName .charAt (0 ))) {
1985- hostName = hostName .length () < 15 ? "a" + hostName : "a" + hostName . substring ( 1 );
1990+ hostName = "a" + hostName .substring ( hostName . length () < MAX_WINDOWS_VM_HOSTNAME_LENGTH ? 0 : 1 );
19861991 }
19871992 }
19881993 return hostName ;
@@ -2007,7 +2012,7 @@ protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO as
20072012 // Truncate vm group name because max length of vm name is 63
20082013 int subStringLength = Math .min (asGroup .getName ().length (), 63 - VM_HOSTNAME_PREFIX .length () - vmHostNameSuffix .length ());
20092014 String name = VM_HOSTNAME_PREFIX + asGroup .getName ().substring (0 , subStringLength ) + vmHostNameSuffix ;
2010- if (!isWindows || name .length () <= 15 ) {
2015+ if (!isWindows || name .length () <= MAX_WINDOWS_VM_HOSTNAME_LENGTH ) {
20112016 return new Pair <>(name , name );
20122017 }
20132018 return new Pair <>(getTrimmedHostNameForWindows (name ), name );
0 commit comments