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