blob: d17457de5c36c25bd6312ec0978ddfd1e06d20df [file] [log] [blame]
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +01001def common = new com.mirantis.mk.Common()
2def salt = new com.mirantis.mk.Salt()
3def python = new com.mirantis.mk.Python()
Pavel Cizinsky32d8bc82019-04-24 10:24:30 +02004def pepperEnv = "pepperEnv"
Denis Egorenko7c066062019-07-25 16:57:48 +04005def askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean()
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +01006
7timeout(time: 12, unit: 'HOURS') {
8 node() {
Denis Egorenko1542bf22019-07-24 17:05:24 +04009 def backupNode = ''
10 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010011 stage('Setup virtualenv for Pepper') {
12 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
13 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040014 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 Cizinskyaa2d21b2019-03-19 11:08:05 +010031 stage('Check backup location') {
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040032 try {
33 backupNode = salt.getMinions(pepperEnv, "I@backupninja:client")[0]
34 salt.minionsReachable(pepperEnv, "I@salt:master", backupNode)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010035 }
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 Egorenko1542bf22019-07-24 17:05:24 +040042
Martin Polreichde3066a2019-08-02 15:33:18 +020043 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 Egorenko1542bf22019-07-24 17:05:24 +040048 if (askConfirmation) {
Martin Polreichde3066a2019-08-02 15:33:18 +020049 input message: "Confirm to proceed anyway."
Denis Egorenko1542bf22019-07-24 17:05:24 +040050 }
Martin Polreichde3066a2019-08-02 15:33:18 +020051 } 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 Egorenko1542bf22019-07-24 17:05:24 +040068 }
69 }
70 }
71
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040072 try {
73 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
74 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010075 }
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 Berezovskiy8428b8d2019-07-22 16:54:06 +040083 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 Berezovskiyc5a153c2019-08-09 21:07:37 +040091 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 Cizinskyaa2d21b2019-03-19 11:08:05 +010096 }
97 stage('Backup') {
Martin Polreich30a37cf2019-05-14 10:16:34 +020098 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, backupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010099 def outputPattern = java.util.regex.Pattern.compile("\\d+")
100 def outputMatcher = outputPattern.matcher(output)
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400101 if (outputMatcher.find()) {
102 try {
103 result = outputMatcher.getAt([0, 1, 2, 3])
104 }
105 catch (Exception e) {
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100106 common.errorMsg(e.getMessage())
107 common.errorMsg("Parsing failed.")
108 currentBuild.result = "FAILURE"
109 return
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400110 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100111 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400112 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 Polreich30a37cf2019-05-14 10:16:34 +0200116 currentBuild.result = "FAILURE"
117 return
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100118 }
119 }
120 }
121}