blob: 6013ec0eec531b45a03d061574606586e52d2f80 [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) {
Denis Egorenko9056c622019-11-07 15:48:43 +040063 def barbicanBackendPresent = salt.getPillar(pepperEnv, "I@salt:master", "_param:barbican_backend").get('return')[0].values()[0]
64 if (barbicanBackendPresent == 'dogtag') {
65 try {
66 def dogtagPillar = salt.getPillar(pepperEnv, "I@dogtag:server", "dogtag:server").get('return')[0].values()[0]
67 if (dogtagPillar.isEmpty()) {
68 throw new Exception("Problem with dogtag pillar on I@dogtag:server node.")
69 }
Martin Polreichd4f95412019-08-23 15:47:37 +020070 }
Denis Egorenko9056c622019-11-07 15:48:43 +040071 catch (Exception e) {
72 common.errorMsg(e.getMessage())
73 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.")
74 throw e
75 }
76 } else {
77 backupDogtag = false
78 common.warningMsg('Backup for Dogtag is enabled, but service itself is not present. Skipping...')
Martin Polreichd4f95412019-08-23 15:47:37 +020079 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +040080 }
81 }
Pavel Cizinsky2c681d62019-03-19 11:08:05 +010082 stage('Check backup location') {
Martin Polreichd4f95412019-08-23 15:47:37 +020083 if (backupSaltMasterAndMaas) {
Denis Egorenko37f41db2019-07-24 17:05:24 +040084 try {
Martin Polreichd4f95412019-08-23 15:47:37 +020085 saltMasterBackupNode = salt.getMinionsSorted(pepperEnv, saltMasterTargetMatcher)[0]
86 salt.minionsReachable(pepperEnv, "I@salt:master", saltMasterBackupNode)
87 }
88 catch (Exception e) {
89 common.errorMsg(e.getMessage())
90 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar on Salt master node or the minion is not reachable")
91 currentBuild.result = "FAILURE"
Denis Egorenkoade98ae2019-10-31 16:41:53 +040092 throw e
Martin Polreichd4f95412019-08-23 15:47:37 +020093 }
94
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +040095 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
Martin Polreichd4f95412019-08-23 15:47:37 +020096 if (!maasNodes.isEmpty()) {
97 def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
98 if (! postgresqlMajorVersion) {
99 common.errorMsg("Can't get _param:postgresql_major_version parameter, which is required to determine postgresql-client version. Is it defined in pillar?")
100 if (askConfirmation) {
101 input message: "Confirm to proceed anyway."
102 }
103 } else {
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +0400104 def postgresqlClientPackages = "postgresql-client-${postgresqlMajorVersion}"
Martin Polreichd4f95412019-08-23 15:47:37 +0200105 try {
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +0400106 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackages, 'output': false])) {
Martin Polreichd4f95412019-08-23 15:47:37 +0200107 if (askConfirmation) {
108 input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${saltMasterBackupNode}? It's required to make backup. Click to confirm."
109 } else {
110 common.infoMsg("Package ${postgresqlClientPackages} will be installed. It's required to make backup.")
111 }
112 // update also common fake package
Ivan Berezovskiy60e16ba2019-10-29 18:55:17 +0400113 salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackages}"])
Martin Polreichd4f95412019-08-23 15:47:37 +0200114 }
115 } catch (Exception e) {
116 common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${saltMasterBackupNode}.")
117 if (askConfirmation) {
118 input message: "Do you want to continue? Click to confirm"
119 }
Denis Egorenko37f41db2019-07-24 17:05:24 +0400120 }
Denis Egorenko37f41db2019-07-24 17:05:24 +0400121 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200122 }
123 }
124 if (backupDogtag) {
125 try {
126 dogtagBackupNode = salt.getMinionsSorted(pepperEnv, dogtagTagetMatcher)[0]
127 salt.minionsReachable(pepperEnv, "I@salt:master", dogtagBackupNode)
128 }
129 catch (Exception e) {
130 common.errorMsg(e.getMessage())
131 common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and dogtag:server pillars defined or the minion is not reachable")
132 currentBuild.result = "FAILURE"
Denis Egorenkoade98ae2019-10-31 16:41:53 +0400133 throw e
Denis Egorenko37f41db2019-07-24 17:05:24 +0400134 }
135 }
136
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +0400137 try {
138 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
139 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100140 }
141 catch (Exception e) {
142 common.errorMsg(e.getMessage())
143 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
144 currentBuild.result = "FAILURE"
Denis Egorenkoade98ae2019-10-31 16:41:53 +0400145 throw e
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100146 }
147 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +0400148 stage('Prepare for backup') {
Martin Polreichd4f95412019-08-23 15:47:37 +0200149 if (backupSaltMasterAndMaas) {
150 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
151 salt.enforceState(['saltId': pepperEnv, 'target': saltMasterTargetMatcher, 'state': 'backupninja'])
152 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:master:initial_data:source'))
153 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:minion:initial_data:source'))
154 // TODO: Remove ssh-keyscan once we have openssh meta for backupninja implemented
155 [backupServer, backupMasterSource, backupMinionSource].unique().each {
156 salt.cmdRun(pepperEnv, saltMasterBackupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
157 }
158 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
159 if (!maasNodes.isEmpty()) {
160 common.infoMsg("Trying to save maas file permissions on ${maasNodes} if possible")
161 salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt || true')
162 }
Ivan Berezovskiy2219bf42019-07-22 16:54:06 +0400163 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200164 if (backupDogtag) {
165 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
166 salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja'])
Ivan Berezovskiy34035bf2019-08-09 21:07:37 +0400167 }
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100168 }
169 stage('Backup') {
Martin Polreichd4f95412019-08-23 15:47:37 +0200170 if (backupSaltMasterAndMaas) {
171 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
172 checkBackupninjaLog(output, "Salt Master/MAAS")
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100173 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200174 if (backupDogtag) {
175 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
176 checkBackupninjaLog(output, "Dogtag")
177 }
178 }
179 stage('Results') {
180 if (logBackupSuccess.size() > 0) {
181 common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}")
182 }
183 if (logBackupFailure.size() > 0) {
184 common.errorMsg("Following backups has failed: ${logBackupFailure.join(",")}. Make sure to check the logs.")
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100185 currentBuild.result = "FAILURE"
Pavel Cizinsky2c681d62019-03-19 11:08:05 +0100186 }
187 }
188 }
Martin Polreichd4f95412019-08-23 15:47:37 +0200189}