blob: f6eaae23c5e1c51153a3efcc2a3d3b248755e15e [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() {
Denis Egorenko1542bf22019-07-24 17:05:24 +04008 def backupNode = ''
9 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010010 stage('Setup virtualenv for Pepper') {
11 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
12 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040013 stage('Verify pillar for backups') {
14 try {
15 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data')
16 if (masterPillar['return'].isEmpty()) {
17 throw new Exception('Problem with salt-master pillar.')
18 }
19 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
20 if (minionPillar['return'].isEmpty()) {
21 throw new Exception('Problem with salt-minion pillar.')
22 }
23 }
24 catch (Exception e) {
25 common.errorMsg(e.getMessage())
26 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')
27 return
28 }
29 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010030 stage('Check backup location') {
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040031 try {
32 backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0]
33 salt.minionsReachable(pepperEnv, "I@salt:master", backupNode)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010034 }
35 catch (Exception e) {
36 common.errorMsg(e.getMessage())
37 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar or the minion is not reachable")
38 currentBuild.result = "FAILURE"
39 return
40 }
Denis Egorenko1542bf22019-07-24 17:05:24 +040041
42 def postgresqlMajorVersion = salt.getPillar(venvPepper, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
43 if (! postgresqlMajorVersion) {
44 input message: "Can't get _param:postgresql_major_version parameter, which is required to determine postgresql-client version. Is it defined in pillar? Confirm to proceed anyway."
45 } else {
46 def postgresqlClientPackage = "postgresql-client-${postgresqlMajorVersion}"
47 try {
48 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': backupNode, 'packageName': postgresqlClientPackage, 'output': false])) {
49 if (askConfirmation) {
50 input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${backupNode}? It's required to make backup. Click to confirm"
51 }
52 // update also common fake package
53 salt.runSaltProcessStep(pepperEnv, backupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackage}"])
54 }
55 } catch (Exception e) {
56 common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${backupNode}.")
57 if (askConfirmation) {
58 input message: "Do you want to continue? Click to confirm"
59 }
60 }
61 }
62
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040063 try {
64 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
65 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010066 }
67 catch (Exception e) {
68 common.errorMsg(e.getMessage())
69 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
70 currentBuild.result = "FAILURE"
71 return
72 }
73 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040074 stage('Prepare for backup') {
75 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
76 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:client', 'state': 'backupninja'])
77 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:master:initial_data:source'))
78 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:minion:initial_data:source'))
79 [backupServer, backupMasterSource, backupMinionSource].unique().each {
80 salt.cmdRun(pepperEnv, backupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
81 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010082 }
83 stage('Backup') {
Martin Polreich30a37cf2019-05-14 10:16:34 +020084 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, backupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010085 def outputPattern = java.util.regex.Pattern.compile("\\d+")
86 def outputMatcher = outputPattern.matcher(output)
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040087 if (outputMatcher.find()) {
88 try {
89 result = outputMatcher.getAt([0, 1, 2, 3])
90 }
91 catch (Exception e) {
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010092 common.errorMsg(e.getMessage())
93 common.errorMsg("Parsing failed.")
94 currentBuild.result = "FAILURE"
95 return
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040096 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010097 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040098 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
99 common.successMsg("Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
100 } else {
101 common.errorMsg("Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
Martin Polreich30a37cf2019-05-14 10:16:34 +0200102 currentBuild.result = "FAILURE"
103 return
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100104 }
105 }
106 }
107}