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 | def removePartition(master, target, partition_uuid) { |
| 26 | def partition = "" |
| 27 | try { |
| 28 | // partition = /dev/sdi2 |
| 29 | partition = runCephCommand(master, target, "blkid | grep ${partition_uuid} ")['return'][0].values()[0].split("(?<=[0-9])")[0] |
| 30 | } catch (Exception e) { |
| 31 | common.warningMsg(e) |
| 32 | } |
| 33 | |
| 34 | if (partition?.trim()) { |
| 35 | // dev = /dev/sdi |
| 36 | def dev = partition.replaceAll('\\d+$', "") |
| 37 | // part_id = 2 |
| 38 | def part_id = partition.substring(partition.lastIndexOf("/")+1).replaceAll("[^0-9]", "") |
| 39 | runCephCommand(master, target, "parted ${dev} rm ${part_id}") |
| 40 | } |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | def runCephCommand(master, target, cmd) { |
| 45 | return salt.cmdRun(master, target, cmd) |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Jiri Broulik | 96c867a | 2017-11-07 16:14:10 +0100 | [diff] [blame] | 48 | def waitForHealthy(master, count=0, attempts=300) { |
| 49 | // wait for healthy cluster |
| 50 | while (count<attempts) { |
| 51 | def health = runCephCommand(master, ADMIN_HOST, 'ceph health')['return'][0].values()[0] |
| 52 | if (health.contains('HEALTH_OK')) { |
| 53 | common.infoMsg('Cluster is healthy') |
| 54 | break; |
| 55 | } |
| 56 | count++ |
| 57 | sleep(10) |
| 58 | } |
| 59 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 60 | timeout(time: 12, unit: 'HOURS') { |
| 61 | node("python") { |
Jiri Broulik | 96c867a | 2017-11-07 16:14:10 +0100 | [diff] [blame] | 62 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 63 | // create connection to salt master |
| 64 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 65 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 66 | if (flags.size() > 0) { |
| 67 | stage('Set cluster flags') { |
| 68 | for (flag in flags) { |
| 69 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd set ' + flag) |
| 70 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 71 | } |
| 72 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 73 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 74 | def osd_ids = [] |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 75 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 76 | // get list of osd disks of the host |
| 77 | salt.runSaltProcessStep(pepperEnv, HOST, 'saltutil.sync_grains', [], null, true, 5) |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame^] | 78 | def cephGrain = salt.getGrain(pepperEnv, HOST, 'ceph')['return'] |
| 79 | if(cephGrain['return'].isEmpty()){ |
| 80 | throw new Exception("Ceph salt grain cannot be found!") |
| 81 | } |
| 82 | def ceph_disks = cephGrain['return'][0].values()[0].values()[0]['ceph_disk'] |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 83 | common.prettyPrint(ceph_disks) |
Jiri Broulik | adc7ecd | 2017-10-18 06:59:27 +0200 | [diff] [blame] | 84 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 85 | for (i in ceph_disks) { |
| 86 | def osd_id = i.getKey().toString() |
| 87 | if (osd_id in osds || OSD == '*') { |
| 88 | osd_ids.add('osd.' + osd_id) |
| 89 | print("Will delete " + osd_id) |
| 90 | } else { |
| 91 | print("Skipping " + osd_id) |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 92 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 93 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 94 | |
| 95 | // wait for healthy cluster |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame^] | 96 | if (WAIT_FOR_HEALTHY.toBoolean()) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 97 | waitForHealthy(pepperEnv) |
| 98 | } |
| 99 | |
| 100 | // `ceph osd out <id> <id>` |
| 101 | stage('Set OSDs out') { |
| 102 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd out ' + osd_ids.join(' ')) |
| 103 | } |
| 104 | |
| 105 | // wait for healthy cluster |
Jakub Josef | ed670ca | 2018-01-18 14:22:20 +0100 | [diff] [blame^] | 106 | if (WAIT_FOR_HEALTHY.toBoolean()) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 107 | sleep(5) |
| 108 | waitForHealthy(pepperEnv) |
| 109 | } |
| 110 | |
| 111 | // stop osd daemons |
| 112 | stage('Stop OSD daemons') { |
| 113 | for (i in osd_ids) { |
| 114 | salt.runSaltProcessStep(pepperEnv, HOST, 'service.stop', ['ceph-osd@' + i.replaceAll('osd.', '')], null, true) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // `ceph osd crush remove osd.2` |
| 119 | stage('Remove OSDs from CRUSH') { |
| 120 | for (i in osd_ids) { |
| 121 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd crush remove ' + i) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // remove keyring `ceph auth del osd.3` |
| 126 | stage('Remove OSD keyrings from auth') { |
| 127 | for (i in osd_ids) { |
| 128 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph auth del ' + i) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // remove osd `ceph osd rm osd.3` |
| 133 | stage('Remove OSDs') { |
| 134 | for (i in osd_ids) { |
| 135 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd rm ' + i) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | for (osd_id in osd_ids) { |
| 140 | |
| 141 | id = osd_id.replaceAll('osd.', '') |
| 142 | def dmcrypt = "" |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 143 | try { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 144 | 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] | 145 | } catch (Exception e) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 146 | common.warningMsg(e) |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 149 | if (dmcrypt?.trim()) { |
| 150 | mount = runCephCommand(pepperEnv, HOST, "lsblk -rp | grep /var/lib/ceph/osd/ceph-${id} -B1")['return'][0].values()[0] |
| 151 | dev = mount.split()[0].replaceAll("[0-9]","") |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 152 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 153 | // remove partition tables |
| 154 | stage("dd part table on ${dev}") { |
| 155 | runCephCommand(pepperEnv, HOST, "dd if=/dev/zero of=${dev} bs=512 count=1 conv=notrunc") |
| 156 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 157 | |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 158 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 159 | // remove journal, block_db, block_wal partition `parted /dev/sdj rm 3` |
| 160 | stage('Remove journal / block_db / block_wal partition') { |
| 161 | def partition_uuid = "" |
| 162 | def journal_partition_uuid = "" |
| 163 | def block_db_partition_uuid = "" |
| 164 | def block_wal_partition_uuid = "" |
| 165 | try { |
| 166 | journal_partition_uuid = runCephCommand(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep journal | grep partuuid") |
| 167 | journal_partition_uuid = journal_partition_uuid.toString().trim().split("\n")[0].substring(journal_partition_uuid.toString().trim().lastIndexOf("/")+1) |
| 168 | } catch (Exception e) { |
| 169 | common.infoMsg(e) |
| 170 | } |
| 171 | try { |
| 172 | block_db_partition_uuid = runCephCommand(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep 'block.db' | grep partuuid") |
| 173 | block_db_partition_uuid = block_db_partition_uuid.toString().trim().split("\n")[0].substring(block_db_partition_uuid.toString().trim().lastIndexOf("/")+1) |
| 174 | } catch (Exception e) { |
| 175 | common.infoMsg(e) |
| 176 | } |
| 177 | |
| 178 | try { |
| 179 | block_wal_partition_uuid = runCephCommand(pepperEnv, HOST, "ls -la /var/lib/ceph/osd/ceph-${id}/ | grep 'block.wal' | grep partuuid") |
| 180 | block_wal_partition_uuid = block_wal_partition_uuid.toString().trim().split("\n")[0].substring(block_wal_partition_uuid.toString().trim().lastIndexOf("/")+1) |
| 181 | } catch (Exception e) { |
| 182 | common.infoMsg(e) |
| 183 | } |
| 184 | |
| 185 | // set partition_uuid = 2c76f144-f412-481e-b150-4046212ca932 |
| 186 | if (journal_partition_uuid?.trim()) { |
| 187 | partition_uuid = journal_partition_uuid |
| 188 | } else if (block_db_partition_uuid?.trim()) { |
| 189 | partition_uuid = block_db_partition_uuid |
| 190 | } |
| 191 | |
| 192 | // if disk has journal, block_db or block_wal on different disk, then remove the partition |
| 193 | if (partition_uuid?.trim()) { |
| 194 | removePartition(pepperEnv, HOST, partition_uuid) |
| 195 | } |
| 196 | if (block_wal_partition_uuid?.trim()) { |
| 197 | removePartition(pepperEnv, HOST, block_wal_partition_uuid) |
| 198 | } |
Jiri Broulik | eb7b82f | 2017-11-30 13:55:40 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 201 | // remove cluster flags |
| 202 | if (flags.size() > 0) { |
| 203 | stage('Unset cluster flags') { |
| 204 | for (flag in flags) { |
| 205 | common.infoMsg('Removing flag ' + flag) |
| 206 | runCephCommand(pepperEnv, ADMIN_HOST, 'ceph osd unset ' + flag) |
| 207 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
Tomáš Kukrál | f72096d | 2017-08-11 12:58:03 +0200 | [diff] [blame] | 211 | } |