Use checkout to COMMON_SCRIPTS_COMMIT in swarm-create-cfg-config-drive

- Drop using Jenkins csm plugin to clone and checkout repository
  because it gets wrong commit (from master) instead of specified
  in the COMMON_SCRIPTS_COMMIT.
- Use 'release/' prefix to checkout the repo branches instead
  of tags
- Use 'git branch -f' for pipeline-library and mk-pipelines
  to move the branch on the commit from REFSPEC
- Show checked out commit if refspec or commit is specified
- Remove old ISO on target node before downloading

Change-Id: I15ddffe63a0dddf524f49fc25d7f3d4b1b4d8aab
(cherry picked from commit e3884652d27a941779751dff03cfc7d483c8a56a)
diff --git a/jobs/pipelines/swarm-create-cfg-config-drive.groovy b/jobs/pipelines/swarm-create-cfg-config-drive.groovy
index 46d5010..b2901ee 100644
--- a/jobs/pipelines/swarm-create-cfg-config-drive.groovy
+++ b/jobs/pipelines/swarm-create-cfg-config-drive.groovy
@@ -40,30 +40,35 @@
                           usernameVariable: "GERRIT_USERNAME",
                           passwordVariable: "GERRIT_PASSWORD"]]) {
 
-            dir("mcp-common-scripts-git") {
-                cloned = gerrit.gerritPatchsetCheckout([
-                    credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
-                    gerritBranch: "${MCP_VERSION}",
-                    gerritRefSpec: "${MCP_COMMON_SCRIPTS_REFS}",
-                    gerritScheme: "ssh",
-                    gerritName: "${GERRIT_USERNAME}",
-                    gerritHost: "gerrit.mcp.mirantis.net",
-                    gerritPort: "29418",
-                    gerritProject: "mcp/mcp-common-scripts"
-                ])
-            }
-            if (!cloned) {
-                error("Failed to clone the repository mcp/mcp-common-scripts")
-            }
-
             sh ("""\
                 set -ex
                 eval \$(ssh-agent)
                 ssh-add ${GERRIT_KEY}
+                git clone ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp/mcp-common-scripts mcp-common-scripts-git
                 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mk/mk-pipelines mk-pipelines
                 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp-ci/pipeline-library pipeline-library
             """)
 
+            if (COMMON_SCRIPTS_COMMIT != '') {
+                sh ("""\
+                    set -ex
+                    cd mcp-common-scripts-git
+                    git checkout ${COMMON_SCRIPTS_COMMIT}
+                    git log -1
+                """)
+            }
+
+            if (MCP_COMMON_SCRIPTS_REFS != '') {
+                sh ("""\
+                    set -ex
+                    eval \$(ssh-agent)
+                    ssh-add ${GERRIT_KEY}
+                    cd mcp-common-scripts-git
+                    git fetch ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.com:29418/mcp/mcp-common-scripts ${MCP_COMMON_SCRIPTS_REFS} && git checkout FETCH_HEAD
+                    git log -1
+                """)
+            }
+
             if (PIPELINE_LIBRARY_REF != '') {
                 sh ("""\
                     set -ex
@@ -72,6 +77,8 @@
                     cd pipeline-library
                     git fetch https://gerrit.mcp.mirantis.net/mcp-ci/pipeline-library ${PIPELINE_LIBRARY_REF}
                     git tag ${MCP_VERSION} FETCH_HEAD -f
+                    git branch -f release/${MCP_VERSION} FETCH_HEAD
+                    git log -1
                 """)
             }
             if (MK_PIPELINES_REF != '') {
@@ -82,49 +89,12 @@
                     cd mk-pipelines
                     git fetch https://gerrit.mcp.mirantis.net/mcp-ci/mk-pipelines ${MK_PIPELINES_REF}
                     git tag ${MCP_VERSION} FETCH_HEAD -f
+                    git branch -f release/${MCP_VERSION} FETCH_HEAD
+                    git log -1
                 """)
             }
 
-            // dir("mk-pipelines-git") {
-            //     cloned = gerrit.gerritPatchsetCheckout([
-            //         credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
-            //         gerritRefSpec: "${MK_PIPELINES_REF}",
-            //         gerritScheme: "ssh",
-            //         gerritName: "${GERRIT_USERNAME}",
-            //         gerritHost: "gerrit.mcp.mirantis.net",
-            //         gerritPort: "29418",
-            //         gerritProject: "mk/mk-pipelines"
-            //     ])
-            // }
-            // if (!cloned) {
-            //     error("Failed to clone the repository mk/mk-pipelines")
-            // }
-
-            // dir("pipeline-library-git") {
-            //     cloned = gerrit.gerritPatchsetCheckout([
-            //         credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
-            //         gerritRefSpec: "${PIPELINE_LIBRARY_REF}",
-            //         gerritScheme: "ssh",
-            //         gerritName: "${GERRIT_USERNAME}",
-            //         gerritHost: "gerrit.mcp.mirantis.net",
-            //         gerritPort: "29418",
-            //         gerritProject: "mcp-ci/pipeline-library"
-            //     ])
-            // }
-            // if (!cloned) {
-            //     error("Failed to clone the repository mcp-ci/pipeline-library")
-            // }
         }
-        //if (PIPELINE_LIBRARY_REF != '') {
-        //   sh "cd pipeline-library; git tag ${MCP_VERSION} FETCH_HEAD -f ; cd .."
-        //}
-        //if (MK_PIPELINES_REF != '') {
-        //   sh "cd mk-pipelines; git tag ${MCP_VERSION} FETCH_HEAD -f; cd .."
-        //}
-
-        // gerrit.gerritPatchsetCheckout() doesn't support clonning bare repository
-        // sh "git clone --mirror mk-pipelines-git mk-pipelines"
-        // sh "git clone --mirror pipeline-library-git pipeline-library"
     }
 
     stage("Prepare arguments for generation config drive") {
@@ -247,6 +217,11 @@
 
     stage("Download config drive to slave") {
         if (DOWNLOAD_CONFIG_DRIVE == 'true') {
+            println "Remove previous config drive ISO"
+            sh("""\
+                rm /home/jenkins/images/${iso_name} || true
+            """)
+
             def b_res = build job: 'download-config-drive',
                 parameters: [
                         string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
diff --git a/src/com/mirantis/system_qa/SharedPipeline.groovy b/src/com/mirantis/system_qa/SharedPipeline.groovy
index 32ac0e1..7a5f036 100644
--- a/src/com/mirantis/system_qa/SharedPipeline.groovy
+++ b/src/com/mirantis/system_qa/SharedPipeline.groovy
@@ -152,8 +152,8 @@
 
 def swarm_bootstrap_salt_cluster_devops() {
         def common = new com.mirantis.mk.Common()
-        def cookiecutter_template_commit = env.COOKIECUTTER_TEMPLATE_COMMIT ?: env.MCP_VERSION
-        def salt_models_system_commit = env.SALT_MODELS_SYSTEM_COMMIT ?: env.MCP_VERSION
+        def cookiecutter_template_commit = env.COOKIECUTTER_TEMPLATE_COMMIT ?: "release/${env.MCP_VERSION}"
+        def salt_models_system_commit = env.SALT_MODELS_SYSTEM_COMMIT ?: "release/${env.MCP_VERSION}"
         def tcp_qa_refs = env.TCP_QA_REFS ?: ''
         def mk_pipelines_ref = env.MK_PIPELINES_REF ?: ''
         def pipeline_library_ref = env.PIPELINE_LIBRARY_REF ?: ''
@@ -286,8 +286,8 @@
         println("IPV4_NET_TENANT=" + IPV4_NET_TENANT)
         println("IPV4_NET_EXTERNAL=" + IPV4_NET_EXTERNAL)
 
-        def cookiecuttertemplate_commit = env.COOKIECUTTER_TEMPLATE_COMMIT ?: env.MCP_VERSION
-        def saltmodels_system_commit = env.SALT_MODELS_SYSTEM_COMMIT ?: env.MCP_VERSION
+        def cookiecuttertemplate_commit = env.COOKIECUTTER_TEMPLATE_COMMIT ?: "release/${env.MCP_VERSION}"
+        def saltmodels_system_commit = env.SALT_MODELS_SYSTEM_COMMIT ?: "release/${env.MCP_VERSION}"
         def tcp_qa_refs = env.TCP_QA_REFS ?: ''
         def environment_template_ref_change = env.ENVIRONMENT_TEMPLATE_REF_CHANGE ?: ''
         def cookiecutter_ref_change = env.COOKIECUTTER_REF_CHANGE ?: ''
@@ -344,7 +344,7 @@
                 string(name: 'MODEL_URL_OBJECT_TYPE', value: "git"),
                 booleanParam(name: 'DOWNLOAD_CONFIG_DRIVE', value: true),
                 string(name: 'MCP_VERSION', value: "${MCP_VERSION}"),
-                string(name: 'COMMON_SCRIPTS_COMMIT', value: "${MCP_VERSION}"),
+                string(name: 'COMMON_SCRIPTS_COMMIT', value: "release/${env.MCP_VERSION}"),
                 string(name: 'NODE_NAME', value: "${NODE_NAME}"),
                 string(name: 'CONFIG_DRIVE_ISO_NAME', value: "${CFG01_CONFIG_IMAGE_NAME}"),
                 string(name: 'SALT_MASTER_DEPLOY_IP', value: SALT_MASTER_IP),