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