Merge "Refactor test-salt-models-pipeline"
diff --git a/build-debian-packages-pipeline.groovy b/build-debian-packages-pipeline.groovy
index 8536b5e..91ddb16 100644
--- a/build-debian-packages-pipeline.groovy
+++ b/build-debian-packages-pipeline.groovy
@@ -62,6 +62,9 @@
     }
     stage("build-binary") {
       dsc = sh script: "ls build-area/*.dsc", returnStdout: true
+      if(common.validInputParam("PRE_BUILD_SCRIPT")) {
+        writeFile([file:"pre-build-script.sh", text: env['PRE_BUILD_SCRIPT']])
+      }
       debian.buildBinary(
         dsc.trim(),
         OS+":"+DIST,
diff --git a/generate-cookiecutter-products-auto.groovy b/generate-cookiecutter-products-auto.groovy
deleted file mode 100644
index 735dfdc..0000000
--- a/generate-cookiecutter-products-auto.groovy
+++ /dev/null
@@ -1,160 +0,0 @@
-/**
- * Generate cookiecutter cluster by individual products
- *
- * Expected parameters:
- *   COOKIECUTTER_TEMPLATE_CREDENTIALS  Credentials to the Cookiecutter template repo.
- *   COOKIECUTTER_TEMPLATE_URL          Cookiecutter template repo address.
- *   COOKIECUTTER_TEMPLATE_BRANCH       Branch for the template.
- *   COOKIECUTTER_TEMPLATE_CONTEXT      Context parameters for the template generation.
- *   EMAIl_ADDRESS                      Email to send a created tar file
- *   RECLASS_MODEL_URL                  Reclass model repo address
- *   RECLASS_MODEL_CREDENTIALS          Credentials to the Reclass model repo.
- *   RECLASS_MODEL_BRANCH               Branch for the template to push to model.
- *
-**/
-
-common = new com.mirantis.mk.Common()
-git = new com.mirantis.mk.Git()
-python = new com.mirantis.mk.Python()
-
-timestamps {
-    node() {
-        def templateEnv = "${env.WORKSPACE}/template"
-        def modelEnv = "${env.WORKSPACE}/model"
-
-        try {
-            def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
-            def templateBaseDir = "${env.WORKSPACE}/template"
-            def templateDir = "${templateEnv}/template/dir"
-            def templateOutputDir = templateBaseDir
-            def cutterEnv = "${env.WORKSPACE}/cutter"
-            def jinjaEnv = "${env.WORKSPACE}/jinja"
-            def clusterName = templateContext.default_context.cluster_name
-            def clusterDomain = templateContext.default_context.cluster_domain
-            def targetBranch = "feature/${clusterName}"
-            def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
-
-            currentBuild.description = clusterName
-            print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
-
-            stage ('Download Cookiecutter template') {
-                git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
-            }
-
-            stage ('Download full Reclass model') {
-                if (RECLASS_MODEL_URL != '') {
-                    git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
-                }
-            }
-
-            stage('Generate base infrastructure') {
-                templateDir = "${templateEnv}/cluster_product/infra"
-                templateOutputDir = "${env.WORKSPACE}/template/output/infra"
-                sh "mkdir -p ${templateOutputDir}"
-                sh "mkdir -p ${outputDestination}"
-                python.setupCookiecutterVirtualenv(cutterEnv)
-                python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
-                sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
-            }
-
-            stage('Generate product CI/CD') {
-                if (templateContext.default_context.cicd_enabled.toBoolean()) {
-                    templateDir = "${templateEnv}/cluster_product/cicd"
-                    templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
-                    sh "mkdir -p ${templateOutputDir}"
-                    python.setupCookiecutterVirtualenv(cutterEnv)
-                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
-                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
-                }
-            }
-
-            stage('Generate product OpenContrail') {
-                if (templateContext.default_context.opencontrail_enabled.toBoolean()) {
-                    templateDir = "${templateEnv}/cluster_product/opencontrail"
-                    templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
-                    sh "mkdir -p ${templateOutputDir}"
-                    python.setupCookiecutterVirtualenv(cutterEnv)
-                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
-                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
-                }
-            }
-
-            stage('Generate product Kubernetes') {
-                if (templateContext.default_context.kubernetes_enabled.toBoolean()) {
-                    templateDir = "${templateEnv}/cluster_product/kubernetes"
-                    templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
-                    sh "mkdir -p ${templateOutputDir}"
-                    python.setupCookiecutterVirtualenv(cutterEnv)
-                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
-                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
-                }
-            }
-
-            stage('Generate product OpenStack') {
-                if (templateContext.default_context.openstack_enabled.toBoolean()) {
-                    templateDir = "${templateEnv}/cluster_product/openstack"
-                    templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
-                    sh "mkdir -p ${templateOutputDir}"
-                    python.setupCookiecutterVirtualenv(cutterEnv)
-                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
-                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
-                }
-            }
-
-            stage('Generate product StackLight') {
-                if (templateContext.default_context.stacklight_enabled.toBoolean()) {
-                    templateDir = "${templateEnv}/cluster_product/stacklight"
-                    templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
-                    sh "mkdir -p ${templateOutputDir}"
-                    python.setupCookiecutterVirtualenv(cutterEnv)
-                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
-                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
-                }
-            }
-
-            stage('Generate new SaltMaster node') {
-                def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
-                def nodeString = """classes:
-- cluster.${clusterName}.infra.config
-parameters:
-    _param:
-        linux_system_codename: xenial
-        reclass_data_revision: master
-    linux:
-        system:
-            name: cfg01
-            domain: ${clusterDomain}
-"""
-                sh "mkdir -p ${modelEnv}/nodes/"
-                writeFile(file: nodeFile, text: nodeString)
-            }
-
-            stage ('Save changes to Reclass model') {
-                if (RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") {
-                    git.changeGitBranch(modelEnv, targetBranch)
-                    git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
-                    git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
-                }
-                sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
-                archiveArtifacts artifacts: "${clusterName}.tar.gz"
-                if(EMAIl_ADDRESS != null && EMAIL_ADDRESS != ""){
-                     emailext(to: EMAIL_ADDRESS,
-                              attachmentsPattern: "${clusterName}.tar.gz",
-                              body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
-                              subject: "Your Salt model ${clusterName}")
-                }
-            }
-
-        } catch (Throwable e) {
-             // If there was an error or exception thrown, the build failed
-             currentBuild.result = "FAILURE"
-             throw e
-        } finally {
-            stage ('Clean workspace directories') {
-                sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
-                sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
-            }
-             // common.sendNotification(currentBuild.result,"",["slack"])
-        }
-    }
-}
diff --git a/generate-cookiecutter-products.groovy b/generate-cookiecutter-products.groovy
index cf40b14..acacf9c 100644
--- a/generate-cookiecutter-products.groovy
+++ b/generate-cookiecutter-products.groovy
@@ -6,11 +6,7 @@
  *   COOKIECUTTER_TEMPLATE_URL          Cookiecutter template repo address.
  *   COOKIECUTTER_TEMPLATE_BRANCH       Branch for the template.
  *   COOKIECUTTER_TEMPLATE_CONTEXT      Context parameters for the template generation.
- *   COOKIECUTTER_INSTALL_CICD          Whether to install CI/CD stack.
- *   COOKIECUTTER_INSTALL_CONTRAIL      Whether to install OpenContrail SDN.
- *   COOKIECUTTER_INSTALL_KUBERNETES    Whether to install Kubernetes.
- *   COOKIECUTTER_INSTALL_OPENSTACK     Whether to install OpenStack cloud.
- *   COOKIECUTTER_INSTALL_STACKLIGHT    Whether to install StackLight monitoring.
+ *   EMAIL_ADDRESS                      Email to send a created tar file
  *   RECLASS_MODEL_URL                  Reclass model repo address
  *   RECLASS_MODEL_CREDENTIALS          Credentials to the Reclass model repo.
  *   RECLASS_MODEL_BRANCH               Branch for the template to push to model.
@@ -28,16 +24,16 @@
         def modelEnv = "${env.WORKSPACE}/model"
 
         try {
-            def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
-            def templateDir = "${templateEnv}/template/dir"
-            def templateBaseDir = "${env.WORKSPACE}/template"
-            def templateOutputDir = templateBaseDir
+            def clusterDomain = templateContext.default_context.cluster_domain
+            def clusterName = templateContext.default_context.cluster_name
             def cutterEnv = "${env.WORKSPACE}/cutter"
             def jinjaEnv = "${env.WORKSPACE}/jinja"
-            def clusterName = templateContext.default_context.cluster_name
-            def clusterDomain = templateContext.default_context.cluster_domain
-            def targetBranch = "feature/${clusterName}"
             def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
+            def targetBranch = "feature/${clusterName}"
+            def templateBaseDir = "${env.WORKSPACE}/template"
+            def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
+            def templateDir = "${templateEnv}/template/dir"
+            def templateOutputDir = templateBaseDir
 
             currentBuild.description = clusterName
             print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
@@ -63,7 +59,7 @@
             }
 
             stage('Generate product CI/CD') {
-                if (COOKIECUTTER_INSTALL_CICD.toBoolean()) {
+                if (templateContext.default_context.cicd_enabled.toBoolean()) {
                     templateDir = "${templateEnv}/cluster_product/cicd"
                     templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
                     sh "mkdir -p ${templateOutputDir}"
@@ -74,7 +70,7 @@
             }
 
             stage('Generate product OpenContrail') {
-                if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) {
+                if (templateContext.default_context.opencontrail_enabled.toBoolean()) {
                     templateDir = "${templateEnv}/cluster_product/opencontrail"
                     templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
                     sh "mkdir -p ${templateOutputDir}"
@@ -85,7 +81,7 @@
             }
 
             stage('Generate product Kubernetes') {
-                if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) {
+                if (templateContext.default_context.kubernetes_enabled.toBoolean()) {
                     templateDir = "${templateEnv}/cluster_product/kubernetes"
                     templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
                     sh "mkdir -p ${templateOutputDir}"
@@ -96,7 +92,7 @@
             }
 
             stage('Generate product OpenStack') {
-                if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) {
+                if (templateContext.default_context.openstack_enabled.toBoolean()) {
                     templateDir = "${templateEnv}/cluster_product/openstack"
                     templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
                     sh "mkdir -p ${templateOutputDir}"
@@ -107,7 +103,7 @@
             }
 
             stage('Generate product StackLight') {
-                if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) {
+                if (templateContext.default_context.stacklight_enabled.toBoolean()) {
                     templateDir = "${templateEnv}/cluster_product/stacklight"
                     templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
                     sh "mkdir -p ${templateOutputDir}"
@@ -135,13 +131,20 @@
             }
 
             stage ('Save changes to Reclass model') {
-                if (COMMIT_CHANGES.toBoolean()) {
+                if (env.getEnvironment().containsKey('COMMIT_CHANGES') && COMMIT_CHANGES.toBoolean() && RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") {
                     git.changeGitBranch(modelEnv, targetBranch)
                     git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
                     git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
                 }
+
                 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
                 archiveArtifacts artifacts: "${clusterName}.tar.gz"
+                if (EMAIl_ADDRESS != null && EMAIL_ADDRESS != ""){
+                     emailext(to: EMAIL_ADDRESS,
+                              attachmentsPattern: "${clusterName}.tar.gz",
+                              body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
+                              subject: "Your Salt model ${clusterName}")
+                }
             }
 
         } catch (Throwable e) {