Merge "Refactor salt formulas test"
diff --git a/generate-cookiecutter-products.groovy b/generate-cookiecutter-products.groovy
index 4326e7e..d2d380a 100644
--- a/generate-cookiecutter-products.groovy
+++ b/generate-cookiecutter-products.groovy
@@ -138,7 +138,7 @@
             }
         }
 
-        stage("Generate config drive") {
+        stage("Generate config drives") {
             // apt package genisoimage is required for this stage
 
             // download create-config-drive
@@ -163,15 +163,38 @@
                 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh"
             }
 
-            // create config-drive
+            // create cfg config-drive
             sh "./create-config-drive ${args}"
             sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
-            // save iso to artifacts
+
+            // save cfg iso to artifacts
             archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
+
+            if (templateContext.default_context.offline_deployment && templateContext.default_context.offline_deployment == 'True'){
+                def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
+                def user_data_script_apt_url = "https://raw.githubusercontent.com/richardfelkl/scripts/master/mirror_config.sh"
+                sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
+
+                def smc_apt = [:]
+                smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
+                smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_address']
+                smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
+                smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
+
+                for (i in common.entries(smc_apt)) {
+                    sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
+                }
+
+                // create apt config-drive
+                sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
+                sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
+
+                // save apt iso to artifacts
+                archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
+            }
         }
 
         stage ('Save changes reclass model') {
-
             sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
             archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
 
diff --git a/update-mirror-image.groovy b/update-mirror-image.groovy
new file mode 100644
index 0000000..39a0209
--- /dev/null
+++ b/update-mirror-image.groovy
@@ -0,0 +1,52 @@
+/**
+ * Update mirror image
+ *
+ * Expected parameters:
+ *   SALT_MASTER_CREDENTIALS    Credentials to the Salt API.
+ *   SALT_MASTER_URL            Full Salt API address [https://10.10.10.1:8000].
+ *
+**/
+
+def common = new com.mirantis.mk.Common()
+def salt = new com.mirantis.mk.Salt()
+def python = new com.mirantis.mk.Python()
+def venvPepper = "venvPepper"
+
+node() {
+    try {
+
+        python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
+
+        stage('Update Aptly packages'){
+            common.infoMsg("Updating Aptly packages.")
+            salt.enforceState(venvPepper, 'I@aptly:server', ['aptly'], true)
+            salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.run', ['/srv/scripts/aptly-update.sh'], null, true)
+        }
+
+        stage('Update Docker images'){
+            common.infoMsg("Updating Docker images.")
+            salt.enforceState(venvPepper, 'I@aptly:server', ['docker.client.registry'], true)
+        }
+
+        stage('Update PyPi packages'){
+            common.infoMsg("Updating PyPi packages.")
+            salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.run', ['pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt'], null, true)
+        }
+
+        stage('Update Git repositories'){
+            common.infoMsg("Updating Git repositories.")
+            salt.enforceState(venvPepper, 'I@aptly:server', ['git.server'], true)
+        }
+
+        stage('Update VM images'){
+            common.infoMsg("Updating VM images.")
+            salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.run', ['/srv/scripts/update-images.sh'], null, true)
+        }
+
+    } 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