Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 1 | /** |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 2 | * Update packages |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 3 | * |
| 4 | * Expected parameters: |
| 5 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API. |
| 6 | * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:8000]. |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | pepperEnv = "pepperEnv" |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 10 | def common = new com.mirantis.mk.Common() |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 11 | def salt = new com.mirantis.mk.Salt() |
| 12 | def ceph = new com.mirantis.mk.Ceph() |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 13 | def python = new com.mirantis.mk.Python() |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 14 | def packages |
| 15 | def command |
| 16 | def commandKwargs |
| 17 | def selMinions = [] |
Ivan Berezovskiy | 373e8a3 | 2019-11-19 20:04:12 +0400 | [diff] [blame] | 18 | def flags = CLUSTER_FLAGS ? CLUSTER_FLAGS.tokenize(',') : [] |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 19 | def runHighState = RUNHIGHSTATE |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 20 | |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 21 | def collectPkgVersion(target, packageName) { |
| 22 | def salt = new com.mirantis.mk.Salt() |
| 23 | return salt.runSaltCommand(pepperEnv, 'local', ['expression': target, 'type': 'compound'], "pkg.version", true, packageName) |
| 24 | } |
| 25 | |
| 26 | def getChangedPkgs(oldVersions, newVersions) { |
| 27 | def common = new com.mirantis.mk.Common() |
| 28 | def changedPkgs = [:] |
| 29 | def updated = false |
| 30 | newVersions.each { k, v -> |
| 31 | changedPkgs[k] = [:] |
| 32 | if (v == null || !v['return'] || oldVersions[k] == null || !oldVersions[k]['return']) { |
| 33 | common.warningMsg("Can't detect package version changes for ceph-${k} packages") |
| 34 | changedPkgs[k]["*"] = true |
| 35 | updated = true |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | // since run was not in batch mode, get only 0 element which contains all output |
| 40 | v['return'][0].each { tgt, newPgkVersion -> |
| 41 | oldVersion = oldVersions[k]['return'][0].get(tgt, "") |
| 42 | if (oldVersion == newPgkVersion) { |
| 43 | changedPkgs[k][tgt] = false |
| 44 | return |
| 45 | } |
| 46 | common.infoMsg("${tgt} has updated ceph ${k} packages ${oldVersion} -> ${newPgkVersion}") |
| 47 | updated = true |
| 48 | changedPkgs[k][tgt] = true |
| 49 | } |
| 50 | } |
| 51 | return ["updated": updated, "changed": changedPkgs] |
| 52 | } |
| 53 | |
| 54 | // if some map contains tgt and has true value - restart needed |
| 55 | def needToRestart(infoPkgs, daemon, tgt) { |
| 56 | if (infoPkgs[daemon].get("*", false) || infoPkgs[daemon].get(tgt, false) || infoPkgs["common"].get(tgt, false)) { |
| 57 | return true |
| 58 | } |
| 59 | return false |
| 60 | } |
| 61 | |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 62 | timeout(time: 12, unit: 'HOURS') { |
| 63 | node() { |
| 64 | try { |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 65 | def targets = ["common": "ceph-common", "osd": "ceph-osd", "mon": "ceph-mon", |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 66 | "mgr" : "ceph-mgr", "radosgw": "radosgw"] |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 67 | |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 68 | def oldPackageVersions = [:] |
| 69 | def newPackageVersions = [:] |
| 70 | |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 71 | stage('Setup virtualenv for Pepper') { |
| 72 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 73 | } |
| 74 | |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 75 | stage('Apply package upgrades on all nodes') { |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 76 | targets.each { key, value -> |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 77 | target = "I@ceph:${key}" |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 78 | packages = value |
| 79 | // check package versions before upgrade to compare it after update run |
| 80 | oldPackageVersions[key] = collectPkgVersion(target, packages) |
| 81 | salt.enforceState(pepperEnv, target, 'linux.system.repo', true) |
| 82 | command = "pkg.install" |
| 83 | commandKwargs = ['only_upgrade': 'true', 'force_yes': 'true'] |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 84 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': target, 'type': 'compound'], command, true, packages, commandKwargs) |
| 85 | salt.printSaltCommandResult(out) |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 86 | // check package version after update |
| 87 | newPackageVersions[key] = collectPkgVersion(target, packages) |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 91 | def packageChanges = getChangedPkgs(oldPackageVersions, newPackageVersions) |
| 92 | |
| 93 | if (!packageChanges["updated"]) { |
| 94 | common.infoMsg("Ceph packages were not updated, skipping service restart") |
| 95 | return |
| 96 | } |
| 97 | |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 98 | stage('Set cluster flags') { |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 99 | common.infoMsg("Ceph packages update detected, setting cluster noout flag") |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 100 | if (flags.size() > 0) { |
| 101 | stage('Set cluster flags') { |
| 102 | for (flag in flags) { |
| 103 | salt.cmdRun(pepperEnv, "I@ceph:mon and I@ceph:common:keyring:admin", 'ceph osd set ' + flag) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 109 | stage("Restart MONs/MGRs") { |
| 110 | ["mon", "mgr"].each {daemon -> |
| 111 | selMinions = salt.getMinions(pepperEnv, "I@ceph:${daemon}") |
| 112 | for (tgt in selMinions) { |
| 113 | if (!needToRestart(packageChanges["changed"], daemon, tgt)) { |
| 114 | common.infoMsg("Node ${tgt} has no updated ceph packages, skipping service restart on it") |
| 115 | continue |
| 116 | } |
| 117 | // runSaltProcessStep 'service.restart' don't work for this services |
| 118 | salt.cmdRun(pepperEnv, tgt, "systemctl restart ceph-${daemon}.target") |
| 119 | ceph.waitForHealthy(pepperEnv, tgt, flags) |
| 120 | if (runHighState) { |
| 121 | salt.enforceHighstate(pepperEnv, tgt) |
| 122 | } |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 123 | } |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | stage('Restart OSDs') { |
Mateusz Los | e28ebd7 | 2020-04-28 16:36:11 +0200 | [diff] [blame] | 128 | def device_grain_name = "ceph_disk" |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 129 | selMinions = salt.getMinions(pepperEnv, "I@ceph:osd") |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 130 | for (tgt in selMinions) { |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 131 | if (!needToRestart(packageChanges["changed"], "osd", tgt)) { |
| 132 | common.infoMsg("Node ${tgt} has no updated ceph packages, skipping service restart on it") |
| 133 | continue |
| 134 | } |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 135 | salt.runSaltProcessStep(pepperEnv, tgt, 'saltutil.sync_grains', [], null, true, 5) |
mjedynski | af258b0 | 2019-12-09 15:17:58 +0100 | [diff] [blame] | 136 | def ceph_disks = salt.getGrain(pepperEnv, tgt, 'ceph')['return'][0].values()[0].values()[0][device_grain_name] |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 137 | |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 138 | def osd_ids = [] |
| 139 | for (i in ceph_disks) { |
| 140 | def osd_id = i.getKey().toString() |
| 141 | osd_ids.add('osd.' + osd_id) |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 142 | } |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 143 | |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 144 | salt.cmdRun(pepperEnv, tgt, 'ceph osd set noout') |
Ivan Berezovskiy | 373e8a3 | 2019-11-19 20:04:12 +0400 | [diff] [blame] | 145 | flags = 'noout' in flags ? flags : flags + ['noout'] |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 146 | |
| 147 | for (i in osd_ids) { |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 148 | salt.runSaltProcessStep(pepperEnv, tgt, 'service.restart', ['ceph-osd@' + i.replaceAll('osd.', '')], null, true) |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 149 | // wait for healthy cluster |
Tomek Jaroszyk | d085e51 | 2020-11-09 13:58:02 +0100 | [diff] [blame] | 150 | ceph.waitForHealthy(pepperEnv, tgt, flags, 100) |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 151 | } |
| 152 | |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 153 | if (runHighState) { |
| 154 | salt.enforceHighstate(pepperEnv, tgt) |
| 155 | } |
| 156 | |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 157 | salt.cmdRun(pepperEnv, tgt, 'ceph osd unset noout') |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 161 | stage('Restart RGWs') { |
| 162 | selMinions = salt.getMinions(pepperEnv, "I@ceph:radosgw") |
| 163 | for (tgt in selMinions) { |
Denis Egorenko | 5f335e3 | 2021-06-18 17:14:45 +0400 | [diff] [blame] | 164 | if (!needToRestart(packageChanges["changed"], "radosgw", tgt)) { |
| 165 | common.infoMsg("Node ${tgt} has no updated ceph packages, skipping service restart on it") |
| 166 | continue |
| 167 | } |
Mateusz Los | abf4ef5 | 2020-05-11 10:37:23 +0200 | [diff] [blame] | 168 | salt.cmdRun(pepperEnv, tgt, "systemctl restart ceph-radosgw.target") |
| 169 | ceph.waitForHealthy(pepperEnv, tgt, flags) |
| 170 | if (runHighState) { |
| 171 | salt.enforceHighstate(pepperEnv, tgt) |
| 172 | } |
| 173 | } |
| 174 | } |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 175 | } catch (Throwable e) { |
| 176 | // If there was an error or exception thrown, the build failed |
Mateusz Los | 574aefb | 2020-05-11 09:59:32 +0200 | [diff] [blame] | 177 | if (flags.size() > 0) { |
| 178 | stage('Unset cluster flags') { |
| 179 | for (flag in flags) { |
| 180 | salt.cmdRun(pepperEnv, "I@ceph:mon and I@ceph:common:keyring:admin", 'ceph osd unset ' + flag) |
| 181 | } |
| 182 | } |
| 183 | } |
Alena Kiseleva | 3a49b80 | 2018-12-07 17:03:02 +0300 | [diff] [blame] | 184 | currentBuild.result = "FAILURE" |
| 185 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 186 | throw e |
| 187 | } |
| 188 | } |
Alena Kiseleva | 7f3c386 | 2019-06-12 13:16:33 +0300 | [diff] [blame] | 189 | } |