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') { |
Ivan Berezovskiy | 844da96 | 2019-07-24 15:30:54 +0400 | [diff] [blame] | 44 | def sysstatTargets = 'I@xtrabackup:client or I@xtrabackup:server' |
| 45 | def sysstatTargetsNodes = salt.getMinions(pepperEnv, sysstatTargets) |
| 46 | try { |
| 47 | if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': sysstatTargets, 'packageName': 'sysstat', 'output': false])) { |
| 48 | if (askConfirmation) { |
| 49 | input message: "Do you want to install 'sysstat' package on targeted nodes: ${sysstatTargetsNodes}? Click to confirm" |
| 50 | } |
| 51 | salt.runSaltProcessStep(pepperEnv, sysstatTargets, 'pkg.install', ['sysstat']) |
| 52 | } |
| 53 | } catch (Exception e) { |
| 54 | common.errorMsg("Unable to determine status of sysstat package on target nodes: ${sysstatTargetsNodes}.") |
| 55 | if (askConfirmation) { |
| 56 | input message: "Do you want to continue? Click to confirm" |
| 57 | } |
| 58 | } |
Martin Polreich | 208c487 | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 59 | resultCode = galera.verifyGaleraStatus(pepperEnv, false, checkTimeSync) |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 60 | if (resultCode == 128) { |
| 61 | common.errorMsg("Unable to connect to Galera Master. Trying slaves...") |
Martin Polreich | 208c487 | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 62 | resultCode = galera.verifyGaleraStatus(pepperEnv, true, checkTimeSync) |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 63 | if (resultCode == 129) { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 64 | 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] | 65 | currentBuild.result = "FAILURE" |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 66 | return |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 67 | } else if (resultCode == 130) { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 68 | 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] | 69 | currentBuild.result = "FAILURE" |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 70 | return |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 73 | if (resultCode == 131) { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 74 | common.errorMsg("Time desynced - Please fix this issue and rerun the pipeline.") |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 75 | currentBuild.result = "FAILURE" |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 76 | return |
| 77 | } |
| 78 | if (resultCode == 140 || resultCode == 141) { |
| 79 | common.errorMsg("Disk utilization check failed - Please fix this issue and rerun the pipeline.") |
| 80 | currentBuild.result = "FAILURE" |
| 81 | return |
Martin Polreich | 721b725 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 82 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 83 | if (resultCode == 1) { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 84 | if (askConfirmation) { |
| 85 | input message: "There was a problem with parsing the status output or with determining it. Do you want to run a restore?" |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 86 | } else { |
| 87 | common.warningMsg("There was a problem with parsing the status output or with determining it. Try to restore.") |
| 88 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 89 | } else if (resultCode > 1) { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 90 | if (askConfirmation) { |
| 91 | input message: "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] | 92 | } else { |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 93 | 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] | 94 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 95 | } else { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 96 | if (askConfirmation) { |
| 97 | input message: "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] | 98 | } else { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 99 | common.warningMsg("There seems to be everything alright with the cluster, no backup and no restoration will be done.") |
| 100 | currentBuild.result = "SUCCESS" |
| 101 | return |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 102 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 103 | } |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 104 | } |
| 105 | if (runBackupDb) { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 106 | if (askConfirmation) { |
| 107 | input message: "Are you sure you want to run a backup? Click to confirm" |
| 108 | } |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 109 | stage('Backup') { |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 110 | deployBuild = build(job: 'galera_backup_database', parameters: [ |
| 111 | [$class: 'StringParameterValue', name: 'SALT_MASTER_URL', value: SALT_MASTER_URL], |
| 112 | [$class: 'StringParameterValue', name: 'SALT_MASTER_CREDENTIALS', value: SALT_MASTER_CREDENTIALS], |
| 113 | [$class: 'StringParameterValue', name: 'OVERRIDE_BACKUP_NODE', value: "none"], |
| 114 | ] |
Martin Polreich | 0f3b85d | 2019-04-02 14:22:11 +0200 | [diff] [blame] | 115 | ) |
| 116 | } |
| 117 | } |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 118 | if (runRestoreDb) { |
| 119 | stage('Restore') { |
| 120 | if (askConfirmation) { |
| 121 | input message: "Are you sure you want to run a restore? Click to confirm" |
Sergey | 6579de6 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 122 | } |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 123 | try { |
| 124 | if ((!askConfirmation && resultCode > 0) || askConfirmation) { |
| 125 | galera.restoreGaleraCluster(pepperEnv, runRestoreDb) |
| 126 | } |
| 127 | } catch (Exception e) { |
| 128 | common.errorMsg("Restoration process has failed.") |
| 129 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 130 | } |
Ivan Berezovskiy | 6482411 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 131 | stage('Verify restoration result') { |
| 132 | common.retry(verificationRetries, 15) { |
| 133 | exitCode = galera.verifyGaleraStatus(pepperEnv, false, false) |
| 134 | if (exitCode >= 1) { |
| 135 | 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.") |
| 136 | } else { |
| 137 | common.infoMsg("Restoration procedure seems to be successful. See verification report to be sure.") |
| 138 | currentBuild.result = "SUCCESS" |
| 139 | } |
Martin Polreich | f7889b5 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 140 | } |
Martin Polreich | 7bc654c | 2019-01-18 14:17:52 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
Martin Polreich | f7a1bb0 | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 143 | } |
| 144 | } |