Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 1 | /** |
| 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]. |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 7 | * ASK_CONFIRMATION Ask confirmation for restore |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 8 | * CHECK_TIME_SYNC Set to true to check time synchronization accross selected nodes. |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 9 | * VERIFICATION_RETRIES Number of restries to verify the restoration. |
Martin Polreich | ddfdb61 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 10 | * RESTORE_TYPE Sets restoration method |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 11 | * |
| 12 | **/ |
| 13 | |
| 14 | def common = new com.mirantis.mk.Common() |
| 15 | def salt = new com.mirantis.mk.Salt() |
Martin Polreich | 208c487 | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 16 | def galera = new com.mirantis.mk.Galera() |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 17 | def python = new com.mirantis.mk.Python() |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 18 | def pepperEnv = "pepperEnv" |
| 19 | def resultCode = 99 |
Martin Polreich | ddfdb61 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 20 | def restoreType = env.RESTORE_TYPE |
| 21 | def runRestoreDb = false |
| 22 | def runBackupDb = false |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 23 | |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 24 | askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean() |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 25 | checkTimeSync = (env.getProperty('CHECK_TIME_SYNC') ?: true).toBoolean() |
Martin Polreich | 49f16c0 | 2019-02-04 13:14:01 +0100 | [diff] [blame] | 26 | if (common.validInputParam('VERIFICATION_RETRIES') && VERIFICATION_RETRIES.isInteger()) { |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 27 | verificationRetries = VERIFICATION_RETRIES.toInteger() |
| 28 | } else { |
| 29 | verificationRetries = 5 |
| 30 | } |
Martin Polreich | ddfdb61 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 31 | if (restoreType.equals("BACKUP_AND_RESTORE") || restoreType.equals("ONLY_RESTORE")) { |
| 32 | runRestoreDb = true |
| 33 | } |
| 34 | if (restoreType.equals("BACKUP_AND_RESTORE")) { |
| 35 | runBackupDb = true |
| 36 | } |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 37 | |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 38 | timeout(time: 12, unit: 'HOURS') { |
| 39 | node() { |
| 40 | stage('Setup virtualenv for Pepper') { |
| 41 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 42 | } |
Martin Polreich | ddfdb61 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 43 | stage('Verify status') { |
Martin Polreich | 208c487 | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 44 | resultCode = galera.verifyGaleraStatus(pepperEnv, false, checkTimeSync) |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 45 | if (resultCode == 128) { |
| 46 | common.errorMsg("Unable to connect to Galera Master. Trying slaves...") |
Martin Polreich | 208c487 | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 47 | resultCode = galera.verifyGaleraStatus(pepperEnv, true, checkTimeSync) |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 48 | if (resultCode == 129) { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 49 | common.errorMsg("Unable to obtain Galera slave minions list". "Without fixing this issue, pipeline cannot continue in verification, backup and restoration.") |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 50 | currentBuild.result = "FAILURE" |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 51 | return |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 52 | } else if (resultCode == 130) { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 53 | common.errorMsg("Neither master or slaves are reachable. Without fixing this issue, pipeline cannot continue in verification, backup and restoration.") |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 54 | currentBuild.result = "FAILURE" |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 55 | return |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 58 | if (resultCode == 131) { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 59 | common.errorMsg("Time desynced - Please fix this issue and rerun the pipeline.") |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 60 | currentBuild.result = "FAILURE" |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 61 | return |
| 62 | } |
| 63 | if (resultCode == 140 || resultCode == 141) { |
| 64 | common.errorMsg("Disk utilization check failed - Please fix this issue and rerun the pipeline.") |
| 65 | currentBuild.result = "FAILURE" |
| 66 | return |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 67 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 68 | if (resultCode == 1) { |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 69 | if(askConfirmation){ |
| 70 | common.warningMsg("There was a problem with parsing the status output or with determining it. Do you want to run a restore?") |
| 71 | } else { |
| 72 | common.warningMsg("There was a problem with parsing the status output or with determining it. Try to restore.") |
| 73 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 74 | } else if (resultCode > 1) { |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 75 | if(askConfirmation){ |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 76 | common.warningMsg("There's something wrong with the cluster, do you want to continue with backup and/or restore?") |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 77 | } else { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 78 | common.warningMsg("There's something wrong with the cluster, try to backup and/or restore.") |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 79 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 80 | } else { |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 81 | if(askConfirmation){ |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 82 | common.warningMsg("There seems to be everything alright with the cluster, do you still want to continue with backup and/or restore?") |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 83 | } else { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 84 | common.warningMsg("There seems to be everything alright with the cluster, no backup and no restoration will be done.") |
| 85 | currentBuild.result = "SUCCESS" |
| 86 | return |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 87 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 88 | } |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 89 | } |
| 90 | if (runBackupDb) { |
| 91 | stage('Backup') { |
| 92 | common.infoMsg("Running backup job.") |
| 93 | deployBuild = build( job: "galera-database-backup-pipeline", parameters: [ |
Martin Polreich | 7b1766c | 2019-04-11 16:45:54 +0200 | [diff] [blame^] | 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"], |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 97 | ] |
| 98 | ) |
| 99 | } |
| 100 | } |
| 101 | stage('Restore') { |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 102 | if(askConfirmation){ |
| 103 | input message: "Are you sure you want to run a restore? Click to confirm" |
| 104 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 105 | try { |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 106 | if((!askConfirmation && resultCode > 0) || askConfirmation){ |
Martin Polreich | ddfdb61 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 107 | galera.restoreGaleraCluster(pepperEnv, runRestoreDb) |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 108 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 109 | } catch (Exception e) { |
| 110 | common.errorMsg("Restoration process has failed.") |
| 111 | } |
| 112 | } |
Martin Polreich | 7bc654c | 2019-01-18 14:17:52 +0100 | [diff] [blame] | 113 | stage('Verify restoration result') { |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 114 | common.retry(verificationRetries, 15) { |
Martin Polreich | 208c487 | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 115 | exitCode = galera.verifyGaleraStatus(pepperEnv, false, false) |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 116 | if (exitCode >= 1) { |
Martin Polreich | 49f16c0 | 2019-02-04 13:14:01 +0100 | [diff] [blame] | 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 15 seconds.") |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 118 | } else { |
| 119 | common.infoMsg("Restoration procedure seems to be successful. See verification report to be sure.") |
| 120 | currentBuild.result = "SUCCESS" |
| 121 | } |
Martin Polreich | 7bc654c | 2019-01-18 14:17:52 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 124 | } |
| 125 | } |