diff --git a/Orchestrator/Workflows/Generate Unique Hostname/03_search_for_matching_VM_names.png b/Orchestrator/Workflows/Generate Unique Hostname/03_search_for_matching_VM_names.png new file mode 100644 index 0000000..f451e77 Binary files /dev/null and b/Orchestrator/Workflows/Generate Unique Hostname/03_search_for_matching_VM_names.png differ diff --git a/Orchestrator/Workflows/Generate Unique Hostname/04_generate_candidateVmName.js b/Orchestrator/Workflows/Generate Unique Hostname/04_generate_candidateVmName.js new file mode 100644 index 0000000..ba83d97 --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/04_generate_candidateVmName.js @@ -0,0 +1,30 @@ +/* JavaScript: generate candidateVmName + Appends appropriate numbering sequence to nameBase + Inputs: digitCount (number), nameBase (string), digits (number), hostnameSequence (number), computerNames (ConfigurationElement) + Outputs: computerNames (ConfigurationElement), candidateVmName (string), hostnameSequence (number) + Exception binding: errMsg (string) +*/ +if (digits) { + hostnameSequence = digits; + System.log("Manually setting sequence to user-provided input: " + digits) +} else { + try { + hostnameSequence = computerNames.getAttributeWithKey(nameBase).value; + System.debug("Found hostname base " + nameBase + " with sequence " + hostnameSequence); + } catch (e) { + System.debug("Hostname base " + nameBase + " does not exist, it will be created."); + } finally { + hostnameSequence++; + if (hostnameSequence.toString().length > digitCount) { + errMsg = 'All out of potential VM names, aborting...'; + throw(errMsg); + } + System.debug("Adding " + nameBase + " with sequence " + hostnameSequence); + computerNames.setAttributeWithKey(nameBase,hostnameSequence); + } +} +var hostnameNum = hostnameSequence.toString(); +var leadingZeroes = new Array(digitCount - hostnameNum.length + 1).join("0"); +hostnameNum = leadingZeroes + hostnameNum; +candidateVmName = (nameBase + hostnameNum).toUpperCase(); +System.log("Proposed VM name: " + candidateVmName); \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/05_check_for_VM_name_conflict.js b/Orchestrator/Workflows/Generate Unique Hostname/05_check_for_VM_name_conflict.js new file mode 100644 index 0000000..5b539d3 --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/05_check_for_VM_name_conflict.js @@ -0,0 +1,15 @@ +/* JavaScript: check for VM name conflict + Iterates through an array of VM object to check for naming collisions. + Inputs: candidateVmName (string), vms (Array/VC:VirtualMachine) + Outputs: conflict (boolean) + Exception binding: errMsg (string) +*/ +vms.forEach(function(vm) { + if (vm.name.toUpperCase() === candidateVmName) { + conflict = true; + errMsg = "Found a conflicting VM name!"; + System.warn(errMsg); + throw(errMsg); + } +}); +System.log("No VM name conflicts found for " + candidateVmName); \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/06_check_for_DNS_conflict.js b/Orchestrator/Workflows/Generate Unique Hostname/06_check_for_DNS_conflict.js new file mode 100644 index 0000000..0c98bdb --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/06_check_for_DNS_conflict.js @@ -0,0 +1,18 @@ +/* JavaScript: check for DNS conflict + Checks for conflicting DNS records. + Inputs: candidateVmName (string), conflict (boolean), domain (string) + Outputs: conflict (boolean) + Exception binding: errMsg (string) +*/ +if (conflict) { + System.debug("Existing conflict found, skipping DNS check..."); +} else { + if (System.resolveHostName(candidateVmName + "." + domain)) { + conflict = true; + errMsg = "Conflicting DNS record found!"; + System.warn(errMsg); + throw(errMsg); + } else { + System.log("No DNS conflict for " + candidateVmName) + } +} \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/07_check_for_AD_conflict.js b/Orchestrator/Workflows/Generate Unique Hostname/07_check_for_AD_conflict.js new file mode 100644 index 0000000..4f65321 --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/07_check_for_AD_conflict.js @@ -0,0 +1,20 @@ +/* JavaScript: check for AD conflict + Checks for conflicting names in Active Directory + Inputs: candidateVmName (string), conflict (boolean), adHost (AD:AdHost) + Outputs: conflict (boolean) + Exception binding: errMsg (string) +*/ +if (conflict) { + System.debug("Existing conflict found, skipping AD check..."); +} else { + var computer = ActiveDirectory.getComputerAD(candidateVmName, adHost); + System.log("Searched AD for: " + candidateVmName); + if (computer) { + conflict = true; + errMsg = "Conflicting AD object found!" + System.warn(errMsg); + throw(errMsg); + } else { + System.log("No AD conflict found for " + candidateVmName); + } +} \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/08_return_nextVmName.js b/Orchestrator/Workflows/Generate Unique Hostname/08_return_nextVmName.js new file mode 100644 index 0000000..32bdfff --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/08_return_nextVmName.js @@ -0,0 +1,7 @@ +/* JavaScript: return nextVmName + Returns the selected nextVmName after passing all collision checks + Inputs: candidateVmName (string) + Outputs: nextVmName (string) +*/ +nextVmName = candidateVmName; +System.log(" ***** Selecting [" + nextVmName + "] as the next VM name ***** ") \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/09_remove_lock.js b/Orchestrator/Workflows/Generate Unique Hostname/09_remove_lock.js new file mode 100644 index 0000000..1af4b6b --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/09_remove_lock.js @@ -0,0 +1,7 @@ +/* JavaScript: remove lock + Removes the lock to re-allow subsequent workflow runs + Inputs: none + Outputs: none +*/ +System.debug("Releasing lock..."); +LockingSystem.unlock("namingLock","eventBroker"); \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/a01_conflict_encountered.js b/Orchestrator/Workflows/Generate Unique Hostname/a01_conflict_encountered.js new file mode 100644 index 0000000..2e63e6e --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/a01_conflict_encountered.js @@ -0,0 +1,13 @@ +/* JavaScript: conflict encountered + Resets the conflict flag and passes control back to the 'generate candidateVmName' + Inputs: digits (number) + Outputs: conflict (boolean) + Exception binding: errMsg (string) +*/ +if (digits) { + errMsg = 'User-specified name is not available, aborting...'; + throw(errMsg); +} else { + System.log("Conflict encountered, trying a new name...") + conflict = false +} \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/z01_errors.js b/Orchestrator/Workflows/Generate Unique Hostname/z01_errors.js new file mode 100644 index 0000000..b4393a5 --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/z01_errors.js @@ -0,0 +1,7 @@ +/* JavaScript: errors + Terminate the workflow in the event of an unrecoverable error + Inputs: errMsg (string) + Outputs: none +*/ +System.error("FATAL ERROR!"); +System.error(errMsg); \ No newline at end of file diff --git a/Orchestrator/Workflows/Generate Unique Hostname/z02_remove_lock.js b/Orchestrator/Workflows/Generate Unique Hostname/z02_remove_lock.js new file mode 100644 index 0000000..1af4b6b --- /dev/null +++ b/Orchestrator/Workflows/Generate Unique Hostname/z02_remove_lock.js @@ -0,0 +1,7 @@ +/* JavaScript: remove lock + Removes the lock to re-allow subsequent workflow runs + Inputs: none + Outputs: none +*/ +System.debug("Releasing lock..."); +LockingSystem.unlock("namingLock","eventBroker"); \ No newline at end of file