Jiri Broulik | f2abf42 | 2017-07-03 17:04:19 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Update packages on given nodes |
| 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]. |
| 7 | * |
| 8 | **/ |
| 9 | |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 10 | common = new com.mirantis.mk.Common() |
| 11 | salt = new com.mirantis.mk.Salt() |
| 12 | python = new com.mirantis.mk.Python() |
Jiri Broulik | f2abf42 | 2017-07-03 17:04:19 +0200 | [diff] [blame] | 13 | |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 14 | def pepperEnv = "pepperEnv" |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 15 | |
| 16 | def getValueForPillarKey(pepperEnv, target, pillarKey) { |
| 17 | def out = salt.getReturnValues(salt.getPillar(pepperEnv, target, pillarKey)) |
| 18 | if (out == '') { |
| 19 | throw new Exception("Cannot get value for ${pillarKey} key on ${target} target") |
| 20 | } |
| 21 | return out.toString() |
| 22 | } |
| 23 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 24 | timeout(time: 12, unit: 'HOURS') { |
| 25 | node() { |
Jiri Broulik | f2abf42 | 2017-07-03 17:04:19 +0200 | [diff] [blame] | 26 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 27 | stage('Setup virtualenv for Pepper') { |
| 28 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Jiri Broulik | f2abf42 | 2017-07-03 17:04:19 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Inessa Vasilevskaya | b0d2503 | 2018-10-22 08:45:26 +0200 | [diff] [blame] | 31 | stage('Opencontrail controllers health check') { |
| 32 | try { |
| 33 | salt.enforceState(pepperEnv, 'I@opencontrail:control or I@opencontrail:collector', 'opencontrail.upgrade.verify', true, true) |
| 34 | } catch (Exception er) { |
| 35 | common.errorMsg("Opencontrail controllers health check stage found issues with services. Please take a look at the logs above.") |
| 36 | throw er |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | stage('Backup') { |
| 41 | salt.cmdRun(pepperEnv, 'I@cassandra:backup:client', 'bash /usr/local/bin/cassandra-backup-runner-call.sh') |
| 42 | } |
| 43 | |
Jiri Broulik | 5d4dd2c | 2018-04-16 17:50:07 +0200 | [diff] [blame] | 44 | stage('Restore') { |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 45 | // get opencontrail version |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 46 | def contrailVersion = getValueForPillarKey(pepperEnv, "I@opencontrail:control:role:primary", "_param:opencontrail_version") |
| 47 | common.infoMsg("OpenContrail version is ${contrailVersion}") |
| 48 | if (contrailVersion.startsWith('4')) { |
| 49 | controllerImage = getValueForPillarKey(pepperEnv, "I@opencontrail:control:role:primary", |
| 50 | "docker:client:compose:opencontrail:service:controller:container_name") |
| 51 | common.infoMsg("Applying db restore procedure for OpenContrail 4.X version") |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 52 | try { |
| 53 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', 'doctrail controller systemctl stop contrail-database' ) |
| 54 | } catch (Exception err) { |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 55 | common.errorMsg('An error has been occurred during cassandra db shutdown: ' + err.getMessage()) |
| 56 | throw err |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 57 | } |
| 58 | try { |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 59 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "docker exec ${controllerImage} bash -c 'for f in \$(ls /var/lib/cassandra/); do rm -r /var/lib/cassandra/\$f; done'") |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 60 | } catch (Exception err) { |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 61 | common.errorMsg('Cannot cleanup cassandra data: ' + err.getMessage()) |
| 62 | throw err |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 63 | } |
| 64 | try { |
| 65 | salt.cmdRun(pepperEnv, 'I@cassandra:backup:client', 'doctrail controller systemctl start contrail-database' ) |
| 66 | } catch (Exception err) { |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 67 | common.errorMsg('An error has been occurred during cassandra db startup: ' + err.getMessage()) |
| 68 | throw err |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 69 | } |
| 70 | // remove restore-already-happenned file if any is present |
| 71 | try { |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 72 | salt.cmdRun(pepperEnv, 'I@cassandra:backup:client', 'rm /var/backups/cassandra/dbrestored') |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 73 | } catch (Exception err) { |
| 74 | common.warningMsg('/var/backups/cassandra/dbrestored not present? ' + err.getMessage()) |
| 75 | } |
| 76 | // perform actual backup |
| 77 | salt.enforceState(pepperEnv, 'I@cassandra:backup:client', "cassandra") |
| 78 | salt.runSaltProcessStep(pepperEnv, 'I@cassandra:backup:client', 'system.reboot', null, [], true, 5) |
| 79 | sleep(5) |
| 80 | salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control and not I@cassandra:backup:client', 'system.reboot', null, [], true, 5) |
| 81 | // the lovely wait-60-seconds mantra before restarting supervisor-database service |
| 82 | sleep(60) |
| 83 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "doctrail controller systemctl restart contrail-database") |
Inessa Vasilevskaya | b0d2503 | 2018-10-22 08:45:26 +0200 | [diff] [blame] | 84 | // another mantra, wait till all services are up |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 85 | sleep(60) |
Inessa Vasilevskaya | b0d2503 | 2018-10-22 08:45:26 +0200 | [diff] [blame] | 86 | } else { |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 87 | try { |
| 88 | salt.runSaltProcessStep(pepperEnv, 'I@neutron:server', 'service.stop', ['neutron-server'], null, true) |
| 89 | } catch (Exception er) { |
| 90 | common.warningMsg('neutron-server service already stopped') |
| 91 | } |
| 92 | try { |
| 93 | salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'service.stop', ['supervisor-config'], null, true) |
| 94 | } catch (Exception er) { |
| 95 | common.warningMsg('Supervisor-config service already stopped') |
| 96 | } |
| 97 | // Cassandra restore section |
| 98 | try { |
| 99 | salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'service.stop', ['supervisor-database'], null, true) |
| 100 | } catch (Exception er) { |
| 101 | common.warningMsg('Supervisor-database service already stopped') |
| 102 | } |
| 103 | try { |
| 104 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "mkdir -p /root/cassandra/cassandra.bak") |
| 105 | } catch (Exception er) { |
| 106 | common.warningMsg('Directory already exists') |
| 107 | } |
| 108 | |
| 109 | try { |
| 110 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "mv /var/lib/cassandra/* /root/cassandra/cassandra.bak") |
| 111 | } catch (Exception er) { |
| 112 | common.warningMsg('Files were already moved') |
| 113 | } |
| 114 | try { |
| 115 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "rm -rf /var/lib/cassandra/*") |
| 116 | } catch (Exception er) { |
| 117 | common.warningMsg('Directory already empty') |
| 118 | } |
| 119 | |
Anton Samoylov | f3b0ca2 | 2019-06-21 19:53:26 +0400 | [diff] [blame^] | 120 | def backupDir = getValueForPillarKey(pepperEnv, "I@cassandra:backup:client", "cassandra:backup:backup_dir") |
Inessa Vasilevskaya | ea45439 | 2018-09-07 17:25:34 +0200 | [diff] [blame] | 121 | common.infoMsg("Backup directory is ${backupDir}") |
| 122 | salt.runSaltProcessStep(pepperEnv, 'I@cassandra:backup:client', 'file.remove', ["${backupDir}/dbrestored"], null, true) |
| 123 | |
| 124 | salt.runSaltProcessStep(pepperEnv, 'I@cassandra:backup:client', 'service.start', ['supervisor-database'], null, true) |
| 125 | |
| 126 | // wait until supervisor-database service is up |
| 127 | salt.commandStatus(pepperEnv, 'I@cassandra:backup:client', 'service supervisor-database status', 'running') |
| 128 | sleep(60) |
| 129 | |
| 130 | // performs restore |
| 131 | salt.enforceState(pepperEnv, 'I@cassandra:backup:client', "cassandra.backup") |
| 132 | salt.runSaltProcessStep(pepperEnv, 'I@cassandra:backup:client', 'system.reboot', null, null, true, 5) |
| 133 | sleep(5) |
| 134 | salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control and not I@cassandra:backup:client', 'system.reboot', null, null, true, 5) |
| 135 | |
| 136 | // wait until supervisor-database service is up |
| 137 | salt.commandStatus(pepperEnv, 'I@cassandra:backup:client', 'service supervisor-database status', 'running') |
| 138 | salt.commandStatus(pepperEnv, 'I@opencontrail:control and not I@cassandra:backup:client', 'service supervisor-database status', 'running') |
| 139 | sleep(5) |
| 140 | |
| 141 | salt.runSaltProcessStep(pepperEnv, 'I@opencontrail:control', 'service.restart', ['supervisor-database'], null, true) |
| 142 | salt.runSaltProcessStep(pepperEnv, 'I@neutron:server', 'service.start', ['neutron-server'], null, true) |
| 143 | |
| 144 | // wait until contrail-status is up |
| 145 | salt.commandStatus(pepperEnv, 'I@opencontrail:control', "contrail-status | grep -v == | grep -v \'disabled on boot\' | grep -v nodemgr | grep -v active | grep -v backup", null, false) |
| 146 | |
| 147 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "nodetool status") |
| 148 | salt.cmdRun(pepperEnv, 'I@opencontrail:control', "contrail-status") |
Jiri Broulik | 60dcab3 | 2018-03-08 17:42:06 +0100 | [diff] [blame] | 149 | } |
Jiri Broulik | f2abf42 | 2017-07-03 17:04:19 +0200 | [diff] [blame] | 150 | } |
Inessa Vasilevskaya | b0d2503 | 2018-10-22 08:45:26 +0200 | [diff] [blame] | 151 | |
| 152 | stage('Opencontrail controllers health check') { |
| 153 | common.retry(3, 20){ |
Inessa Vasilevskaya | b0d2503 | 2018-10-22 08:45:26 +0200 | [diff] [blame] | 154 | salt.enforceState(pepperEnv, 'I@opencontrail:control or I@opencontrail:collector', 'opencontrail.upgrade.verify', true, true) |
| 155 | } |
| 156 | } |
Jiri Broulik | f2abf42 | 2017-07-03 17:04:19 +0200 | [diff] [blame] | 157 | } |
| 158 | } |