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() { |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame^] | 8 | def backupNode = '' |
| 9 | def backupServer = '' |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 10 | stage('Setup virtualenv for Pepper') { |
| 11 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 12 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 13 | stage('Verify pillar for backups') { |
| 14 | try { |
| 15 | def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data') |
| 16 | if (masterPillar['return'].isEmpty()) { |
| 17 | throw new Exception('Problem with salt-master pillar.') |
| 18 | } |
| 19 | def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data') |
| 20 | if (minionPillar['return'].isEmpty()) { |
| 21 | throw new Exception('Problem with salt-minion pillar.') |
| 22 | } |
| 23 | } |
| 24 | catch (Exception e) { |
| 25 | common.errorMsg(e.getMessage()) |
| 26 | 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') |
| 27 | return |
| 28 | } |
| 29 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 30 | stage('Check backup location') { |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 31 | try { |
| 32 | backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0] |
| 33 | salt.minionsReachable(pepperEnv, "I@salt:master", backupNode) |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 34 | } |
| 35 | catch (Exception e) { |
| 36 | common.errorMsg(e.getMessage()) |
| 37 | common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar or the minion is not reachable") |
| 38 | currentBuild.result = "FAILURE" |
| 39 | return |
| 40 | } |
Denis Egorenko | 1542bf2 | 2019-07-24 17:05:24 +0400 | [diff] [blame^] | 41 | |
| 42 | def postgresqlMajorVersion = salt.getPillar(venvPepper, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0] |
| 43 | if (! postgresqlMajorVersion) { |
| 44 | input message: "Can't get _param:postgresql_major_version parameter, which is required to determine postgresql-client version. Is it defined in pillar? Confirm to proceed anyway." |
| 45 | } else { |
| 46 | def postgresqlClientPackage = "postgresql-client-${postgresqlMajorVersion}" |
| 47 | try { |
| 48 | if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': backupNode, 'packageName': postgresqlClientPackage, 'output': false])) { |
| 49 | if (askConfirmation) { |
| 50 | input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${backupNode}? It's required to make backup. Click to confirm" |
| 51 | } |
| 52 | // update also common fake package |
| 53 | salt.runSaltProcessStep(pepperEnv, backupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackage}"]) |
| 54 | } |
| 55 | } catch (Exception e) { |
| 56 | common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${backupNode}.") |
| 57 | if (askConfirmation) { |
| 58 | input message: "Do you want to continue? Click to confirm" |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 63 | try { |
| 64 | backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0] |
| 65 | salt.minionsReachable(pepperEnv, "I@salt:master", backupServer) |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 66 | } |
| 67 | catch (Exception e) { |
| 68 | common.errorMsg(e.getMessage()) |
| 69 | common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable") |
| 70 | currentBuild.result = "FAILURE" |
| 71 | return |
| 72 | } |
| 73 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 74 | stage('Prepare for backup') { |
| 75 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja']) |
| 76 | salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:client', 'state': 'backupninja']) |
| 77 | def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:master:initial_data:source')) |
| 78 | def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, backupNode, 'salt:minion:initial_data:source')) |
| 79 | [backupServer, backupMasterSource, backupMinionSource].unique().each { |
| 80 | salt.cmdRun(pepperEnv, backupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts") |
| 81 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 82 | } |
| 83 | stage('Backup') { |
Martin Polreich | 30a37cf | 2019-05-14 10:16:34 +0200 | [diff] [blame] | 84 | 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] | 85 | def outputPattern = java.util.regex.Pattern.compile("\\d+") |
| 86 | def outputMatcher = outputPattern.matcher(output) |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 87 | if (outputMatcher.find()) { |
| 88 | try { |
| 89 | result = outputMatcher.getAt([0, 1, 2, 3]) |
| 90 | } |
| 91 | catch (Exception e) { |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 92 | common.errorMsg(e.getMessage()) |
| 93 | common.errorMsg("Parsing failed.") |
| 94 | currentBuild.result = "FAILURE" |
| 95 | return |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 96 | } |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 97 | } |
Ivan Berezovskiy | 8428b8d | 2019-07-22 16:54:06 +0400 | [diff] [blame] | 98 | if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) { |
| 99 | common.successMsg("Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.") |
| 100 | } else { |
| 101 | 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] | 102 | currentBuild.result = "FAILURE" |
| 103 | return |
Pavel Cizinsky | aa2d21b | 2019-03-19 11:08:05 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |