blob: e11a5472f56a70e81f4afa123890631e1cffa72c [file] [log] [blame]
Martin Polreichaae1b9d2018-12-05 11:12:23 +01001/**
2 * Verify and restore Galera cluster
3 *
4 * Expected parameters:
5 * SALT_MASTER_CREDENTIALS Credentials to the Salt API.
6 * SALT_MASTER_URL Full Salt API address [http://10.10.10.1:8000].
Martin Polreich0d538262019-02-01 14:46:10 +01007 * ASK_CONFIRMATION Ask confirmation for restore
Martin Polreich0d538262019-02-01 14:46:10 +01008 * VERIFICATION_RETRIES Number of restries to verify the restoration.
Martin Polreich2aa74402019-01-21 14:42:48 +01009 * CHECK_TIME_SYNC Set to true to check time synchronization accross selected nodes.
Martin Polreich7ba33592019-03-21 15:12:15 +010010 * RESTORE_TYPE Sets restoration method
Martin Polreichaae1b9d2018-12-05 11:12:23 +010011 *
12**/
13
14def common = new com.mirantis.mk.Common()
15def salt = new com.mirantis.mk.Salt()
Martin Polreich71a08db2019-02-15 10:09:10 +010016def galera = new com.mirantis.mk.Galera()
Martin Polreichaae1b9d2018-12-05 11:12:23 +010017def python = new com.mirantis.mk.Python()
Martin Polreichaae1b9d2018-12-05 11:12:23 +010018def pepperEnv = "pepperEnv"
19def resultCode = 99
Martin Polreich7ba33592019-03-21 15:12:15 +010020def restoreType = env.RESTORE_TYPE
21def runRestoreDb = false
22def runBackupDb = false
Martin Polreichaae1b9d2018-12-05 11:12:23 +010023
Martin Polreich0d538262019-02-01 14:46:10 +010024askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean()
Martin Polreich2aa74402019-01-21 14:42:48 +010025checkTimeSync = (env.getProperty('CHECK_TIME_SYNC') ?: true).toBoolean()
Sergeyc8a8a792019-01-15 17:27:59 +040026
Martin Polreich0d538262019-02-01 14:46:10 +010027if (common.validInputParam(VERIFICATION_RETRIES) && VERIFICATION_RETRIES.isInteger()) {
28 verificationRetries = VERIFICATION_RETRIES.toInteger()
29} else {
30 verificationRetries = 5
31}
Martin Polreich7ba33592019-03-21 15:12:15 +010032if (restoreType.equals("BACKUP_AND_RESTORE") || restoreType.equals("ONLY_RESTORE")) {
33 runRestoreDb = true
34}
35if (restoreType.equals("BACKUP_AND_RESTORE")) {
36 runBackupDb = true
37}
Martin Polreich0d538262019-02-01 14:46:10 +010038
Martin Polreichaae1b9d2018-12-05 11:12:23 +010039timeout(time: 12, unit: 'HOURS') {
40 node() {
41 stage('Setup virtualenv for Pepper') {
42 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
43 }
Martin Polreich7ba33592019-03-21 15:12:15 +010044 stage('Verify status') {
Martin Polreich71a08db2019-02-15 10:09:10 +010045 resultCode = galera.verifyGaleraStatus(pepperEnv, false, checkTimeSync)
Martin Polreichaae1b9d2018-12-05 11:12:23 +010046 if (resultCode == 128) {
47 common.errorMsg("Unable to connect to Galera Master. Trying slaves...")
Martin Polreich71a08db2019-02-15 10:09:10 +010048 resultCode = galera.verifyGaleraStatus(pepperEnv, true, checkTimeSync)
Martin Polreichaae1b9d2018-12-05 11:12:23 +010049 if (resultCode == 129) {
Martin Polreich7ba33592019-03-21 15:12:15 +010050 common.errorMsg("Unable to obtain Galera slave minions list". "Without fixing this issue, pipeline cannot continue in verification, backup and restoration.")
Martin Polreichaae1b9d2018-12-05 11:12:23 +010051 currentBuild.result = "FAILURE"
Martin Polreich0d538262019-02-01 14:46:10 +010052 return
Martin Polreichaae1b9d2018-12-05 11:12:23 +010053 } else if (resultCode == 130) {
Martin Polreich7ba33592019-03-21 15:12:15 +010054 common.errorMsg("Neither master or slaves are reachable. Without fixing this issue, pipeline cannot continue in verification, backup and restoration.")
Martin Polreichaae1b9d2018-12-05 11:12:23 +010055 currentBuild.result = "FAILURE"
Martin Polreich0d538262019-02-01 14:46:10 +010056 return
Martin Polreichaae1b9d2018-12-05 11:12:23 +010057 }
58 }
Martin Polreich2aa74402019-01-21 14:42:48 +010059 if (resultCode == 131) {
Martin Polreich323ffde2019-05-07 15:56:38 +020060 common.errorMsg("Time desynced - Please fix this issue and rerun the pipeline.")
Martin Polreich2aa74402019-01-21 14:42:48 +010061 currentBuild.result = "FAILURE"
Martin Polreich323ffde2019-05-07 15:56:38 +020062 return
63 }
64 if (resultCode == 140 || resultCode == 141) {
65 common.errorMsg("Disk utilization check failed - Please fix this issue and rerun the pipeline.")
66 currentBuild.result = "FAILURE"
67 return
Martin Polreich2aa74402019-01-21 14:42:48 +010068 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +010069 if (resultCode == 1) {
Sergeyc8a8a792019-01-15 17:27:59 +040070 if(askConfirmation){
71 common.warningMsg("There was a problem with parsing the status output or with determining it. Do you want to run a restore?")
72 } else {
73 common.warningMsg("There was a problem with parsing the status output or with determining it. Try to restore.")
74 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +010075 } else if (resultCode > 1) {
Sergeyc8a8a792019-01-15 17:27:59 +040076 if(askConfirmation){
Martin Polreich7ba33592019-03-21 15:12:15 +010077 common.warningMsg("There's something wrong with the cluster, do you want to continue with backup and/or restore?")
Sergeyc8a8a792019-01-15 17:27:59 +040078 } else {
Martin Polreich7ba33592019-03-21 15:12:15 +010079 common.warningMsg("There's something wrong with the cluster, try to backup and/or restore.")
Sergeyc8a8a792019-01-15 17:27:59 +040080 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +010081 } else {
Sergeyc8a8a792019-01-15 17:27:59 +040082 if(askConfirmation){
Martin Polreich7ba33592019-03-21 15:12:15 +010083 common.warningMsg("There seems to be everything alright with the cluster, do you still want to continue with backup and/or restore?")
Sergeyc8a8a792019-01-15 17:27:59 +040084 } else {
Martin Polreich7ba33592019-03-21 15:12:15 +010085 common.warningMsg("There seems to be everything alright with the cluster, no backup and no restoration will be done.")
86 currentBuild.result = "SUCCESS"
87 return
Sergeyc8a8a792019-01-15 17:27:59 +040088 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +010089 }
Martin Polreich7ba33592019-03-21 15:12:15 +010090 }
91 if (runBackupDb) {
92 stage('Backup') {
93 deployBuild = build( job: "galera-database-backup-pipeline", parameters: [
94 [$class: 'StringParameterValue', name: 'SALT_MASTER_URL', value: SALT_MASTER_URL],
95 [$class: 'StringParameterValue', name: 'SALT_MASTER_CREDENTIALS', value: SALT_MASTER_CREDENTIALS],
96 [$class: 'StringParameterValue', name: 'OVERRIDE_BACKUP_NODE', value: "none"],
97 ]
98 )
99 }
100 }
101 stage('Restore') {
Sergeyc8a8a792019-01-15 17:27:59 +0400102 if(askConfirmation){
103 input message: "Are you sure you want to run a restore? Click to confirm"
104 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +0100105 try {
Sergeyc8a8a792019-01-15 17:27:59 +0400106 if((!askConfirmation && resultCode > 0) || askConfirmation){
Martin Polreich7ba33592019-03-21 15:12:15 +0100107 galera.restoreGaleraCluster(pepperEnv, runRestoreDb)
Sergeyc8a8a792019-01-15 17:27:59 +0400108 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +0100109 } catch (Exception e) {
110 common.errorMsg("Restoration process has failed.")
111 }
112 }
Martin Polreichc9466c72019-01-18 14:17:52 +0100113 stage('Verify restoration result') {
Martin Polreich0d538262019-02-01 14:46:10 +0100114 common.retry(verificationRetries, 15) {
Martin Polreich71a08db2019-02-15 10:09:10 +0100115 exitCode = galera.verifyGaleraStatus(pepperEnv, false, false)
Martin Polreich0d538262019-02-01 14:46:10 +0100116 if (exitCode >= 1) {
117 error("Verification attempt finished with an error. This may be caused by cluster not having enough time to come up or to sync. Next verification attempt in 5 seconds.")
118 } else {
119 common.infoMsg("Restoration procedure seems to be successful. See verification report to be sure.")
120 currentBuild.result = "SUCCESS"
121 }
Martin Polreichc9466c72019-01-18 14:17:52 +0100122 }
123 }
Martin Polreichaae1b9d2018-12-05 11:12:23 +0100124 }
125}