blob: 58254f384b5843ba676bd0ef64497c9ad04a92ca [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"
Denis Egorenko7c066062019-07-25 16:57:48 +04005def askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean()
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +01006
7timeout(time: 12, unit: 'HOURS') {
8 node() {
Denis Egorenko1542bf22019-07-24 17:05:24 +04009 def backupNode = ''
10 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010011 stage('Setup virtualenv for Pepper') {
12 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
13 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040014 stage('Verify pillar for backups') {
15 try {
16 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data')
17 if (masterPillar['return'].isEmpty()) {
18 throw new Exception('Problem with salt-master pillar.')
19 }
20 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
21 if (minionPillar['return'].isEmpty()) {
22 throw new Exception('Problem with salt-minion pillar.')
23 }
24 }
25 catch (Exception e) {
26 common.errorMsg(e.getMessage())
27 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')
28 return
29 }
30 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010031 stage('Check backup location') {
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040032 try {
33 backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0]
34 salt.minionsReachable(pepperEnv, "I@salt:master", backupNode)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010035 }
36 catch (Exception e) {
37 common.errorMsg(e.getMessage())
38 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar or the minion is not reachable")
39 currentBuild.result = "FAILURE"
40 return
41 }
Denis Egorenko1542bf22019-07-24 17:05:24 +040042
Denis Egorenko7c066062019-07-25 16:57:48 +040043 def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
Denis Egorenko1542bf22019-07-24 17:05:24 +040044 if (! postgresqlMajorVersion) {
45 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."
46 } else {
47 def postgresqlClientPackage = "postgresql-client-${postgresqlMajorVersion}"
48 try {
49 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': backupNode, 'packageName': postgresqlClientPackage, 'output': false])) {
50 if (askConfirmation) {
Denis Egorenko7c066062019-07-25 16:57:48 +040051 input message: "Do you want to install ${postgresqlClientPackage} package on targeted nodes: ${backupNode}? It's required to make backup. Click to confirm"
Denis Egorenko1542bf22019-07-24 17:05:24 +040052 }
53 // update also common fake package
54 salt.runSaltProcessStep(pepperEnv, backupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackage}"])
55 }
56 } catch (Exception e) {
Denis Egorenko7c066062019-07-25 16:57:48 +040057 common.errorMsg("Unable to determine status of ${postgresqlClientPackage} packages on target nodes: ${backupNode}.")
Denis Egorenko1542bf22019-07-24 17:05:24 +040058 if (askConfirmation) {
59 input message: "Do you want to continue? Click to confirm"
60 }
61 }
62 }
63
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040064 try {
65 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
66 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010067 }
68 catch (Exception e) {
69 common.errorMsg(e.getMessage())
70 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
71 currentBuild.result = "FAILURE"
72 return
73 }
74 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040075 stage('Prepare for backup') {
76 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
77 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:client', 'state': 'backupninja'])
78 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:master:initial_data:source'))
79 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:minion:initial_data:source'))
80 [backupServer, backupMasterSource, backupMinionSource].unique().each {
81 salt.cmdRun(pepperEnv, backupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
82 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010083 }
84 stage('Backup') {
Martin Polreich30a37cf2019-05-14 10:16:34 +020085 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, backupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010086 def outputPattern = java.util.regex.Pattern.compile("\\d+")
87 def outputMatcher = outputPattern.matcher(output)
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040088 if (outputMatcher.find()) {
89 try {
90 result = outputMatcher.getAt([0, 1, 2, 3])
91 }
92 catch (Exception e) {
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010093 common.errorMsg(e.getMessage())
94 common.errorMsg("Parsing failed.")
95 currentBuild.result = "FAILURE"
96 return
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040097 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010098 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040099 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
100 common.successMsg("Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
101 } else {
102 common.errorMsg("Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
Martin Polreich30a37cf2019-05-14 10:16:34 +0200103 currentBuild.result = "FAILURE"
104 return
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100105 }
106 }
107 }
108}