add cookiecutter generation pipeline

Change-Id: If28e32ce8242ecd7bb9cbb8ba8cdb680fa7ccb8c
diff --git a/generate-cookiecutter-products.groovy b/generate-cookiecutter-products.groovy
new file mode 100644
index 0000000..3dc2d8c
--- /dev/null
+++ b/generate-cookiecutter-products.groovy
@@ -0,0 +1,145 @@
+/**
+ * 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.
+ *   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.
+ *   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() {
+        try {
+            def templateEnv = "${env.WORKSPACE}/template"
+            def templateContext = python.loadJson(COOKIECUTTER_TEMPLATE_CONTEXT)
+            def templateDir = "${templateEnv}/template/dir"
+            def templateOutputDir = "${env.WORKSPACE}/template"
+            def modelEnv = "${env.WORKSPACE}/model"
+            def cutterEnv = "${env.WORKSPACE}/cutter"
+            def jinjaEnv = "${env.WORKSPACE}/jinja"
+            def clusterName = templateContext.cluster_name
+            def clusterDomain = templateContext.cluster_domain
+            def targetBranch = "feature/${clusterName}"
+
+            stage ('Download Cookiecutter template') {
+                git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
+            }
+
+            stage ('Download full Reclass model') {
+                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}"
+                python.setupCookiecutterVirtualenv(cutterEnv)
+                python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv)
+            }
+
+            stage('Generate product CI/CD') {
+                if (COOKIECUTTER_INSTALL_CICD.toBoolean()) {
+                    templateDir = "${templateEnv}/cluster_product/cicd"
+                    templateOutputDir = "${env.WORKSPACE}/template/output_cicd"
+                    sh "mkdir -p ${templateOutputDir}"
+                    python.setupCookiecutterVirtualenv(cutterEnv)
+                    python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv)
+                }
+            }
+
+            stage('Generate product OpenContrail') {
+                if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) {
+                    templateDir = "${templateEnv}/cluster_product/contrail"
+                    templateOutputDir = "${env.WORKSPACE}/template/output_contrail"
+                    sh "mkdir -p ${templateOutputDir}"
+                    python.setupCookiecutterVirtualenv(cutterEnv)
+                    python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv)
+                }
+            }
+
+            stage('Generate product Kubernetes') {
+                if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) {
+                    templateDir = "${templateEnv}/cluster_product/kubernetes"
+                    templateOutputDir = "${env.WORKSPACE}/template/output_kubernetes"
+                    sh "mkdir -p ${templateOutputDir}"
+                    python.setupCookiecutterVirtualenv(cutterEnv)
+                    python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv)
+                }
+            }
+
+            stage('Generate product OpenStack') {
+                if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) {
+                    templateDir = "${templateEnv}/cluster_product/openstack"
+                    templateOutputDir = "${env.WORKSPACE}/template/output_openstack"
+                    sh "mkdir -p ${templateOutputDir}"
+                    python.setupCookiecutterVirtualenv(cutterEnv)
+                    python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv)
+                }
+            }
+
+            stage('Generate product StackLight') {
+                if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) {
+                    templateDir = "${templateEnv}/cluster_product/stacklight"
+                    templateOutputDir = "${env.WORKSPACE}/template/output_stacklight"
+                    sh "mkdir -p ${templateOutputDir}"
+                    python.setupCookiecutterVirtualenv(cutterEnv)
+                    python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv)
+                }
+            }
+
+            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}
+"""
+                writeFile(file: nodeFile, text: nodeString)
+            }
+
+            stage('Inject changes to Reclass model') {
+                git.changeGitBranch(modelEnv, targetBranch)
+                def outputSource = "${templateOutputDir}/${clusterName}"
+                def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
+                sh(returnStdout: true, script: "cp ${outputSource} ${outputDestination} -r")
+                git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
+            }
+
+            stage ('Push changes to Reclass model') {
+                git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
+            }
+
+            stage ('Clean workspace directories') {
+                sh(returnStdout: true, script: "rm ${templateEnv} -rf")
+                sh(returnStdout: true, script: "rm ${modelEnv} -rf")
+            }
+
+        } catch (Throwable e) {
+             // If there was an error or exception thrown, the build failed
+             currentBuild.result = "FAILURE"
+             throw e
+        } finally {
+             // common.sendNotification(currentBuild.result,"",["slack"])
+        }
+    }
+}