Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Remove Ceph node from existing cluster |
| 4 | * |
| 5 | * Requred parameters: |
| 6 | * SALT_MASTER_URL URL of Salt master |
| 7 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| 8 | * HOST Host (minion id) to be removed |
| 9 | * HOST_TYPE Type of Ceph node to be removed. Valid values are mon/osd/rgw |
| 10 | * ADMIN_HOST Host (minion id) with admin keyring |
| 11 | * WAIT_FOR_HEALTHY Wait for cluster rebalance before stoping daemons |
| 12 | * GENERATE_CRUSHMAP Set to true if the crush map should be generated |
| 13 | * |
| 14 | */ |
| 15 | |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 16 | def common = new com.mirantis.mk.Common() |
| 17 | def salt = new com.mirantis.mk.Salt() |
| 18 | def ceph = new com.mirantis.mk.Ceph() |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 19 | def python = new com.mirantis.mk.Python() |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 20 | def pepperEnv = "pepperEnv" |
| 21 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 22 | timeout(time: 12, unit: 'HOURS') { |
| 23 | node("python") { |
Jiri Broulik | 96c867a | 2017-11-07 16:14:10 +0100 | [diff] [blame] | 24 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 25 | // create connection to salt master |
| 26 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 27 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 28 | matches = ["osd", "mon", "rgw"] |
| 29 | def found = false |
| 30 | for (s in matches) { |
| 31 | if (HOST_TYPE.toLowerCase() == s) { |
| 32 | found = true |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 36 | if (!found) { |
| 37 | common.errorMsg("No such HOST_TYPE was found. Please insert one of the following types: mon/osd/rgw") |
| 38 | throw new InterruptedException() |
| 39 | } |
| 40 | |
Mateusz Los | 13d0520 | 2020-05-11 09:51:13 +0200 | [diff] [blame^] | 41 | def checknode = salt.runSaltProcessStep(pepperEnv, HOST, 'test.ping') |
| 42 | if (checknode['return'][0].values().isEmpty()) { |
| 43 | common.errorMsg("Host not found") |
| 44 | throw new InterruptedException() |
| 45 | } |
| 46 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 47 | stage('Refresh_pillar') { |
| 48 | salt.runSaltProcessStep(pepperEnv, '*', 'saltutil.refresh_pillar', [], null, true, 5) |
| 49 | } |
| 50 | |
| 51 | // split minion id on '.' and remove '*' |
| 52 | def target = HOST.split("\\.")[0].replace("*", "") |
| 53 | |
| 54 | salt.runSaltProcessStep(pepperEnv, 'I@salt:master', 'saltutil.sync_grains', [], null, true, 5) |
| 55 | def _pillar = salt.getGrain(pepperEnv, 'I@salt:master', 'domain') |
| 56 | domain = _pillar['return'][0].values()[0].values()[0] |
| 57 | |
| 58 | if (HOST_TYPE.toLowerCase() == 'rgw') { |
| 59 | // Remove Ceph rgw |
| 60 | stage('Remove Ceph RGW') { |
| 61 | salt.enforceState(pepperEnv, 'I@ceph:radosgw', ['keepalived', 'haproxy'], true) |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 62 | } |
Mateusz Los | 9f50377 | 2019-05-07 15:10:45 +0200 | [diff] [blame] | 63 | |
| 64 | stage('Purge Ceph RGW pkgs') { |
| 65 | salt.runSaltProcessStep(pepperEnv, HOST, 'pkg.purge', 'ceph-common,libcephfs2,python-cephfs,radosgw,python-rados,python-rbd,python-rgw') |
| 66 | } |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 69 | if (HOST_TYPE.toLowerCase() != 'osd') { |
| 70 | |
| 71 | // virsh destroy rgw04.deploy-name.local; virsh undefine rgw04.deploy-name.local; |
| 72 | stage('Destroy/Undefine VM') { |
| 73 | _pillar = salt.getGrain(pepperEnv, 'I@salt:control', 'id') |
| 74 | def kvm01 = _pillar['return'][0].values()[0].values()[0] |
| 75 | |
| 76 | _pillar = salt.getPillar(pepperEnv, "${kvm01}", "salt:control:cluster:internal:node:${target}:provider") |
| 77 | def targetProvider = _pillar['return'][0].values()[0] |
| 78 | |
| 79 | salt.cmdRun(pepperEnv, "${targetProvider}", "virsh destroy ${target}.${domain}") |
| 80 | salt.cmdRun(pepperEnv, "${targetProvider}", "virsh undefine ${target}.${domain}") |
| 81 | } |
| 82 | } else if (HOST_TYPE.toLowerCase() == 'osd') { |
| 83 | def osd_ids = [] |
Mateusz Los | 2ae66db | 2020-04-28 16:36:11 +0200 | [diff] [blame] | 84 | def device_grain_name = "ceph_disk" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 85 | // get list of osd disks of the host |
| 86 | salt.runSaltProcessStep(pepperEnv, HOST, 'saltutil.sync_grains', [], null, true, 5) |
mjedynski | 12955c7 | 2019-12-09 15:17:58 +0100 | [diff] [blame] | 87 | def ceph_disks = salt.getGrain(pepperEnv, HOST, 'ceph')['return'][0].values()[0].values()[0][device_grain_name] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 88 | |
| 89 | for (i in ceph_disks) { |
| 90 | def osd_id = i.getKey().toString() |
| 91 | osd_ids.add('osd.' + osd_id) |
| 92 | print("Will delete " + osd_id) |
| 93 | } |
| 94 | |
| 95 | // `ceph osd out <id> <id>` |
| 96 | stage('Set OSDs out') { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 97 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd out ' + osd_ids.join(' ')) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // wait for healthy cluster |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 101 | if (WAIT_FOR_HEALTHY.toBoolean()) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 102 | sleep(5) |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 103 | ceph.waitForHealthy(pepperEnv, ADMIN_HOST) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // stop osd daemons |
| 107 | stage('Stop OSD daemons') { |
| 108 | for (i in osd_ids) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 109 | salt.runSaltProcessStep(pepperEnv, HOST, 'service.stop', ['ceph-osd@' + i.replaceAll('osd.', '')], null, true) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | // `ceph osd crush remove osd.2` |
| 114 | stage('Remove OSDs from CRUSH') { |
| 115 | for (i in osd_ids) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 116 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd crush remove ' + i) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | // remove keyring `ceph auth del osd.3` |
| 121 | stage('Remove OSD keyrings from auth') { |
| 122 | for (i in osd_ids) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 123 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph auth del ' + i) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | // remove osd `ceph osd rm osd.3` |
| 128 | stage('Remove OSDs') { |
| 129 | for (i in osd_ids) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 130 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd rm ' + i) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | for (osd_id in osd_ids) { |
| 135 | |
| 136 | id = osd_id.replaceAll('osd.', '') |
| 137 | def dmcrypt = "" |
| 138 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 139 | dmcrypt = salt.cmdRun(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep dmcrypt")['return'][0].values()[0] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 140 | } catch (Exception e) { |
| 141 | common.warningMsg(e) |
| 142 | } |
| 143 | |
| 144 | if (dmcrypt?.trim()) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 145 | mount = salt.cmdRun(pepperEnv, HOST, "lsblk -rp | grep /var/lib/ceph/osd/ceph-${id} -B1")['return'][0].values()[0] |
| 146 | dev = mount.split()[0].replaceAll("[0-9]", "") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 147 | |
| 148 | // remove partition tables |
| 149 | stage("dd part table on ${dev}") { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 150 | salt.cmdRun(pepperEnv, HOST, "dd if=/dev/zero of=${dev} bs=512 count=1 conv=notrunc") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } |
| 154 | // remove journal, block_db, block_wal partition `parted /dev/sdj rm 3` |
| 155 | stage('Remove journal / block_db / block_wal partition') { |
| 156 | def partition_uuid = "" |
| 157 | def journal_partition_uuid = "" |
| 158 | def block_db_partition_uuid = "" |
| 159 | def block_wal_partition_uuid = "" |
| 160 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 161 | journal_partition_uuid = salt.cmdRun(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep journal | grep partuuid") |
| 162 | journal_partition_uuid = journal_partition_uuid.toString().trim().split("\n")[0].substring(journal_partition_uuid.toString().trim().lastIndexOf("/") + 1) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 163 | } catch (Exception e) { |
| 164 | common.infoMsg(e) |
| 165 | } |
| 166 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 167 | block_db_partition_uuid = salt.cmdRun(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep 'block.db' | grep partuuid") |
| 168 | block_db_partition_uuid = block_db_partition_uuid.toString().trim().split("\n")[0].substring(block_db_partition_uuid.toString().trim().lastIndexOf("/") + 1) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 169 | } catch (Exception e) { |
| 170 | common.infoMsg(e) |
| 171 | } |
| 172 | |
| 173 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 174 | block_wal_partition_uuid = salt.cmdRun(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep 'block.wal' | grep partuuid") |
| 175 | block_wal_partition_uuid = block_wal_partition_uuid.toString().trim().split("\n")[0].substring(block_wal_partition_uuid.toString().trim().lastIndexOf("/") + 1) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 176 | } catch (Exception e) { |
| 177 | common.infoMsg(e) |
| 178 | } |
| 179 | |
| 180 | // set partition_uuid = 2c76f144-f412-481e-b150-4046212ca932 |
| 181 | if (journal_partition_uuid?.trim()) { |
| 182 | partition_uuid = journal_partition_uuid |
| 183 | } else if (block_db_partition_uuid?.trim()) { |
| 184 | partition_uuid = block_db_partition_uuid |
| 185 | } |
| 186 | |
| 187 | // if disk has journal, block_db or block_wal on different disk, then remove the partition |
| 188 | if (partition_uuid?.trim()) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 189 | ceph.removePartition(pepperEnv, HOST, partition_uuid) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 190 | } |
| 191 | if (block_wal_partition_uuid?.trim()) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 192 | ceph.removePartition(pepperEnv, HOST, block_wal_partition_uuid) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // purge Ceph pkgs |
| 198 | stage('Purge Ceph OSD pkgs') { |
Mateusz Los | 9f50377 | 2019-05-07 15:10:45 +0200 | [diff] [blame] | 199 | salt.runSaltProcessStep(pepperEnv, HOST, 'pkg.purge', 'ceph-base,ceph-common,ceph-fuse,ceph-mds,ceph-osd,python-cephfs,librados2,python-rados,python-rbd,python-rgw') |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | stage('Remove OSD host from crushmap') { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 203 | def hostname = salt.cmdRun(pepperEnv, HOST, "hostname -s")['return'][0].values()[0].split('\n')[0] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 204 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 205 | salt.cmdRun(pepperEnv, ADMIN_HOST, "ceph osd crush remove ${hostname}") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 206 | } catch (Exception e) { |
| 207 | common.warningMsg(e) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // stop salt-minion service and move its configuration |
| 212 | stage('Stop salt-minion') { |
| 213 | salt.cmdRun(pepperEnv, HOST, "mv /etc/salt/minion.d/minion.conf minion.conf") |
| 214 | salt.runSaltProcessStep(pepperEnv, HOST, 'service.stop', ['salt-minion'], [], null, true, 5) |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 218 | stage('Remove salt-key') { |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 219 | try { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 220 | salt.cmdRun(pepperEnv, 'I@salt:master', "salt-key -d ${target}.${domain} -y") |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 221 | } catch (Exception e) { |
| 222 | common.warningMsg(e) |
| 223 | } |
Jiri Broulik | c7ec65e | 2017-11-30 16:55:58 +0100 | [diff] [blame] | 224 | try { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 225 | salt.cmdRun(pepperEnv, 'I@salt:master', "rm /srv/salt/reclass/nodes/_generated/${target}.${domain}.yml") |
Jiri Broulik | c7ec65e | 2017-11-30 16:55:58 +0100 | [diff] [blame] | 226 | } catch (Exception e) { |
| 227 | common.warningMsg(e) |
| 228 | } |
| 229 | } |
| 230 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 231 | stage('Remove keyring') { |
| 232 | def keyring = "" |
| 233 | def keyring_lines = "" |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 234 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 235 | keyring_lines = salt.cmdRun(pepperEnv, ADMIN_HOST, "ceph auth list | grep ${target}")['return'][0].values()[0].split('\n') |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 236 | } catch (Exception e) { |
| 237 | common.warningMsg(e) |
| 238 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 239 | for (line in keyring_lines) { |
| 240 | if (line.toLowerCase().contains(target.toLowerCase())) { |
| 241 | keyring = line |
| 242 | break |
| 243 | } |
| 244 | } |
| 245 | if (keyring?.trim()) { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 246 | salt.cmdRun(pepperEnv, ADMIN_HOST, "ceph auth del ${keyring}") |
Jiri Broulik | 3485b2c | 2017-11-28 15:06:12 +0100 | [diff] [blame] | 247 | } |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 248 | } |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 249 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 250 | if (HOST_TYPE.toLowerCase() == 'mon') { |
| 251 | // Update Monmap |
| 252 | stage('Update monmap') { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 253 | salt.cmdRun(pepperEnv, 'I@ceph:mon', "ceph mon getmap -o monmap.backup") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 254 | try { |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 255 | salt.cmdRun(pepperEnv, 'I@ceph:mon', "ceph mon remove ${target}") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 256 | } catch (Exception e) { |
| 257 | common.warningMsg(e) |
| 258 | } |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 259 | salt.cmdRun(pepperEnv, 'I@ceph:mon', "monmaptool /tmp/monmap --rm ${target}") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | def target_hosts = salt.getMinions(pepperEnv, 'I@ceph:common') |
| 263 | print target_hosts |
| 264 | |
| 265 | // Update configs |
| 266 | stage('Update Ceph configs') { |
| 267 | for (tgt in target_hosts) { |
| 268 | salt.enforceState(pepperEnv, tgt, 'ceph.common', true) |
| 269 | } |
| 270 | } |
Mateusz Los | 9f50377 | 2019-05-07 15:10:45 +0200 | [diff] [blame] | 271 | |
| 272 | stage('Purge Ceph MON pkgs') { |
| 273 | salt.runSaltProcessStep(pepperEnv, HOST, 'pkg.purge', 'ceph-base,ceph-common,ceph-mgr,ceph-mon,libcephfs2,python-cephfs,python-rbd,python-rgw') |
| 274 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Mateusz Los | bb35289 | 2019-10-09 11:36:10 +0200 | [diff] [blame] | 277 | def crushmap_target = salt.getMinions(pepperEnv, "I@ceph:setup:crush") |
Ivan Berezovskiy | 19c685a | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 278 | if (HOST_TYPE.toLowerCase() == 'osd' && GENERATE_CRUSHMAP.toBoolean() == true && crushmap_target) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 279 | stage('Generate CRUSHMAP') { |
| 280 | salt.enforceState(pepperEnv, 'I@ceph:setup:crush', 'ceph.setup.crush', true) |
| 281 | } |
Jiri Broulik | 99887c8 | 2017-10-31 09:27:52 +0100 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |