blob: 4bc2b923ba44bf699a116d64f15ccdf4f1da3156 [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()
Martin Polreichd60c86d2019-08-23 15:47:37 +02006def backupSaltMasterAndMaas = (env.getProperty('BACKUP_SALTMASTER_AND_MAAS') ?: true).toBoolean()
7def backupDogtag = (env.getProperty('BACKUP_DOGTAG') ?: true).toBoolean()
Oleksii Molchanov8d8bf172020-11-10 21:31:15 +02008def backupKeystone = (env.getProperty('BACKUP_KEYSTONE_CREDENTIAL_KEYS') ?: true).toBoolean()
Martin Polreichd60c86d2019-08-23 15:47:37 +02009def saltMasterTargetMatcher = "I@backupninja:client and I@salt:master"
10def dogtagTagetMatcher = "I@backupninja:client and I@dogtag:server"
Oleksii Molchanov8d8bf172020-11-10 21:31:15 +020011def keystoneTargetMatcher = "I@backupninja:client and I@keystone:server"
Martin Polreichd60c86d2019-08-23 15:47:37 +020012logBackupSuccess = []
13logBackupFailure = []
14
15def checkBackupninjaLog(output, backupName='', terminateOnFailure=true) {
Martin Polreich8706c022019-09-30 16:51:13 +020016 def common = new com.mirantis.mk.Common()
Martin Polreichd60c86d2019-08-23 15:47:37 +020017 def outputPattern = java.util.regex.Pattern.compile("\\d+")
18 def outputMatcher = outputPattern.matcher(output)
19 if (outputMatcher.find()) {
20 try {
21 result = outputMatcher.getAt([0, 1, 2, 3])
22 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
23 common.successMsg("[${backupName}] - Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
24 logBackupSuccess.add(backupName)
25 } else {
26 common.errorMsg("[${backupName}] - Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
27 logBackupFailure.add(backupName)
28 }
29 }
30 catch (Exception e) {
31 common.errorMsg(e.getMessage())
32 common.errorMsg("[${backupName}] - Backupninja log parsing failed.")
33 logBackupFailure.add(backupName)
34 }
35 }
36}
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010037
38timeout(time: 12, unit: 'HOURS') {
39 node() {
Martin Polreichd60c86d2019-08-23 15:47:37 +020040 def saltMasterBackupNode = ''
41 def dogtagBackupNode = ''
Oleksii Molchanov8d8bf172020-11-10 21:31:15 +020042 def keystoneBackupNode = ''
Denis Egorenko1542bf22019-07-24 17:05:24 +040043 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010044 stage('Setup virtualenv for Pepper') {
45 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
46 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040047 stage('Verify pillar for backups') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020048 if (backupSaltMasterAndMaas) {
49 try {
Denis Egorenkoad3e8302019-10-31 16:41:53 +040050 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data').get('return')[0].values()[0]
51 if (masterPillar.isEmpty()) {
Martin Polreichd60c86d2019-08-23 15:47:37 +020052 throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.")
53 }
Denis Egorenkoad3e8302019-10-31 16:41:53 +040054 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data').get('return')[0].values()[0]
55 if (minionPillar.isEmpty()) {
Martin Polreichd60c86d2019-08-23 15:47:37 +020056 throw new Exception("Problem with salt-minion pillar on I@salt:master node.")
57 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040058 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020059 catch (Exception e) {
60 common.errorMsg(e.getMessage())
61 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 Egorenkoad3e8302019-10-31 16:41:53 +040062 throw e
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040063 }
64 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020065 if (backupDogtag) {
Denis Egorenko5b6cdd92019-11-07 15:48:43 +040066 def barbicanBackendPresent = salt.getPillar(pepperEnv, "I@salt:master", "_param:barbican_backend").get('return')[0].values()[0]
67 if (barbicanBackendPresent == 'dogtag') {
68 try {
69 def dogtagPillar = salt.getPillar(pepperEnv, "I@dogtag:server", "dogtag:server").get('return')[0].values()[0]
70 if (dogtagPillar.isEmpty()) {
71 throw new Exception("Problem with dogtag pillar on I@dogtag:server node.")
72 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020073 }
Denis Egorenko5b6cdd92019-11-07 15:48:43 +040074 catch (Exception e) {
75 common.errorMsg(e.getMessage())
76 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.")
77 throw e
78 }
79 } else {
80 backupDogtag = false
81 common.warningMsg('Backup for Dogtag is enabled, but service itself is not present. Skipping...')
Martin Polreichd60c86d2019-08-23 15:47:37 +020082 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040083 }
84 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010085 stage('Check backup location') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020086 if (backupSaltMasterAndMaas) {
87 try {
88 saltMasterBackupNode = salt.getMinionsSorted(pepperEnv, saltMasterTargetMatcher)[0]
89 salt.minionsReachable(pepperEnv, "I@salt:master", saltMasterBackupNode)
90 }
91 catch (Exception e) {
92 common.errorMsg(e.getMessage())
93 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar on Salt master node or the minion is not reachable")
94 currentBuild.result = "FAILURE"
Denis Egorenkoad3e8302019-10-31 16:41:53 +040095 throw e
Martin Polreichd60c86d2019-08-23 15:47:37 +020096 }
Denis Egorenko1542bf22019-07-24 17:05:24 +040097
Ivan Berezovskiye57361c2019-10-29 18:55:17 +040098 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
Martin Polreichd60c86d2019-08-23 15:47:37 +020099 if (!maasNodes.isEmpty()) {
100 def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
101 if (! postgresqlMajorVersion) {
102 common.errorMsg("Can't get _param:postgresql_major_version parameter, which is required to determine postgresql-client version. Is it defined in pillar?")
Martin Polreichde3066a2019-08-02 15:33:18 +0200103 if (askConfirmation) {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200104 input message: "Confirm to proceed anyway."
Martin Polreichde3066a2019-08-02 15:33:18 +0200105 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200106 } else {
Ivan Berezovskiye57361c2019-10-29 18:55:17 +0400107 def postgresqlClientPackages = "postgresql-client-${postgresqlMajorVersion}"
Martin Polreichd60c86d2019-08-23 15:47:37 +0200108 try {
Ivan Berezovskiye57361c2019-10-29 18:55:17 +0400109 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackages, 'output': false])) {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200110 if (askConfirmation) {
111 input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${saltMasterBackupNode}? It's required to make backup. Click to confirm."
112 } else {
113 common.infoMsg("Package ${postgresqlClientPackages} will be installed. It's required to make backup.")
114 }
115 // update also common fake package
Ivan Berezovskiye57361c2019-10-29 18:55:17 +0400116 salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackages}"])
Martin Polreichd60c86d2019-08-23 15:47:37 +0200117 }
118 } catch (Exception e) {
119 common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${saltMasterBackupNode}.")
120 if (askConfirmation) {
121 input message: "Do you want to continue? Click to confirm"
122 }
Martin Polreichde3066a2019-08-02 15:33:18 +0200123 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400124 }
125 }
126 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200127 if (backupDogtag) {
128 try {
129 dogtagBackupNode = salt.getMinionsSorted(pepperEnv, dogtagTagetMatcher)[0]
130 salt.minionsReachable(pepperEnv, "I@salt:master", dogtagBackupNode)
131 }
132 catch (Exception e) {
133 common.errorMsg(e.getMessage())
134 common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and dogtag:server pillars defined or the minion is not reachable")
135 currentBuild.result = "FAILURE"
Denis Egorenkoad3e8302019-10-31 16:41:53 +0400136 throw e
Martin Polreichd60c86d2019-08-23 15:47:37 +0200137 }
138 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400139
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400140 try {
141 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
142 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100143 }
144 catch (Exception e) {
145 common.errorMsg(e.getMessage())
146 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
147 currentBuild.result = "FAILURE"
Denis Egorenkoad3e8302019-10-31 16:41:53 +0400148 throw e
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100149 }
Oleksii Molchanov8d8bf172020-11-10 21:31:15 +0200150 if (backupKeystone) {
151 try {
152 keystoneBackupNode = salt.getMinionsSorted(pepperEnv, keystoneTargetMatcher)[0]
153 salt.minionsReachable(pepperEnv, "I@salt:master", keystoneBackupNode)
154 }
155 catch (Exception e) {
156 common.errorMsg(e.getMessage())
157 common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and keystone:server pillars defined or the minion is not reachable")
158 currentBuild.result = "FAILURE"
159 throw e
160 }
161 }
162
163 try {
164 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
165 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
166 }
167 catch (Exception e) {
168 common.errorMsg(e.getMessage())
169 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
170 currentBuild.result = "FAILURE"
171 throw e
172 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100173 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400174 stage('Prepare for backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200175 if (backupSaltMasterAndMaas) {
176 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
177 salt.enforceState(['saltId': pepperEnv, 'target': saltMasterTargetMatcher, 'state': 'backupninja'])
178 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:master:initial_data:source'))
179 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:minion:initial_data:source'))
180 // TODO: Remove ssh-keyscan once we have openssh meta for backupninja implemented
181 [backupServer, backupMasterSource, backupMinionSource].unique().each {
182 salt.cmdRun(pepperEnv, saltMasterBackupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
183 }
184 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
185 if (!maasNodes.isEmpty()) {
186 common.infoMsg("Trying to save maas file permissions on ${maasNodes} if possible")
Ivan Berezovskiyaa4a0ee2019-11-26 19:07:07 +0400187 salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && ' +
188 'getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt &&' +
189 'getfacl -pR /etc/maas/ > /etc/maas/file_permissions.txt || true')
Martin Polreichd60c86d2019-08-23 15:47:37 +0200190 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400191 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200192 if (backupDogtag) {
193 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
194 salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja'])
Ivan Berezovskiyc5a153c2019-08-09 21:07:37 +0400195 }
Oleksii Molchanov8d8bf172020-11-10 21:31:15 +0200196 if (backupKeystone) {
197 salt.enforceState(['saltId': pepperEnv, 'target': keystoneTargetMatcher, 'state': 'backupninja'])
198 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
199 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100200 }
201 stage('Backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200202 if (backupSaltMasterAndMaas) {
203 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
204 checkBackupninjaLog(output, "Salt Master/MAAS")
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100205 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200206 if (backupDogtag) {
207 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
208 checkBackupninjaLog(output, "Dogtag")
209 }
Oleksii Molchanov8d8bf172020-11-10 21:31:15 +0200210 if (backupKeystone) {
211 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, keystoneBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
212 checkBackupninjaLog(output, "Keystone")
213 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200214 }
215 stage('Results') {
216 if (logBackupSuccess.size() > 0) {
217 common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}")
218 }
219 if (logBackupFailure.size() > 0) {
220 common.errorMsg("Following backups has failed: ${logBackupFailure.join(",")}. Make sure to check the logs.")
Martin Polreich30a37cf2019-05-14 10:16:34 +0200221 currentBuild.result = "FAILURE"
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100222 }
223 }
224 }
225}