Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 1 | def common = new com.mirantis.mk.Common() |
| 2 | def salt = new com.mirantis.mk.Salt() |
| 3 | def python = new com.mirantis.mk.Python() |
Pavel Cizinsky | 32d8bc8 | 2019-04-24 10:24:30 +0200 | [diff] [blame] | 4 | def pepperEnv = "pepperEnv" |
Denis Egorenko | 7c06606 | 2019-07-25 16:57:48 +0400 | [diff] [blame] | 5 | def askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean() |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 6 | def backupSaltMasterAndMaas = (env.getProperty('BACKUP_SALTMASTER_AND_MAAS') ?: true).toBoolean() |
| 7 | def backupDogtag = (env.getProperty('BACKUP_DOGTAG') ?: true).toBoolean() |
| 8 | def saltMasterTargetMatcher = "I@backupninja:client and I@salt:master" |
| 9 | def dogtagTagetMatcher = "I@backupninja:client and I@dogtag:server" |
| 10 | logBackupSuccess = [] |
| 11 | logBackupFailure = [] |
| 12 | |
| 13 | def checkBackupninjaLog(output, backupName='', terminateOnFailure=true) { |
Martin Polreich | 8706c02 | 2019-09-30 16:51:13 +0200 | [diff] [blame] | 14 | def common = new com.mirantis.mk.Common() |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 15 | 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 Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 35 | |
| 36 | timeout(time: 12, unit: 'HOURS') { |
| 37 | node() { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 38 | def saltMasterBackupNode = '' |
| 39 | def dogtagBackupNode = '' |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 40 | def backupServer = '' |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 41 | stage('Setup virtualenv for Pepper') { |
| 42 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 43 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 44 | stage('Verify pillar for backups') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 45 | if (backupSaltMasterAndMaas) { |
| 46 | try { |
Denis Egorenko | ad3e830 | 2019-10-31 16:41:53 +0400 | [diff] [blame] | 47 | def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data').get('return')[0].values()[0] |
| 48 | if (masterPillar.isEmpty()) { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 49 | throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.") |
| 50 | } |
Denis Egorenko | ad3e830 | 2019-10-31 16:41:53 +0400 | [diff] [blame] | 51 | def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data').get('return')[0].values()[0] |
| 52 | if (minionPillar.isEmpty()) { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 53 | throw new Exception("Problem with salt-minion pillar on I@salt:master node.") |
| 54 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 55 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 56 | 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 Egorenko | ad3e830 | 2019-10-31 16:41:53 +0400 | [diff] [blame] | 59 | throw e |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 60 | } |
| 61 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 62 | if (backupDogtag) { |
Denis Egorenko | 5b6cdd9 | 2019-11-07 15:48:43 +0400 | [diff] [blame] | 63 | 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 Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 70 | } |
Denis Egorenko | 5b6cdd9 | 2019-11-07 15:48:43 +0400 | [diff] [blame] | 71 | 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 Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 79 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 80 | } |
| 81 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 82 | stage('Check backup location') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 83 | 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 Egorenko | ad3e830 | 2019-10-31 16:41:53 +0400 | [diff] [blame] | 92 | throw e |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 93 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 94 | |
Ivan Berezovskiy | e57361c | 2019-10-29 18:55:17 +0400 | [diff] [blame] | 95 | def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region') |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 96 | 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 Polreich | de3066a | 2019-08-02 15:33:18 +0200 | [diff] [blame] | 100 | if (askConfirmation) { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 101 | input message: "Confirm to proceed anyway." |
Martin Polreich | de3066a | 2019-08-02 15:33:18 +0200 | [diff] [blame] | 102 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 103 | } else { |
Ivan Berezovskiy | e57361c | 2019-10-29 18:55:17 +0400 | [diff] [blame] | 104 | def postgresqlClientPackages = "postgresql-client-${postgresqlMajorVersion}" |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 105 | try { |
Ivan Berezovskiy | e57361c | 2019-10-29 18:55:17 +0400 | [diff] [blame] | 106 | if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackages, 'output': false])) { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 107 | 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 Berezovskiy | e57361c | 2019-10-29 18:55:17 +0400 | [diff] [blame] | 113 | salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackages}"]) |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 114 | } |
| 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 Polreich | de3066a | 2019-08-02 15:33:18 +0200 | [diff] [blame] | 120 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 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 Egorenko | ad3e830 | 2019-10-31 16:41:53 +0400 | [diff] [blame] | 133 | throw e |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 134 | } |
| 135 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 136 | |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 137 | try { |
| 138 | backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0] |
| 139 | salt.minionsReachable(pepperEnv, "I@salt:master", backupServer) |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 140 | } |
| 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 Egorenko | ad3e830 | 2019-10-31 16:41:53 +0400 | [diff] [blame] | 145 | throw e |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 148 | stage('Prepare for backup') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 149 | 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 Berezovskiy | aa4a0ee | 2019-11-26 19:07:07 +0400 | [diff] [blame] | 161 | 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 Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 164 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 165 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 166 | if (backupDogtag) { |
| 167 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja']) |
| 168 | salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja']) |
Ivan Berezovskiy | c5a153c | 2019-08-09 21:07:37 +0400 | [diff] [blame] | 169 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 170 | } |
| 171 | stage('Backup') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 172 | 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 Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 175 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame] | 176 | 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 Polreich | 30a37cf | 2019-05-14 10:16:34 +0200 | [diff] [blame] | 187 | currentBuild.result = "FAILURE" |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |