Create configdrive using gerrit instead of github

- clone mk-pipelines and pipeline-library from
  gerrit.mcp.mirantis.com using stored credentials
- workaround gerrit.gerritPatchsetCheckout() with
  additional "--mirror" clone to get the mirror of
  the repository instead of working tree
- increase BOOTSTRAP_TIMEOUT from 1200 to 1800 because
  MAAS is importing resources too long

Related-Bug: #PROD-24777

Change-Id: Ia001c823e1860f2f75fb37abbd5be7061ec9f57f
diff --git a/jobs/pipelines/swarm-bootstrap-salt-cluster-devops.groovy b/jobs/pipelines/swarm-bootstrap-salt-cluster-devops.groovy
index 7a6daaa..36ea29a 100644
--- a/jobs/pipelines/swarm-bootstrap-salt-cluster-devops.groovy
+++ b/jobs/pipelines/swarm-bootstrap-salt-cluster-devops.groovy
@@ -96,7 +96,7 @@
                     export LAB_CONFIG_NAME=${LAB_CONFIG_NAME}
                     export MANAGER=devops
                     export SHUTDOWN_ENV_ON_TEARDOWN=false
-                    export BOOTSTRAP_TIMEOUT=1200
+                    export BOOTSTRAP_TIMEOUT=1800
                     export PYTHONIOENCODING=UTF-8
                     export REPOSITORY_SUITE=${MCP_VERSION}
                     export TEST_GROUP=test_bootstrap_salt
diff --git a/jobs/pipelines/swarm-create-cfg-config-drive.groovy b/jobs/pipelines/swarm-create-cfg-config-drive.groovy
index c43fb56..e921963 100644
--- a/jobs/pipelines/swarm-create-cfg-config-drive.groovy
+++ b/jobs/pipelines/swarm-create-cfg-config-drive.groovy
@@ -1,5 +1,6 @@
 import java.text.SimpleDateFormat
 
+def gerrit = new com.mirantis.mk.Gerrit()
 def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
 def date = new Date()
 def common_scripts_commit = "${COMMON_SCRIPTS_COMMIT}"
@@ -31,28 +32,82 @@
         step([$class: 'WsCleanup'])
     }
 
-    stage("Get scripts") {
-      // apt package genisoimage is required for this stage
-      // download create-config-drive
+    stage("Get mk-pipelines, pipeline-library and mcp-common-scripts repositories") {
+        def cloned = true
+        withCredentials([[$class: 'SSHUserPrivateKeyBinding',
+                          keyFileVariable: "GERRIT_KEY",
+                          credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
+                          usernameVariable: "GERRIT_USERNAME",
+                          passwordVariable: "GERRIT_PASSWORD"]]) {
 
-      def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${common_scripts_commit}/config-drive/create_config_drive.sh"
-      sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
+            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")
+            }
 
-      def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${common_scripts_commit}/config-drive/master_config.yaml"
-      sh "wget -O user_data ${user_data_script_url}"
-    }
+            dir("mk-pipelines-git") {
+                cloned = gerrit.gerritPatchsetCheckout([
+                    credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
+                    gerritBranch: "${MCP_VERSION}",
+                    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")
+            }
 
-    stage("Clone mk-pipelines and pipeline-library") {
-        sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git -b ${MCP_VERSION} mk-pipelines"
-        sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git -b ${MCP_VERSION} pipeline-library"
+            dir("pipeline-library-git") {
+                cloned = gerrit.gerritPatchsetCheckout([
+                    credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
+                    gerritBranch: "${MCP_VERSION}",
+                    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 fetch https://gerrit.mcp.mirantis.net/mcp-ci/pipeline-library ${PIPELINE_LIBRARY_REF} ; git tag ${MCP_VERSION} FETCH_HEAD -f ; cd .."
+           sh "cd pipeline-library-git; git tag ${MCP_VERSION} FETCH_HEAD -f ; cd .."
         }
         if (MK_PIPELINES_REF != '') {
-           sh "cd mk-pipelines; git fetch https://gerrit.mcp.mirantis.net/mcp-ci/mk-pipelines ${MK_PIPELINES_REF} ; git tag ${MCP_VERSION} FETCH_HEAD -f; cd .."
+           sh "cd mk-pipelines-git; git tag ${MCP_VERSION} FETCH_HEAD -f; cd .."
         }
+
+        // gerrit.gerritPatchsetCheckout() doesn't support clonning bare repository
+        sh "git clone --mirror mk-pipelines-git -b ${MCP_VERSION} mk-pipelines"
+        sh "git clone --mirror pipeline-library-git -b ${MCP_VERSION} pipeline-library"
+    }
+
+    stage("Prepare arguments for generation config drive") {
+
+        config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
+        user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
+        sh "chmod +x ${config_drive_script_path}"
+
         //args = "--user-data user_data --vendor-data user_data2 --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
-        args = "--user-data user_data2 --vendor-data user_data --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
+        args = "--user-data user_data2 --vendor-data ${user_data_script_path} --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
     }
 
     stage("Get cluster model") {
@@ -70,7 +125,7 @@
 
     stage("Set data"){
         for (i in entries(smc)) {
-            sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data"
+            sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
         }
     }
 
@@ -151,8 +206,8 @@
 
     stage("Create config-drive"){
       // create cfg config-drive
-      //sh "sed -i 's,config_dir/vendor-data,config_dir/user-data1,g' ./create-config-drive"
-      sh "./create-config-drive ${args}"
+      // apt package genisoimage is required for this stage
+      sh "./${config_drive_script_path} ${args}"
     }
 
     stage("Save artifacts") {