blob: 1af5e5b5d531c51184868dd418a12af8521d1eab [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()
Martin Polreichd60c86d2019-08-23 15:47:37 +02006def backupSaltMasterAndMaas = (env.getProperty('BACKUP_SALTMASTER_AND_MAAS') ?: true).toBoolean()
7def backupDogtag = (env.getProperty('BACKUP_DOGTAG') ?: true).toBoolean()
8def saltMasterTargetMatcher = "I@backupninja:client and I@salt:master"
9def dogtagTagetMatcher = "I@backupninja:client and I@dogtag:server"
10logBackupSuccess = []
11logBackupFailure = []
12
13def checkBackupninjaLog(output, backupName='', terminateOnFailure=true) {
Martin Polreich8706c022019-09-30 16:51:13 +020014 def common = new com.mirantis.mk.Common()
Martin Polreichd60c86d2019-08-23 15:47:37 +020015 def outputPattern = java.util.regex.Pattern.compile("\\d+")
16 def outputMatcher = outputPattern.matcher(output)
17 if (outputMatcher.find()) {
18 try {
19 result = outputMatcher.getAt([0, 1, 2, 3])
20 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
21 common.successMsg("[${backupName}] - Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
22 logBackupSuccess.add(backupName)
23 } else {
24 common.errorMsg("[${backupName}] - Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
25 logBackupFailure.add(backupName)
26 }
27 }
28 catch (Exception e) {
29 common.errorMsg(e.getMessage())
30 common.errorMsg("[${backupName}] - Backupninja log parsing failed.")
31 logBackupFailure.add(backupName)
32 }
33 }
34}
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010035
36timeout(time: 12, unit: 'HOURS') {
37 node() {
Martin Polreichd60c86d2019-08-23 15:47:37 +020038 def saltMasterBackupNode = ''
39 def dogtagBackupNode = ''
Denis Egorenko1542bf22019-07-24 17:05:24 +040040 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010041 stage('Setup virtualenv for Pepper') {
42 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
43 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040044 stage('Verify pillar for backups') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020045 if (backupSaltMasterAndMaas) {
46 try {
47 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data')
48 if (masterPillar['return'].isEmpty()) {
49 throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.")
50 }
51 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
52 if (minionPillar['return'].isEmpty()) {
53 throw new Exception("Problem with salt-minion pillar on I@salt:master node.")
54 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040055 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020056 catch (Exception e) {
57 common.errorMsg(e.getMessage())
58 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')
59 return
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040060 }
61 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020062 if (backupDogtag) {
63 try {
64 def dogtagPillar = salt.getPillar(pepperEnv, "I@salt:master", "dogtag:server")
Martin Polreich8706c022019-09-30 16:51:13 +020065 if (dogtagPillar['return'].isEmpty()) {
Martin Polreichd60c86d2019-08-23 15:47:37 +020066 throw new Exception("Problem with dogtag pillar on I@dogtag:server node.")
67 }
68 }
69 catch (Exception e) {
70 common.errorMsg(e.getMessage())
71 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.")
72 return
73 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040074 }
75 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010076 stage('Check backup location') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020077 if (backupSaltMasterAndMaas) {
78 try {
79 saltMasterBackupNode = salt.getMinionsSorted(pepperEnv, saltMasterTargetMatcher)[0]
80 salt.minionsReachable(pepperEnv, "I@salt:master", saltMasterBackupNode)
81 }
82 catch (Exception e) {
83 common.errorMsg(e.getMessage())
84 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar on Salt master node or the minion is not reachable")
85 currentBuild.result = "FAILURE"
86 return
87 }
Denis Egorenko1542bf22019-07-24 17:05:24 +040088
Martin Polreichd60c86d2019-08-23 15:47:37 +020089 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:server')
90 if (!maasNodes.isEmpty()) {
91 def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
92 if (! postgresqlMajorVersion) {
93 common.errorMsg("Can't get _param:postgresql_major_version parameter, which is required to determine postgresql-client version. Is it defined in pillar?")
Martin Polreichde3066a2019-08-02 15:33:18 +020094 if (askConfirmation) {
Martin Polreichd60c86d2019-08-23 15:47:37 +020095 input message: "Confirm to proceed anyway."
Martin Polreichde3066a2019-08-02 15:33:18 +020096 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020097 } else {
98 def postgresqlClientPackage = "postgresql-client-${postgresqlMajorVersion}"
99 try {
100 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackage, 'output': false])) {
101 if (askConfirmation) {
102 input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${saltMasterBackupNode}? It's required to make backup. Click to confirm."
103 } else {
104 common.infoMsg("Package ${postgresqlClientPackages} will be installed. It's required to make backup.")
105 }
106 // update also common fake package
107 salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackage}"])
108 }
109 } catch (Exception e) {
110 common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${saltMasterBackupNode}.")
111 if (askConfirmation) {
112 input message: "Do you want to continue? Click to confirm"
113 }
Martin Polreichde3066a2019-08-02 15:33:18 +0200114 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400115 }
116 }
117 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200118 if (backupDogtag) {
119 try {
120 dogtagBackupNode = salt.getMinionsSorted(pepperEnv, dogtagTagetMatcher)[0]
121 salt.minionsReachable(pepperEnv, "I@salt:master", dogtagBackupNode)
122 }
123 catch (Exception e) {
124 common.errorMsg(e.getMessage())
125 common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and dogtag:server pillars defined or the minion is not reachable")
126 currentBuild.result = "FAILURE"
127 return
128 }
129 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400130
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400131 try {
132 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
133 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100134 }
135 catch (Exception e) {
136 common.errorMsg(e.getMessage())
137 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
138 currentBuild.result = "FAILURE"
139 return
140 }
141 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400142 stage('Prepare for backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200143 if (backupSaltMasterAndMaas) {
144 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
145 salt.enforceState(['saltId': pepperEnv, 'target': saltMasterTargetMatcher, 'state': 'backupninja'])
146 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:master:initial_data:source'))
147 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:minion:initial_data:source'))
148 // TODO: Remove ssh-keyscan once we have openssh meta for backupninja implemented
149 [backupServer, backupMasterSource, backupMinionSource].unique().each {
150 salt.cmdRun(pepperEnv, saltMasterBackupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
151 }
152 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
153 if (!maasNodes.isEmpty()) {
154 common.infoMsg("Trying to save maas file permissions on ${maasNodes} if possible")
155 salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt || true')
156 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400157 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200158 if (backupDogtag) {
159 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
160 salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja'])
Ivan Berezovskiyc5a153c2019-08-09 21:07:37 +0400161 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100162 }
163 stage('Backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200164 if (backupSaltMasterAndMaas) {
165 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
166 checkBackupninjaLog(output, "Salt Master/MAAS")
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100167 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200168 if (backupDogtag) {
169 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
170 checkBackupninjaLog(output, "Dogtag")
171 }
172 }
173 stage('Results') {
174 if (logBackupSuccess.size() > 0) {
175 common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}")
176 }
177 if (logBackupFailure.size() > 0) {
178 common.errorMsg("Following backups has failed: ${logBackupFailure.join(",")}. Make sure to check the logs.")
Martin Polreich30a37cf2019-05-14 10:16:34 +0200179 currentBuild.result = "FAILURE"
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100180 }
181 }
182 }
183}