blob: e70781411fc208bd5cff978948a2415179919346 [file] [log] [blame]
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +01001def common = new com.mirantis.mk.Common()
2def salt = new com.mirantis.mk.Salt()
3def python = new com.mirantis.mk.Python()
Pavel Cizinsky32d8bc82019-04-24 10:24:30 +02004def pepperEnv = "pepperEnv"
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +01005
6timeout(time: 12, unit: 'HOURS') {
7 node() {
8 stage('Setup virtualenv for Pepper') {
9 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
10 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040011 stage('Verify pillar for backups') {
12 try {
13 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data')
14 if (masterPillar['return'].isEmpty()) {
15 throw new Exception('Problem with salt-master pillar.')
16 }
17 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
18 if (minionPillar['return'].isEmpty()) {
19 throw new Exception('Problem with salt-minion pillar.')
20 }
21 }
22 catch (Exception e) {
23 common.errorMsg(e.getMessage())
24 common.errorMsg('Please fix your pillar. For more information check docs: https://docs.mirantis.com/mcp/latest/mcp-operations-guide/backup-restore/salt-master.html')
25 return
26 }
27 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010028 stage('Check backup location') {
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040029 try {
30 backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0]
31 salt.minionsReachable(pepperEnv, "I@salt:master", backupNode)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010032 }
33 catch (Exception e) {
34 common.errorMsg(e.getMessage())
35 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar or the minion is not reachable")
36 currentBuild.result = "FAILURE"
37 return
38 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040039 try {
40 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
41 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010042 }
43 catch (Exception e) {
44 common.errorMsg(e.getMessage())
45 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
46 currentBuild.result = "FAILURE"
47 return
48 }
49 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040050 stage('Prepare for backup') {
51 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
52 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:client', 'state': 'backupninja'])
53 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:master:initial_data:source'))
54 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:minion:initial_data:source'))
55 [backupServer, backupMasterSource, backupMinionSource].unique().each {
56 salt.cmdRun(pepperEnv, backupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
57 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010058 }
59 stage('Backup') {
Martin Polreich30a37cf2019-05-14 10:16:34 +020060 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, backupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010061 def outputPattern = java.util.regex.Pattern.compile("\\d+")
62 def outputMatcher = outputPattern.matcher(output)
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040063 if (outputMatcher.find()) {
64 try {
65 result = outputMatcher.getAt([0, 1, 2, 3])
66 }
67 catch (Exception e) {
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010068 common.errorMsg(e.getMessage())
69 common.errorMsg("Parsing failed.")
70 currentBuild.result = "FAILURE"
71 return
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040072 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010073 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040074 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
75 common.successMsg("Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
76 } else {
77 common.errorMsg("Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
Martin Polreich30a37cf2019-05-14 10:16:34 +020078 currentBuild.result = "FAILURE"
79 return
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010080 }
81 }
82 }
83}