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