blob: 74f38e68b7221626b3cb8b5f39bad58e72e46e58 [file] [log] [blame]
Pavel Cizinsky2c681d62019-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()
4def pepperEnv = "pepperEnv"
Denis Egorenkoc304b4f2019-07-25 16:57:48 +04005def askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean()
Martin Polreichd4f95412019-08-23 15:47:37 +02006def backupSaltMasterAndMaas = (env.getProperty('BACKUP_SALTMASTER_AND_MAAS') ?: true).toBoolean()
7def backupDogtag = (env.getProperty('BACKUP_DOGTAG') ?: true).toBoolean()
8def saltMasterTargetMatcher = "I@backupninja:client and I@salt:master"
9def dogtagTagetMatcher = "I@backupninja:client and I@dogtag:server"
10logBackupSuccess = []
11logBackupFailure = []
12
13def checkBackupninjaLog(output, backupName='', terminateOnFailure=true) {
Martin Polreich3772d892019-09-30 16:51:13 +020014 def common = new com.mirantis.mk.Common()
Martin Polreichd4f95412019-08-23 15:47:37 +020015 def outputPattern = java.util.regex.Pattern.compile("\\d+")
16 def outputMatcher = outputPattern.matcher(output)
17 if (outputMatcher.find()) {
18 try {
19 result = outputMatcher.getAt([0, 1, 2, 3])
20 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
21 common.successMsg("[${backupName}] - Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
22 logBackupSuccess.add(backupName)
23 } else {
24 common.errorMsg("[${backupName}] - Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
25 logBackupFailure.add(backupName)
26 }
27 }
28 catch (Exception e) {
29 common.errorMsg(e.getMessage())
30 common.errorMsg("[${backupName}] - Backupninja log parsing failed.")
31 logBackupFailure.add(backupName)
32 }
33 }
34}
Pavel Cizinsky2c681d62019-03-19 11:08:05 +010035
36timeout(time: 12, unit: 'HOURS') {
37 node() {
Martin Polreichd4f95412019-08-23 15:47:37 +020038 def saltMasterBackupNode = ''
39 def dogtagBackupNode = ''
Denis Egorenko37f41db2019-07-24 17:05:24 +040040 def backupServer = ''
Pavel Cizinsky2c681d62019-03-19 11:08:05 +010041 stage('Setup virtualenv for Pepper') {
42 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
43 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +040044 stage('Verify pillar for backups') {
Martin Polreichd4f95412019-08-23 15:47:37 +020045 if (backupSaltMasterAndMaas) {
46 try {
Denis Egorenkoade98ae2019-10-31 16:41:53 +040047 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data').get('return')[0].values()[0]
48 if (masterPillar.isEmpty()) {
Martin Polreichd4f95412019-08-23 15:47:37 +020049 throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.")
50 }
Denis Egorenkoade98ae2019-10-31 16:41:53 +040051 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data').get('return')[0].values()[0]
52 if (minionPillar.isEmpty()) {
Martin Polreichd4f95412019-08-23 15:47:37 +020053 throw new Exception("Problem with salt-minion pillar on I@salt:master node.")
54 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +040055 }
Martin Polreichd4f95412019-08-23 15:47:37 +020056 catch (Exception e) {
57 common.errorMsg(e.getMessage())
58 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')
Denis Egorenkoade98ae2019-10-31 16:41:53 +040059 throw e
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +040060 }
61 }
Martin Polreichd4f95412019-08-23 15:47:37 +020062 if (backupDogtag) {
63 try {
Denis Egorenkoade98ae2019-10-31 16:41:53 +040064 def dogtagPillar = salt.getPillar(pepperEnv, "I@dogtag:server", "dogtag:server").get('return')[0].values()[0]
65 if (dogtagPillar.isEmpty()) {
Martin Polreichd4f95412019-08-23 15:47:37 +020066 throw new Exception("Problem with dogtag pillar on I@dogtag:server node.")
67 }
68 }
69 catch (Exception e) {
70 common.errorMsg(e.getMessage())
71 common.errorMsg("Looks like dogtag pillar is not defined. Fix your pillar or disable dogtag backup by setting the BACKUP_DOGTAG parameter to False if you're using different barbican backend.")
Denis Egorenkoade98ae2019-10-31 16:41:53 +040072 throw e
Martin Polreichd4f95412019-08-23 15:47:37 +020073 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +040074 }
75 }
Pavel Cizinsky2c681d62019-03-19 11:08:05 +010076 stage('Check backup location') {
Martin Polreichd4f95412019-08-23 15:47:37 +020077 if (backupSaltMasterAndMaas) {
Denis Egorenko37f41db2019-07-24 17:05:24 +040078 try {
Martin Polreichd4f95412019-08-23 15:47:37 +020079 saltMasterBackupNode = salt.getMinionsSorted(pepperEnv, saltMasterTargetMatcher)[0]
80 salt.minionsReachable(pepperEnv, "I@salt:master", saltMasterBackupNode)
81 }
82 catch (Exception e) {
83 common.errorMsg(e.getMessage())
84 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar on Salt master node or the minion is not reachable")
85 currentBuild.result = "FAILURE"
Denis Egorenkoade98ae2019-10-31 16:41:53 +040086 throw e
Martin Polreichd4f95412019-08-23 15:47:37 +020087 }
88
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +040089 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
Martin Polreichd4f95412019-08-23 15:47:37 +020090 if (!maasNodes.isEmpty()) {
91 def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
92 if (! postgresqlMajorVersion) {
93 common.errorMsg("Can't get _param:postgresql_major_version parameter, which is required to determine postgresql-client version. Is it defined in pillar?")
94 if (askConfirmation) {
95 input message: "Confirm to proceed anyway."
96 }
97 } else {
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +040098 def postgresqlClientPackages = "postgresql-client-${postgresqlMajorVersion}"
Martin Polreichd4f95412019-08-23 15:47:37 +020099 try {
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +0400100 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackages, 'output': false])) {
Martin Polreichd4f95412019-08-23 15:47:37 +0200101 if (askConfirmation) {
102 input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${saltMasterBackupNode}? It's required to make backup. Click to confirm."
103 } else {
104 common.infoMsg("Package ${postgresqlClientPackages} will be installed. It's required to make backup.")
105 }
106 // update also common fake package
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +0400107 salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackages}"])
Martin Polreichd4f95412019-08-23 15:47:37 +0200108 }
109 } catch (Exception e) {
110 common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${saltMasterBackupNode}.")
111 if (askConfirmation) {
112 input message: "Do you want to continue? Click to confirm"
113 }
Denis Egorenko37f41db2019-07-24 17:05:24 +0400114 }
Denis Egorenko37f41db2019-07-24 17:05:24 +0400115 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200116 }
117 }
118 if (backupDogtag) {
119 try {
120 dogtagBackupNode = salt.getMinionsSorted(pepperEnv, dogtagTagetMatcher)[0]
121 salt.minionsReachable(pepperEnv, "I@salt:master", dogtagBackupNode)
122 }
123 catch (Exception e) {
124 common.errorMsg(e.getMessage())
125 common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and dogtag:server pillars defined or the minion is not reachable")
126 currentBuild.result = "FAILURE"
Denis Egorenkoade98ae2019-10-31 16:41:53 +0400127 throw e
Denis Egorenko37f41db2019-07-24 17:05:24 +0400128 }
129 }
130
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +0400131 try {
132 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
133 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100134 }
135 catch (Exception e) {
136 common.errorMsg(e.getMessage())
137 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
138 currentBuild.result = "FAILURE"
Denis Egorenkoade98ae2019-10-31 16:41:53 +0400139 throw e
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100140 }
141 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +0400142 stage('Prepare for backup') {
Martin Polreichd4f95412019-08-23 15:47:37 +0200143 if (backupSaltMasterAndMaas) {
144 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
145 salt.enforceState(['saltId': pepperEnv, 'target': saltMasterTargetMatcher, 'state': 'backupninja'])
146 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:master:initial_data:source'))
147 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:minion:initial_data:source'))
148 // TODO: Remove ssh-keyscan once we have openssh meta for backupninja implemented
149 [backupServer, backupMasterSource, backupMinionSource].unique().each {
150 salt.cmdRun(pepperEnv, saltMasterBackupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
151 }
152 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
153 if (!maasNodes.isEmpty()) {
154 common.infoMsg("Trying to save maas file permissions on ${maasNodes} if possible")
155 salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt || true')
156 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +0400157 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200158 if (backupDogtag) {
159 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
160 salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja'])
Ivan Berezovskiy34035bf2019-08-09 21:07:37 +0400161 }
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100162 }
163 stage('Backup') {
Martin Polreichd4f95412019-08-23 15:47:37 +0200164 if (backupSaltMasterAndMaas) {
165 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
166 checkBackupninjaLog(output, "Salt Master/MAAS")
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100167 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200168 if (backupDogtag) {
169 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
170 checkBackupninjaLog(output, "Dogtag")
171 }
172 }
173 stage('Results') {
174 if (logBackupSuccess.size() > 0) {
175 common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}")
176 }
177 if (logBackupFailure.size() > 0) {
178 common.errorMsg("Following backups has failed: ${logBackupFailure.join(",")}. Make sure to check the logs.")
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100179 currentBuild.result = "FAILURE"
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100180 }
181 }
182 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200183}