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" |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 5 | |
| 6 | timeout(time: 12, unit: 'HOURS') { |
| 7 | node() { |
| 8 | stage('Setup virtualenv for Pepper') { |
| 9 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 10 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 11 | stage('Verify pillar for backups') { |
| 12 | try { |
| 13 | def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data') |
| 14 | if (masterPillar['return'].isEmpty()) { |
| 15 | throw new Exception('Problem with salt-master pillar.') |
| 16 | } |
| 17 | def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data') |
| 18 | if (minionPillar['return'].isEmpty()) { |
| 19 | throw new Exception('Problem with salt-minion pillar.') |
| 20 | } |
| 21 | } |
| 22 | catch (Exception e) { |
| 23 | common.errorMsg(e.getMessage()) |
| 24 | 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') |
| 25 | return |
| 26 | } |
| 27 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 28 | stage('Check backup location') { |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 29 | try { |
| 30 | backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0] |
| 31 | salt.minionsReachable(pepperEnv, "I@salt:master", backupNode) |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 32 | } |
| 33 | catch (Exception e) { |
| 34 | common.errorMsg(e.getMessage()) |
| 35 | common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar or the minion is not reachable") |
| 36 | currentBuild.result = "FAILURE" |
| 37 | return |
| 38 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 39 | try { |
| 40 | backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0] |
| 41 | salt.minionsReachable(pepperEnv, "I@salt:master", backupServer) |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 42 | } |
| 43 | catch (Exception e) { |
| 44 | common.errorMsg(e.getMessage()) |
| 45 | common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable") |
| 46 | currentBuild.result = "FAILURE" |
| 47 | return |
| 48 | } |
| 49 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 50 | stage('Prepare for backup') { |
| 51 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja']) |
| 52 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:client', 'state': 'backupninja']) |
| 53 | def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:master:initial_data:source')) |
| 54 | def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:minion:initial_data:source')) |
| 55 | [backupServer, backupMasterSource, backupMinionSource].unique().each { |
| 56 | salt.cmdRun(pepperEnv, backupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts") |
| 57 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 58 | } |
| 59 | stage('Backup') { |
Martin Polreich | 30a37cf | 2019-05-14 10:16:34 +0200 | [diff] [blame] | 60 | def output = salt.getReturnValues(salt.cmdRun(pepperEnv, backupNode, "su root -c 'backupninja --now -d'")).readLines()[-2] |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 61 | def outputPattern = java.util.regex.Pattern.compile("\\d+") |
| 62 | def outputMatcher = outputPattern.matcher(output) |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 63 | if (outputMatcher.find()) { |
| 64 | try { |
| 65 | result = outputMatcher.getAt([0, 1, 2, 3]) |
| 66 | } |
| 67 | catch (Exception e) { |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 68 | common.errorMsg(e.getMessage()) |
| 69 | common.errorMsg("Parsing failed.") |
| 70 | currentBuild.result = "FAILURE" |
| 71 | return |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 72 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 73 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame^] | 74 | if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) { |
| 75 | common.successMsg("Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.") |
| 76 | } else { |
| 77 | common.errorMsg("Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.") |
Martin Polreich | 30a37cf | 2019-05-14 10:16:34 +0200 | [diff] [blame] | 78 | currentBuild.result = "FAILURE" |
| 79 | return |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |