Limit concurrent build on the same node for models and formulas

Change-Id: I38340abcc1367a2ab0e774cfe8bd2ffb5ee8c909
diff --git a/test-salt-formulas-env.groovy b/test-salt-formulas-env.groovy
index 1f646b9..4edcdb7 100644
--- a/test-salt-formulas-env.groovy
+++ b/test-salt-formulas-env.groovy
@@ -19,74 +19,76 @@
 
 def checkouted = false
 
-node("python") {
-  try {
-    stage("checkout") {
-      if (defaultGitRef && defaultGitUrl) {
-        checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
-      } else {
-        throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
-      }
-    }
-    stage("cleanup") {
-      if (checkouted) {
-        sh("make clean")
-      }
-    }
-    stage("kitchen") {
-      if (checkouted) {
-        if (fileExists(".kitchen.yml")) {
-          common.infoMsg(".kitchen.yml found, running kitchen tests")
-          ruby.ensureRubyEnv()
-          if (fileExists(".travis.yml")) {
-            common.infoMsg(".travis.yml found, running custom kitchen init")
-            def kitchenConfigYML = readYaml(file: ".travis.yml")
-            def kitchenInit = kitchenConfigYML["install"]
-            def kitchenInstalled = false
-            if (kitchenInit && !kitchenInit.isEmpty()) {
-              for (int i = 0; i < kitchenInit.size(); i++) {
-                if (kitchenInit[i].trim().startsWith("test -e Gemfile")) { //found Gemfile config
-                  common.infoMsg("Custom Gemfile configuration found, using them")
-                  ruby.installKitchen(kitchenInit[i].trim())
-                  kitchenInstalled = true
-                }
-              }
-            }
-            if (!kitchenInstalled) {
-              ruby.installKitchen()
-            }
-          } else {
-            common.infoMsg(".travis.yml not found, running default kitchen init")
-            ruby.installKitchen()
-          }
-          common.infoMsg("Running part of kitchen test")
-          if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
-            def cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
-            def suite = ruby.getSuiteName(KITCHEN_ENV)
-            if (suite && suite != "") {
-              common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
-              ruby.runKitchenTests(cleanEnv, suite)
-            } else {
-              common.warningMsg("No SUITE was found. Running with all suites.")
-              ruby.runKitchenTests(cleanEnv, "")
-            }
-          } else {
-            throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
-          }
+throttle(['test-formula']) {
+  node("python") {
+    try {
+      stage("checkout") {
+        if (defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
         } else {
-          throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
+          throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
         }
       }
-    }
-  } catch (Throwable e) {
-    // If there was an error or exception thrown, the build failed
-    currentBuild.result = "FAILURE"
-    ruby.runKitchenCommand("destroy")
-    throw e
-  } finally {
-    if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
-      common.errorMsg("----------------KITCHEN LOG:---------------")
-      println readFile(".kitchen/logs/kitchen.log")
+      stage("cleanup") {
+        if (checkouted) {
+          sh("make clean")
+        }
+      }
+      stage("kitchen") {
+        if (checkouted) {
+          if (fileExists(".kitchen.yml")) {
+            common.infoMsg(".kitchen.yml found, running kitchen tests")
+            ruby.ensureRubyEnv()
+            if (fileExists(".travis.yml")) {
+              common.infoMsg(".travis.yml found, running custom kitchen init")
+              def kitchenConfigYML = readYaml(file: ".travis.yml")
+              def kitchenInit = kitchenConfigYML["install"]
+              def kitchenInstalled = false
+              if (kitchenInit && !kitchenInit.isEmpty()) {
+                for (int i = 0; i < kitchenInit.size(); i++) {
+                  if (kitchenInit[i].trim().startsWith("test -e Gemfile")) { //found Gemfile config
+                    common.infoMsg("Custom Gemfile configuration found, using them")
+                    ruby.installKitchen(kitchenInit[i].trim())
+                    kitchenInstalled = true
+                  }
+                }
+              }
+              if (!kitchenInstalled) {
+                ruby.installKitchen()
+              }
+            } else {
+              common.infoMsg(".travis.yml not found, running default kitchen init")
+              ruby.installKitchen()
+            }
+            common.infoMsg("Running part of kitchen test")
+            if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
+              def cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
+              def suite = ruby.getSuiteName(KITCHEN_ENV)
+              if (suite && suite != "") {
+                common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
+                ruby.runKitchenTests(cleanEnv, suite)
+              } else {
+                common.warningMsg("No SUITE was found. Running with all suites.")
+                ruby.runKitchenTests(cleanEnv, "")
+              }
+            } else {
+              throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
+            }
+          } else {
+            throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
+          }
+        }
+      }
+    } catch (Throwable e) {
+      // If there was an error or exception thrown, the build failed
+      currentBuild.result = "FAILURE"
+      ruby.runKitchenCommand("destroy")
+      throw e
+    } finally {
+      if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
+        common.errorMsg("----------------KITCHEN LOG:---------------")
+        println readFile(".kitchen/logs/kitchen.log")
+      }
     }
   }
 }
diff --git a/test-salt-model-node.groovy b/test-salt-model-node.groovy
index bcc0c8b..eaf0104 100644
--- a/test-salt-model-node.groovy
+++ b/test-salt-model-node.groovy
@@ -23,44 +23,47 @@
 def defaultGitUrl = DEFAULT_GIT_URL
 
 def checkouted = false
-node("python") {
-  try{
-    stage("checkout") {
-      if(defaultGitRef != "" && defaultGitUrl != "") {
-          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
-      } else {
-        throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_URL or DEFAULT_GIT_REF is null")
-      }
-      if(checkouted) {
-        if (fileExists('classes/system')) {
-          if (SYSTEM_GIT_URL == "") {
-            ssh.prepareSshAgentKey(CREDENTIALS_ID)
-            dir('classes/system') {
-              remoteUrl = git.getGitRemote()
-              ssh.ensureKnownHosts(remoteUrl)
-            }
-            ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
-          } else {
-            dir('classes/system') {
-              if (!gerrit.gerritPatchsetCheckout(SYSTEM_GIT_URL, SYSTEM_GIT_REF, "HEAD", CREDENTIALS_ID)) {
-                common.errorMsg("Failed to obtain system reclass with url: ${SYSTEM_GIT_URL} and ${SYSTEM_GIT_REF}")
+
+throttle(['test-model']) {
+  node("python") {
+    try{
+      stage("checkout") {
+        if(defaultGitRef != "" && defaultGitUrl != "") {
+            checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
+        } else {
+          throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_URL or DEFAULT_GIT_REF is null")
+        }
+        if(checkouted) {
+          if (fileExists('classes/system')) {
+            if (SYSTEM_GIT_URL == "") {
+              ssh.prepareSshAgentKey(CREDENTIALS_ID)
+              dir('classes/system') {
+                remoteUrl = git.getGitRemote()
+                ssh.ensureKnownHosts(remoteUrl)
+              }
+              ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
+            } else {
+              dir('classes/system') {
+                if (!gerrit.gerritPatchsetCheckout(SYSTEM_GIT_URL, SYSTEM_GIT_REF, "HEAD", CREDENTIALS_ID)) {
+                  common.errorMsg("Failed to obtain system reclass with url: ${SYSTEM_GIT_URL} and ${SYSTEM_GIT_REF}")
+                }
               }
             }
           }
         }
       }
-    }
 
-    stage("test node") {
-      if (checkouted) {
-        def workspace = common.getWorkspace()
-        saltModelTesting.setupAndTestNode(NODE_TARGET, CLUSTER_NAME, EXTRA_FORMULAS, workspace, FORMULAS_SOURCE, FORMULAS_REVISION, MAX_CPU_PER_JOB.toInteger())
+      stage("test node") {
+        if (checkouted) {
+          def workspace = common.getWorkspace()
+          saltModelTesting.setupAndTestNode(NODE_TARGET, CLUSTER_NAME, EXTRA_FORMULAS, workspace, FORMULAS_SOURCE, FORMULAS_REVISION, MAX_CPU_PER_JOB.toInteger())
+        }
       }
+    } catch (Throwable e) {
+       // If there was an error or exception thrown, the build failed
+       currentBuild.result = "FAILURE"
+       currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
+       throw e
     }
-  } catch (Throwable e) {
-     // If there was an error or exception thrown, the build failed
-     currentBuild.result = "FAILURE"
-     currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
-     throw e
   }
 }