From ba0af03b4306593eac239dfc21d9bcc1bcb6aca7 Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Wed, 5 Dec 2018 15:14:08 -0800 Subject: [PATCH] ceph-volume: add support for inventory command Signed-off-by: Noah Watkins --- library/ceph_volume.py | 41 ++++++++++++++++++++++++++++--------- library/test_ceph_volume.py | 28 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/library/ceph_volume.py b/library/ceph_volume.py index f47aa98f8..85d25242d 100644 --- a/library/ceph_volume.py +++ b/library/ceph_volume.py @@ -3,6 +3,7 @@ import datetime import copy import json import os +import six ANSIBLE_METADATA = { 'metadata_version': '1.0', @@ -36,9 +37,9 @@ options: default: bluestore action: description: - - The action to take. Either creating OSDs or zapping devices. + - The action to take. Creating OSDs and zapping or querying devices. required: true - choices: ['create', 'zap', 'batch', 'prepare', 'activate', 'list'] + choices: ['create', 'zap', 'batch', 'prepare', 'activate', 'list', 'inventory'] default: create data: description: @@ -127,6 +128,10 @@ options: description: - List potential Ceph LVM metadata on a device required: false + inventory: + description: + - List storage device inventory. + required: false author: - Andrew Schoen (@andrewschoen) @@ -208,8 +213,7 @@ def build_ceph_volume_cmd(action, container_image, cluster=None): if cluster: cmd.extend(['--cluster', cluster]) - cmd.append('lvm') - cmd.append(action) + cmd.extend(action if not isinstance(action, six.string_types) else [action]) return cmd @@ -285,7 +289,7 @@ def batch(module, container_image): fatal('batch_devices must be provided if action is "batch"', module) # Build the CLI - action = 'batch' + action = ['lvm', 'batch'] cmd = build_ceph_volume_cmd(action, container_image, cluster) cmd.extend(['--%s' % objectstore]) cmd.append('--yes') @@ -356,6 +360,7 @@ def prepare_or_create_osd(module, action, container_image): dmcrypt = module.params.get('dmcrypt', None) # Build the CLI + action = ['lvm', action] cmd = build_ceph_volume_cmd(action, container_image, cluster) cmd.extend(['--%s' % objectstore]) cmd.append('--data') @@ -394,7 +399,7 @@ def list_osd(module, container_image): data = get_data(data, data_vg) # Build the CLI - action = 'list' + action = ['lvm', 'list'] cmd = build_ceph_volume_cmd(action, container_image, cluster) if data: cmd.append(data) @@ -402,6 +407,16 @@ def list_osd(module, container_image): return cmd +def list_storage_inventory(module, container_image): + ''' + List storage inventory. + ''' + + action = 'inventory' + cmd = build_ceph_volume_cmd(action, container_image) + cmd.append('--format=json') + + return cmd def activate_osd(): ''' @@ -409,7 +424,7 @@ def activate_osd(): ''' # build the CLI - action = 'activate' + action = ['lvm', 'activate'] container_image = None cmd = build_ceph_volume_cmd(action, container_image) cmd.append('--all') @@ -437,7 +452,7 @@ def zap_devices(module, container_image): data = get_data(data, data_vg) # build the CLI - action = 'zap' + action = ['lvm', 'zap'] cmd = build_ceph_volume_cmd(action, container_image) cmd.append('--destroy') cmd.append(data) @@ -463,7 +478,8 @@ def run_module(): 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 + 'create', 'zap', 'batch', 'prepare', 'activate', 'list', + 'inventory'], default='create'), # noqa 4502 data=dict(type='str', required=False), data_vg=dict(type='str', required=False), journal=dict(type='str', required=False), @@ -559,6 +575,11 @@ def run_module(): rc, cmd, out, err = exec_command( module, list_osd(module, container_image)) + elif action == 'inventory': + # List storage device inventory. + rc, cmd, out, err = exec_command( + module, list_storage_inventory(module, container_image)) + elif action == 'batch': # Batch prepare AND activate OSDs report = module.params.get('report', None) @@ -611,7 +632,7 @@ def run_module(): else: module.fail_json( - msg='State must either be "create" or "prepare" or "activate" or "list" or "zap" or "batch".', changed=False, rc=1) # noqa E501 + msg='State must either be "create" or "prepare" or "activate" or "list" or "zap" or "batch" or "inventory".', changed=False, rc=1) # noqa E501 endd = datetime.datetime.now() delta = endd - startd diff --git a/library/test_ceph_volume.py b/library/test_ceph_volume.py index bb25727de..f7808e5d1 100644 --- a/library/test_ceph_volume.py +++ b/library/test_ceph_volume.py @@ -132,6 +132,34 @@ class TestCephVolumeModule(object): result = ceph_volume.list_osd(fake_module, fake_container_image) assert result == expected_command_list + def test_list_storage_inventory(self): + fake_module = MagicMock() + fake_container_image = None + expected_command_list = ['ceph-volume', + 'inventory', + '--format=json', + ] + result = ceph_volume.list_storage_inventory(fake_module, fake_container_image) + assert result == expected_command_list + + def test_list_storage_inventory_container(self): + fake_module = MagicMock() + fake_container_image = "docker.io/ceph/daemon:latest-luminous" + expected_command_list = ['docker', 'run', '--rm', '--privileged', '--net=host', # noqa E501 + '-v', '/run/lock/lvm:/run/lock/lvm:z', + '-v', '/var/run/udev/:/var/run/udev/:z', + '-v', '/dev:/dev', '-v', '/etc/ceph:/etc/ceph:z', # noqa E501 + '-v', '/run/lvm/lvmetad.socket:/run/lvm/lvmetad.socket', # noqa E501 + '-v', '/var/lib/ceph/:/var/lib/ceph/:z', + '-v', '/var/log/ceph/:/var/log/ceph/:z', + '--entrypoint=ceph-volume', + 'docker.io/ceph/daemon:latest-luminous', + 'inventory', + '--format=json', + ] + result = ceph_volume.list_storage_inventory(fake_module, fake_container_image) + assert result == expected_command_list + def test_create_osd_container(self): fake_module = MagicMock() fake_module.params = {'data': '/dev/sda', -- 2.39.5