Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Remove OSD 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 | * |
| 9 | * HOST Host (minion id) to be removed |
Jiri Broulik | 2c00f4c | 2017-10-26 13:23:11 +0200 | [diff] [blame] | 10 | * OSD Comma separated list of osd ids to be removed |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 11 | * ADMIN_HOST Host (minion id) with admin keyring |
| 12 | * CLUSTER_FLAGS Comma separated list of tags to apply to cluster |
| 13 | * WAIT_FOR_HEALTHY Wait for cluster rebalance before stoping daemons |
Tomek Jaroszyk | 2683a0e | 2020-09-04 15:42:52 +0200 | [diff] [blame] | 14 | * CLEANDISK Wipe data disk of removed osd |
| 15 | * CLEAN_ORPHANS Wipe partition left over after unknown osd |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 19 | def common = new com.mirantis.mk.Common() |
| 20 | def salt = new com.mirantis.mk.Salt() |
| 21 | def ceph = new com.mirantis.mk.Ceph() |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 22 | def python = new com.mirantis.mk.Python() |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 23 | |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 24 | def pepperEnv = "pepperEnv" |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 25 | def flags = CLUSTER_FLAGS.tokenize(',') |
Tomáš Kukrál | 9d6228b | 2017-08-15 16:54:55 +0200 | [diff] [blame] | 26 | def osds = OSD.tokenize(',') |
Tomek Jaroszyk | 2683a0e | 2020-09-04 15:42:52 +0200 | [diff] [blame] | 27 | def cleanDisk = CLEANDISK.toBoolean() |
| 28 | def cleanOrphans = CLEAN_ORPHANS.toBoolean() |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 29 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 30 | timeout(time: 12, unit: 'HOURS') { |
| 31 | node("python") { |
Jiri Broulik | 96c867a | 2017-11-07 16:14:10 +0100 | [diff] [blame] | 32 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 33 | // create connection to salt master |
| 34 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 35 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 36 | def osd_ids = [] |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 37 | |
Mateusz Los | b5663e6 | 2020-05-11 09:51:13 +0200 | [diff] [blame] | 38 | def checknode = salt.runSaltProcessStep(pepperEnv, HOST, 'test.ping') |
| 39 | if (checknode['return'][0].values().isEmpty()) { |
| 40 | common.errorMsg("Host not found") |
| 41 | throw new InterruptedException() |
| 42 | } |
| 43 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 44 | // get list of osd disks of the host |
| 45 | salt.runSaltProcessStep(pepperEnv, HOST, 'saltutil.sync_grains', [], null, true, 5) |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 46 | def cephGrain = salt.getGrain(pepperEnv, HOST, 'ceph') |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 47 | def ceph_disks = cephGrain['return'][0].values()[0].values()[0]['ceph_disk'] |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 48 | |
Ivan Berezovskiy | 2325dcb | 2019-11-05 17:42:57 +0400 | [diff] [blame] | 49 | if (cephGrain['return'].isEmpty()) { |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame] | 50 | throw new Exception("Ceph salt grain cannot be found!") |
| 51 | } |
Jiri Broulik | adc7ecd | 2017-10-18 06:59:27 +0200 | [diff] [blame] | 52 | |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 53 | if (flags.size() > 0) { |
| 54 | stage('Set cluster flags') { |
| 55 | for (flag in flags) { |
| 56 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd set ' + flag) |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | try { |
| 62 | for (i in ceph_disks) { |
| 63 | def osd_id = i.getKey().toString() |
| 64 | if (osd_id in osds || OSD == '*') { |
| 65 | osd_ids.add('osd.' + osd_id) |
| 66 | print("Will delete " + osd_id) |
| 67 | } else { |
| 68 | print("Skipping " + osd_id) |
| 69 | } |
| 70 | } |
| 71 | |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 72 | // `ceph osd out <id> <id>` |
| 73 | stage('Set OSDs out') { |
Tomek Jaroszyk | 7c836ba | 2020-09-07 15:02:03 +0200 | [diff] [blame] | 74 | if ( !osd_ids.isEmpty() ) { |
| 75 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd out ' + osd_ids.join(' ')) |
| 76 | } |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // wait for healthy cluster |
| 80 | if (WAIT_FOR_HEALTHY.toBoolean()) { |
| 81 | sleep(5) |
| 82 | ceph.waitForHealthy(pepperEnv, ADMIN_HOST) |
| 83 | } |
| 84 | |
| 85 | // stop osd daemons |
| 86 | stage('Stop OSD daemons') { |
| 87 | for (i in osd_ids) { |
| 88 | salt.runSaltProcessStep(pepperEnv, HOST, 'service.stop', ['ceph-osd@' + i.replaceAll('osd.', '')], null, true) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // `ceph osd crush remove osd.2` |
| 93 | stage('Remove OSDs from CRUSH') { |
| 94 | for (i in osd_ids) { |
| 95 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd crush remove ' + i) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // remove keyring `ceph auth del osd.3` |
| 100 | stage('Remove OSD keyrings from auth') { |
| 101 | for (i in osd_ids) { |
| 102 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph auth del ' + i) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // remove osd `ceph osd rm osd.3` |
| 107 | stage('Remove OSDs') { |
| 108 | for (i in osd_ids) { |
| 109 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd rm ' + i) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | for (osd_id in osd_ids) { |
| 114 | id = osd_id.replaceAll('osd.', '') |
| 115 | |
| 116 | // remove journal, block_db, block_wal partition `parted /dev/sdj rm 3` |
| 117 | stage('Remove journal / block_db / block_wal partition') { |
| 118 | def partition_uuid = "" |
| 119 | def journal_partition_uuid = "" |
| 120 | def block_db_partition_uuid = "" |
| 121 | def block_wal_partition_uuid = "" |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 122 | try { |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 123 | journal_partition_uuid = salt.cmdRun(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/journal_uuid")['return'][0].values()[0].split("\n")[0] |
| 124 | } |
| 125 | catch (Exception e) { |
| 126 | common.infoMsg(e) |
| 127 | } |
| 128 | try { |
| 129 | block_db_partition_uuid = salt.cmdRun(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/block.db_uuid")['return'][0].values()[0].split("\n")[0] |
mjedynski | af258b0 | 2019-12-09 15:17:58 +0100 | [diff] [blame] | 130 | } |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 131 | catch (Exception e) { |
| 132 | common.infoMsg(e) |
| 133 | } |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 134 | |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 135 | try { |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 136 | block_wal_partition_uuid = salt.cmdRun(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/block.wal_uuid")['return'][0].values()[0].split("\n")[0] |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 137 | } |
| 138 | catch (Exception e) { |
| 139 | common.infoMsg(e) |
| 140 | } |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 141 | |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 142 | // remove partition_uuid = 2c76f144-f412-481e-b150-4046212ca932 |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 143 | if (journal_partition_uuid?.trim()) { |
| 144 | ceph.removePartition(pepperEnv, HOST, journal_partition_uuid) |
| 145 | } |
| 146 | if (block_db_partition_uuid?.trim()) { |
| 147 | ceph.removePartition(pepperEnv, HOST, block_db_partition_uuid) |
| 148 | } |
| 149 | if (block_wal_partition_uuid?.trim()) { |
| 150 | ceph.removePartition(pepperEnv, HOST, block_wal_partition_uuid) |
| 151 | } |
| 152 | |
| 153 | try { |
| 154 | salt.cmdRun(pepperEnv, HOST, "partprobe") |
| 155 | } |
| 156 | catch (Exception e) { |
| 157 | common.warningMsg(e) |
| 158 | } |
| 159 | } |
| 160 | if (cleanDisk) { |
| 161 | // remove data / block / lockbox partition `parted /dev/sdj rm 3` |
| 162 | stage('Remove data / block / lockbox partition') { |
| 163 | def data_partition_uuid = "" |
| 164 | def block_partition_uuid = "" |
| 165 | def osd_fsid = "" |
| 166 | def lvm = "" |
| 167 | def lvm_enabled= salt.getPillar(pepperEnv,"I@ceph:osd","ceph:osd:lvm_enabled")['return'].first().containsValue(true) |
| 168 | try { |
| 169 | osd_fsid = salt.cmdRun(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/fsid")['return'][0].values()[0].split("\n")[0] |
| 170 | if (lvm_enabled) { |
| 171 | lvm = salt.runSaltCommand(pepperEnv, 'local', ['expression': HOST, 'type': 'compound'], 'cmd.run', null, "salt-call lvm.lvdisplay --output json -l quiet")['return'][0].values()[0] |
| 172 | lvm = new groovy.json.JsonSlurperClassic().parseText(lvm) |
| 173 | lvm["local"].each { lv, params -> |
| 174 | if (params["Logical Volume Name"].contains(osd_fsid)) { |
| 175 | data_partition_uuid = params["Logical Volume Name"].minus("/dev/") |
| 176 | } |
| 177 | } |
| 178 | } |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 179 | } |
| 180 | catch (Exception e) { |
| 181 | common.infoMsg(e) |
| 182 | } |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 183 | try { |
| 184 | block_partition_uuid = salt.cmdRun(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/block_uuid")['return'][0].values()[0].split("\n")[0] |
| 185 | } |
| 186 | catch (Exception e) { |
| 187 | common.infoMsg(e) |
| 188 | } |
| 189 | |
| 190 | // remove partition_uuid = 2c76f144-f412-481e-b150-4046212ca932 |
| 191 | if (block_partition_uuid?.trim()) { |
| 192 | ceph.removePartition(pepperEnv, HOST, block_partition_uuid) |
| 193 | try{ |
| 194 | salt.cmdRun(pepperEnv, HOST, "ceph-volume lvm zap `readlink /var/lib/ceph/osd/ceph-${id}/block` --destroy") |
| 195 | } |
| 196 | catch (Exception e) { |
| 197 | common.infoMsg(e) |
| 198 | } |
| 199 | } |
| 200 | if (data_partition_uuid?.trim()) { |
| 201 | ceph.removePartition(pepperEnv, HOST, data_partition_uuid, 'data', id) |
| 202 | } |
Tomek Jaroszyk | d8fdf67 | 2020-08-21 18:40:59 +0200 | [diff] [blame] | 203 | else { |
| 204 | ceph.removePartition(pepperEnv, HOST, osd_fsid, 'data', id) |
| 205 | } |
Mateusz Los | 1535aa2 | 2020-05-11 10:33:58 +0200 | [diff] [blame] | 206 | } |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 207 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 208 | } |
Tomek Jaroszyk | 2683a0e | 2020-09-04 15:42:52 +0200 | [diff] [blame] | 209 | if (cleanOrphans) { |
| 210 | stage('Remove orphan partitions') { |
| 211 | def orphans = [] |
| 212 | def disks = salt.cmdRun(pepperEnv, ADMIN_HOST, "ceph-disk list --format json")['return'][0].values()[0] |
| 213 | for (disk in disks) { |
| 214 | for (partition in disk.get('partitions')) { |
| 215 | if (partition.get('type') == 'block.db' && !partition.containsKey('block.db_for')) { |
| 216 | orphans.add(partition['uuid']) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | for (orphan in orphans) { |
| 221 | ceph.removePartition(pepperEnv, HOST, orphan) |
| 222 | } |
| 223 | } |
| 224 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 225 | } |
Tomek Jaroszyk | e46eb69 | 2020-07-07 12:07:40 +0200 | [diff] [blame] | 226 | finally { |
| 227 | // remove cluster flags |
| 228 | if (flags.size() > 0) { |
| 229 | stage('Unset cluster flags') { |
| 230 | for (flag in flags) { |
| 231 | common.infoMsg('Removing flag ' + flag) |
| 232 | salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd unset ' + flag) |
| 233 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 234 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
Tomek Jaroszyk | 0cb8392 | 2020-04-07 11:20:31 +0200 | [diff] [blame] | 238 | } |