Merge "Add support of OC4.X for 'Zookeeper - restore' pipeline"
diff --git a/generate-cookiecutter-products.groovy b/generate-cookiecutter-products.groovy
index 1fc38b3..f98251f 100644
--- a/generate-cookiecutter-products.groovy
+++ b/generate-cookiecutter-products.groovy
@@ -82,14 +82,117 @@
     }
     common.warningMsg("Fetching:\n" +
         "DISTRIB_REVISION from ${distribRevision}")
-    common.infoMsg("Using context:\n" + context)
-    print prettyPrint(toJson(context))
+    common.infoMsg('Using context:\n')
+    print(prettyPrint(toJson(context)))
     return context
 
 }
 
+def generateModel(context, contextName, saltMasterName, virtualenv, modelEnv, templateEnvDir, multiModels = true) {
+    def common = new com.mirantis.mk.Common()
+    def generatedModel = multiModels ? "${modelEnv}/${contextName}" : modelEnv
+    def templateContext = readYaml text: context
+    def clusterDomain = templateContext.default_context.cluster_domain
+    def clusterName = templateContext.default_context.cluster_name
+    def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
+    def templateBaseDir = templateEnvDir
+    def templateDir = "${templateEnvDir}/dir"
+    def templateOutputDir = templateBaseDir
+    dir(templateEnvDir) {
+        if (fileExists(new File(templateEnvDir, 'tox.ini').toString())) {
+            tempContextFile = 'tempContext.yaml'
+            writeFile file: tempContextFile, text: context
+            common.warningMsg('Generating models using context:\n')
+            print(context)
+            withEnv(["CONFIG_FILE=$tempContextFile",
+                     "OUTPUT_DIR=$templateOutputDir",
+            ]) {
+                print('[Cookiecutter build] Result:\n' +
+                    sh(returnStdout: true, script: 'tox -ve generate_auto'))
+            }
+            // dropme after impelementation new format
+            sh "mkdir -p ${generatedModel}/nodes/"
+            def nodeFile = "${generatedModel}/nodes/${saltMasterName}.${clusterDomain}.yml"
+            def nodeString = """classes:
+- cluster.${clusterName}.infra.config
+parameters:
+  _param:
+    linux_system_codename: xenial
+    reclass_data_revision: master
+  linux:
+    system:
+      name: ${saltMasterName}
+      domain: ${clusterDomain}
+    """
+            writeFile(file: nodeFile, text: nodeString)
+            //
+        } else {
+            common.warningMsg("Old format: Generating model from context ${contextName}")
+            def productList = ["infra", "cicd", "kdt", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
+            for (product in productList) {
+                // get templateOutputDir and productDir
+                templateOutputDir = "${templateEnvDir}/output/${product}"
+                productDir = product
+                templateDir = "${templateEnvDir}/cluster_product/${productDir}"
+                // Bw for 2018.8.1 and older releases
+                if (product.startsWith("stacklight") && (!fileExists(templateDir))) {
+                    common.warningMsg("Old release detected! productDir => 'stacklight2' ")
+                    productDir = "stacklight2"
+                    templateDir = "${templateEnvDir}/cluster_product/${productDir}"
+                }
+                // generate infra unless its explicitly disabled
+                if ((product == "infra" && templateContext.default_context.get("infra_enabled", "True").toBoolean())
+                    || (templateContext.default_context.get(product + "_enabled", "False").toBoolean())) {
+
+                    common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
+
+                    sh "rm -rf ${templateOutputDir} || true"
+                    sh "mkdir -p ${templateOutputDir}"
+                    sh "mkdir -p ${outputDestination}"
+
+                    buildCookiecutterTemplate(templateDir, context, templateOutputDir, virtualenv, templateBaseDir)
+                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
+                } else {
+                    common.warningMsg("Product " + product + " is disabled")
+                }
+            }
+
+            def localRepositories = templateContext.default_context.local_repositories
+            localRepositories = localRepositories ? localRepositories.toBoolean() : false
+            def offlineDeployment = templateContext.default_context.offline_deployment
+            offlineDeployment = offlineDeployment ? offlineDeployment.toBoolean() : false
+            if (localRepositories && !offlineDeployment) {
+                def mcpVersion = templateContext.default_context.mcp_version
+                def aptlyModelUrl = templateContext.default_context.local_model_url
+                def ssh = new com.mirantis.mk.Ssh()
+                dir(path: modelEnv) {
+                    ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
+                    if (!(mcpVersion in ["nightly", "testing", "stable"])) {
+                        ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
+                    }
+                }
+            }
+
+            def nodeFile = "${generatedModel}/nodes/${saltMasterName}.${clusterDomain}.yml"
+            def nodeString = """classes:
+- cluster.${clusterName}.infra.config
+parameters:
+  _param:
+    linux_system_codename: xenial
+    reclass_data_revision: master
+  linux:
+    system:
+      name: ${saltMasterName}
+      domain: ${clusterDomain}
+    """
+            sh "mkdir -p ${generatedModel}/nodes/"
+            writeFile(file: nodeFile, text: nodeString)
+        }
+    }
+}
+
 timeout(time: 1, unit: 'HOURS') {
-    node(slaveNode) {
+    node('jsl-image-publisher.mcp.mirantis.net') {
         def context = globalVariatorsUpdate()
         def RequesterEmail = context.get('email_address', '')
         def templateEnv = "${env.WORKSPACE}/template"
@@ -161,9 +264,13 @@
                     if (context.get('cfg_failsafe_ssh_public_key')) {
                         writeFile file: 'failsafe-ssh-key.pub', text: context['cfg_failsafe_ssh_public_key']
                     }
-                    python.setupCookiecutterVirtualenv(cutterEnv)
+                    templateEnv
+                    if (!fileExists(new File(templateEnv, 'tox.ini').toString())) {
+                        python.setupCookiecutterVirtualenv(cutterEnv)
+                    }
                     // FIXME refactor generateModel
-                    python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
+                    //python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
+                    generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
                     git.commitGitChanges(modelEnv, "Create model ${context['cluster_name']}", "${user}@localhost", "${user}")
                 }
             }
@@ -316,7 +423,7 @@
             throw e
         } finally {
             stage('Clean workspace directories') {
-                sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
+                //sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
             }
             // common.sendNotification(currentBuild.result,"",["slack"])
         }
diff --git a/test-salt-formulas-pipeline.groovy b/test-salt-formulas-pipeline.groovy
index 088a744..6d7e622 100644
--- a/test-salt-formulas-pipeline.groovy
+++ b/test-salt-formulas-pipeline.groovy
@@ -19,6 +19,7 @@
 
 def checkouted = false
 
+envOverrides = []
 futureFormulas = []
 failedFormulas = []
 
@@ -176,6 +177,7 @@
             if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
               if (fileExists(".kitchen.openstack.yml")) {
                 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
+                envOverrides.add("KITCHEN_YAML=.kitchen.openstack.yml")
                 if (fileExists(".kitchen.yml")) {
                   common.infoMsg("Ignoring the docker Kitchen test configuration file.")
                 }
@@ -191,8 +193,8 @@
                 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
                 ruby.installKitchen()
               }
-              common.infoMsg = ruby.runKitchenCommand("list -b")
-              kitchenEnvs = ruby.runKitchenCommand("list -b").split()
+              common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' '))
+              kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split()
               common.infoMsg(kitchenEnvs)
               common.infoMsg("Running kitchen testing in parallel mode")
               if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
diff --git a/test-salt-model-wrapper.groovy b/test-salt-model-wrapper.groovy
index 9913c5c..42aa4e9 100644
--- a/test-salt-model-wrapper.groovy
+++ b/test-salt-model-wrapper.groovy
@@ -55,9 +55,9 @@
 }
 
 // run needed job with params
-def runTests(String jobName, ArrayList jobParams, String threadName = '') {
+def runTests(String jobName, ArrayList jobParams, String threadName = '', Boolean voteOverride = null) {
     threadName = threadName ? threadName : jobName
-    def propagateStatus = voteMatrix.get(jobName, true)
+    def propagateStatus = voteOverride != null ? voteOverride : voteMatrix.get(jobName, true)
     return {
         def jobBuild = build job: jobName, propagate: false, parameters: jobParams
         jobResultComments[threadName] = ['url': jobBuild.absoluteUrl, 'status': jobBuild.result, 'job': jobName]
@@ -205,7 +205,9 @@
                         buildTestParamsOld['COOKIECUTTER_TEMPLATE_REF'] = ''
                         buildTestParamsOld['COOKIECUTTER_TEMPLATE_BRANCH'] = oldRef
                         String threadName = "${branchJobName}-${oldRef}"
-                        branches[threadName] = runTests(branchJobName, yamlJobParameters(buildTestParamsOld), threadName)
+                        // disable votes for release/2018.11.0 branch
+                        overrideVote = oldRef == 'release/2018.11.0' ? false : null
+                        branches[threadName] = runTests(branchJobName, yamlJobParameters(buildTestParamsOld), threadName, overrideVote)
                     }
                 }
                 if (gerritProject == cookiecutterTemplatesRepo) {