/
ceph_volume.py
616 lines (504 loc) · 16.7 KB
/
ceph_volume.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/python
import datetime
import copy
import json
import os
ANSIBLE_METADATA = {
'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: ceph_volume
short_description: Create ceph OSDs with ceph-volume
description:
- Using the ceph-volume utility available in Ceph this module
can be used to create ceph OSDs that are backed by logical volumes.
- Only available in ceph versions luminous or greater.
options:
cluster:
description:
- The ceph cluster name.
required: false
default: ceph
objectstore:
description:
- The objectstore of the OSD, either filestore or bluestore
- Required if action is 'create'
required: false
choices: ['bluestore', 'filestore']
default: bluestore
action:
description:
- The action to take. Either creating OSDs or zapping devices.
required: true
choices: ['create', 'zap', 'batch', 'prepare', 'activate', 'list']
default: create
data:
description:
- The logical volume name or device to use for the OSD data.
required: true
data_vg:
description:
- If data is a lv, this must be the name of the volume group it belongs to.
required: false
journal:
description:
- The logical volume name or partition to use as a filestore journal.
- Only applicable if objectstore is 'filestore'.
required: false
journal_vg:
description:
- If journal is a lv, this must be the name of the volume group it belongs to.
- Only applicable if objectstore is 'filestore'.
required: false
db:
description:
- A partition or logical volume name to use for block.db.
- Only applicable if objectstore is 'bluestore'.
required: false
db_vg:
description:
- If db is a lv, this must be the name of the volume group it belongs to. # noqa E501
- Only applicable if objectstore is 'bluestore'.
required: false
wal:
description:
- A partition or logical volume name to use for block.wal.
- Only applicable if objectstore is 'bluestore'.
required: false
wal_vg:
description:
- If wal is a lv, this must be the name of the volume group it belongs to. # noqa E501
- Only applicable if objectstore is 'bluestore'.
required: false
crush_device_class:
description:
- Will set the crush device class for the OSD.
required: false
dmcrypt:
description:
- If set to True the OSD will be encrypted with dmcrypt.
required: false
batch_devices:
description:
- A list of devices to pass to the 'ceph-volume lvm batch' subcommand.
- Only applicable if action is 'batch'.
required: false
osds_per_device:
description:
- The number of OSDs to create per device.
- Only applicable if action is 'batch'.
required: false
default: 1
journal_size:
description:
- The size in MB of filestore journals.
- Only applicable if action is 'batch'.
required: false
default: 5120
block_db_size:
description:
- The size in bytes of bluestore block db lvs.
- The default of -1 means to create them as big as possible.
- Only applicable if action is 'batch'.
required: false
default: -1
report:
description:
- If provided the --report flag will be passed to 'ceph-volume lvm batch'.
- No OSDs will be created.
- Results will be returned in json format.
- Only applicable if action is 'batch'.
required: false
containerized:
description:
- Wether or not this is a containerized cluster. The value is
assigned or not depending on how the playbook runs.
required: false
default: None
author:
- Andrew Schoen (@andrewschoen)
'''
EXAMPLES = '''
- name: set up a filestore osd with an lv data and a journal partition
ceph_volume:
objectstore: filestore
data: data-lv
data_vg: data-vg
journal: /dev/sdc1
action: create
- name: set up a bluestore osd with a raw device for data
ceph_volume:
objectstore: bluestore
data: /dev/sdc
action: create
- name: set up a bluestore osd with an lv for data and partitions for block.wal and block.db # noqa E501
ceph_volume:
objectstore: bluestore
data: data-lv
data_vg: data-vg
db: /dev/sdc1
wal: /dev/sdc2
action: create
'''
from ansible.module_utils.basic import AnsibleModule # noqa 4502
def container_exec(binary, container_image):
'''
Build the CLI to run a command inside a container
'''
command_exec = ["docker", "run", "--rm", "--privileged", "--net=host",
"-v", "/dev:/dev", "-v", "/etc/ceph:/etc/ceph:z",
"-v", "/run/lvm/lvmetad.socket:/run/lvm/lvmetad.socket",
"-v", "/var/lib/ceph/:/var/lib/ceph/:z",
os.path.join("--entrypoint=" + binary),
container_image]
return command_exec
def get_data(data, data_vg):
if data_vg:
data = "{0}/{1}".format(data_vg, data)
return data
def get_journal(journal, journal_vg):
if journal_vg:
journal = "{0}/{1}".format(journal_vg, journal)
return journal
def get_db(db, db_vg):
if db_vg:
db = "{0}/{1}".format(db_vg, db)
return db
def get_wal(wal, wal_vg):
if wal_vg:
wal = "{0}/{1}".format(wal_vg, wal)
return wal
def _list(module):
cmd = [
'ceph-volume',
'lvm',
'list',
'--format=json',
]
result = dict(
changed=False,
cmd=cmd,
stdout='',
stderr='',
rc='',
start='',
end='',
delta='',
)
if module.check_mode:
return result
startd = datetime.datetime.now()
rc, out, err = module.run_command(cmd, encoding=None)
endd = datetime.datetime.now()
delta = endd - startd
result = dict(
cmd=cmd,
stdout=out.rstrip(b"\r\n"),
stderr=err.rstrip(b"\r\n"),
rc=rc,
start=str(startd),
end=str(endd),
delta=str(delta),
changed=True,
)
if rc != 0:
module.fail_json(msg='non-zero return code', **result)
module.exit_json(**result)
def batch(module):
cluster = module.params['cluster']
objectstore = module.params['objectstore']
batch_devices = module.params['batch_devices']
crush_device_class = module.params.get('crush_device_class', None)
dmcrypt = module.params['dmcrypt']
osds_per_device = module.params['osds_per_device']
journal_size = module.params['journal_size']
block_db_size = module.params['block_db_size']
report = module.params['report']
subcommand = 'batch'
if not batch_devices:
module.fail_json(
msg='batch_devices must be provided if action is "batch"', changed=False, rc=1) # noqa 4502
if "CEPH_CONTAINER_IMAGE" in os.environ:
container_image = os.getenv("CEPH_CONTAINER_IMAGE")
else:
container_image = None
cmd = ceph_volume_cmd(subcommand, container_image, cluster)
cmd.extend(["--%s" % objectstore])
cmd.extend("--yes")
cmd.extend("--no-systemd ")
if crush_device_class:
cmd.extend(["--crush-device-class", crush_device_class])
if dmcrypt:
cmd.append("--dmcrypt")
if osds_per_device > 1:
cmd.extend(["--osds-per-device", osds_per_device])
if objectstore == "filestore":
cmd.extend(["--journal-size", journal_size])
if objectstore == "bluestore" and block_db_size != "-1":
cmd.extend(["--block-db-size", block_db_size])
report_flags = [
"--report",
"--format=json",
]
cmd.extend(batch_devices)
result = dict(
changed=False,
cmd=cmd,
stdout='',
stderr='',
rc='',
start='',
end='',
delta='',
)
if module.check_mode:
return result
startd = datetime.datetime.now()
report_cmd = copy.copy(cmd)
report_cmd.extend(report_flags)
rc, out, err = module.run_command(report_cmd, encoding=None)
try:
report_result = json.loads(out)
except ValueError:
result = dict(
cmd=report_cmd,
stdout=out.rstrip(b"\r\n"),
stderr=err.rstrip(b"\r\n"),
rc=rc,
changed=True,
)
module.fail_json(msg='non-zero return code', **result)
if not report:
rc, out, err = module.run_command(cmd, encoding=None)
else:
cmd = report_cmd
endd = datetime.datetime.now()
delta = endd - startd
changed = True
if not report:
changed = report_result['changed']
result = dict(
cmd=cmd,
stdout=out.rstrip(b"\r\n"),
stderr=err.rstrip(b"\r\n"),
rc=rc,
start=str(startd),
end=str(endd),
delta=str(delta),
changed=changed,
)
if rc != 0:
module.fail_json(msg='non-zero return code', **result)
module.exit_json(**result)
def ceph_volume_cmd(subcommand, container_image, cluster=None):
if container_image:
binary = "ceph-volume"
cmd = container_exec(
binary, container_image)
else:
binary = ["ceph-volume"]
cmd = binary
if cluster:
cmd.extend(["--cluster", cluster])
cmd.append('lvm')
cmd.append(subcommand)
return cmd
def activate_osd(module, container_image=None):
subcommand = "activate"
cmd = ceph_volume_cmd(subcommand)
cmd.append("--all")
return True
def prepare_osd(module):
cluster = module.params['cluster']
objectstore = module.params['objectstore']
data = module.params['data']
data_vg = module.params.get('data_vg', None)
journal = module.params.get('journal', None)
journal_vg = module.params.get('journal_vg', None)
db = module.params.get('db', None)
db_vg = module.params.get('db_vg', None)
wal = module.params.get('wal', None)
wal_vg = module.params.get('wal_vg', None)
crush_device_class = module.params.get('crush_device_class', None)
dmcrypt = module.params['dmcrypt']
subcommand = "prepare"
if "CEPH_CONTAINER_IMAGE" in os.environ:
container_image = os.getenv("CEPH_CONTAINER_IMAGE")
else:
container_image = None
cmd = ceph_volume_cmd(subcommand, container_image, cluster)
cmd.extend(["--%s" % objectstore])
cmd.append("--data")
data = get_data(data, data_vg)
cmd.append(data)
if journal:
journal = get_journal(journal, journal_vg)
cmd.extend(["--journal", journal])
if db:
db = get_db(db, db_vg)
cmd.extend(["--block.db", db])
if wal:
wal = get_wal(wal, wal_vg)
cmd.extend(["--block.wal", wal])
if crush_device_class:
cmd.extend(["--crush-device-class", crush_device_class])
if dmcrypt:
cmd.append("--dmcrypt")
result = dict(
changed=False,
cmd=cmd,
stdout='',
stderr='',
rc='',
start='',
end='',
delta='',
)
if module.check_mode:
return result
# check to see if osd already exists
# FIXME: this does not work when data is a raw device
# support for 'lvm list' and raw devices
# was added with https://github.com/ceph/ceph/pull/20620 but
# has not made it to a luminous release as of 12.2.4
ceph_volume_list_cmd_args = ["lvm", "list", data]
if container_image:
binary = "ceph-volume"
ceph_volume_list_cmd = container_exec(
binary, container_image) + ceph_volume_list_cmd_args
else:
binary = ["ceph-volume"]
ceph_volume_list_cmd = binary + ceph_volume_list_cmd_args
rc, out, err = module.run_command(ceph_volume_list_cmd, encoding=None)
if rc == 0:
result["stdout"] = "skipped, since {0} is already used for an osd".format( # noqa E501
data)
result['rc'] = 0
module.exit_json(**result)
startd = datetime.datetime.now()
rc, out, err = module.run_command(cmd, encoding=None)
endd = datetime.datetime.now()
delta = endd - startd
result = dict(
cmd=cmd,
stdout=out.rstrip(b"\r\n"),
stderr=err.rstrip(b"\r\n"),
rc=rc,
start=str(startd),
end=str(endd),
delta=str(delta),
changed=True,
)
if rc != 0:
module.fail_json(msg='non-zero return code', **result)
module.exit_json(**result)
def zap_devices(module):
"""
Will run 'ceph-volume lvm zap' on all devices, lvs and partitions
used to create the OSD. The --destroy flag is always passed so that
if an OSD was originally created with a raw device or partition for
'data' then any lvs that were created by ceph-volume are removed.
"""
data = module.params['data']
data_vg = module.params.get('data_vg', None)
journal = module.params.get('journal', None)
journal_vg = module.params.get('journal_vg', None)
db = module.params.get('db', None)
db_vg = module.params.get('db_vg', None)
wal = module.params.get('wal', None)
wal_vg = module.params.get('wal_vg', None)
base_zap_cmd = [
'ceph-volume',
'lvm',
'zap',
# for simplicity always --destroy. It will be needed
# for raw devices and will noop for lvs.
'--destroy',
]
commands = []
data = get_data(data, data_vg)
commands.append(base_zap_cmd + [data])
if journal:
journal = get_journal(journal, journal_vg)
commands.append(base_zap_cmd + [journal])
if db:
db = get_db(db, db_vg)
commands.append(base_zap_cmd + [db])
if wal:
wal = get_wal(wal, wal_vg)
commands.append(base_zap_cmd + [wal])
result = dict(
changed=True,
rc=0,
)
command_results = []
for cmd in commands:
startd = datetime.datetime.now()
rc, out, err = module.run_command(cmd, encoding=None)
endd = datetime.datetime.now()
delta = endd - startd
cmd_result = dict(
cmd=cmd,
stdout_lines=out.split("\n"),
stderr_lines=err.split("\n"),
rc=rc,
start=str(startd),
end=str(endd),
delta=str(delta),
)
if rc != 0:
module.fail_json(msg='non-zero return code', **cmd_result)
command_results.append(cmd_result)
result["commands"] = command_results
module.exit_json(**result)
def run_module():
module_args = dict(
cluster=dict(type='str', required=False, default='ceph'),
objectstore=dict(type='str', required=False, choices=[
'bluestore', 'filestore'], default='bluestore'),
action=dict(type='str', required=False, choices=[
'create', 'zap', 'batch', 'prepare', 'activate', 'list'], default='create'), # noqa 4502
data=dict(type='str', required=False),
data_vg=dict(type='str', required=False),
journal=dict(type='str', required=False),
journal_vg=dict(type='str', required=False),
db=dict(type='str', required=False),
db_vg=dict(type='str', required=False),
wal=dict(type='str', required=False),
wal_vg=dict(type='str', required=False),
crush_device_class=dict(type='str', required=False),
dmcrypt=dict(type='bool', required=False, default=False),
batch_devices=dict(type='list', required=False, default=[]),
osds_per_device=dict(type='int', required=False, default=1),
journal_size=dict(type='str', required=False, default="5120"),
block_db_size=dict(type='str', required=False, default="-1"),
report=dict(type='bool', required=False, default=False),
containerized=dict(type='str', required=False, default=False),
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
action = module.params['action']
if action == "create":
prepare_osd(module)
activate_osd(module)
elif action == "prepare":
prepare_osd(module)
elif action == "activate":
activate_osd(module)
elif action == "zap":
zap_devices(module)
elif action == "batch":
batch(module)
elif action == "list":
_list(module)
module.fail_json(
msg='State must either be "present" or "absent".', changed=False, rc=1)
def main():
run_module()
if __name__ == '__main__':
main()