blob: 405d47834c7f548b4172bb2de3afda9dbf44fb02 [file] [log] [blame]
Tomáš Kukrálf72096d2017-08-11 12:58:03 +02001/**
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 Broulik2c00f4c2017-10-26 13:23:11 +020010 * OSD Comma separated list of osd ids to be removed
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020011 * 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 Jaroszyk2683a0e2020-09-04 15:42:52 +020014 * CLEANDISK Wipe data disk of removed osd
15 * CLEAN_ORPHANS Wipe partition left over after unknown osd
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020016 *
17 */
18
Ivan Berezovskiy2325dcb2019-11-05 17:42:57 +040019def common = new com.mirantis.mk.Common()
20def salt = new com.mirantis.mk.Salt()
21def ceph = new com.mirantis.mk.Ceph()
chnyda625f4b42017-10-11 14:10:31 +020022def python = new com.mirantis.mk.Python()
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020023
chnyda625f4b42017-10-11 14:10:31 +020024def pepperEnv = "pepperEnv"
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020025def flags = CLUSTER_FLAGS.tokenize(',')
Tomáš Kukrál9d6228b2017-08-15 16:54:55 +020026def osds = OSD.tokenize(',')
Tomek Jaroszyk2683a0e2020-09-04 15:42:52 +020027def cleanDisk = CLEANDISK.toBoolean()
28def cleanOrphans = CLEAN_ORPHANS.toBoolean()
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020029
Jakub Josefa63f9862018-01-11 17:58:38 +010030timeout(time: 12, unit: 'HOURS') {
31 node("python") {
Jiri Broulik96c867a2017-11-07 16:14:10 +010032
Jakub Josefa63f9862018-01-11 17:58:38 +010033 // create connection to salt master
34 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020035
Jakub Josefa63f9862018-01-11 17:58:38 +010036 def osd_ids = []
Tomáš Kukrálf72096d2017-08-11 12:58:03 +020037
Mateusz Losb5663e62020-05-11 09:51:13 +020038 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 Josefa63f9862018-01-11 17:58:38 +010044 // get list of osd disks of the host
45 salt.runSaltProcessStep(pepperEnv, HOST, 'saltutil.sync_grains', [], null, true, 5)
Jiri Broulika5bc8f62018-01-31 15:04:40 +010046 def cephGrain = salt.getGrain(pepperEnv, HOST, 'ceph')
Tomek Jaroszyke46eb692020-07-07 12:07:40 +020047 def ceph_disks = cephGrain['return'][0].values()[0].values()[0]['ceph_disk']
Jiri Broulika5bc8f62018-01-31 15:04:40 +010048
Ivan Berezovskiy2325dcb2019-11-05 17:42:57 +040049 if (cephGrain['return'].isEmpty()) {
Jakub Josefed670ca2018-01-18 14:22:20 +010050 throw new Exception("Ceph salt grain cannot be found!")
51 }
Jiri Broulikadc7ecd2017-10-18 06:59:27 +020052
Tomek Jaroszyke46eb692020-07-07 12:07:40 +020053 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 Broulika5bc8f62018-01-31 15:04:40 +010057 }
58 }
Tomek Jaroszyke46eb692020-07-07 12:07:40 +020059 }
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 Jaroszyke46eb692020-07-07 12:07:40 +020072 // `ceph osd out <id> <id>`
73 stage('Set OSDs out') {
Tomek Jaroszyk7c836ba2020-09-07 15:02:03 +020074 if ( !osd_ids.isEmpty() ) {
75 salt.cmdRun(pepperEnv, ADMIN_HOST, 'ceph osd out ' + osd_ids.join(' '))
76 }
Tomek Jaroszyke46eb692020-07-07 12:07:40 +020077 }
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 Los1535aa22020-05-11 10:33:58 +0200122 try {
Tomek Jaroszyke46eb692020-07-07 12:07:40 +0200123 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]
mjedynskiaf258b02019-12-09 15:17:58 +0100130 }
Mateusz Los1535aa22020-05-11 10:33:58 +0200131 catch (Exception e) {
132 common.infoMsg(e)
133 }
Tomek Jaroszyke46eb692020-07-07 12:07:40 +0200134
Mateusz Los1535aa22020-05-11 10:33:58 +0200135 try {
Tomek Jaroszyke46eb692020-07-07 12:07:40 +0200136 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 Los1535aa22020-05-11 10:33:58 +0200137 }
138 catch (Exception e) {
139 common.infoMsg(e)
140 }
Jiri Broulika5bc8f62018-01-31 15:04:40 +0100141
Mateusz Los1535aa22020-05-11 10:33:58 +0200142 // remove partition_uuid = 2c76f144-f412-481e-b150-4046212ca932
Tomek Jaroszyke46eb692020-07-07 12:07:40 +0200143 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 Los1535aa22020-05-11 10:33:58 +0200179 }
180 catch (Exception e) {
181 common.infoMsg(e)
182 }
Tomek Jaroszyke46eb692020-07-07 12:07:40 +0200183 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 Jaroszykd8fdf672020-08-21 18:40:59 +0200203 else {
204 ceph.removePartition(pepperEnv, HOST, osd_fsid, 'data', id)
205 }
Mateusz Los1535aa22020-05-11 10:33:58 +0200206 }
Jiri Broulika5bc8f62018-01-31 15:04:40 +0100207 }
Jiri Broulikeb7b82f2017-11-30 13:55:40 +0100208 }
Tomek Jaroszyk2683a0e2020-09-04 15:42:52 +0200209 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 Broulikeb7b82f2017-11-30 13:55:40 +0100225 }
Tomek Jaroszyke46eb692020-07-07 12:07:40 +0200226 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 Josefa63f9862018-01-11 17:58:38 +0100234 }
Tomáš Kukrálf72096d2017-08-11 12:58:03 +0200235 }
236 }
237 }
Tomek Jaroszyk0cb83922020-04-07 11:20:31 +0200238}