Merge "Install ceph before openstack"
diff --git a/cloud-update.groovy b/cloud-update.groovy
index 2729d98..8802c1b 100644
--- a/cloud-update.groovy
+++ b/cloud-update.groovy
@@ -388,7 +388,7 @@
     } else {
         def salt = new com.mirantis.mk.Salt()
         for (s in services) {
-            def outputServicesStr = salt.getReturnValues(salt.cmdRun(pepperEnv, "${probe}*", "service --status-all | grep ${s} | awk \'{print \$4}\'"))
+            def outputServicesStr = salt.getReturnValues(salt.cmdRun(pepperEnv, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
             def servicesList = outputServicesStr.tokenize("\n").init() //init() returns the items from the Iterable excluding the last item
             if (servicesList) {
                 for (name in servicesList) {
diff --git a/docker-build-image-pipeline.groovy b/docker-build-image-pipeline.groovy
index 1fbd9f0..a39051f 100644
--- a/docker-build-image-pipeline.groovy
+++ b/docker-build-image-pipeline.groovy
@@ -9,94 +9,101 @@
  * REGISTRY_URL - Docker registry URL (can be empty)
  * ARTIFACTORY_URL - URL to artifactory
  * ARTIFACTORY_NAMESPACE - Artifactory namespace (oss, cicd,...)
+ * UPLOAD_TO_DOCKER_HUB    - True\False
  * REGISTRY_CREDENTIALS_ID - Docker hub credentials id
  *
-**/
+ **/
 
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
 def git = new com.mirantis.mk.Git()
 def dockerLib = new com.mirantis.mk.Docker()
 def artifactory = new com.mirantis.mcp.MCPArtifactory()
+
+slaveNode = env.SLAVE_NODE ?: 'docker'
+uploadToDockerHub = env.UPLOAD_TO_DOCKER_HUB ?: false
+
 timeout(time: 12, unit: 'HOURS') {
-  node("docker") {
-    def workspace = common.getWorkspace()
-    def imageTagsList = IMAGE_TAGS.tokenize(" ")
-    try{
+    node(slaveNode) {
+        def workspace = common.getWorkspace()
+        def imageTagsList = env.IMAGE_TAGS.tokenize(" ")
+        try {
 
-      def buildArgs = []
-      try {
-        buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ')
-      } catch (Throwable e) {
-        buildArgs = []
-      }
-      def dockerApp
-        stage("checkout") {
-           git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID)
-        }
+            def buildArgs = []
+            try {
+                buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ')
+            } catch (Throwable e) {
+                buildArgs = []
+            }
+            def dockerApp
+            stage("checkout") {
+                git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID)
+            }
 
-        if (IMAGE_BRANCH == "master") {
-          try {
-            def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim()
-            def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim()
-            imageTagsList << tag
-            revision = revision ? revision : "0"
-            if(Integer.valueOf(revision) > 0){
-              imageTagsList << "${tag}-${revision}"
+            if (IMAGE_BRANCH == "master") {
+                try {
+                    def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim()
+                    def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim()
+                    imageTagsList << tag
+                    revision = revision ? revision : "0"
+                    if (Integer.valueOf(revision) > 0) {
+                        imageTagsList << "${tag}-${revision}"
+                    }
+                    if (!imageTagsList.contains("latest")) {
+                        imageTagsList << "latest"
+                    }
+                } catch (Exception e) {
+                    common.infoMsg("Impossible to find any tag")
+                }
             }
-            if (!imageTagsList.contains("latest")) {
-              imageTagsList << "latest"
-              //workaround for all of our docker images
-              imageTagsList << "nightly"
-            }
-          } catch (Exception e) {
-            common.infoMsg("Impossible to find any tag")
-          }
-        }
 
-        stage("build") {
-          common.infoMsg("Building docker image ${IMAGE_NAME}")
-          dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
-          if(!dockerApp){
-            throw new Exception("Docker build image failed")
-          }
-        }
-        stage("upload to docker hub"){
-          docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
-            for(int i=0;i<imageTagsList.size();i++){
-              common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]} to dockerhub")
-              dockerApp.push(imageTagsList[i])
+            stage("build") {
+                common.infoMsg("Building docker image ${IMAGE_NAME}")
+                dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
+                if (!dockerApp) {
+                    throw new Exception("Docker build image failed")
+                }
             }
-          }
+            stage("upload to docker hub") {
+                if (uploadToDockerHub) {
+                    docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
+                        for (int i = 0; i < imageTagsList.size(); i++) {
+                            common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]} to dockerhub")
+                            dockerApp.push(imageTagsList[i])
+                        }
+                    }
+                } else {
+                    common.infoMsg('upload to docker hub skipped')
+                }
+            }
+            stage("upload to artifactory") {
+                if (common.validInputParam("ARTIFACTORY_URL") && common.validInputParam("ARTIFACTORY_NAMESPACE")) {
+                    def artifactoryName = "mcp-ci";
+                    def artifactoryServer = Artifactory.server(artifactoryName)
+                    def shortImageName = IMAGE_NAME
+                    if (IMAGE_NAME.contains("/")) {
+                        shortImageName = IMAGE_NAME.tokenize("/")[1]
+                    }
+                    for (imageTag in imageTagsList) {
+                        sh "docker tag ${IMAGE_NAME}:${imageTagsList[0]} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}"
+                        for (artifactoryRepo in ["docker-dev-local", "docker-prod-local"]) {
+                            common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTag} to artifactory ${artifactoryName} using repo ${artifactoryRepo}")
+                            artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL,
+                                "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}",
+                                imageTag, artifactoryRepo)
+                        }
+                    }
+                } else {
+                    common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped")
+                }
+            }
+        } 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
+        } finally {
+            common.sendNotification(currentBuild.result, "", ["slack"])
         }
-        stage("upload to artifactory"){
-          if(common.validInputParam("ARTIFACTORY_URL") && common.validInputParam("ARTIFACTORY_NAMESPACE")) {
-             def artifactoryName = "mcp-ci";
-             def artifactoryServer = Artifactory.server(artifactoryName)
-             def shortImageName = IMAGE_NAME
-             if (IMAGE_NAME.contains("/")) {
-                shortImageName = IMAGE_NAME.tokenize("/")[1]
-             }
-             for (imageTag in imageTagsList) {
-               sh "docker tag ${IMAGE_NAME} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}"
-               for(artifactoryRepo in ["docker-dev-local", "docker-prod-local"]){
-                 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTag} to artifactory ${artifactoryName} using repo ${artifactoryRepo}")
-                 artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL,
-                                                   "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}",
-                                                   imageTag, artifactoryRepo)
-               }
-             }
-          }else{
-            common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped")
-          }
-        }
-    } 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
-    } finally {
-       common.sendNotification(currentBuild.result,"",["slack"])
     }
-  }
 }
diff --git a/docker-mirror-images.groovy b/docker-mirror-images.groovy
index 07a80e7..d88c9d1 100644
--- a/docker-mirror-images.groovy
+++ b/docker-mirror-images.groovy
@@ -8,51 +8,101 @@
  *   REGISTRY_URL                              Target Docker Registry URL
  *   IMAGE_TAG                                 Tag to use when pushing images
  *   SOURCE_IMAGE_TAG                          Tag to use when pulling images(optional,if SOURCE_IMAGE_TAG has been found)
+ *   SET_DEFAULT_ARTIFACTORY_PROPERTIES        Add extra props. directly to artifactory,
  *   IMAGE_LIST                                List of images to mirror
+ *     Example: docker.elastic.co/elasticsearch/elasticsearch:5.4.1 docker-prod-local.docker.mirantis.net/mirantis/external/docker.elastic.co/elasticsearch
+ *              docker.elastic.co/elasticsearch/elasticsearch:SUBS_SOURCE_IMAGE_TAG docker-prod-local.docker.mirantis.net/mirantis/external/elasticsearch:${IMAGE_TAG}*              Will be proceed like:
+ *              docker tag docker.elastic.co/elasticsearch/elasticsearch:5.4.1 docker-prod-local.docker.mirantis.net/mirantis/external/docker.elastic.co/elasticsearch/elasticsearch:5.4.1
+ *
  *
  */
-import java.util.regex.Pattern;
+import java.util.regex.Pattern
+import groovy.json.JsonSlurper
 
-def common = new com.mirantis.mk.Common()
+common = new com.mirantis.mk.Common()
+external = false
+externalMarker = '/mirantis/external/'
 
-@NonCPS
+slaveNode = env.SLAVE_NODE ?: 'docker'
+setDefaultArtifactoryProperties = env.SET_DEFAULT_ARTIFACTORY_PROPERTIES ?: true
+
 def getImageName(String image) {
     def regex = Pattern.compile('(?:.+/)?([^:]+)(?::.+)?')
     def matcher = regex.matcher(image)
-    if(matcher.find()){
+    if (matcher.find()) {
         def imageName = matcher.group(1)
         return imageName
-    }else{
-        throw new IllegalArgumentException("Wrong format of image name.")
+    } else {
+        error("Wrong format of image name.")
     }
 }
-timeout(time: 12, unit: 'HOURS') {
-    node("docker") {
+
+timeout(time: 4, unit: 'HOURS') {
+    node(slaveNode) {
         try {
-            stage("Mirror Docker Images"){
-                def creds = common.getPasswordCredentials(TARGET_REGISTRY_CREDENTIALS_ID)
-                sh "docker login --username=${creds.username} --password=${creds.password.toString()} ${REGISTRY_URL}"
+            stage("Mirror Docker Images") {
+
                 def images = IMAGE_LIST.tokenize('\n')
-                def imageName, imagePath, targetRegistry, imageArray
-                for (image in images){
-                    if(image.trim().indexOf(' ') == -1){
-                        throw new IllegalArgumentException("Wrong format of image and target repository input")
+                def imageName, sourceImage, targetRegistryPath, imageArray
+                for (image in images) {
+                    if (image.trim().indexOf(' ') == -1) {
+                        error("Wrong format of image and target repository input")
                     }
                     imageArray = image.trim().tokenize(' ')
-                    imagePath = imageArray[0]
-                    if (imagePath.contains('SUBS_SOURCE_IMAGE_TAG')) {
-                        common.warningMsg("Replacing SUBS_SOURCE_IMAGE_TAG => ${SOURCE_IMAGE_TAG}")
-                        imagePath = imagePath.replace('SUBS_SOURCE_IMAGE_TAG', SOURCE_IMAGE_TAG)
+                    sourceImage = imageArray[0]
+                    if (sourceImage.contains('SUBS_SOURCE_IMAGE_TAG')) {
+                        common.warningMsg("Replacing SUBS_SOURCE_IMAGE_TAG => ${env.SOURCE_IMAGE_TAG}")
+                        sourceImage = sourceImage.replace('SUBS_SOURCE_IMAGE_TAG', env.SOURCE_IMAGE_TAG)
                     }
-                    targetRegistry = imageArray[1]
-                    imageName = getImageName(imagePath)
-                    sh """docker pull ${imagePath}
-                          docker tag ${imagePath} ${targetRegistry}/${imageName}:${IMAGE_TAG}
-                          docker push ${targetRegistry}/${imageName}:${IMAGE_TAG}"""
+                    targetRegistryPath = imageArray[1]
+                    targetRegistry = imageArray[1].split('/')[0]
+                    imageName = getImageName(sourceImage)
+                    targetImageFull = "${targetRegistryPath}/${imageName}:${env.IMAGE_TAG}"
+                    srcImage = docker.image(sourceImage)
+                    srcImage.pull()
+                    // Use sh-docker call for tag, due magic code in plugin:
+                    // https://github.com/jenkinsci/docker-workflow-plugin/blob/docker-workflow-1.17/src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy#L168-L170
+                    sh("docker tag ${srcImage.id} ${targetImageFull}")
+                    common.infoMsg("Attempt to push docker image into remote registry: ${env.REGISTRY_URL}")
+                    sh("docker push ${targetImageFull}")
+                    if (targetImageFull.contains(externalMarker)) {
+                        external = true
+                    }
+
+                    if (setDefaultArtifactoryProperties) {
+                        common.infoMsg("Processing artifactory props for : ${targetImageFull}")
+                        LinkedHashMap artifactoryProperties = [:]
+                        // Get digest of pushed image
+                        String unique_image_id = sh(
+                            script: "docker inspect --format='{{index .RepoDigests 0}}' '${targetImageFull}'",
+                            returnStdout: true,
+                        ).trim()
+                        def image_sha256 = unique_image_id.tokenize(':')[1]
+                        def ret = new URL("https://${targetRegistry}/artifactory/api/search/checksum?sha256=${image_sha256}").getText()
+                        // Most probably, we would get many images, especially for external images. We need to guess
+                        // exactly one, which we pushing now
+                        guessImage = targetImageFull.replace(':', '/').replace(targetRegistry, '')
+                        ArrayList img_data = new JsonSlurper().parseText(ret)['results']
+                        img_data*.uri.each { imgUrl ->
+                            if (imgUrl.contains(guessImage)) {
+                                artifactoryProperties = [
+                                    'com.mirantis.targetTag'    : env.IMAGE_TAG,
+                                    'com.mirantis.uniqueImageId': unique_image_id,
+                                ]
+                                if (external) {
+                                    artifactoryProperties << ['com.mirantis.externalImage': external]
+                                }
+                                common.infoMsg("artifactoryProperties=> ${artifactoryProperties}")
+                                // Call pipeline-library routine to set properties
+                                def mcp_artifactory = new com.mirantis.mcp.MCPArtifactory()
+                                mcp_artifactory.setProperties(imgUrl - '/manifest.json', artifactoryProperties)
+                            }
+                        }
+                    }
                 }
             }
         } catch (Throwable e) {
-            // If there was an error or exception thrown, the build failed
+            // Stub for future processing
             currentBuild.result = "FAILURE"
             throw e
         }
diff --git a/gating-pipeline.groovy b/gating-pipeline.groovy
index e42524b..99f487d 100644
--- a/gating-pipeline.groovy
+++ b/gating-pipeline.groovy
@@ -3,79 +3,83 @@
  * CREDENTIALS_ID - Gerrit credentails ID
  * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...)
  *
-**/
+ **/
 
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
 def ssh = new com.mirantis.mk.Ssh()
-timeout(time: 12, unit: 'HOURS') {
-  node("python") {
-    try{
-      // test if change is not already merged
-      ssh.prepareSshAgentKey(CREDENTIALS_ID)
-      ssh.ensureKnownHosts(GERRIT_HOST)
-      def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
-      def doSubmit = false
-      def giveVerify = false
-      stage("test") {
-        if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){
-          // test max CodeReview
-          if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Code-Review", "+")){
-            doSubmit = true
-            def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
-            def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
-            def jobsNamespace = JOBS_NAMESPACE
-            def plural_namespaces = ['salt-formulas', 'salt-models']
-            // remove plural s on the end of job namespace
-            if (JOBS_NAMESPACE in plural_namespaces){
-              jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
-            }
-            // salt-formulas tests have -latest on end of the name
-            if(JOBS_NAMESPACE.equals("salt-formulas")){
-              gerritProject=gerritProject+"-latest"
-            }
-            def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
-            if (_jobExists(testJob)) {
-              common.infoMsg("Test job ${testJob} found, running")
-              def patchsetVerified =  gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")
-              build job: testJob, parameters: [
-                [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
-                [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
-              ]
-              giveVerify = true
-            } else {
-              common.infoMsg("Test job ${testJob} not found")
-            }
-          } else {
-            common.errorMsg("Change don't have a CodeReview, skipping gate")
-          }
-        } else {
-          common.infoMsg("Test job skipped")
-        }
-      }
-      stage("submit review"){
-        if(gerritChange.status == "MERGED"){
-          common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
-        }else if(doSubmit){
-          if(giveVerify){
-            common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
-            ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --verified +1 --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
-          }else{
-            ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
-          }
-          common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
-        }
-      }
-    } 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
-    }
-  }
-}
+
 
 @NonCPS
-def _jobExists(jobName){
-  return Jenkins.instance.items.find{it -> it.name.equals(jobName)}
+def isJobExists(jobName) {
+    return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
 }
+
+slaveNode = env.SLAVE_NODE ?: 'docker'
+
+timeout(time: 12, unit: 'HOURS') {
+    node(slaveNode) {
+        try {
+            // test if change is not already merged
+            ssh.prepareSshAgentKey(CREDENTIALS_ID)
+            ssh.ensureKnownHosts(GERRIT_HOST)
+            def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
+            def doSubmit = false
+            def giveVerify = false
+            stage("test") {
+                if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")) {
+                    // test max CodeReview
+                    if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
+                        doSubmit = true
+                        def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
+                        def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
+                        def jobsNamespace = JOBS_NAMESPACE
+                        def plural_namespaces = ['salt-formulas', 'salt-models']
+                        // remove plural s on the end of job namespace
+                        if (JOBS_NAMESPACE in plural_namespaces) {
+                            jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
+                        }
+                        // salt-formulas tests have -latest on end of the name
+                        if (JOBS_NAMESPACE.equals("salt-formulas")) {
+                            gerritProject = gerritProject + "-latest"
+                        }
+                        def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
+                        if (isJobExists(testJob)) {
+                            common.infoMsg("Test job ${testJob} found, running")
+                            def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")
+                            build job: testJob, parameters: [
+                                [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
+                                [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
+                            ]
+                            giveVerify = true
+                        } else {
+                            common.infoMsg("Test job ${testJob} not found")
+                        }
+                    } else {
+                        common.errorMsg("Change don't have a CodeReview, skipping gate")
+                    }
+                } else {
+                    common.infoMsg("Test job skipped")
+                }
+            }
+            stage("submit review") {
+                if (gerritChange.status == "MERGED") {
+                    common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
+                } else if (doSubmit) {
+                    if (giveVerify) {
+                        common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
+                        ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --verified +1 --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
+                    } else {
+                        ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
+                    }
+                    common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
+                }
+            }
+        } 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
+        }
+    }
+}
\ No newline at end of file
diff --git a/generate-cookiecutter-products.groovy b/generate-cookiecutter-products.groovy
index 549a4d3..5cc1689 100644
--- a/generate-cookiecutter-products.groovy
+++ b/generate-cookiecutter-products.groovy
@@ -104,31 +104,19 @@
             for (product in productList) {
 
                 // get templateOutputDir and productDir
-                if (product.startsWith("stacklight")) {
-                    templateOutputDir = "${env.WORKSPACE}/output/stacklight"
-
-                    def stacklightVersion
-                    try {
-                        stacklightVersion = templateContext.default_context['stacklight_version']
-                    } catch (Throwable e) {
-                        common.warningMsg('Stacklight version loading failed')
-                    }
-
-                    if (stacklightVersion) {
-                        productDir = "stacklight" + stacklightVersion
-                    } else {
-                        productDir = "stacklight1"
-                    }
-
-                } else {
-                    templateOutputDir = "${env.WORKSPACE}/output/${product}"
-                    productDir = product
+                templateOutputDir = "${env.WORKSPACE}/output/${product}"
+                productDir = product
+                templateDir = "${templateEnv}/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}"
                 }
 
                 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
                     && templateContext.default_context["${product}_enabled"].toBoolean())) {
 
-                    templateDir = "${templateEnv}/cluster_product/${productDir}"
                     common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
 
                     sh "rm -rf ${templateOutputDir} || true"
@@ -179,12 +167,12 @@
                     common.infoMsg("Attempt to run test against formula-version: ${mcpVersion}")
                     try {
                         def config = [
-                            'dockerHostname': "${saltMaster}.${clusterDomain}",
-                            'reclassEnv': testEnv,
-                            'formulasRevision': mcpVersion,
-                            'reclassVersion': reclassVersion,
+                            'dockerHostname'     : "${saltMaster}.${clusterDomain}",
+                            'reclassEnv'         : testEnv,
+                            'formulasRevision'   : mcpVersion,
+                            'reclassVersion'     : reclassVersion,
                             'dockerContainerName': DockerCName,
-                            'testContext': 'salt-model-node'
+                            'testContext'        : 'salt-model-node'
                         ]
                         testResult = saltModelTesting.testNode(config)
                         common.infoMsg("Test finished: SUCCESS")
@@ -201,7 +189,7 @@
 
                 // download create-config-drive
                 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
-                def mcpCommonScriptsBranch = templateContext.default_context.mcp_common_scripts_branch
+                def mcpCommonScriptsBranch = templateContext['default_context']['mcp_common_scripts_branch']
                 if (mcpCommonScriptsBranch == '') {
                     mcpCommonScriptsBranch = mcpVersion
                     // Don't have n/t/s for mcp-common-scripts repo, therefore use master
@@ -210,16 +198,21 @@
                         mcpCommonScriptsBranch = 'master'
                     }
                 }
-                def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/create_config_drive.sh"
-                def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/master_config.sh"
-                common.retry(3, 5) {
-                    sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
-                    sh "wget -O user_data.sh ${user_data_script_url}"
-                }
+
+                def commonScriptsRepoUrl = 'https://gerrit.mcp.mirantis.net/mcp/mcp-common-scripts'
+                checkout([
+                    $class           : 'GitSCM',
+                    branches         : [[name: 'FETCH_HEAD'],],
+                    extensions       : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
+                    userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: mcpCommonScriptsBranch],],
+                ])
+
+                sh "cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive"
+                sh "[ -f mcp-common-scripts/config-drive/master_config.sh ] && cp mcp-common-scripts/config-drive/master_config.sh user_data || cp mcp-common-scripts/config-drive/master_config.yaml user_data"
 
                 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
                 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
-                args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
+                args = "--user-data user_data --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
 
                 // load data from model
                 def smc = [:]
@@ -251,7 +244,7 @@
                 }
 
                 for (i in common.entries(smc)) {
-                    sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
+                    sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
                 }
 
                 // create cfg config-drive
@@ -263,8 +256,7 @@
 
                 if (templateContext['default_context']['local_repositories'] == 'True') {
                     def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
-                    def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
-                    sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
+                    sh "cp mcp-common-scripts/config-drive/mirror_config.sh mirror_config.sh"
 
                     def smc_apt = [:]
                     smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
@@ -289,7 +281,6 @@
                 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
                 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
 
-
                 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
                     emailext(to: EMAIL_ADDRESS,
                         attachmentsPattern: "output-${clusterName}/*",
diff --git a/opencontrail40-upgrade.groovy b/opencontrail40-upgrade.groovy
index 76243e5..bf35d97 100644
--- a/opencontrail40-upgrade.groovy
+++ b/opencontrail40-upgrade.groovy
@@ -26,13 +26,16 @@
 def command = 'cmd.shell'
 
 def controlPkgs = 'contrail-config,contrail-config-openstack,contrail-control,contrail-dns,contrail-lib,contrail-nodemgr,contrail-utils,contrail-web-controller,contrail-web-core,neutron-plugin-contrail,python-contrail,contrail-database'
+def thirdPartyControlPkgsToRemove = 'redis-server,ifmap-server,supervisor'
 def analyticsPkgs = 'contrail-analytics,contrail-lib,contrail-nodemgr,contrail-utils,python-contrail,contrail-database'
+def thirdPartyAnalyticsPkgsToRemove = 'redis-server,supervisor'
 //def cmpPkgs = ['contrail-lib', 'contrail-nodemgr', 'contrail-utils', 'contrail-vrouter-agent', 'contrail-vrouter-utils', 'python-contrail', 'python-contrail-vrouter-api', 'python-opencontrail-vrouter-netns', 'contrail-vrouter-dkms']
 def CMP_PKGS = 'contrail-lib contrail-nodemgr contrail-utils contrail-vrouter-agent contrail-vrouter-utils python-contrail python-contrail-vrouter-api python-opencontrail-vrouter-netns contrail-vrouter-dkms'
 def KERNEL_MODULE_RELOAD = 'service supervisor-vrouter stop;ifdown vhost0;rmmod vrouter;modprobe vrouter;ifup vhost0;service supervisor-vrouter start;'
-def analyticsServices = ['supervisor-analytics', 'supervisor-database', 'zookeeper']
+def analyticsServices = ['supervisor-analytics', 'supervisor-database', 'zookeeper', 'redis-server']
 def configServices = ['contrail-webui-jobserver', 'contrail-webui-webserver', 'supervisor-config', 'supervisor-database', 'zookeeper']
-def controlServices = ['ifmap-server', 'supervisor-control']
+def controlServices = ['ifmap-server', 'supervisor-control', 'redis-server']
+def thirdPartyServicesToDisable = ['kafka', 'zookeeper', 'cassandra']
 def config4Services = ['zookeeper', 'contrail-webui-middleware', 'contrail-webui', 'contrail-api', 'contrail-schema', 'contrail-svc-monitor', 'contrail-device-manager', 'contrail-config-nodemgr', 'contrail-database']
 
 def void runCommonCommands(target, command, args, check, salt, pepperEnv, common) {
@@ -107,7 +110,7 @@
                     common.errorMsg('Cassandra failed to backup. Please fix it before continuing.')
                     throw er
                 }
-                
+
                 salt.runSaltProcessStep(pepperEnv, 'I@neutron:server', 'service.stop', ['neutron-server'])
 
                 try {
@@ -167,14 +170,14 @@
             salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'archive.tar', ['zcvf', '/root/contrail-zookeeper.tgz', '/var/lib/zoopeeker'])
             salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:collector', 'archive.tar', ['zcvf', '/root/contrail-analytics-database.tgz', '/var/lib/cassandra'])
             salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:collector', 'archive.tar', ['zcvf', '/root/contrail-analytics-zookeeper.tgz', '/var/lib/zookeeper'])
-            //salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'pkg.remove', [controlPkgs])
-            //salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:collector', 'pkg.remove', [analyticsPkgs])
-            for (service in controlServices) {
+            for (service in (controlServices + thirdPartyServicesToDisable)) {
                 salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'service.disable', [service])
             }
-            for (service in analyticsServices) {
+            for (service in (analyticsServices + thirdPartyServicesToDisable)) {
                 salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:collector', 'service.disable', [service])
-                }
+            }
+            salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'pkg.remove', [controlPkgs + ',' + thirdPartyControlPkgsToRemove])
+            salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:collector', 'pkg.remove', [analyticsPkgs + ',' + thirdPartyAnalyticsPkgsToRemove])
         }
 
 
@@ -305,6 +308,12 @@
 
                 salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control and *01*', 'cmd.shell', ['cd /etc/docker/compose/opencontrail/; docker-compose down'], null, true)
                 salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control and *01*', 'state.sls', ['opencontrail', 'exclude=opencontrail.client'])
+                for (service in (controlServices + thirdPartyServicesToDisable)) {
+                    salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'service.enable', [service])
+                }
+                for (service in (analyticsServices + thirdPartyServicesToDisable)) {
+                    salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:collector', 'service.enable', [service])
+                }
             }
         }
 
diff --git a/test-cookiecutter-reclass.groovy b/test-cookiecutter-reclass.groovy
index b380bfd..becfb15 100644
--- a/test-cookiecutter-reclass.groovy
+++ b/test-cookiecutter-reclass.groovy
@@ -17,7 +17,8 @@
 git = new com.mirantis.mk.Git()
 python = new com.mirantis.mk.Python()
 
-slaveNode = env.SLAVE_NODE ?: 'python&&docker'
+slaveNode = env.SLAVE_NODE ?: 'docker'
+checkIncludeOrder = env.CHECK_INCLUDE_ORDER ?: false
 
 // Global var's
 alreadyMerged = false
@@ -32,12 +33,14 @@
                  GERRIT_CHANGE_NUMBER: null]
 //
 //ccTemplatesRepo = env.COOKIECUTTER_TEMPLATE_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/mk/cookiecutter-templates'
+gerritDataCCHEAD = [:]
 gerritDataCC = [:]
 gerritDataCC << gerritConData
 gerritDataCC['gerritBranch'] = env.COOKIECUTTER_TEMPLATE_BRANCH ?: 'master'
 gerritDataCC['gerritProject'] = 'mk/cookiecutter-templates'
 //
 //reclassSystemRepo = env.RECLASS_SYSTEM_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/salt-models/reclass-system'
+gerritDataRSHEAD = [:]
 gerritDataRS = [:]
 gerritDataRS << gerritConData
 gerritDataRS['gerritBranch'] = env.RECLASS_MODEL_BRANCH ?: 'master'
@@ -98,22 +101,18 @@
         for (product in productList) {
 
             // get templateOutputDir and productDir
-            if (product.startsWith("stacklight")) {
-                templateOutputDir = "${templateEnvDir}/output/stacklight"
-                try {
-                    productDir = "stacklight" + templateContext.default_context['stacklight_version']
-                } catch (Throwable e) {
-                    productDir = "stacklight1"
-                }
-            } else {
-                templateOutputDir = "${templateEnvDir}/output/${product}"
-                productDir = product
+            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}"
             }
-
             if (product == "infra" || (templateContext.default_context["${product}_enabled"]
                 && templateContext.default_context["${product}_enabled"].toBoolean())) {
 
-                templateDir = "${templateEnvDir}/cluster_product/${productDir}"
                 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
 
                 sh "rm -rf ${templateOutputDir} || true"
@@ -134,7 +133,7 @@
     return {
         dir(copyTo) {
             copyArtifacts(projectName: jobName, selector: specific(build), filter: "nodesinfo.tar.gz")
-            sh "tar -xvf nodesinfo.tar.gz"
+            sh "tar -xf nodesinfo.tar.gz"
             sh "rm -v nodesinfo.tar.gz"
         }
     }
@@ -251,20 +250,23 @@
             "<br/>Test env variables has been changed:" +
             "<br/>COOKIECUTTER_TEMPLATE_BRANCH => ${gerritDataCC['gerritBranch']}" +
             "<br/>DISTRIB_REVISION =>${testDistribRevision}" +
-            "<br/>RECLASS_MODEL_BRANCH=> ${gerritDataRS['gerritBranch']}" + message
+            "<br/>RECLASS_MODEL_BRANCH=> ${gerritDataRS['gerritBranch']}" + message + "<br/>"
         common.warningMsg(message)
         currentBuild.description = currentBuild.description ? message + "<br/>" + currentBuild.description : message
     } else {
         // Check for passed variables:
-        if (env.RECLASS_SYSTEM_GIT_REF) {
-            gerritDataRS['gerritRefSpec'] = RECLASS_SYSTEM_GIT_REF
-        }
-        if (env.COOKIECUTTER_TEMPLATE_REF) {
-            gerritDataCC['gerritRefSpec'] = COOKIECUTTER_TEMPLATE_REF
-        }
-        message = "<font color='red'>Manual run detected!</font>" + "<br/>"
+        gerritDataRS['gerritRefSpec'] = env.RECLASS_SYSTEM_GIT_REF ?: null
+        gerritDataCC['gerritRefSpec'] = env.COOKIECUTTER_TEMPLATE_REF ?: null
+        message = "<font color='red'>Non-gerrit trigger run detected!</font>" + "<br/>"
         currentBuild.description = currentBuild.description ? message + "<br/>" + currentBuild.description : message
     }
+    gerritDataCCHEAD << gerritDataCC
+    gerritDataCCHEAD['gerritRefSpec'] = null
+    gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
+    gerritDataRSHEAD << gerritDataRS
+    gerritDataRSHEAD['gerritRefSpec'] = null
+    gerritDataRSHEAD['GERRIT_CHANGE_NUMBER'] = null
+
 }
 
 def replaceGeneratedValues(path) {
@@ -293,35 +295,32 @@
     // tar.gz
     // ├── contexts
     // │   └── ceph.yml
-    // ├── global_reclass <<< reclass system
+    // ├── ${reclassDirName} <<< reclass system
     // ├── model
     // │   └── ceph       <<< from `context basename`
     // │       ├── classes
     // │       │   ├── cluster
-    // │       │   └── system -> ../../../global_reclass
+    // │       │   └── system -> ../../../${reclassDirName}
     // │       └── nodes
     // │           └── cfg01.ceph-cluster-domain.local.yml
     dir(envPath) {
         for (String context : contextList) {
             def basename = common.GetBaseName(context, '.yml')
             dir("${envPath}/model/${basename}") {
-                sh(script: 'mkdir -p classes/; ln -sfv ../../../../global_reclass classes/system ')
+                sh(script: "mkdir -p classes/; ln -sfv ../../../../${common.GetBaseName(archiveName, '.tar.gz')} classes/system ")
             }
         }
         // replace all generated passwords/secrets/keys with hardcode value for infra/secrets.yaml
         replaceGeneratedValues("${envPath}/model")
-        // Save all models and all contexts. Warning! `h` flag must be used.
-        sh(script: "set -ex; tar -chzf ${archiveName} --exclude='*@tmp' model contexts", returnStatus: true)
-        archiveArtifacts artifacts: archiveName
-        // move for "Compare Pillars" stage
-        sh(script: "mv -v ${archiveName} ${env.WORKSPACE}")
+        // Save all models and all contexts. Warning! `h` flag must be used!
+        sh(script: "set -ex; tar -czhf ${env.WORKSPACE}/${archiveName} --exclude='*@tmp' model contexts", returnStatus: true)
     }
+    archiveArtifacts artifacts: archiveName
 }
 
 timeout(time: 1, unit: 'HOURS') {
     node(slaveNode) {
         globalVariatorsUpdate()
-        def gerritDataCCHEAD = [:]
         def templateEnvHead = "${env.WORKSPACE}/EnvHead/"
         def templateEnvPatched = "${env.WORKSPACE}/EnvPatched/"
         def contextFileListHead = []
@@ -338,16 +337,24 @@
                 // Prepare 2 env - for patchset, and for HEAD
                 def paralellEnvs = [:]
                 paralellEnvs.failFast = true
-                paralellEnvs['downloadEnvPatched'] = StepPrepareGit(templateEnvPatched, gerritDataCC)
-                gerritDataCCHEAD << gerritDataCC
-                gerritDataCCHEAD['gerritRefSpec'] = null; gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
                 paralellEnvs['downloadEnvHead'] = StepPrepareGit(templateEnvHead, gerritDataCCHEAD)
-                parallel paralellEnvs
+                if (gerritDataCC.get('gerritRefSpec', null)) {
+                    paralellEnvs['downloadEnvPatched'] = StepPrepareGit(templateEnvPatched, gerritDataCC)
+                    parallel paralellEnvs
+                } else {
+                    paralellEnvs['downloadEnvPatched'] = { common.warningMsg('No need to process: downloadEnvPatched') }
+                    parallel paralellEnvs
+                    sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
+                }
             }
             stage("Check workflow_definition") {
                 // Check only for patchset
                 python.setupVirtualenv(vEnv, 'python2', [], "${templateEnvPatched}/requirements.txt")
-                common.infoMsg(python.runVirtualenvCommand(vEnv, "python ${templateEnvPatched}/workflow_definition_test.py"))
+                if (gerritDataCC.get('gerritRefSpec', null)) {
+                    common.infoMsg(python.runVirtualenvCommand(vEnv, "python ${templateEnvPatched}/workflow_definition_test.py"))
+                } else {
+                    common.infoMsg('No need to process: workflow_definition')
+                }
             }
 
             stage("generate models") {
@@ -364,18 +371,29 @@
                 // Generate over 2env's - for patchset, and for HEAD
                 def paralellEnvs = [:]
                 paralellEnvs.failFast = true
-                paralellEnvs['GenerateEnvPatched'] = StepGenerateModels(contextFileListPatched, vEnv, templateEnvPatched)
                 paralellEnvs['GenerateEnvHead'] = StepGenerateModels(contextFileListHead, vEnv, templateEnvHead)
-                parallel paralellEnvs
-
-                // Collect artifacts
-                dir(templateEnvPatched) {
-                    // Collect only models. For backward comparability - who know, probably someone use it..
-                    sh(script: "tar -czf model.tar.gz -C model ../contexts .", returnStatus: true)
-                    archiveArtifacts artifacts: "model.tar.gz"
+                if (gerritDataCC.get('gerritRefSpec', null)) {
+                    paralellEnvs['GenerateEnvPatched'] = StepGenerateModels(contextFileListPatched, vEnv, templateEnvPatched)
+                    parallel paralellEnvs
+                } else {
+                    paralellEnvs['GenerateEnvPatched'] = { common.warningMsg('No need to process: GenerateEnvPatched') }
+                    parallel paralellEnvs
+                    sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
                 }
 
-                StepPrepareGit("${env.WORKSPACE}/global_reclass/", gerritDataRS).call()
+                // We need 2 git's, one for HEAD, one for PATCHed.
+                // if no patch, use head for both
+                RSHeadDir = common.GetBaseName(headReclassArtifactName, '.tar.gz')
+                RSPatchedDir = common.GetBaseName(patchedReclassArtifactName, '.tar.gz')
+                common.infoMsg("gerritDataRS= ${gerritDataRS}")
+                common.infoMsg("gerritDataRSHEAD= ${gerritDataRSHEAD}")
+                if (gerritDataRS.get('gerritRefSpec', null)) {
+                    StepPrepareGit("${env.WORKSPACE}/${RSPatchedDir}/", gerritDataRS).call()
+                    StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRSHEAD).call()
+                } else {
+                    StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRS).call()
+                    sh("cd ${env.WORKSPACE} ; ln -svf ${RSHeadDir} ${RSPatchedDir}")
+                }
                 // link all models, to use one global reclass
                 // For HEAD
                 linkReclassModels(contextFileListHead, templateEnvHead, headReclassArtifactName)
@@ -392,7 +410,7 @@
                    tar -xzf ${headReclassArtifactName}  --directory ${compareRoot}/old
                    """)
                 common.warningMsg('infra/secrets.yml has been skipped from compare!')
-                result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml\'")
+                result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml|\\.git\'")
                 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
             }
             stage("TestContexts Head/Patched") {
@@ -427,6 +445,49 @@
                 result = '\n' + common.comparePillars(reclassNodeInfoDir, env.BUILD_URL, '')
                 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
             }
+            stage('Check include order') {
+                if (!checkIncludeOrder) {
+                    common.infoMsg('Check include order require to much time, and currently disabled!')
+
+                } else {
+                    def correctIncludeOrder = ["service", "system", "cluster"]
+                    dir(reclassInfoPatchedPath) {
+                        def nodeInfoFiles = findFiles(glob: "**/*.reclass.nodeinfo")
+                        def messages = ["<b>Wrong include ordering found</b><ul>"]
+                        def stepsForParallel = [:]
+                        nodeInfoFiles.each { nodeInfo ->
+                            stepsForParallel.put("Checking ${nodeInfo.path}:", {
+                                def node = readYaml file: nodeInfo.path
+                                def classes = node['classes']
+                                def curClassID = 0
+                                def prevClassID = 0
+                                def wrongOrder = false
+                                for (String className in classes) {
+                                    def currentClass = className.tokenize('.')[0]
+                                    curClassID = correctIncludeOrder.indexOf(currentClass)
+                                    if (currentClass != correctIncludeOrder[prevClassID]) {
+                                        if (prevClassID > curClassID) {
+                                            wrongOrder = true
+                                            common.warningMsg("File ${nodeInfo.path} contains wrong order of classes including: Includes for ${className} should be declared before ${correctIncludeOrder[prevClassID]} includes")
+                                        } else {
+                                            prevClassID = curClassID
+                                        }
+                                    }
+                                }
+                                if (wrongOrder) {
+                                    messages.add("<li>${nodeInfo.path} contains wrong order of classes including</li>")
+                                }
+                            })
+                        }
+                        parallel stepsForParallel
+                        def includerOrder = '<b>No wrong include order</b>'
+                        if (messages.size() != 1) {
+                            includerOrder = messages.join('')
+                        }
+                        currentBuild.description = currentBuild.description ? currentBuild.description + includerOrder : includerOrder
+                    }
+                }
+            }
             sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
 
         } catch (Throwable e) {
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index afd2857..47dde97 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -2,34 +2,17 @@
 def common = new com.mirantis.mk.Common()
 
 
-slaveNode = env.SLAVE_NODE ?: 'python&&docker'
+def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
+def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
 
-def gerritCredentials
-try {
-    gerritCredentials = CREDENTIALS_ID
-} catch (MissingPropertyException e) {
-    gerritCredentials = "gerrit"
-}
+def gerritRef = env.GERRIT_REFSPEC ?: null
+def defaultGitRef = env.DEFAULT_GIT_REF ?: null
+def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
 
-def gerritRef
-try {
-    gerritRef = GERRIT_REFSPEC
-} catch (MissingPropertyException e) {
-    gerritRef = null
-}
-
-def defaultGitRef, defaultGitUrl
-try {
-    defaultGitRef = DEFAULT_GIT_REF
-    defaultGitUrl = DEFAULT_GIT_URL
-} catch (MissingPropertyException e) {
-    defaultGitRef = null
-    defaultGitUrl = null
-}
 def checkouted = false
 def merged = false
 def systemRefspec = "HEAD"
-def formulasRevision = 'testing'
+
 timeout(time: 12, unit: 'HOURS') {
     node(slaveNode) {
         try {
@@ -67,35 +50,35 @@
 
                         def branches = [:]
                         def testModels = documentationOnly ? [] : TEST_MODELS.split(',')
-                        for (int i = 0; i < testModels.size(); i++) {
-                            def cluster = testModels[i]
-                            def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
-                            branches["${cluster}"] = {
-                                build job: "test-salt-model-${cluster}", parameters: [
-                                    [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
-                                    [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
-                                    [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
-                                    [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec],
-                                    [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: formulasRevision],
-                                ]
+                        if (['master'].contains(env.GERRIT_BRANCH)) {
+                            for (int i = 0; i < testModels.size(); i++) {
+                                def cluster = testModels[i]
+                                def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
+                                branches["${cluster}"] = {
+                                    build job: "test-salt-model-${cluster}", parameters: [
+                                        [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
+                                        [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
+                                        [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
+                                        [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: systemRefspec]
+                                    ]
+                                }
                             }
+                        } else {
+                            common.warningMsg("Tests for ${testModels} skipped!")
                         }
                         branches["cookiecutter"] = {
                             build job: "test-mk-cookiecutter-templates", parameters: [
                                 [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_URL', value: defaultGitUrl],
-                                [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: systemRefspec],
-                                [$class: 'StringParameterValue', name: 'DISTRIB_REVISION', value: formulasRevision]
-
+                                [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: systemRefspec]
                             ]
                         }
                         parallel branches
                     } else {
-                        throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+                        error("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
                     }
                 }
             }
         } 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