Merge "Add sleep after Contrail Control installation"
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 9fe862e..8dfec19 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -542,7 +542,6 @@
             sleep(delay)
         }
     }
-    currentBuild.result = "FAILURE"
     throw new Exception("Failed after $times retries")
 }
 
diff --git a/src/com/mirantis/mk/JenkinsUtils.groovy b/src/com/mirantis/mk/JenkinsUtils.groovy
index 3288560..0434814 100644
--- a/src/com/mirantis/mk/JenkinsUtils.groovy
+++ b/src/com/mirantis/mk/JenkinsUtils.groovy
@@ -125,9 +125,9 @@
  * @param jobName job name
  * @return job object that matches jobName
  */
-def getJobByName(jobName){
+def getJobByName(jobName, regexp=false){
     for(item in Hudson.instance.items) {
-        if(item.name == jobName){
+        if (regexp && item.name ==~ jobName || item.name == jobName) {
             return item
         }
     }
@@ -149,3 +149,71 @@
     }
     return params
 }
+
+/**
+ * Get list of causes actions for given build
+ *
+ * @param build Job build object (like, currentBuild.rawBuild)
+ * @return list of causes actions for given build
+ */
+@NonCPS
+def getBuildCauseActions(build) {
+    def causeAction = build.actions.find { it -> it instanceof hudson.model.CauseAction }
+    if(causeAction) {
+        return causeAction.causes
+    } else {
+        return []
+    }
+}
+
+/**
+ * Get list of builds, triggered by Gerrit with given build
+ * @param build Job build object (like, currentBuild.rawBuild)
+ * @return list of builds with names and numbers
+ */
+@NonCPS
+def getGerritBuildContext(build) {
+    def causes = getBuildCauseActions(build)
+    if (causes) {
+      def gerritTriggerCause = causes.find { cause ->
+          cause instanceof com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause
+      }
+      return gerritTriggerCause.context.getOtherBuilds()
+    } else {
+      return []
+    }
+}
+
+/**
+ * Wait for other jobs
+ * @param config config parameter:
+ *   builds - List of job build objects, which should be checked
+ *   checkBuilds - List of job names or regexps, which should be used to check provided builds list
+ *   regexp - Wheither to use regexp or simple string matching
+ */
+def waitForOtherBuilds(LinkedHashMap config){
+  def common = new com.mirantis.mk.Common()
+  def builds = config.get('builds')
+  def checkBuilds = config.get('checkBuilds')
+  def regexp = config.get('regexp', false)
+  def waitForBuilds = builds.findAll { build ->
+    def jobName = build.fullDisplayName.tokenize(' ')[0]
+    if (regexp) {
+      checkBuilds.find { jobName ==~ it }
+    } else {
+      jobName in checkBuilds
+    }
+  }
+  if (waitForBuilds) {
+    def waiting = true
+    common.infoMsg("Waiting for next jobs: ${waitForBuilds}")
+    while(waiting) {
+      waiting = false
+      waitForBuilds.each { job ->
+        if (job.inProgress) {
+          waiting = true
+        }
+      }
+    }
+  }
+}
diff --git a/src/com/mirantis/mk/Orchestrate.groovy b/src/com/mirantis/mk/Orchestrate.groovy
index 6ababfd..29a8626 100644
--- a/src/com/mirantis/mk/Orchestrate.groovy
+++ b/src/com/mirantis/mk/Orchestrate.groovy
@@ -580,7 +580,7 @@
     }
 
     if (salt.testTarget(master, "I@nova:compute ${extra_tgt}")) {
-        salt.cmdRun(master, "I@nova:compute ${extra_tgt}", 'exec 0>&-; exec 1>&-; exec 2>&-; nohup bash -c "ip link | grep vhost && echo no_reboot || sleep 5 && reboot & "', false)
+        salt.cmdRun(master, "I@nova:compute ${extra_tgt}", 'exec 0>&-; exec 1>&-; exec 2>&-; nohup bash -c "ip link | grep vhost && echo no_reboot || reboot & "', false)
     }
 
     sleep(300)
diff --git a/src/com/mirantis/mk/Python.groovy b/src/com/mirantis/mk/Python.groovy
index 6183f51..08da637 100644
--- a/src/com/mirantis/mk/Python.groovy
+++ b/src/com/mirantis/mk/Python.groovy
@@ -262,12 +262,22 @@
  * @param templatePath path to cookiecutter template repo (optional)
  */
 def buildCookiecutterTemplate(template, context, outputDir = '.', path = null, templatePath = ".") {
+    def common = new com.mirantis.mk.Common()
     configFile = "default_config.yaml"
-    configString = "default_context:\n"
     writeFile file: configFile, text: context
-    command = ". ${path}/bin/activate; if [ -f ${templatePath}/generate.py ]; then python ${templatePath}/generate.py --config-file ${configFile} --template ${template} --output-dir ${outputDir}; else cookiecutter --config-file ${configFile} --output-dir ${outputDir} --overwrite-if-exists --verbose --no-input ${template}; fi"
-    output = sh (returnStdout: true, script: command)
-    echo("[Cookiecutter build] Output: ${output}")
+    if (fileExists(new File(templatePath, 'tox.ini').toString())) {
+        withEnv(["CONFIG_FILE=$configFile",
+                 "OUTPUT_DIR=$outputDir",
+                 "TEMPLATE=$template"
+        ]) {
+            output = sh(returnStdout: true, script: "tox -ve generate")
+        }
+    } else {
+        common.warningMsg('Old Cookiecutter env detected!')
+        command = ". ${path}/bin/activate; if [ -f ${templatePath}/generate.py ]; then python ${templatePath}/generate.py --config-file ${configFile} --template ${template} --output-dir ${outputDir}; else cookiecutter --config-file ${configFile} --output-dir ${outputDir} --overwrite-if-exists --verbose --no-input ${template}; fi"
+        output = sh(returnStdout: true, script: command)
+    }
+    common.infoMsg("[Cookiecutter build] Result: ${output}")
 }
 
 /**
diff --git a/src/com/mirantis/mk/Salt.groovy b/src/com/mirantis/mk/Salt.groovy
index 2c6a182..d126964 100644
--- a/src/com/mirantis/mk/Salt.groovy
+++ b/src/com/mirantis/mk/Salt.groovy
@@ -1078,7 +1078,7 @@
          def value = entry[1]
 
          common.debugMsg("Set salt override ${key}=${value}")
-         runSaltProcessStep(saltId, "I@salt:master ${extra_tgt}", 'reclass.cluster_meta_set', [key, value], false)
+         runSaltProcessStep(saltId, "I@salt:master ${extra_tgt}", 'reclass.cluster_meta_set', ["name=${key}", "value=${value}"], false)
     }
     runSaltProcessStep(saltId, "I@salt:master ${extra_tgt}", 'cmd.run', ["git -C ${reclass_dir} update-index --skip-worktree classes/cluster/overrides.yml"])
 }
diff --git a/src/com/mirantis/mk/SaltModelTesting.groovy b/src/com/mirantis/mk/SaltModelTesting.groovy
index 16e469c..c93587f 100644
--- a/src/com/mirantis/mk/SaltModelTesting.groovy
+++ b/src/com/mirantis/mk/SaltModelTesting.groovy
@@ -60,7 +60,7 @@
         } catch (Exception e) {
             common.warningMsg(e)
             if ( !(distribRevision in [ 'nightly', 'proposed', 'testing' ] )) {
-                extraRepoSource = "deb http://apt.mcp.mirantis.net/xenial ${distribRevision} extra"
+                extraRepoSource = "deb [arch=amd64] http://apt.mcp.mirantis.net/xenial ${distribRevision} extra"
             }
         }
 
@@ -287,7 +287,7 @@
 
     config['runCommands'] = [
         '001_Clone_salt_formulas_scripts': {
-            sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
+            sh(script: 'git clone http://gerrit.mcp.mirantis.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true)
         },
 
         '002_Prepare_something'          : {