| Pavel Cizinsky | 2c681d6 | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 1 | def common = new com.mirantis.mk.Common() | 
 | 2 | def salt = new com.mirantis.mk.Salt() | 
 | 3 | def python = new com.mirantis.mk.Python() | 
 | 4 | def pepperEnv = "pepperEnv" | 
 | 5 |  | 
 | 6 | timeout(time: 12, unit: 'HOURS') { | 
 | 7 |     node() { | 
 | 8 |         stage('Setup virtualenv for Pepper') { | 
 | 9 |             python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) | 
 | 10 |         } | 
 | 11 |         stage('Check backup location') { | 
 | 12 |             try{ | 
 | 13 |               backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0] | 
 | 14 |               salt.minionsReachable(pepperEnv, "I@salt:master", backupNode) | 
 | 15 |             } | 
 | 16 |             catch (Exception e) { | 
 | 17 |                 common.errorMsg(e.getMessage()) | 
 | 18 |                 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar or the minion is not reachable") | 
 | 19 |                 currentBuild.result = "FAILURE" | 
 | 20 |                 return | 
 | 21 |             } | 
 | 22 |             try{ | 
 | 23 |               backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0] | 
 | 24 |               salt.minionsReachable(pepperEnv, "I@salt:master", backupServer) | 
 | 25 |             } | 
 | 26 |             catch (Exception e) { | 
 | 27 |                 common.errorMsg(e.getMessage()) | 
 | 28 |                 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable") | 
 | 29 |                 currentBuild.result = "FAILURE" | 
 | 30 |                 return | 
 | 31 |             } | 
 | 32 |         } | 
 | 33 |         stage ('Prepare for backup') { | 
 | 34 |                 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja']) | 
 | 35 |                 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:client', 'state': 'backupninja']) | 
 | 36 |         } | 
 | 37 |         stage('Backup') { | 
 | 38 |             def output = salt.getReturnValues(salt.cmdRun(pepperEnv, backupNode, "su root -c 'backupninja --now -d'")).readLines()[-2] | 
 | 39 |             def outputPattern = java.util.regex.Pattern.compile("\\d+") | 
 | 40 |             def outputMatcher = outputPattern.matcher(output) | 
 | 41 |               if (outputMatcher.find()) { | 
 | 42 |                   try{ | 
 | 43 |                   result = outputMatcher.getAt([0,1,2,3]) | 
 | 44 |                   } | 
 | 45 |                   catch (Exception e){ | 
 | 46 |                     common.errorMsg(e.getMessage()) | 
 | 47 |                     common.errorMsg("Parsing failed.") | 
 | 48 |                     currentBuild.result = "FAILURE" | 
 | 49 |                     return | 
 | 50 |                   } | 
 | 51 |             } | 
 | 52 |             if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)){ | 
 | 53 |               common.successMsg("Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] +" warnings.") | 
 | 54 |             } | 
 | 55 |             else { | 
 | 56 |                 common.errorMsg("Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] +" warnings.") | 
 | 57 |                 currentBuild.result = "FAILURE" | 
 | 58 |                 return | 
 | 59 |             } | 
 | 60 |         } | 
 | 61 |     } | 
 | 62 | } |