vishesh92 commented on code in PR #11327:
URL: https://github.com/apache/cloudstack/pull/11327#discussion_r2394158431
##########
server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java:
##########
@@ -1954,13 +1960,46 @@ public void updateVmDetails(Map<String, String>
deployParams, Map<String, String
}
}
- @Override
- public String getNextVmHostName(AutoScaleVmGroupVO asGroup) {
+ protected String getTrimmedHostNameForWindows(String name) {
+ String valid = name.replaceAll("[^a-zA-Z0-9-]", "");
+ String hostName = valid.length() > 15 ? valid.substring(valid.length()
- 15) : valid;
+ if (!hostName.isEmpty() && !Character.isLetter(hostName.charAt(0))) {
+ for (int i = 0; i < hostName.length(); i++) {
+ if (Character.isLetter(hostName.charAt(i))) {
+ hostName = hostName.charAt(i) + hostName.substring(i + 1);
+ break;
+ }
+ }
+ if (!Character.isLetter(hostName.charAt(0))) {
+ hostName = hostName.length() < 15 ? "a" + hostName : "a" +
hostName.substring(1);
+ }
+ }
+ return hostName;
+ }
+
+ protected boolean isWindowsOs(VirtualMachineTemplate template) {
+ GuestOSVO guestOSVO = guestOSDao.findById(template.getGuestOSId());
+ if (guestOSVO == null) {
+ return false;
+ }
+ String osName = StringUtils.firstNonBlank(guestOSVO.getName(),
guestOSVO.getDisplayName());
+ if (StringUtils.isBlank(osName)) {
+ return false;
+ }
+ return osName.toLowerCase().contains("windows");
+ }
+
+ protected Pair<String, String>
getNextVmHostAndDisplayName(AutoScaleVmGroupVO asGroup, VirtualMachineTemplate
template) {
+ boolean isWindows = isWindowsOs(template);
String vmHostNameSuffix = "-" + asGroup.getNextVmSeq() + "-" +
RandomStringUtils.random(VM_HOSTNAME_RANDOM_SUFFIX_LENGTH, 0,
0, true, false, (char[])null, new SecureRandom()).toLowerCase();
// Truncate vm group name because max length of vm name is 63
int subStringLength = Math.min(asGroup.getName().length(), 63 -
VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length());
- return VM_HOSTNAME_PREFIX + asGroup.getName().substring(0,
subStringLength) + vmHostNameSuffix;
+ String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0,
subStringLength) + vmHostNameSuffix;
Review Comment:
Value for `VM_HOSTNAME_PREFIX` is `autoScaleVm-` which is 12 characters
leaving only 3 characters for the rest of the string. If we assume the ASG name
is 3 letters only, that itself will become 15 characters and won't be unique
for VMs.
##########
server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java:
##########
@@ -1954,13 +1960,46 @@ public void updateVmDetails(Map<String, String>
deployParams, Map<String, String
}
}
- @Override
- public String getNextVmHostName(AutoScaleVmGroupVO asGroup) {
+ protected String getTrimmedHostNameForWindows(String name) {
+ String valid = name.replaceAll("[^a-zA-Z0-9-]", "");
+ String hostName = valid.length() > 15 ? valid.substring(valid.length()
- 15) : valid;
+ if (!hostName.isEmpty() && !Character.isLetter(hostName.charAt(0))) {
+ for (int i = 0; i < hostName.length(); i++) {
+ if (Character.isLetter(hostName.charAt(i))) {
+ hostName = hostName.charAt(i) + hostName.substring(i + 1);
+ break;
+ }
+ }
+ if (!Character.isLetter(hostName.charAt(0))) {
+ hostName = hostName.length() < 15 ? "a" + hostName : "a" +
hostName.substring(1);
+ }
+ }
+ return hostName;
+ }
+
+ protected boolean isWindowsOs(VirtualMachineTemplate template) {
+ GuestOSVO guestOSVO = guestOSDao.findById(template.getGuestOSId());
+ if (guestOSVO == null) {
+ return false;
+ }
+ String osName = StringUtils.firstNonBlank(guestOSVO.getName(),
guestOSVO.getDisplayName());
+ if (StringUtils.isBlank(osName)) {
+ return false;
+ }
+ return osName.toLowerCase().contains("windows");
+ }
+
+ protected Pair<String, String>
getNextVmHostAndDisplayName(AutoScaleVmGroupVO asGroup, VirtualMachineTemplate
template) {
+ boolean isWindows = isWindowsOs(template);
String vmHostNameSuffix = "-" + asGroup.getNextVmSeq() + "-" +
RandomStringUtils.random(VM_HOSTNAME_RANDOM_SUFFIX_LENGTH, 0,
0, true, false, (char[])null, new SecureRandom()).toLowerCase();
// Truncate vm group name because max length of vm name is 63
int subStringLength = Math.min(asGroup.getName().length(), 63 -
VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length());
- return VM_HOSTNAME_PREFIX + asGroup.getName().substring(0,
subStringLength) + vmHostNameSuffix;
+ String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0,
subStringLength) + vmHostNameSuffix;
+ if (!isWindows || name.length() <= 15) {
Review Comment:
IMO, we can get rid of the `name.length() <= 15` check.
`VM_HOSTNAME_RANDOM_SUFFIX_LENGTH` is set to 6. So, excluding the size of
ASG's name, `name.length` will always be at least 18 characters. So, this check
will always be false.
Also, let's use a constant instead of the number `15` or add some
documentation on from where did we arrive at the number 15.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]