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 |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | common = new com.mirantis.mk.Common() |
| 18 | salt = new com.mirantis.mk.Salt() |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 19 | def python = new com.mirantis.mk.Python() |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 20 | |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 21 | def pepperEnv = "pepperEnv" |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 22 | def flags = CLUSTER_FLAGS.tokenize(',') |
Tomáš Kukrál | 9d6228b | 2017-08-15 16:54:55 +0200 | [diff] [blame] | 23 | def osds = OSD.tokenize(',') |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 24 | |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 25 | |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 26 | def removePartition(master, target, partition_uuid, type='', id=-1) { |
| 27 | def partition = "" |
| 28 | if (type == 'lockbox') { |
| 29 | try { |
| 30 | // umount - partition = /dev/sdi2 |
| 31 | partition = runCephCommand(master, target, "lsblk -rp | grep -v mapper | grep ${partition_uuid} ")['return'][0].values()[0].split()[0] |
| 32 | runCephCommand(master, target, "umount ${partition}") |
| 33 | } catch (Exception e) { |
| 34 | common.warningMsg(e) |
| 35 | } |
| 36 | } else if (type == 'data') { |
| 37 | try { |
| 38 | // umount - partition = /dev/sdi2 |
| 39 | partition = runCephCommand(master, target, "df | grep /var/lib/ceph/osd/ceph-${id}")['return'][0].values()[0].split()[0] |
| 40 | runCephCommand(master, target, "umount ${partition}") |
| 41 | } catch (Exception e) { |
| 42 | common.warningMsg(e) |
| 43 | } |
| 44 | try { |
| 45 | // partition = /dev/sdi2 |
| 46 | partition = runCephCommand(master, target, "blkid | grep ${partition_uuid} ")['return'][0].values()[0].split(":")[0] |
| 47 | } catch (Exception e) { |
| 48 | common.warningMsg(e) |
| 49 | } |
| 50 | } else { |
| 51 | try { |
| 52 | // partition = /dev/sdi2 |
| 53 | partition = runCephCommand(master, target, "blkid | grep ${partition_uuid} ")['return'][0].values()[0].split(":")[0] |
| 54 | } catch (Exception e) { |
| 55 | common.warningMsg(e) |
| 56 | } |
| 57 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 58 | if (partition?.trim()) { |
Mateusz Los | aba63db | 2019-09-02 18:28:55 +0200 | [diff] [blame] | 59 | def part_id |
| 60 | if (partition.contains("nvme")) { |
| 61 | part_id = partition.substring(partition.lastIndexOf("p")+1).replaceAll("[^0-9]+", "") |
| 62 | } |
| 63 | else { |
| 64 | part_id = partition.substring(partition.lastIndexOf("/")+1).replaceAll("[^0-9]+", "") |
| 65 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 66 | def dev = partition.replaceAll('\\d+$', "") |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 67 | runCephCommand(master, target, "Ignore | parted ${dev} rm ${part_id}") |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 68 | } |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | def runCephCommand(master, target, cmd) { |
| 73 | return salt.cmdRun(master, target, cmd) |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Jiri Broulik | 96c867a | 2017-11-07 16:14:10 +0100 | [diff] [blame] | 76 | def waitForHealthy(master, count=0, attempts=300) { |
| 77 | // wait for healthy cluster |
| 78 | while (count<attempts) { |
| 79 | def health = runCephCommand(master, ADMIN_HOST, 'ceph health')['return'][0].values()[0] |
| 80 | if (health.contains('HEALTH_OK')) { |
| 81 | common.infoMsg('Cluster is healthy') |
| 82 | break; |
| 83 | } |
| 84 | count++ |
| 85 | sleep(10) |
| 86 | } |
| 87 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 88 | timeout(time: 12, unit: 'HOURS') { |
| 89 | node("python") { |
Jiri Broulik | 96c867a | 2017-11-07 16:14:10 +0100 | [diff] [blame] | 90 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 91 | // create connection to salt master |
| 92 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 93 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 94 | if (flags.size() > 0) { |
| 95 | stage('Set cluster flags') { |
| 96 | for (flag in flags) { |
| 97 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd set ' + flag) |
| 98 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 99 | } |
| 100 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 101 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 102 | def osd_ids = [] |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 103 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 104 | // get list of osd disks of the host |
| 105 | salt.runSaltProcessStep(pepperEnv, HOST, 'saltutil.sync_grains', [], null, true, 5) |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 106 | def cephGrain = salt.getGrain(pepperEnv, HOST, 'ceph') |
| 107 | |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame] | 108 | if(cephGrain['return'].isEmpty()){ |
| 109 | throw new Exception("Ceph salt grain cannot be found!") |
| 110 | } |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 111 | common.print(cephGrain) |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame] | 112 | def ceph_disks = cephGrain['return'][0].values()[0].values()[0]['ceph_disk'] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 113 | common.prettyPrint(ceph_disks) |
Jiri Broulik | adc7ecd | 2017-10-18 06:59:27 +0200 | [diff] [blame] | 114 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 115 | for (i in ceph_disks) { |
| 116 | def osd_id = i.getKey().toString() |
| 117 | if (osd_id in osds || OSD == '*') { |
| 118 | osd_ids.add('osd.' + osd_id) |
| 119 | print("Will delete " + osd_id) |
| 120 | } else { |
| 121 | print("Skipping " + osd_id) |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 122 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 123 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 124 | |
| 125 | // wait for healthy cluster |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame] | 126 | if (WAIT_FOR_HEALTHY.toBoolean()) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 127 | waitForHealthy(pepperEnv) |
| 128 | } |
| 129 | |
| 130 | // `ceph osd out <id> <id>` |
| 131 | stage('Set OSDs out') { |
| 132 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd out ' + osd_ids.join(' ')) |
| 133 | } |
| 134 | |
| 135 | // wait for healthy cluster |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame] | 136 | if (WAIT_FOR_HEALTHY.toBoolean()) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 137 | sleep(5) |
| 138 | waitForHealthy(pepperEnv) |
| 139 | } |
| 140 | |
| 141 | // stop osd daemons |
| 142 | stage('Stop OSD daemons') { |
| 143 | for (i in osd_ids) { |
| 144 | salt.runSaltProcessStep(pepperEnv, HOST, 'service.stop', ['ceph-osd@' + i.replaceAll('osd.', '')], null, true) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // `ceph osd crush remove osd.2` |
| 149 | stage('Remove OSDs from CRUSH') { |
| 150 | for (i in osd_ids) { |
| 151 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd crush remove ' + i) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // remove keyring `ceph auth del osd.3` |
| 156 | stage('Remove OSD keyrings from auth') { |
| 157 | for (i in osd_ids) { |
| 158 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph auth del ' + i) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // remove osd `ceph osd rm osd.3` |
| 163 | stage('Remove OSDs') { |
| 164 | for (i in osd_ids) { |
| 165 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd rm ' + i) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | for (osd_id in osd_ids) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 170 | id = osd_id.replaceAll('osd.', '') |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 171 | /* |
| 172 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 173 | def dmcrypt = "" |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 174 | try { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 175 | dmcrypt = runCephCommand(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep dmcrypt")['return'][0].values()[0] |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 176 | } catch (Exception e) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 177 | common.warningMsg(e) |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 180 | if (dmcrypt?.trim()) { |
| 181 | mount = runCephCommand(pepperEnv, HOST, "lsblk -rp | grep /var/lib/ceph/osd/ceph-${id} -B1")['return'][0].values()[0] |
| 182 | dev = mount.split()[0].replaceAll("[0-9]","") |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 183 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 184 | // remove partition tables |
| 185 | stage("dd part table on ${dev}") { |
| 186 | runCephCommand(pepperEnv, HOST, "dd if=/dev/zero of=${dev} bs=512 count=1 conv=notrunc") |
| 187 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 188 | |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 189 | } |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 190 | */ |
| 191 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 192 | // remove journal, block_db, block_wal partition `parted /dev/sdj rm 3` |
| 193 | stage('Remove journal / block_db / block_wal partition') { |
| 194 | def partition_uuid = "" |
| 195 | def journal_partition_uuid = "" |
| 196 | def block_db_partition_uuid = "" |
| 197 | def block_wal_partition_uuid = "" |
| 198 | try { |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 199 | journal_partition_uuid = runCephCommand(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/journal_uuid")['return'][0].values()[0].split("\n")[0] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 200 | } catch (Exception e) { |
| 201 | common.infoMsg(e) |
| 202 | } |
| 203 | try { |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 204 | block_db_partition_uuid = runCephCommand(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/block.db_uuid")['return'][0].values()[0].split("\n")[0] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 205 | } catch (Exception e) { |
| 206 | common.infoMsg(e) |
| 207 | } |
| 208 | |
| 209 | try { |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 210 | block_wal_partition_uuid = runCephCommand(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/block.wal_uuid")['return'][0].values()[0].split("\n")[0] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 211 | } catch (Exception e) { |
| 212 | common.infoMsg(e) |
| 213 | } |
| 214 | |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 215 | // remove partition_uuid = 2c76f144-f412-481e-b150-4046212ca932 |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 216 | if (journal_partition_uuid?.trim()) { |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 217 | removePartition(pepperEnv, HOST, journal_partition_uuid) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 218 | } |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 219 | if (block_db_partition_uuid?.trim()) { |
| 220 | removePartition(pepperEnv, HOST, block_db_partition_uuid) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 221 | } |
| 222 | if (block_wal_partition_uuid?.trim()) { |
| 223 | removePartition(pepperEnv, HOST, block_wal_partition_uuid) |
| 224 | } |
Jiri Broulik | a5bc8f6 | 2018-01-31 15:04:40 +0100 | [diff] [blame] | 225 | |
| 226 | try { |
| 227 | runCephCommand(pepperEnv, HOST, "partprobe") |
| 228 | } catch (Exception e) { |
| 229 | common.warningMsg(e) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // remove data / block / lockbox partition `parted /dev/sdj rm 3` |
| 234 | stage('Remove data / block / lockbox partition') { |
| 235 | def data_partition_uuid = "" |
| 236 | def block_partition_uuid = "" |
| 237 | def lockbox_partition_uuid = "" |
| 238 | try { |
| 239 | data_partition_uuid = runCephCommand(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/fsid")['return'][0].values()[0].split("\n")[0] |
| 240 | common.print(data_partition_uuid) |
| 241 | } catch (Exception e) { |
| 242 | common.infoMsg(e) |
| 243 | } |
| 244 | try { |
| 245 | block_partition_uuid = runCephCommand(pepperEnv, HOST, "cat /var/lib/ceph/osd/ceph-${id}/block_uuid")['return'][0].values()[0].split("\n")[0] |
| 246 | } catch (Exception e) { |
| 247 | common.infoMsg(e) |
| 248 | } |
| 249 | |
| 250 | try { |
| 251 | lockbox_partition_uuid = data_partition_uuid |
| 252 | } catch (Exception e) { |
| 253 | common.infoMsg(e) |
| 254 | } |
| 255 | |
| 256 | // remove partition_uuid = 2c76f144-f412-481e-b150-4046212ca932 |
| 257 | if (block_partition_uuid?.trim()) { |
| 258 | removePartition(pepperEnv, HOST, block_partition_uuid) |
| 259 | } |
| 260 | if (data_partition_uuid?.trim()) { |
| 261 | removePartition(pepperEnv, HOST, data_partition_uuid, 'data', id) |
| 262 | } |
| 263 | if (lockbox_partition_uuid?.trim()) { |
| 264 | removePartition(pepperEnv, HOST, lockbox_partition_uuid, 'lockbox') |
| 265 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 266 | } |
| 267 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 268 | // remove cluster flags |
| 269 | if (flags.size() > 0) { |
| 270 | stage('Unset cluster flags') { |
| 271 | for (flag in flags) { |
| 272 | common.infoMsg('Removing flag ' + flag) |
| 273 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd unset ' + flag) |
| 274 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 278 | } |