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) { |
| 14 | def outputPattern = java.util.regex.Pattern.compile("\\d+") |
| 15 | def outputMatcher = outputPattern.matcher(output) |
| 16 | if (outputMatcher.find()) { |
| 17 | try { |
| 18 | result = outputMatcher.getAt([0, 1, 2, 3]) |
| 19 | if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) { |
| 20 | common.successMsg("[${backupName}] - Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.") |
| 21 | logBackupSuccess.add(backupName) |
| 22 | } else { |
| 23 | common.errorMsg("[${backupName}] - Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.") |
| 24 | logBackupFailure.add(backupName) |
| 25 | } |
| 26 | } |
| 27 | catch (Exception e) { |
| 28 | common.errorMsg(e.getMessage()) |
| 29 | common.errorMsg("[${backupName}] - Backupninja log parsing failed.") |
| 30 | logBackupFailure.add(backupName) |
| 31 | } |
| 32 | } |
| 33 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 34 | |
| 35 | timeout(time: 12, unit: 'HOURS') { |
| 36 | node() { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 37 | def saltMasterBackupNode = '' |
| 38 | def dogtagBackupNode = '' |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 39 | def backupServer = '' |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 40 | stage('Setup virtualenv for Pepper') { |
| 41 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 42 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 43 | stage('Verify pillar for backups') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 44 | if (backupSaltMasterAndMaas) { |
| 45 | try { |
| 46 | def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data') |
| 47 | if (masterPillar['return'].isEmpty()) { |
| 48 | throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.") |
| 49 | } |
| 50 | def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data') |
| 51 | if (minionPillar['return'].isEmpty()) { |
| 52 | throw new Exception("Problem with salt-minion pillar on I@salt:master node.") |
| 53 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 54 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 55 | catch (Exception e) { |
| 56 | common.errorMsg(e.getMessage()) |
| 57 | 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') |
| 58 | return |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 59 | } |
| 60 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 61 | if (backupDogtag) { |
| 62 | try { |
| 63 | def dogtagPillar = salt.getPillar(pepperEnv, "I@salt:master", "dogtag:server") |
| 64 | if (masterPillar['return'].isEmpty()) { |
| 65 | throw new Exception("Problem with dogtag pillar on I@dogtag:server node.") |
| 66 | } |
| 67 | } |
| 68 | catch (Exception e) { |
| 69 | common.errorMsg(e.getMessage()) |
| 70 | 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.") |
| 71 | return |
| 72 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 73 | } |
| 74 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 75 | stage('Check backup location') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 76 | if (backupSaltMasterAndMaas) { |
| 77 | try { |
| 78 | saltMasterBackupNode = salt.getMinionsSorted(pepperEnv, saltMasterTargetMatcher)[0] |
| 79 | salt.minionsReachable(pepperEnv, "I@salt:master", saltMasterBackupNode) |
| 80 | } |
| 81 | catch (Exception e) { |
| 82 | common.errorMsg(e.getMessage()) |
| 83 | common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar on Salt master node or the minion is not reachable") |
| 84 | currentBuild.result = "FAILURE" |
| 85 | return |
| 86 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 87 | |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 88 | def maasNodes = salt.getMinions(pepperEnv, 'I@maas:server') |
| 89 | if (!maasNodes.isEmpty()) { |
| 90 | def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0] |
| 91 | if (! postgresqlMajorVersion) { |
| 92 | 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] | 93 | if (askConfirmation) { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 94 | input message: "Confirm to proceed anyway." |
Martin Polreich | de3066a | 2019-08-02 15:33:18 +0200 | [diff] [blame] | 95 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 96 | } else { |
| 97 | def postgresqlClientPackage = "postgresql-client-${postgresqlMajorVersion}" |
| 98 | try { |
| 99 | if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackage, 'output': false])) { |
| 100 | if (askConfirmation) { |
| 101 | input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${saltMasterBackupNode}? It's required to make backup. Click to confirm." |
| 102 | } else { |
| 103 | common.infoMsg("Package ${postgresqlClientPackages} will be installed. It's required to make backup.") |
| 104 | } |
| 105 | // update also common fake package |
| 106 | salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackage}"]) |
| 107 | } |
| 108 | } catch (Exception e) { |
| 109 | common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${saltMasterBackupNode}.") |
| 110 | if (askConfirmation) { |
| 111 | input message: "Do you want to continue? Click to confirm" |
| 112 | } |
Martin Polreich | de3066a | 2019-08-02 15:33:18 +0200 | [diff] [blame] | 113 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 117 | if (backupDogtag) { |
| 118 | try { |
| 119 | dogtagBackupNode = salt.getMinionsSorted(pepperEnv, dogtagTagetMatcher)[0] |
| 120 | salt.minionsReachable(pepperEnv, "I@salt:master", dogtagBackupNode) |
| 121 | } |
| 122 | catch (Exception e) { |
| 123 | common.errorMsg(e.getMessage()) |
| 124 | common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and dogtag:server pillars defined or the minion is not reachable") |
| 125 | currentBuild.result = "FAILURE" |
| 126 | return |
| 127 | } |
| 128 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame] | 129 | |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 130 | try { |
| 131 | backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0] |
| 132 | salt.minionsReachable(pepperEnv, "I@salt:master", backupServer) |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 133 | } |
| 134 | catch (Exception e) { |
| 135 | common.errorMsg(e.getMessage()) |
| 136 | common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable") |
| 137 | currentBuild.result = "FAILURE" |
| 138 | return |
| 139 | } |
| 140 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 141 | stage('Prepare for backup') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 142 | if (backupSaltMasterAndMaas) { |
| 143 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja']) |
| 144 | salt.enforceState(['saltId': pepperEnv, 'target': saltMasterTargetMatcher, 'state': 'backupninja']) |
| 145 | def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:master:initial_data:source')) |
| 146 | def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:minion:initial_data:source')) |
| 147 | // TODO: Remove ssh-keyscan once we have openssh meta for backupninja implemented |
| 148 | [backupServer, backupMasterSource, backupMinionSource].unique().each { |
| 149 | salt.cmdRun(pepperEnv, saltMasterBackupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts") |
| 150 | } |
| 151 | def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region') |
| 152 | if (!maasNodes.isEmpty()) { |
| 153 | common.infoMsg("Trying to save maas file permissions on ${maasNodes} if possible") |
| 154 | salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt || true') |
| 155 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 156 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 157 | if (backupDogtag) { |
| 158 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja']) |
| 159 | salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja']) |
Ivan Berezovskiy | c5a153c | 2019-08-09 21:07:37 +0400 | [diff] [blame] | 160 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 161 | } |
| 162 | stage('Backup') { |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 163 | if (backupSaltMasterAndMaas) { |
| 164 | def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2] |
| 165 | checkBackupninjaLog(output, "Salt Master/MAAS") |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 166 | } |
Martin Polreich | d60c86d | 2019-08-23 15:47:37 +0200 | [diff] [blame^] | 167 | if (backupDogtag) { |
| 168 | def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2] |
| 169 | checkBackupninjaLog(output, "Dogtag") |
| 170 | } |
| 171 | } |
| 172 | stage('Results') { |
| 173 | if (logBackupSuccess.size() > 0) { |
| 174 | common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}") |
| 175 | } |
| 176 | if (logBackupFailure.size() > 0) { |
| 177 | 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] | 178 | currentBuild.result = "FAILURE" |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |