From f33c2ccd34be26d4938786e3ebe0461617e07004 Mon Sep 17 00:00:00 2001 From: Teoman ONAY Date: Tue, 29 Nov 2022 09:47:58 +0100 Subject: [PATCH] Initialize rbd pool at pool creation When creating a RBD pool it needs to be initialized as per documentation[1] Modified (pre_)generate_ceph_cmd to make it usable with any command with the same parameters as the ceph command [1]https://docs.ceph.com/en/latest/rbd/rados-rbd-cmds/#create-a-block-device-pool Signed-off-by: Teoman ONAY (cherry picked from commit c39bbc3deaf7a02d36daa39d752e91e94afb3c3e) # Conflicts: # library/ceph_ec_profile.py # library/ceph_key.py # tests/library/test_ceph_key.py --- library/ceph_crush_rule.py | 28 ++--- library/ceph_dashboard_user.py | 48 ++++----- library/ceph_ec_profile.py | 33 +++--- library/ceph_fs.py | 44 ++++---- library/ceph_key.py | 57 ++++++++++ library/ceph_mgr_module.py | 12 +-- library/ceph_osd.py | 6 +- library/ceph_osd_flag.py | 8 +- library/ceph_pool.py | 149 ++++++++++++++++----------- module_utils/ca_common.py | 30 ++++-- tests/library/test_ceph_key.py | 19 ++++ tests/library/test_ceph_pool.py | 52 +++++++++- tests/module_utils/test_ca_common.py | 22 ++-- 13 files changed, 335 insertions(+), 173 deletions(-) diff --git a/library/ceph_crush_rule.py b/library/ceph_crush_rule.py index 87d4f1e2d..88ec8434c 100644 --- a/library/ceph_crush_rule.py +++ b/library/ceph_crush_rule.py @@ -18,12 +18,12 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: from ansible.module_utils.ca_common import exit_module, \ - generate_ceph_cmd, \ + generate_cmd, \ is_containerized, \ exec_command except ImportError: from module_utils.ca_common import exit_module, \ - generate_ceph_cmd, \ + generate_cmd, \ is_containerized, \ exec_command import datetime @@ -142,10 +142,10 @@ def create_rule(module, container_image=None): if profile: args.append(profile) - cmd = generate_ceph_cmd(['osd', 'crush', 'rule'], - args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'crush', 'rule'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -160,10 +160,10 @@ def get_rule(module, container_image=None): args = ['dump', name, '--format=json'] - cmd = generate_ceph_cmd(['osd', 'crush', 'rule'], - args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'crush', 'rule'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -178,10 +178,10 @@ def remove_rule(module, container_image=None): args = ['rm', name] - cmd = generate_ceph_cmd(['osd', 'crush', 'rule'], - args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'crush', 'rule'], + args=args, + cluster=cluster, + container_image=container_image) return cmd diff --git a/library/ceph_dashboard_user.py b/library/ceph_dashboard_user.py index 22cc760f1..89fdafff9 100644 --- a/library/ceph_dashboard_user.py +++ b/library/ceph_dashboard_user.py @@ -17,13 +17,13 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import generate_ceph_cmd, \ + from ansible.module_utils.ca_common import generate_cmd, \ is_containerized, \ exec_command, \ exit_module, \ fatal except ImportError: - from module_utils.ca_common import generate_ceph_cmd, is_containerized, exec_command, exit_module, fatal # noqa: E501 + from module_utils.ca_common import generate_cmd, is_containerized, exec_command, exit_module, fatal # noqa: E501 import datetime import json @@ -123,11 +123,11 @@ def create_user(module, container_image=None): args = ['ac-user-create', '-i', '-', name] - cmd = generate_ceph_cmd(sub_cmd=['dashboard'], - args=args, - cluster=cluster, - container_image=container_image, - interactive=True) + cmd = generate_cmd(sub_cmd=['dashboard'], + args=args, + cluster=cluster, + container_image=container_image, + interactive=True) return cmd @@ -145,10 +145,10 @@ def set_roles(module, container_image=None): args.extend(roles) - cmd = generate_ceph_cmd(sub_cmd=['dashboard'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['dashboard'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -163,11 +163,11 @@ def set_password(module, container_image=None): args = ['ac-user-set-password', '-i', '-', name] - cmd = generate_ceph_cmd(sub_cmd=['dashboard'], - args=args, - cluster=cluster, - container_image=container_image, - interactive=True) + cmd = generate_cmd(sub_cmd=['dashboard'], + args=args, + cluster=cluster, + container_image=container_image, + interactive=True) return cmd @@ -182,10 +182,10 @@ def get_user(module, container_image=None): args = ['ac-user-show', name, '--format=json'] - cmd = generate_ceph_cmd(sub_cmd=['dashboard'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['dashboard'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -200,10 +200,10 @@ def remove_user(module, container_image=None): args = ['ac-user-delete', name] - cmd = generate_ceph_cmd(sub_cmd=['dashboard'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['dashboard'], + args=args, + cluster=cluster, + container_image=container_image) return cmd diff --git a/library/ceph_ec_profile.py b/library/ceph_ec_profile.py index 183d2c5d0..380d00b91 100644 --- a/library/ceph_ec_profile.py +++ b/library/ceph_ec_profile.py @@ -18,12 +18,12 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: from ansible.module_utils.ca_common import is_containerized, \ - generate_ceph_cmd, \ + generate_cmd, \ exec_command, \ exit_module except ImportError: from module_utils.ca_common import is_containerized, \ - generate_ceph_cmd, \ + generate_cmd, \ exec_command, \ exit_module import datetime @@ -113,10 +113,10 @@ def get_profile(module, name, cluster='ceph', container_image=None): args = ['get', name, '--format=json'] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'erasure-code-profile'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'erasure-code-profile'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -132,10 +132,10 @@ def create_profile(module, name, k, m, stripe_unit, cluster='ceph', force=False, if force: args.append('--force') - cmd = generate_ceph_cmd(sub_cmd=['osd', 'erasure-code-profile'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'erasure-code-profile'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -147,10 +147,10 @@ def delete_profile(module, name, cluster='ceph', container_image=None): args = ['rm', name] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'erasure-code-profile'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'erasure-code-profile'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -205,7 +205,12 @@ def run_module(): current_profile = json.loads(out) if current_profile['k'] != k or \ current_profile['m'] != m or \ +<<<<<<< HEAD current_profile.get('stripe_unit', stripe_unit) != stripe_unit: +======= + current_profile.get('stripe_unit', stripe_unit) != stripe_unit or \ + current_profile.get('crush-device-class', crush_device_class) != crush_device_class: # noqa: E501 +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) rc, cmd, out, err = exec_command(module, create_profile(module, name, diff --git a/library/ceph_fs.py b/library/ceph_fs.py index 1be8881dc..10db2b161 100644 --- a/library/ceph_fs.py +++ b/library/ceph_fs.py @@ -19,12 +19,12 @@ from ansible.module_utils.basic import AnsibleModule try: from ansible.module_utils.ca_common import is_containerized, \ exec_command, \ - generate_ceph_cmd, \ + generate_cmd, \ exit_module except ImportError: from module_utils.ca_common import is_containerized, \ exec_command, \ - generate_ceph_cmd, \ + generate_cmd, \ exit_module import datetime @@ -119,10 +119,10 @@ def create_fs(module, container_image=None): args = ['new', name, metadata, data] - cmd = generate_ceph_cmd(sub_cmd=['fs'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['fs'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -137,10 +137,10 @@ def get_fs(module, container_image=None): args = ['get', name, '--format=json'] - cmd = generate_ceph_cmd(sub_cmd=['fs'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['fs'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -155,10 +155,10 @@ def remove_fs(module, container_image=None): args = ['rm', name, '--yes-i-really-mean-it'] - cmd = generate_ceph_cmd(sub_cmd=['fs'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['fs'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -173,10 +173,10 @@ def fail_fs(module, container_image=None): args = ['fail', name] - cmd = generate_ceph_cmd(sub_cmd=['fs'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['fs'], + args=args, + cluster=cluster, + container_image=container_image) return cmd @@ -192,10 +192,10 @@ def set_fs(module, container_image=None): args = ['set', name, 'max_mds', str(max_mds)] - cmd = generate_ceph_cmd(sub_cmd=['fs'], - args=args, - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['fs'], + args=args, + cluster=cluster, + container_image=container_image) return cmd diff --git a/library/ceph_key.py b/library/ceph_key.py index 382d4f1f8..572c29278 100644 --- a/library/ceph_key.py +++ b/library/ceph_key.py @@ -19,9 +19,21 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: +<<<<<<< HEAD from ansible.module_utils.ca_common import is_containerized, container_exec, fatal except ImportError: from module_utils.ca_common import is_containerized, container_exec, fatal +======= + from ansible.module_utils.ca_common import generate_cmd, \ + is_containerized, \ + container_exec, \ + fatal +except ImportError: + from module_utils.ca_common import generate_cmd, \ + is_containerized, \ + container_exec, \ + fatal +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) import datetime import json import os @@ -326,8 +338,17 @@ def create_key(module, result, cluster, user, user_key_path, name, secret, caps, cluster, name, secret, caps, dest, container_image)) if import_key or user != 'client.admin': +<<<<<<< HEAD cmd_list.append(generate_ceph_cmd( cluster, args, user, user_key_path, container_image)) +======= + cmd_list.append(generate_cmd(sub_cmd=['auth'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image)) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) return cmd_list @@ -344,8 +365,17 @@ def delete_key(cluster, user, user_key_path, name, container_image=None): name, ] +<<<<<<< HEAD cmd_list.append(generate_ceph_cmd( cluster, args, user, user_key_path, container_image)) +======= + cmd_list.append(generate_cmd(sub_cmd=['auth'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image)) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) return cmd_list @@ -364,8 +394,17 @@ def get_key(cluster, user, user_key_path, name, dest, container_image=None): dest, ] +<<<<<<< HEAD cmd_list.append(generate_ceph_cmd( cluster, args, user, user_key_path, container_image)) +======= + cmd_list.append(generate_cmd(sub_cmd=['auth'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image)) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) return cmd_list @@ -384,8 +423,17 @@ def info_key(cluster, name, user, user_key_path, output_format, container_image= output_format, ] +<<<<<<< HEAD cmd_list.append(generate_ceph_cmd( cluster, args, user, user_key_path, container_image)) +======= + cmd_list.append(generate_cmd(sub_cmd=['auth'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image)) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) return cmd_list @@ -403,8 +451,17 @@ def list_keys(cluster, user, user_key_path, container_image=None): 'json', ] +<<<<<<< HEAD cmd_list.append(generate_ceph_cmd( cluster, args, user, user_key_path, container_image)) +======= + cmd_list.append(generate_cmd(sub_cmd=['auth'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image)) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) return cmd_list diff --git a/library/ceph_mgr_module.py b/library/ceph_mgr_module.py index 1971159d3..0c2d177e4 100644 --- a/library/ceph_mgr_module.py +++ b/library/ceph_mgr_module.py @@ -18,11 +18,11 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: from ansible.module_utils.ca_common import exit_module, \ - generate_ceph_cmd, \ + generate_cmd, \ is_containerized except ImportError: from module_utils.ca_common import exit_module, \ - generate_ceph_cmd, \ + generate_cmd, \ is_containerized import datetime @@ -97,10 +97,10 @@ def main(): container_image = is_containerized() - cmd = generate_ceph_cmd(['mgr', 'module'], - [state, name], - cluster=cluster, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['mgr', 'module'], + args=[state, name], + cluster=cluster, + container_image=container_image) if module.check_mode: exit_module( diff --git a/library/ceph_osd.py b/library/ceph_osd.py index 63e15010d..0eadc938d 100644 --- a/library/ceph_osd.py +++ b/library/ceph_osd.py @@ -17,9 +17,9 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module, generate_ceph_cmd, is_containerized # noqa: E501 + from ansible.module_utils.ca_common import exit_module, generate_cmd, is_containerized # noqa: E501 except ImportError: - from module_utils.ca_common import exit_module, generate_ceph_cmd, is_containerized # noqa: E501 + from module_utils.ca_common import exit_module, generate_cmd, is_containerized # noqa: E501 import datetime @@ -111,7 +111,7 @@ def main(): container_image = is_containerized() - cmd = generate_ceph_cmd(['osd', state], ids, cluster=cluster, container_image=container_image) # noqa: E501 + cmd = generate_cmd(sub_cmd=['osd', state], args=ids, cluster=cluster, container_image=container_image) # noqa: E501 if state in ['destroy', 'purge']: cmd.append('--yes-i-really-mean-it') diff --git a/library/ceph_osd_flag.py b/library/ceph_osd_flag.py index 1e6fe9611..ef21eaf18 100644 --- a/library/ceph_osd_flag.py +++ b/library/ceph_osd_flag.py @@ -18,11 +18,11 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: from ansible.module_utils.ca_common import exit_module, \ - generate_ceph_cmd, \ + generate_cmd, \ is_containerized except ImportError: from module_utils.ca_common import exit_module, \ - generate_ceph_cmd, \ + generate_cmd, \ is_containerized import datetime @@ -99,9 +99,9 @@ def main(): container_image = is_containerized() if state == 'present': - cmd = generate_ceph_cmd(['osd', 'set'], [name], cluster=cluster, container_image=container_image) # noqa: E501 + cmd = generate_cmd(sub_cmd=['osd', 'set'], args=[name], cluster=cluster, container_image=container_image) # noqa: E501 else: - cmd = generate_ceph_cmd(['osd', 'unset'], [name], cluster=cluster, container_image=container_image) # noqa: E501 + cmd = generate_cmd(sub_cmd=['osd', 'unset'], args=[name], cluster=cluster, container_image=container_image) # noqa: E501 if module.check_mode: exit_module( diff --git a/library/ceph_pool.py b/library/ceph_pool.py index 417236289..9451a71c3 100644 --- a/library/ceph_pool.py +++ b/library/ceph_pool.py @@ -19,14 +19,14 @@ __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import generate_ceph_cmd, \ - pre_generate_ceph_cmd, \ + from ansible.module_utils.ca_common import generate_cmd, \ + pre_generate_cmd, \ is_containerized, \ exec_command, \ exit_module except ImportError: - from module_utils.ca_common import generate_ceph_cmd, \ - pre_generate_ceph_cmd, \ + from module_utils.ca_common import generate_cmd, \ + pre_generate_cmd, \ is_containerized, \ exec_command, \ exit_module @@ -167,12 +167,12 @@ def check_pool_exist(cluster, args = ['stats', name, '-f', output_format] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd @@ -182,7 +182,7 @@ def generate_get_config_cmd(param, user, user_key, container_image=None): - _cmd = pre_generate_ceph_cmd(container_image=container_image) + _cmd = pre_generate_cmd('ceph', container_image=container_image) args = [ '-n', user, @@ -211,12 +211,12 @@ def get_application_pool(cluster, args = ['application', 'get', name, '-f', output_format] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd @@ -233,12 +233,34 @@ def enable_application_pool(cluster, args = ['application', 'enable', name, application] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) + + return cmd + + +def init_rbd_pool(cluster, + name, + user, + user_key, + container_image=None): + ''' + Initialize a rbd pool + ''' + + args = [name] + + cmd = generate_cmd(cmd='rbd', + sub_cmd=['pool', 'init'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd @@ -256,12 +278,12 @@ def disable_application_pool(cluster, args = ['application', 'disable', name, application, '--yes-i-really-mean-it'] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd @@ -279,12 +301,12 @@ def get_pool_details(module, args = ['ls', 'detail', '-f', output_format] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) rc, cmd, out, err = exec_command(module, cmd) @@ -368,18 +390,17 @@ def list_pools(cluster, args.extend(['-f', output_format]) - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd def create_pool(cluster, - name, user, user_key, user_pool_config, @@ -423,12 +444,12 @@ def create_pool(cluster, '--autoscale-mode', user_pool_config['pg_autoscale_mode']['value']]) - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd @@ -440,12 +461,12 @@ def remove_pool(cluster, name, user, user_key, container_image=None): args = ['rm', name, name, '--yes-i-really-really-mean-it'] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) return cmd @@ -465,12 +486,12 @@ def update_pool(module, cluster, name, delta[key]['cli_set_opt'], delta[key]['value']] - cmd = generate_ceph_cmd(sub_cmd=['osd', 'pool'], - args=args, - cluster=cluster, - user=user, - user_key=user_key, - container_image=container_image) + cmd = generate_cmd(sub_cmd=['osd', 'pool'], + args=args, + cluster=cluster, + user=user, + user_key=user_key, + container_image=container_image) rc, cmd, out, err = exec_command(module, cmd) if rc != 0: @@ -634,7 +655,6 @@ def run_module(): else: rc, cmd, out, err = exec_command(module, create_pool(cluster, - name, user, user_key, user_pool_config=user_pool_config, # noqa: E501 @@ -647,6 +667,13 @@ def run_module(): user, user_key, container_image=container_image)) # noqa: E501 + if rc == 0 and user_pool_config['application']['value'] == 'rbd': # noqa: E501 + rc, cmd, out, err = exec_command(module, + init_rbd_pool(cluster, + user_pool_config['pool_name']['value'], # noqa: E501 + user, + user_key, + container_image=container_image)) # noqa: E501 if user_pool_config['min_size']['value']: # not implemented yet pass diff --git a/module_utils/ca_common.py b/module_utils/ca_common.py index 7bd70ce16..f478b75e5 100644 --- a/module_utils/ca_common.py +++ b/module_utils/ca_common.py @@ -2,15 +2,22 @@ import os import datetime -def generate_ceph_cmd(sub_cmd, args, user_key=None, cluster='ceph', user='client.admin', container_image=None, interactive=False): +def generate_cmd(cmd='ceph', + sub_cmd=None, + args=None, + user_key=None, + cluster='ceph', + user='client.admin', + container_image=None, + interactive=False): ''' Generate 'ceph' command line to execute ''' - if not user_key: + if user_key is None: user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user) - cmd = pre_generate_ceph_cmd(container_image=container_image, interactive=interactive) + cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501 base_cmd = [ '-n', @@ -20,8 +27,11 @@ def generate_ceph_cmd(sub_cmd, args, user_key=None, cluster='ceph', user='client '--cluster', cluster ] - base_cmd.extend(sub_cmd) - cmd.extend(base_cmd + args) + + if sub_cmd is not None: + base_cmd.extend(sub_cmd) + + cmd.extend(base_cmd) if args is None else cmd.extend(base_cmd + args) return cmd @@ -59,14 +69,14 @@ def is_containerized(): return container_image -def pre_generate_ceph_cmd(container_image=None, interactive=False): +def pre_generate_cmd(cmd, container_image=None, interactive=False): ''' - Generate ceph prefix comaand + Generate ceph prefix command ''' if container_image: - cmd = container_exec('ceph', container_image, interactive=interactive) + cmd = container_exec(cmd, container_image, interactive=interactive) else: - cmd = ['ceph'] + cmd = [cmd] return cmd @@ -84,7 +94,7 @@ def exec_command(module, cmd, stdin=None): return rc, cmd, out, err -def exit_module(module, out, rc, cmd, err, startd, changed=False, diff=dict(before="", after="")): +def exit_module(module, out, rc, cmd, err, startd, changed=False, diff=dict(before="", after="")): # noqa: E501 endd = datetime.datetime.now() delta = endd - startd diff --git a/tests/library/test_ceph_key.py b/tests/library/test_ceph_key.py index 7f52f245c..79060f85e 100644 --- a/tests/library/test_ceph_key.py +++ b/tests/library/test_ceph_key.py @@ -62,8 +62,17 @@ class TestCephKeyModule(object): 'auth', 'arg' ] +<<<<<<< HEAD result = ceph_key.generate_ceph_cmd( fake_cluster, fake_args, fake_user, fake_key) +======= + result = ceph_key.generate_cmd( + sub_cmd=['auth'], + args=fake_args, + cluster=fake_cluster, + user=fake_user, + user_key=fake_user_key) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) assert result == expected_command_list def test_generate_ceph_cmd_list_container(self): @@ -89,8 +98,18 @@ class TestCephKeyModule(object): fake_cluster, 'auth', 'arg'] +<<<<<<< HEAD result = ceph_key.generate_ceph_cmd( fake_cluster, fake_args, fake_user, fake_user_key, fake_container_image) +======= + result = ceph_key.generate_cmd( + sub_cmd=['auth'], + args=fake_args, + cluster=fake_cluster, + user=fake_user, + user_key=fake_user_key, + container_image=fake_container_image) +>>>>>>> c39bbc3de (Initialize rbd pool at pool creation) assert result == expected_command_list def test_generate_ceph_authtool_cmd_non_container_no_auid(self): diff --git a/tests/library/test_ceph_pool.py b/tests/library/test_ceph_pool.py index 2ce40560f..f072c2611 100644 --- a/tests/library/test_ceph_pool.py +++ b/tests/library/test_ceph_pool.py @@ -2,6 +2,7 @@ import os import sys import ceph_pool from mock.mock import patch +import pytest sys.path.append('./library') fake_user = 'client.admin' @@ -282,6 +283,53 @@ class TestCephPoolModule(object): assert cmd == expected_command + @pytest.mark.parametrize("container_image", [None, fake_container_image_name]) + def test_init_rbd_pool(self, container_image): + if container_image: + expected_command = [ + 'podman', + 'run', + '--rm', + '--net=host', + '-v', + '/etc/ceph:/etc/ceph:z', + '-v', + '/var/lib/ceph/:/var/lib/ceph/:z', + '-v', + '/var/log/ceph/:/var/log/ceph/:z', + '--entrypoint=rbd', + fake_container_image_name, + '-n', + fake_user, + '-k', + fake_user_key, + '--cluster', + fake_cluster_name, + 'pool', + 'init', + self.fake_user_pool_config['pool_name']['value'] + ] + else: + expected_command = [ + 'rbd', + '-n', + fake_user, + '-k', + fake_user_key, + '--cluster', + fake_cluster_name, + 'pool', + 'init', + self.fake_user_pool_config['pool_name']['value'] + ] + + cmd = ceph_pool.init_rbd_pool(fake_cluster_name, + self.fake_user_pool_config['pool_name']['value'], + fake_user, fake_user_key, + container_image) + + assert cmd == expected_command + def test_disable_application_pool(self): expected_command = [ 'podman', @@ -442,7 +490,6 @@ class TestCephPoolModule(object): ] cmd = ceph_pool.create_pool(fake_cluster_name, - self.fake_user_pool_config['pool_name']['value'], fake_user, fake_user_key, self.fake_user_pool_config, container_image=fake_container_image_name) @@ -489,7 +536,6 @@ class TestCephPoolModule(object): ] cmd = ceph_pool.create_pool(fake_cluster_name, - self.fake_user_pool_config['pool_name']['value'], fake_user, fake_user_key, self.fake_user_pool_config, container_image=fake_container_image_name) @@ -535,7 +581,6 @@ class TestCephPoolModule(object): ] cmd = ceph_pool.create_pool(fake_cluster_name, - self.fake_user_pool_config['pool_name']['value'], fake_user, fake_user_key, self.fake_user_pool_config, container_image=fake_container_image_name) @@ -583,7 +628,6 @@ class TestCephPoolModule(object): ] cmd = ceph_pool.create_pool(fake_cluster_name, - self.fake_user_pool_config['pool_name']['value'], fake_user, fake_user_key, self.fake_user_pool_config, container_image=fake_container_image_name) diff --git a/tests/module_utils/test_ca_common.py b/tests/module_utils/test_ca_common.py index 2d0ea5ccf..e562bae2f 100644 --- a/tests/module_utils/test_ca_common.py +++ b/tests/module_utils/test_ca_common.py @@ -38,17 +38,17 @@ class TestCommon(object): @pytest.mark.parametrize('image', [None, fake_container_image]) @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) - def test_pre_generate_ceph_cmd(self, image): + def test_pre_generate_cmd(self, image): if image: expected_cmd = self.fake_container_cmd else: expected_cmd = [self.fake_binary] - assert ca_common.pre_generate_ceph_cmd(image) == expected_cmd + assert ca_common.pre_generate_cmd(self.fake_binary, image) == expected_cmd # noqa: E501 @pytest.mark.parametrize('image', [None, fake_container_image]) @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) - def test_generate_ceph_cmd(self, image): + def test_generate_cmd(self, image): sub_cmd = ['osd', 'pool'] args = ['create', 'foo'] if image: @@ -64,11 +64,11 @@ class TestCommon(object): 'osd', 'pool', 'create', 'foo' ]) - assert ca_common.generate_ceph_cmd(sub_cmd, args, cluster=self.fake_cluster, container_image=image) == expected_cmd + assert ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, cluster=self.fake_cluster, container_image=image) == expected_cmd # noqa: E501 @pytest.mark.parametrize('image', [None, fake_container_image]) @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) - def test_generate_ceph_cmd_different_cluster_name(self, image): + def test_generate_cmd_different_cluster_name(self, image): sub_cmd = ['osd', 'pool'] args = ['create', 'foo'] if image: @@ -84,12 +84,12 @@ class TestCommon(object): 'osd', 'pool', 'create', 'foo' ]) - result = ca_common.generate_ceph_cmd(sub_cmd, args, cluster='foo', container_image=image) + result = ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, cluster='foo', container_image=image) # noqa: E501 assert result == expected_cmd @pytest.mark.parametrize('image', [None, fake_container_image]) @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) - def test_generate_ceph_cmd_different_cluster_name_and_user(self, image): + def test_generate_cmd_different_cluster_name_and_user(self, image): sub_cmd = ['osd', 'pool'] args = ['create', 'foo'] if image: @@ -105,12 +105,12 @@ class TestCommon(object): 'osd', 'pool', 'create', 'foo' ]) - result = ca_common.generate_ceph_cmd(sub_cmd, args, cluster='foo', user='client.foo', container_image=image) + result = ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, cluster='foo', user='client.foo', container_image=image) # noqa: E501 assert result == expected_cmd @pytest.mark.parametrize('image', [None, fake_container_image]) @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) - def test_generate_ceph_cmd_different_user(self, image): + def test_generate_cmd_different_user(self, image): sub_cmd = ['osd', 'pool'] args = ['create', 'foo'] if image: @@ -126,7 +126,7 @@ class TestCommon(object): 'osd', 'pool', 'create', 'foo' ]) - result = ca_common.generate_ceph_cmd(sub_cmd, args, user='client.foo', container_image=image) + result = ca_common.generate_cmd(sub_cmd=sub_cmd, args=args, user='client.foo', container_image=image) # noqa: E501 assert result == expected_cmd @pytest.mark.parametrize('stdin', [None, 'foo']) @@ -137,7 +137,7 @@ class TestCommon(object): stdout = 'ceph version 1.2.3' fake_module.run_command.return_value = 0, stdout, stderr expected_cmd = [self.fake_binary, '--version'] - _rc, _cmd, _out, _err = ca_common.exec_command(fake_module, expected_cmd, stdin=stdin) + _rc, _cmd, _out, _err = ca_common.exec_command(fake_module, expected_cmd, stdin=stdin) # noqa: E501 assert _rc == rc assert _cmd == expected_cmd assert _err == stderr