Merge "make commit optional"
diff --git a/cicd-lab-pipeline.groovy b/cicd-lab-pipeline.groovy
index adaedd0..a9f4edd 100644
--- a/cicd-lab-pipeline.groovy
+++ b/cicd-lab-pipeline.groovy
@@ -120,7 +120,9 @@
 
             stage("Deploy GlusterFS") {
                 salt.enforceState(saltMaster, 'I@glusterfs:server', 'glusterfs.server.service', true)
-                salt.enforceState(saltMaster, 'ci01*', 'glusterfs.server.setup', true)
+                retry(2) {
+                    salt.enforceState(saltMaster, 'ci01*', 'glusterfs.server.setup', true)
+                }
                 sleep(5)
                 salt.enforceState(saltMaster, 'I@glusterfs:client', 'glusterfs.client', true)
                 print common.prettyPrint(salt.cmdRun(saltMaster, 'I@glusterfs:client', 'mount|grep fuse.glusterfs || echo "Command failed"'))
@@ -140,6 +142,10 @@
                 print common.prettyPrint(salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'docker node ls'))
             }
 
+            stage("Configure OSS services") {
+                salt.enforceState(saltMaster, 'I@devops_portal:config', 'devops_portal.config')
+            }
+
             stage("Deploy Docker services") {
                 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client')
             }
@@ -259,10 +265,12 @@
 
     And visit services running at 172.16.10.254 (vip address):
 
-        9600    haproxy stats
-        8080    gerrit
-        8081    jenkins
+        9600    HAProxy statistics
+        8080    Gerrit
+        8081    Jenkins
         8089    LDAP administration
+        4440    Rundeck
+        8084    DevOps Portal
         8091    Docker swarm visualizer
         8090    Reclass-generated documentation
 
diff --git a/gating-pipeline.groovy b/gating-pipeline.groovy
index d3feae0..4efb430 100644
--- a/gating-pipeline.groovy
+++ b/gating-pipeline.groovy
@@ -11,9 +11,7 @@
 node("python") {
   try{
     stage("test") {
-      // TEST JOBS ARE DISABLED
-      // because you cannot pass GERRIT_REFSPEC like variables to another pipeline
-      if (false && !SKIP_TEST.equals("true")){
+      if (!SKIP_TEST.equals("true")){
         wrap([$class: 'AnsiColorBuildWrapper']) {
           def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
           def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
@@ -26,12 +24,8 @@
           if (_jobExists(testJob)) {
             common.infoMsg("Test job ${testJob} found, running")
             build job: testJob, parameters: [
-              [$class: 'StringParameterValue', name: 'GERRIT_BRANCH', value: GERRIT_BRANCH],
-              [$class: 'StringParameterValue', name: 'GERRIT_NAME', value: GERRIT_NAME],
-              [$class: 'StringParameterValue', name: 'GERRIT_HOST', value: GERRIT_HOST],
-              [$class: 'StringParameterValue', name: 'GERRIT_PORT', value: GERRIT_PORT],
-              [$class: 'StringParameterValue', name: 'GERRIT_PROJECT', value: GERRIT_PROJECT],
-              [$class: 'StringParameterValue', name: 'GERRIT_REFSPEC', value: GERRIT_REFSPEC]
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"],
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
             ]
           } else {
             common.infoMsg("Test job ${testJob} not found")
@@ -52,7 +46,7 @@
      currentBuild.result = "FAILURE"
      throw e
   } finally {
-     common.sendNotification(currentBuild.result,"",["slack"])
+     //common.sendNotification(currentBuild.result,"",["slack"])
   }
 }
 
diff --git a/lab-pipeline.groovy b/lab-pipeline.groovy
index b4014b6..5b7cc11 100644
--- a/lab-pipeline.groovy
+++ b/lab-pipeline.groovy
@@ -41,7 +41,7 @@
 openstack = new com.mirantis.mk.Openstack()
 salt = new com.mirantis.mk.Salt()
 common = new com.mirantis.mk.Common()
-
+test = new com.mirantis.mk.Test()
 
 timestamps {
     node {
diff --git a/test-groovy-pipeline.groovy b/test-groovy-pipeline.groovy
index 45404cb..71e9fc0 100644
--- a/test-groovy-pipeline.groovy
+++ b/test-groovy-pipeline.groovy
@@ -9,30 +9,50 @@
 gerrit = new com.mirantis.mk.Gerrit()
 common = new com.mirantis.mk.Common()
 
+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
+
 node("docker"){
     try {
         stage ('Checkout source code'){
-            gerrit.gerritPatchsetCheckout ([
-              credentialsId : CREDENTIALS_ID,
-              withWipeOut : true,
-              gerritRefSpec: GERRIT_REFSPEC,
-              gerritName: GERRIT_NAME,
-              gerritHost: GERRIT_HOST,
-              gerritPort: GERRIT_PORT,
-              gerritProject: GERRIT_PROJECT,
-              gerritBranch: GERRIT_BRANCH,
+          if (gerritRef) {
+            // job is triggered by Gerrit
+            checkouted = gerrit.gerritPatchsetCheckout ([
+              credentialsId : CREDENTIALS_ID
             ])
+          } else if(defaultGitRef && defaultGitUrl) {
+              checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
+          }
+          if(!checkouted){
+            common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+          }
         }
         stage ('Run Codenarc tests'){
-            def workspace = common.getWorkspace()
-            def jenkinsUID = common.getJenkinsUid()
-            def jenkinsGID = common.getJenkinsGid()
-            def gradle_report = sh (script: "docker run --rm -v ${workspace}:/usr/bin/app:rw -u ${jenkinsUID}:${jenkinsGID} ${GRADLE_IMAGE} ${GRADLE_CMD}", returnStdout: true).trim()
-            // Compilation failure doesn't fail the build
-            // Check gradle output explicitly
-            common.infoMsg(gradle_report)
-            if ( gradle_report =~ /Compilation failed/ ) {
-                throw new Exception("COMPILATION FAILED!")
+            if(checkouted){
+              def workspace = common.getWorkspace()
+              def jenkinsUID = common.getJenkinsUid()
+              def jenkinsGID = common.getJenkinsGid()
+              def gradle_report = sh (script: "docker run --rm -v ${workspace}:/usr/bin/app:rw -u ${jenkinsUID}:${jenkinsGID} ${GRADLE_IMAGE} ${GRADLE_CMD}", returnStdout: true).trim()
+              // Compilation failure doesn't fail the build
+              // Check gradle output explicitly
+              common.infoMsg(gradle_report)
+              if ( gradle_report =~ /Compilation failed/ ) {
+                  throw new Exception("COMPILATION FAILED!")
+              }
             }
         }
 
diff --git a/test-nodejs-pipeline.groovy b/test-nodejs-pipeline.groovy
index 7e31d33..35d8317 100644
--- a/test-nodejs-pipeline.groovy
+++ b/test-nodejs-pipeline.groovy
@@ -27,7 +27,7 @@
         stage ('Execute commands') {
             assert containerID != null
             def cmds = COMMANDS.tokenize('\n')
-            for (int i = 0; i < cmds.size(), i++) {
+            for (int i = 0; i < cmds.size(); i++) {
                def cmd = cmds[i]
                def output = sh(
                    script: "docker exec ${containerID} ${cmd}",
diff --git a/test-salt-formulas-pipeline.groovy b/test-salt-formulas-pipeline.groovy
index 2a3b2ac..91f977b 100644
--- a/test-salt-formulas-pipeline.groovy
+++ b/test-salt-formulas-pipeline.groovy
@@ -1,16 +1,51 @@
+/**
+ * Test salt formulas pipeline
+ *  DEFAULT_GIT_REF
+ *  DEFAULT_GIT_URL
+ *  CREDENTIALS_ID
+ */
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
+
+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;
+
 node("python") {
   try{
     stage("checkout") {
-      gerrit.gerritPatchsetCheckout ([
+      if (gerritRef) {
+        // job is triggered by Gerrit
+        checkouted = gerrit.gerritPatchsetCheckout ([
           credentialsId : CREDENTIALS_ID
-      ])
+        ])
+      } else if(defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
+      }
+      if(!checkouted){
+        common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+      }
     }
     stage("test") {
-      wrap([$class: 'AnsiColorBuildWrapper']) {
-        sh("make clean")
-        sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
+      if(checkouted){
+        wrap([$class: 'AnsiColorBuildWrapper']) {
+          sh("make clean")
+          sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
+        }
       }
     }
   } catch (Throwable e) {
diff --git a/test-salt-models-pipeline.groovy b/test-salt-models-pipeline.groovy
index 7ac0e81..05695b2 100644
--- a/test-salt-models-pipeline.groovy
+++ b/test-salt-models-pipeline.groovy
@@ -1,3 +1,11 @@
+
+/**
+ *  Test salt models pipeline
+ *  DEFAULT_GIT_REF
+ *  DEFAULT_GIT_URL
+ *  CREDENTIALS_ID
+ */
+
 def common = new com.mirantis.mk.Common()
 def gerrit = new com.mirantis.mk.Gerrit()
 def ssh = new com.mirantis.mk.Ssh()
@@ -10,53 +18,46 @@
   gerritRef = null
 }
 
-def systemGitRef, systemGitUrl
+def defaultGitRef, defaultGitUrl
 try {
-    systemGitRef = RECLASS_SYSTEM_GIT_REF
-    systemGitUrl = RECLASS_SYSTEM_GIT_URL
+    defaultGitRef = DEFAULT_GIT_REF
+    defaultGitUrl = DEFAULT_GIT_URL
 } catch (MissingPropertyException e) {
-    systemGitRef = null
-    systemGitUrl = null
+    defaultGitRef = null
+    defaultGitUrl = null
 }
+def checkouted = false
 
 node("python") {
   try{
     stage("checkout") {
       if (gerritRef) {
-        gerrit.gerritPatchsetCheckout ([
-          credentialsId : CREDENTIALS_ID,
-          gerritRefSpec: GERRIT_REFSPEC,
-          gerritName: GERRIT_NAME,
-          gerritHost: GERRIT_HOST,
-          gerritPort: GERRIT_PORT,
-          gerritProject: GERRIT_PROJECT,
-          gerritBranch: GERRIT_BRANCH,
+        // job is triggered by Gerrit
+        checkouted = gerrit.gerritPatchsetCheckout ([
+          credentialsId : CREDENTIALS_ID
         ])
-      } else {
-        git.checkoutGitRepository('.', GIT_URL, "master", CREDENTIALS_ID)
+      } else if(defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
       }
-
-      if (fileExists('classes/system')) {
-        ssh.prepareSshAgentKey(CREDENTIALS_ID)
-        dir('classes/system') {
-          remoteUrl = git.getGitRemote()
-          ssh.ensureKnownHosts(remoteUrl)
-        }
-        ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
-
-        if (systemGitRef) {
-          common.infoMsg("Fetching alternate system reclass (${systemGitUrl} ${systemGitRef})")
+      if(checkouted){
+        if (fileExists('classes/system')) {
+          ssh.prepareSshAgentKey(CREDENTIALS_ID)
           dir('classes/system') {
-            ssh.ensureKnownHosts(RECLASS_SYSTEM_GIT_URL)
-            ssh.agentSh("git fetch ${systemGitUrl} ${systemGitRef} && git checkout FETCH_HEAD")
+            remoteUrl = git.getGitRemote()
+            ssh.ensureKnownHosts(remoteUrl)
           }
+          ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
         }
+      }else{
+        common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
       }
     }
     stage("test") {
-      timeout(1440) {
-        wrap([$class: 'AnsiColorBuildWrapper']) {
-          sh("make test")
+      if(checkouted){
+        timeout(1440) {
+          wrap([$class: 'AnsiColorBuildWrapper']) {
+            sh("make test")
+          }
         }
       }
     }
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index 6094b43..a63a7df 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -8,29 +8,57 @@
     gerritCredentials = "gerrit"
 }
 
+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
+
 try {
   stage("Checkout") {
     node() {
-      gerrit.gerritPatchsetCheckout ([
-        credentialsId : gerritCredentials
-      ])
+      if (gerritRef) {
+        // job is triggered by Gerrit
+        checkouted = gerrit.gerritPatchsetCheckout ([
+          credentialsId : gerritCredentials
+        ])
+        // change defaultGit variables if job triggered from Gerrit
+        defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
+      } else if(defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", gerritCredentials)
+      }
     }
   }
 
   stage("Test") {
-    def branches = [:]
-    def testModels = TEST_MODELS.split(',')
-
-    for (int i = 0; i < testModels.size(); i++) {
-      def cluster = testModels[i]
-      branches["${cluster}"] = {
-        build job: "test-salt-model-${cluster}", parameters: [
-          [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"],
-          [$class: 'StringParameterValue', name: 'RECLASS_SYSTEM_GIT_REF', value: GERRIT_REFSPEC]
-        ]
-      }
+    if(checkouted){
+      def branches = [:]
+      def testModels = 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"]
+            ]
+          }
+        }
+      parallel branches
+    }else{
+       common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
     }
-    parallel branches
   }
 } catch (Throwable e) {
     // If there was an error or exception thrown, the build failed