blob: 5ec3457d07f2efdec5172369d3498fdf41952cc7 [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) {
14 def outputPattern = java.util.regex.Pattern.compile("\\d+")
15 def outputMatcher = outputPattern.matcher(output)
16 if (outputMatcher.find()) {
17 try {
18 result = outputMatcher.getAt([0, 1, 2, 3])
19 if (result[1] != null && result[1] instanceof String && result[1].isInteger() && (result[1].toInteger() < 1)) {
20 common.successMsg("[${backupName}] - Backup successfully finished " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
21 logBackupSuccess.add(backupName)
22 } else {
23 common.errorMsg("[${backupName}] - Backup failed. Found " + result[1] + " fatals, " + result[2] + " errors " + result[3] + " warnings.")
24 logBackupFailure.add(backupName)
25 }
26 }
27 catch (Exception e) {
28 common.errorMsg(e.getMessage())
29 common.errorMsg("[${backupName}] - Backupninja log parsing failed.")
30 logBackupFailure.add(backupName)
31 }
32 }
33}
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010034
35timeout(time: 12, unit: 'HOURS') {
36 node() {
Martin Polreichd60c86d2019-08-23 15:47:37 +020037 def saltMasterBackupNode = ''
38 def dogtagBackupNode = ''
Denis Egorenko1542bf22019-07-24 17:05:24 +040039 def backupServer = ''
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010040 stage('Setup virtualenv for Pepper') {
41 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
42 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040043 stage('Verify pillar for backups') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020044 if (backupSaltMasterAndMaas) {
45 try {
46 def masterPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:master:initial_data')
47 if (masterPillar['return'].isEmpty()) {
48 throw new Exception("Problem with salt-master pillar on 'I@salt:master' node.")
49 }
50 def minionPillar = salt.getPillar(pepperEnv, "I@salt:master", 'salt:minion:initial_data')
51 if (minionPillar['return'].isEmpty()) {
52 throw new Exception("Problem with salt-minion pillar on I@salt:master node.")
53 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040054 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020055 catch (Exception e) {
56 common.errorMsg(e.getMessage())
57 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')
58 return
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040059 }
60 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020061 if (backupDogtag) {
62 try {
63 def dogtagPillar = salt.getPillar(pepperEnv, "I@salt:master", "dogtag:server")
64 if (masterPillar['return'].isEmpty()) {
65 throw new Exception("Problem with dogtag pillar on I@dogtag:server node.")
66 }
67 }
68 catch (Exception e) {
69 common.errorMsg(e.getMessage())
70 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.")
71 return
72 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +040073 }
74 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +010075 stage('Check backup location') {
Martin Polreichd60c86d2019-08-23 15:47:37 +020076 if (backupSaltMasterAndMaas) {
77 try {
78 saltMasterBackupNode = salt.getMinionsSorted(pepperEnv, saltMasterTargetMatcher)[0]
79 salt.minionsReachable(pepperEnv, "I@salt:master", saltMasterBackupNode)
80 }
81 catch (Exception e) {
82 common.errorMsg(e.getMessage())
83 common.errorMsg("Pipeline wasn't able to detect backupninja:client pillar on Salt master node or the minion is not reachable")
84 currentBuild.result = "FAILURE"
85 return
86 }
Denis Egorenko1542bf22019-07-24 17:05:24 +040087
Martin Polreichd60c86d2019-08-23 15:47:37 +020088 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:server')
89 if (!maasNodes.isEmpty()) {
90 def postgresqlMajorVersion = salt.getPillar(pepperEnv, 'I@salt:master', '_param:postgresql_major_version').get('return')[0].values()[0]
91 if (! postgresqlMajorVersion) {
92 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 +020093 if (askConfirmation) {
Martin Polreichd60c86d2019-08-23 15:47:37 +020094 input message: "Confirm to proceed anyway."
Martin Polreichde3066a2019-08-02 15:33:18 +020095 }
Martin Polreichd60c86d2019-08-23 15:47:37 +020096 } else {
97 def postgresqlClientPackage = "postgresql-client-${postgresqlMajorVersion}"
98 try {
99 if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': saltMasterBackupNode, 'packageName': postgresqlClientPackage, 'output': false])) {
100 if (askConfirmation) {
101 input message: "Do you want to install ${postgresqlClientPackages} package on targeted nodes: ${saltMasterBackupNode}? It's required to make backup. Click to confirm."
102 } else {
103 common.infoMsg("Package ${postgresqlClientPackages} will be installed. It's required to make backup.")
104 }
105 // update also common fake package
106 salt.runSaltProcessStep(pepperEnv, saltMasterBackupNode, 'pkg.install', ["postgresql-client,${postgresqlClientPackage}"])
107 }
108 } catch (Exception e) {
109 common.errorMsg("Unable to determine status of ${postgresqlClientPackages} packages on target nodes: ${saltMasterBackupNode}.")
110 if (askConfirmation) {
111 input message: "Do you want to continue? Click to confirm"
112 }
Martin Polreichde3066a2019-08-02 15:33:18 +0200113 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400114 }
115 }
116 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200117 if (backupDogtag) {
118 try {
119 dogtagBackupNode = salt.getMinionsSorted(pepperEnv, dogtagTagetMatcher)[0]
120 salt.minionsReachable(pepperEnv, "I@salt:master", dogtagBackupNode)
121 }
122 catch (Exception e) {
123 common.errorMsg(e.getMessage())
124 common.errorMsg("Pipeline wasn't able to detect node with backupninja:client and dogtag:server pillars defined or the minion is not reachable")
125 currentBuild.result = "FAILURE"
126 return
127 }
128 }
Denis Egorenko1542bf22019-07-24 17:05:24 +0400129
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400130 try {
131 backupServer = salt.getMinions(pepperEnv, "I@backupninja:server")[0]
132 salt.minionsReachable(pepperEnv, "I@salt:master", backupServer)
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100133 }
134 catch (Exception e) {
135 common.errorMsg(e.getMessage())
136 common.errorMsg("Pipeline wasn't able to detect backupninja:server pillar or the minion is not reachable")
137 currentBuild.result = "FAILURE"
138 return
139 }
140 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400141 stage('Prepare for backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200142 if (backupSaltMasterAndMaas) {
143 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
144 salt.enforceState(['saltId': pepperEnv, 'target': saltMasterTargetMatcher, 'state': 'backupninja'])
145 def backupMasterSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:master:initial_data:source'))
146 def backupMinionSource = salt.getReturnValues(salt.getPillar(pepperEnv, saltMasterBackupNode, 'salt:minion:initial_data:source'))
147 // TODO: Remove ssh-keyscan once we have openssh meta for backupninja implemented
148 [backupServer, backupMasterSource, backupMinionSource].unique().each {
149 salt.cmdRun(pepperEnv, saltMasterBackupNode, "ssh-keygen -F ${it} || ssh-keyscan -H ${it} >> /root/.ssh/known_hosts")
150 }
151 def maasNodes = salt.getMinions(pepperEnv, 'I@maas:region')
152 if (!maasNodes.isEmpty()) {
153 common.infoMsg("Trying to save maas file permissions on ${maasNodes} if possible")
154 salt.cmdRun(pepperEnv, 'I@maas:region', 'which getfacl && getfacl -pR /var/lib/maas/ > /var/lib/maas/file_permissions.txt || true')
155 }
Ivan Berezovskiy8428b8d2019-07-22 16:54:06 +0400156 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200157 if (backupDogtag) {
158 salt.enforceState(['saltId': pepperEnv, 'target': 'I@backupninja:server', 'state': 'backupninja'])
159 salt.enforceState(['saltId': pepperEnv, 'target': dogtagTagetMatcher, 'state': 'backupninja'])
Ivan Berezovskiyc5a153c2019-08-09 21:07:37 +0400160 }
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100161 }
162 stage('Backup') {
Martin Polreichd60c86d2019-08-23 15:47:37 +0200163 if (backupSaltMasterAndMaas) {
164 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, saltMasterBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
165 checkBackupninjaLog(output, "Salt Master/MAAS")
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100166 }
Martin Polreichd60c86d2019-08-23 15:47:37 +0200167 if (backupDogtag) {
168 def output = salt.getReturnValues(salt.cmdRun(pepperEnv, dogtagBackupNode, "su root -c 'backupninja --now -d'")).readLines()[-2]
169 checkBackupninjaLog(output, "Dogtag")
170 }
171 }
172 stage('Results') {
173 if (logBackupSuccess.size() > 0) {
174 common.infoMsg("Following backups finished successfully: ${logBackupSuccess.join(",")}")
175 }
176 if (logBackupFailure.size() > 0) {
177 common.errorMsg("Following backups has failed: ${logBackupFailure.join(",")}. Make sure to check the logs.")
Martin Polreich30a37cf2019-05-14 10:16:34 +0200178 currentBuild.result = "FAILURE"
Pavel Cizinskyaa2d21b2019-03-19 11:08:05 +0100179 }
180 }
181 }
182}