Add method to get environment with lockable resources
Related-Prod: PRODX-3456
Change-Id: I7b8aebc6da81f234230bc23d13e5b2e42469397b
diff --git a/src/com/mirantis/mk/JenkinsUtils.groovy b/src/com/mirantis/mk/JenkinsUtils.groovy
index 52eeb62..c993999 100644
--- a/src/com/mirantis/mk/JenkinsUtils.groovy
+++ b/src/com/mirantis/mk/JenkinsUtils.groovy
@@ -1,5 +1,6 @@
package com.mirantis.mk
import com.cloudbees.groovy.cps.NonCPS
+import org.jenkins.plugins.lockableresources.LockableResourcesManager as LRM
/**
*
@@ -329,3 +330,49 @@
}
return workers
}
+
+/**
+ * Get deployment environment and related jenkins lock label and lock resource
+ *
+ * @param initialEnv (string) Name of initially requested environment e.g. imc-eu or auto
+ * @param namespace (string) Name of environment namespace e.g imc-oscore-team
+ * @param resources (int) Quantity of required lockable resources
+ * @param candidateEnvs (list) List of names of env candidates to choose between
+ * @return (list) List whith environment name, lock label and lock resource
+ */
+def getEnvWithLockableResources(initialEnv, namespace, resources = 1, candidateEnvs = ["imc-eu", "imc-us"]){
+ def common = new com.mirantis.mk.Common()
+ def lockResource = null
+ def env = initialEnv
+ def lockLabel = "${namespace}-${env}"
+ def lrm = LRM.get()
+ if (initialEnv == "auto"){
+ def freeResources = [:]
+ for (cEnv in candidateEnvs){
+ def label = "${namespace}-${cEnv}"
+ freeResources[label] = lrm.getFreeResourceAmount(label)
+ }
+ common.infoMsg("Detecting target environment from candidates ${freeResources}")
+ def max = 0
+ def keys = freeResources.keySet().toList()
+ Collections.shuffle(keys)
+ for (key in keys){
+ if (freeResources[key] >= max){
+ max = freeResources[key]
+ lockLabel = key
+ }
+ }
+ if (max < resources){
+ lockLabel = keys[0]
+ }
+ env = lockLabel.replaceAll("${namespace}-", "")
+ common.infoMsg("Detected target environment ${env} lock ${lockLabel}")
+ }
+ // If no label configured on existing resources, create random lockresource
+ if (! lrm.isValidLabel(lockLabel)){
+ common.infoMsg("Running without locking, lock label ${lockLabel} does not exist")
+ lockLabel = null
+ lockResource = UUID.randomUUID().toString()
+ }
+ return [env, lockLabel, lockResource]
+}