Merge "Use old style for enforseState function without Map parameters" into release/proposed/2019.2.0
diff --git a/backupninja-restore-pipeline.groovy b/backupninja-restore-pipeline.groovy
new file mode 100644
index 0000000..b38cd6a
--- /dev/null
+++ b/backupninja-restore-pipeline.groovy
@@ -0,0 +1,55 @@
+def common = new com.mirantis.mk.Common()
+def salt = new com.mirantis.mk.Salt()
+def python = new com.mirantis.mk.Python()
+def pepperEnv = "pepperEnv"
+
+timeout(time: 12, unit: 'HOURS') {
+    node() {
+        stage('Setup virtualenv for Pepper') {
+            python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
+        }
+        stage('Salt-Master restore') {
+            common.infoMsg('Verify pillar for salt-master backups')
+            try {
+                def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
+                if(masterPillar['return'].isEmpty()) {
+                    throw new Exception('Problem with salt-master pillar.')
+                }
+                def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
+                if(minionPillar['return'].isEmpty()) {
+                    throw new Exception('Problem with salt-minion pillar.')
+                }
+            }
+            catch (Exception e){
+                common.errorMsg(e.getMessage())
+                common.errorMsg('Please fix your pillar. For more information check docs: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/salt-master/salt-master-restore.html')
+                return
+            }
+            common.infoMsg('Performing restore')
+            salt.enforceState(['saltId': pepperEnv, 'target': 'I@salt:master', 'state': 'salt.master.restore'])
+            salt.enforceState(['saltId': pepperEnv, 'target': 'I@salt:master', 'state': 'salt.minion.restore'])
+            salt.fullRefresh(pepperEnv, '*')
+
+            common.infoMsg('Validating output')
+            common.infoMsg('Salt-Keys')
+            salt.cmdRun(pepperEnv, 'I@salt:master', "salt-key")
+            common.infoMsg('Salt-master CA')
+            salt.cmdRun(pepperEnv, 'I@salt:master', "ls -la /etc/pki/ca/salt_master_ca/")
+        }
+        stage('MAAS Restore') {
+            common.infoMsg('Verify pillar for MaaS backup')
+            try {
+                def maaSPillar = salt.getPillar(pepperEnv, "I@maas:server", 'maas:region:database:initial_data')
+                if(maaSPillar['return'].isEmpty()) {
+                    throw new Exception('Problem with MaaS pillar.')
+                }
+            }
+            catch (Exception e){
+                common.errorMsg(e.getMessage())
+                common.errorMsg('Please fix your pillar. For more information check docs: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/backupninja-postgresql/backupninja-postgresql-restore.html')
+                return
+            }
+            salt.enforceState(['saltId': pepperEnv, 'target': 'I@maas:region', 'state': 'maas.region'])
+        }
+    }
+}
diff --git a/upgrade-mcp-release.groovy b/upgrade-mcp-release.groovy
index ca2244f..65e668c 100644
--- a/upgrade-mcp-release.groovy
+++ b/upgrade-mcp-release.groovy
@@ -307,6 +307,10 @@
             }
             python.setupPepperVirtualenv(venvPepper, saltMastURL, saltMastCreds)
             def minions = salt.getMinions(venvPepper, '*')
+            def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0]
+            if (cluster_name == '' || cluster_name == 'null' || cluster_name == null) {
+                error('Pillar data is broken for Salt master node! Please check it manually and re-run pipeline.')
+            }
 
             stage('Update Reclass and Salt-Formulas') {
                 common.infoMsg('Perform: Full salt sync')
@@ -317,7 +321,6 @@
                 common.infoMsg('Perform: archiveReclassInventory before upgrade')
                 archiveReclassInventory(inventoryBeforeFilename)
 
-                def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', '_param:cluster_name').get('return')[0].values()[0]
                 try {
                     salt.cmdRun(venvPepper, 'I@salt:master', 'cd /srv/salt/reclass/ && git status && git diff-index --quiet HEAD --')
                 }