blob: 693ee88bfef139490225709454c5a8607e88c3bd [file] [log] [blame]
Martin Polreichf7a1bb02018-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].
Sergey6579de62019-01-15 17:27:59 +04007 * ASK_CONFIRMATION Ask confirmation for restore
Martin Polreich721b7252019-01-21 14:42:48 +01008 * CHECK_TIME_SYNC Set to true to check time synchronization accross selected nodes.
Martin Polreichf7889b52019-02-01 14:46:10 +01009 * VERIFICATION_RETRIES Number of restries to verify the restoration.
Martin Polreichddfdb612019-03-21 15:12:15 +010010 * RESTORE_TYPE Sets restoration method
Martin Polreichf7a1bb02018-12-05 11:12:23 +010011 *
12**/
13
14def common = new com.mirantis.mk.Common()
15def salt = new com.mirantis.mk.Salt()
Martin Polreich208c4872019-02-15 10:09:10 +010016def galera = new com.mirantis.mk.Galera()
Martin Polreichf7a1bb02018-12-05 11:12:23 +010017def python = new com.mirantis.mk.Python()
Martin Polreichf7a1bb02018-12-05 11:12:23 +010018def pepperEnv = "pepperEnv"
19def resultCode = 99
Martin Polreichddfdb612019-03-21 15:12:15 +010020def restoreType = env.RESTORE_TYPE
21def runRestoreDb = false
22def runBackupDb = false
Martin Polreichf7a1bb02018-12-05 11:12:23 +010023
Sergey6579de62019-01-15 17:27:59 +040024askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean()
Martin Polreich721b7252019-01-21 14:42:48 +010025checkTimeSync = (env.getProperty('CHECK_TIME_SYNC') ?: true).toBoolean()
Martin Polreich49f16c02019-02-04 13:14:01 +010026if (common.validInputParam('VERIFICATION_RETRIES') && VERIFICATION_RETRIES.isInteger()) {
Martin Polreichf7889b52019-02-01 14:46:10 +010027 verificationRetries = VERIFICATION_RETRIES.toInteger()
28} else {
29 verificationRetries = 5
30}
Martin Polreichddfdb612019-03-21 15:12:15 +010031if (restoreType.equals("BACKUP_AND_RESTORE") || restoreType.equals("ONLY_RESTORE")) {
32 runRestoreDb = true
33}
34if (restoreType.equals("BACKUP_AND_RESTORE")) {
35 runBackupDb = true
36}
Sergey6579de62019-01-15 17:27:59 +040037
Martin Polreichf7a1bb02018-12-05 11:12:23 +010038timeout(time: 12, unit: 'HOURS') {
39 node() {
40 stage('Setup virtualenv for Pepper') {
41 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
42 }
Martin Polreichddfdb612019-03-21 15:12:15 +010043 stage('Verify status') {
Martin Polreich208c4872019-02-15 10:09:10 +010044 resultCode = galera.verifyGaleraStatus(pepperEnv, false, checkTimeSync)
Martin Polreichddfdb612019-03-21 15:12:15 +010045 }
46 if (runBackupDb) {
47 stage('Backup') {
48 deployBuild = build( job: "galera-database-backup-pipeline", parameters: [
49 [$class: string_value, name: 'SALT_MASTER_URL', value: SALT_MASTER_URL],
50 [$class: string_value, name: 'SALT_MASTER_CREDENTIALS', value: SALT_MASTER_CREDENTIALS],
51 [$class: string_value, name: 'OVERRIDE_BACKUP_NODE', value: "none"],
52 ]
53 )
54 }
55 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +010056 stage('Restore') {
57 if (resultCode == 128) {
58 common.errorMsg("Unable to connect to Galera Master. Trying slaves...")
Martin Polreich208c4872019-02-15 10:09:10 +010059 resultCode = galera.verifyGaleraStatus(pepperEnv, true, checkTimeSync)
Martin Polreichf7a1bb02018-12-05 11:12:23 +010060 if (resultCode == 129) {
61 common.errorMsg("Unable to obtain Galera slave minions list". "Without fixing this issue, pipeline cannot continue in verification and restoration.")
62 currentBuild.result = "FAILURE"
Martin Polreichf7889b52019-02-01 14:46:10 +010063 return
Martin Polreichf7a1bb02018-12-05 11:12:23 +010064 } else if (resultCode == 130) {
65 common.errorMsg("Neither master or slaves are reachable. Without fixing this issue, pipeline cannot continue in verification and restoration.")
66 currentBuild.result = "FAILURE"
Martin Polreichf7889b52019-02-01 14:46:10 +010067 return
Martin Polreichf7a1bb02018-12-05 11:12:23 +010068 }
69 }
Martin Polreich721b7252019-01-21 14:42:48 +010070 if (resultCode == 131) {
71 common.errorMsg("Time desynced - Click proceed when the issue is fixed or abort.")
72 currentBuild.result = "FAILURE"
73 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +010074 if (resultCode == 1) {
Sergey6579de62019-01-15 17:27:59 +040075 if(askConfirmation){
76 common.warningMsg("There was a problem with parsing the status output or with determining it. Do you want to run a restore?")
77 } else {
78 common.warningMsg("There was a problem with parsing the status output or with determining it. Try to restore.")
79 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +010080 } else if (resultCode > 1) {
Sergey6579de62019-01-15 17:27:59 +040081 if(askConfirmation){
82 common.warningMsg("There's something wrong with the cluster, do you want to run a restore?")
83 } else {
84 common.warningMsg("There's something wrong with the cluster, try to restore.")
85 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +010086 } else {
Sergey6579de62019-01-15 17:27:59 +040087 if(askConfirmation){
88 common.warningMsg("There seems to be everything alright with the cluster, do you still want to run a restore?")
89 } else {
90 common.warningMsg("There seems to be everything alright with the cluster, do nothing")
91 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +010092 }
Sergey6579de62019-01-15 17:27:59 +040093 if(askConfirmation){
94 input message: "Are you sure you want to run a restore? Click to confirm"
95 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +010096 try {
Sergey6579de62019-01-15 17:27:59 +040097 if((!askConfirmation && resultCode > 0) || askConfirmation){
Martin Polreichddfdb612019-03-21 15:12:15 +010098 galera.restoreGaleraCluster(pepperEnv, runRestoreDb)
Sergey6579de62019-01-15 17:27:59 +040099 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +0100100 } catch (Exception e) {
101 common.errorMsg("Restoration process has failed.")
102 }
103 }
Martin Polreich7bc654c2019-01-18 14:17:52 +0100104 stage('Verify restoration result') {
Martin Polreichf7889b52019-02-01 14:46:10 +0100105 common.retry(verificationRetries, 15) {
Martin Polreich208c4872019-02-15 10:09:10 +0100106 exitCode = galera.verifyGaleraStatus(pepperEnv, false, false)
Martin Polreichf7889b52019-02-01 14:46:10 +0100107 if (exitCode >= 1) {
Martin Polreich49f16c02019-02-04 13:14:01 +0100108 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 15 seconds.")
Martin Polreichf7889b52019-02-01 14:46:10 +0100109 } else {
110 common.infoMsg("Restoration procedure seems to be successful. See verification report to be sure.")
111 currentBuild.result = "SUCCESS"
112 }
Martin Polreich7bc654c2019-01-18 14:17:52 +0100113 }
114 }
Martin Polreichf7a1bb02018-12-05 11:12:23 +0100115 }
116}