blob: 3356138f07482886d81d606a2dc643b851e912df [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()
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 Polreich8706c022019-09-30 16:51:13 +020014 def common = new com.mirantis.mk.Common()
Martin Polreichd60c86d2019-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 Cizinskyaa2d21b2019-03-19 11:08:05 +010035
36timeout(time: 12, unit: 'HOURS') {
37 node() {
Martin Polreichd60c86d2019-08-23 15:47:37 +020038 def saltMasterBackupNode = ''
39 def dogtagBackupNode = ''
Denis Egorenko1542bf22019-07-24 17:05:24 +040040 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010041 stage('Setup virtualenv for Pepper') {
42 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
43 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040044 stage('Verify pillar for backups') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020045 if (backupSaltMasterAndMaas) {
46 try {
Denis Egorenkoad3e8302019-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 Polreichd60c86d2019-08-23 15:47:37 +020049 throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.")
50 }
Denis Egorenkoad3e8302019-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 Polreichd60c86d2019-08-23 15:47:37 +020053 throw new Exception("Problem with salt-minion pillar on I@salt:master node.")
54 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040055 }
Martin Polreichd60c86d2019-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 Egorenkoad3e8302019-10-31 16:41:53 +040059 throw e
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040060 }
61 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020062 if (backupDogtag) {
Denis Egorenko5b6cdd92019-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 Polreichd60c86d2019-08-23 15:47:37 +020070 }
Denis Egorenko5b6cdd92019-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 Polreichd60c86d2019-08-23 15:47:37 +020079 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040080 }
81 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010082 stage('Check backup location') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020083 if (backupSaltMasterAndMaas) {
84 try {
85 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 Egorenkoad3e8302019-10-31 16:41:53 +040092 throw e
Martin Polreichd60c86d2019-08-23 15:47:37 +020093 }
Denis Egorenko1542bf22019-07-24 17:05:24 +040094
Ivan Berezovskiye57361c2019-10-29 18:55:17 +040095 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
Martin Polreichd60c86d2019-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?")
Martin Polreichde3066a2019-08-02 15:33:18 +0200100 if (askConfirmation) {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200101 input message: "Confirm to proceed anyway."
Martin Polreichde3066a2019-08-02 15:33:18 +0200102 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200103 } else {
Ivan Berezovskiye57361c2019-10-29 18:55:17 +0400104 def postgresqlClientPackages = "postgresql-client-${postgresqlMajorVersion}"
Martin Polreichd60c86d2019-08-23 15:47:37 +0200105 try {
Ivan Berezovskiye57361c2019-10-29 18:55:17 +0400106 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackages, 'output': false])) {
Martin Polreichd60c86d2019-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 Berezovskiye57361c2019-10-29 18:55:17 +0400113 salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackages}"])
Martin Polreichd60c86d2019-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 }
Martin Polreichde3066a2019-08-02 15:33:18 +0200120 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400121 }
122 }
123 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200124 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 Egorenkoad3e8302019-10-31 16:41:53 +0400133 throw e
Martin Polreichd60c86d2019-08-23 15:47:37 +0200134 }
135 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400136
Ivan Berezovskiy8428b8d2019-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 Cizinskyaa2d21b2019-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 Egorenkoad3e8302019-10-31 16:41:53 +0400145 throw e
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100146 }
147 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400148 stage('Prepare for backup') {
Martin Polreichd60c86d2019-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")
Ivan Berezovskiyaa4a0ee2019-11-26 19:07:07 +0400161 salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && ' +
162 'getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt &&' +
163 'getfacl -pR /etc/maas/ > /etc/maas/file_permissions.txt || true')
Martin Polreichd60c86d2019-08-23 15:47:37 +0200164 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400165 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200166 if (backupDogtag) {
167 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
168 salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja'])
Ivan Berezovskiyc5a153c2019-08-09 21:07:37 +0400169 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100170 }
171 stage('Backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200172 if (backupSaltMasterAndMaas) {
173 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
174 checkBackupninjaLog(output, "Salt Master/MAAS")
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100175 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200176 if (backupDogtag) {
177 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
178 checkBackupninjaLog(output, "Dogtag")
179 }
180 }
181 stage('Results') {
182 if (logBackupSuccess.size() > 0) {
183 common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}")
184 }
185 if (logBackupFailure.size() > 0) {
186 common.errorMsg("Following backups has failed: ${logBackupFailure.join(",")}. Make sure to check the logs.")
Martin Polreich30a37cf2019-05-14 10:16:34 +0200187 currentBuild.result = "FAILURE"
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100188 }
189 }
190 }
191}