Martin Polreich | aae1b9d | 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]. |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 7 | * ASK_CONFIRMATION Ask confirmation for restore |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 8 | * VERIFICATION_RETRIES Number of restries to verify the restoration. |
Martin Polreich | 2aa7440 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 9 | * CHECK_TIME_SYNC Set to true to check time synchronization accross selected nodes. |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 10 | * RESTORE_TYPE Sets restoration method |
Martin Polreich | aae1b9d | 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 | 71a08db | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 16 | def galera = new com.mirantis.mk.Galera() |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 17 | def python = new com.mirantis.mk.Python() |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 18 | def pepperEnv = "pepperEnv" |
| 19 | def resultCode = 99 |
Martin Polreich | 7ba3359 | 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 | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 23 | |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 24 | askConfirmation = (env.getProperty('ASK_CONFIRMATION') ?: true).toBoolean() |
Martin Polreich | 2aa7440 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 25 | checkTimeSync = (env.getProperty('CHECK_TIME_SYNC') ?: true).toBoolean() |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 26 | |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 27 | if (common.validInputParam(VERIFICATION_RETRIES) && VERIFICATION_RETRIES.isInteger()) { |
| 28 | verificationRetries = VERIFICATION_RETRIES.toInteger() |
| 29 | } else { |
| 30 | verificationRetries = 5 |
| 31 | } |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 32 | if (restoreType.equals("BACKUP_AND_RESTORE") || restoreType.equals("ONLY_RESTORE")) { |
| 33 | runRestoreDb = true |
| 34 | } |
| 35 | if (restoreType.equals("BACKUP_AND_RESTORE")) { |
| 36 | runBackupDb = true |
| 37 | } |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 38 | |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 39 | timeout(time: 12, unit: 'HOURS') { |
| 40 | node() { |
| 41 | stage('Setup virtualenv for Pepper') { |
| 42 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 43 | } |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 44 | stage('Verify status') { |
Ivan Berezovskiy | b6d18d5 | 2019-07-24 15:30:54 +0400 | [diff] [blame] | 45 | def sysstatTargets = 'I@xtrabackup:client or I@xtrabackup:server' |
| 46 | def sysstatTargetsNodes = salt.getMinions(pepperEnv, sysstatTargets) |
| 47 | try { |
| 48 | if (!salt.isPackageInstalled(['saltId': pepperEnv, 'target': sysstatTargets, 'packageName': 'sysstat', 'output': false])) { |
| 49 | if (askConfirmation) { |
| 50 | input message: "Do you want to install 'sysstat' package on targeted nodes: ${sysstatTargetsNodes}? Click to confirm" |
| 51 | } |
| 52 | salt.runSaltProcessStep(pepperEnv, sysstatTargets, 'pkg.install', ['sysstat']) |
| 53 | } |
| 54 | } catch (Exception e) { |
| 55 | common.errorMsg("Unable to determine status of sysstat package on target nodes: ${sysstatTargetsNodes}.") |
Martin Polreich | 63b40fc | 2019-08-07 18:07:57 +0200 | [diff] [blame] | 56 | common.errorMsg(e.getMessage()) |
Ivan Berezovskiy | b6d18d5 | 2019-07-24 15:30:54 +0400 | [diff] [blame] | 57 | if (askConfirmation) { |
| 58 | input message: "Do you want to continue? Click to confirm" |
| 59 | } |
| 60 | } |
Martin Polreich | 71a08db | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 61 | resultCode = galera.verifyGaleraStatus(pepperEnv, false, checkTimeSync) |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 62 | if (resultCode == 128) { |
| 63 | common.errorMsg("Unable to connect to Galera Master. Trying slaves...") |
Martin Polreich | 71a08db | 2019-02-15 10:09:10 +0100 | [diff] [blame] | 64 | resultCode = galera.verifyGaleraStatus(pepperEnv, true, checkTimeSync) |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 65 | if (resultCode == 129) { |
Martin Polreich | 63b40fc | 2019-08-07 18:07:57 +0200 | [diff] [blame] | 66 | common.errorMsg("Unable to obtain Galera slave minions list. Without fixing this issue, pipeline cannot continue in verification, backup and restoration. This may be caused by wrong Galera configuration or corrupted pillar data.") |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 67 | currentBuild.result = "FAILURE" |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 68 | return |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 69 | } else if (resultCode == 130) { |
Martin Polreich | 63b40fc | 2019-08-07 18:07:57 +0200 | [diff] [blame] | 70 | common.errorMsg("Neither master or slaves are reachable. Without fixing this issue, pipeline cannot continue in verification, backup and restoration. Is at least one member of the Galera cluster up and running?") |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 71 | currentBuild.result = "FAILURE" |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 72 | return |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
Martin Polreich | 2aa7440 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 75 | if (resultCode == 131) { |
Martin Polreich | 323ffde | 2019-05-07 15:56:38 +0200 | [diff] [blame] | 76 | common.errorMsg("Time desynced - Please fix this issue and rerun the pipeline.") |
Martin Polreich | 2aa7440 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 77 | currentBuild.result = "FAILURE" |
Martin Polreich | 323ffde | 2019-05-07 15:56:38 +0200 | [diff] [blame] | 78 | return |
| 79 | } |
| 80 | if (resultCode == 140 || resultCode == 141) { |
| 81 | common.errorMsg("Disk utilization check failed - Please fix this issue and rerun the pipeline.") |
| 82 | currentBuild.result = "FAILURE" |
| 83 | return |
Martin Polreich | 2aa7440 | 2019-01-21 14:42:48 +0100 | [diff] [blame] | 84 | } |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 85 | if (resultCode == 1) { |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 86 | if (askConfirmation) { |
| 87 | input message: "There was a problem with parsing the status output or with determining it. Do you want to run a restore?" |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 88 | } else { |
| 89 | common.warningMsg("There was a problem with parsing the status output or with determining it. Try to restore.") |
| 90 | } |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 91 | } else if (resultCode > 1) { |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 92 | if (askConfirmation) { |
| 93 | input message: "There's something wrong with the cluster, do you want to continue with backup and/or restore?" |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 94 | } else { |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 95 | common.warningMsg("There's something wrong with the cluster, try to backup and/or restore.") |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 96 | } |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 97 | } else { |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 98 | if (askConfirmation) { |
| 99 | input message: "There seems to be everything alright with the cluster, do you still want to continue with backup and/or restore?" |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 100 | } else { |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 101 | common.warningMsg("There seems to be everything alright with the cluster, no backup and no restoration will be done.") |
| 102 | currentBuild.result = "SUCCESS" |
| 103 | return |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 104 | } |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 105 | } |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 106 | } |
| 107 | if (runBackupDb) { |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 108 | if (askConfirmation) { |
| 109 | input message: "Are you sure you want to run a backup? Click to confirm" |
| 110 | } |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 111 | stage('Backup') { |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 112 | deployBuild = build(job: 'galera_backup_database', parameters: [ |
| 113 | [$class: 'StringParameterValue', name: 'SALT_MASTER_URL', value: SALT_MASTER_URL], |
| 114 | [$class: 'StringParameterValue', name: 'SALT_MASTER_CREDENTIALS', value: SALT_MASTER_CREDENTIALS], |
| 115 | [$class: 'StringParameterValue', name: 'OVERRIDE_BACKUP_NODE', value: "none"], |
| 116 | ] |
Martin Polreich | 7ba3359 | 2019-03-21 15:12:15 +0100 | [diff] [blame] | 117 | ) |
| 118 | } |
| 119 | } |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 120 | if (runRestoreDb) { |
| 121 | stage('Restore') { |
| 122 | if (askConfirmation) { |
| 123 | input message: "Are you sure you want to run a restore? Click to confirm" |
Sergey | c8a8a79 | 2019-01-15 17:27:59 +0400 | [diff] [blame] | 124 | } |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 125 | try { |
| 126 | if ((!askConfirmation && resultCode > 0) || askConfirmation) { |
| 127 | galera.restoreGaleraCluster(pepperEnv, runRestoreDb) |
| 128 | } |
| 129 | } catch (Exception e) { |
| 130 | common.errorMsg("Restoration process has failed.") |
Martin Polreich | 63b40fc | 2019-08-07 18:07:57 +0200 | [diff] [blame] | 131 | common.errorMsg(e.getMessage()) |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 132 | } |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 133 | } |
Ivan Berezovskiy | 6ef32f0 | 2019-07-26 15:55:24 +0400 | [diff] [blame] | 134 | stage('Verify restoration result') { |
| 135 | common.retry(verificationRetries, 15) { |
| 136 | exitCode = galera.verifyGaleraStatus(pepperEnv, false, false) |
| 137 | if (exitCode >= 1) { |
| 138 | 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.") |
| 139 | } else { |
| 140 | common.infoMsg("Restoration procedure seems to be successful. See verification report to be sure.") |
| 141 | currentBuild.result = "SUCCESS" |
| 142 | } |
Martin Polreich | 0d53826 | 2019-02-01 14:46:10 +0100 | [diff] [blame] | 143 | } |
Martin Polreich | c9466c7 | 2019-01-18 14:17:52 +0100 | [diff] [blame] | 144 | } |
| 145 | } |
Martin Polreich | aae1b9d | 2018-12-05 11:12:23 +0100 | [diff] [blame] | 146 | } |
| 147 | } |