diff --git a/plugins/module_utils/ca_common.py b/plugins/module_utils/ca_common.py deleted file mode 100644 index cfbf55a..0000000 --- a/plugins/module_utils/ca_common.py +++ /dev/null @@ -1,151 +0,0 @@ -import os -import datetime -from typing import List -from ansible.module_utils.basic import AnsibleModule - - -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 user_key is None: - user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user) - - cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501 - - base_cmd = [ - '-n', - user, - '-k', - user_key, - '--cluster', - cluster - ] - - 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 - - -def container_exec(binary, container_image, interactive=False): - ''' - Build the docker CLI to run a command inside a container - ''' - - container_binary = os.getenv('CEPH_CONTAINER_BINARY') - command_exec = [container_binary, 'run'] - - if interactive: - command_exec.extend(['--interactive']) - - command_exec.extend(['--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=' + binary, container_image]) - return command_exec - - -def is_containerized(): - ''' - Check if we are running on a containerized cluster - ''' - - if 'CEPH_CONTAINER_IMAGE' in os.environ: - container_image = os.getenv('CEPH_CONTAINER_IMAGE') - else: - container_image = None - - return container_image - - -def pre_generate_cmd(cmd, container_image=None, interactive=False): - ''' - Generate ceph prefix command - ''' - if container_image: - cmd = container_exec(cmd, container_image, interactive=interactive) - else: - cmd = [cmd] - - return cmd - - -def exec_command(module, cmd, stdin=None, check_rc=False): - ''' - Execute command(s) - ''' - - binary_data = False - if stdin: - binary_data = True - rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501 - - return rc, cmd, out, err - - -def build_base_cmd(module: "AnsibleModule") -> List[str]: - cmd = ['cephadm'] - docker = module.params.get('docker') - image = module.params.get('image') - fsid = module.params.get('fsid') - - if docker: - cmd.append('--docker') - if image: - cmd.extend(['--image', image]) - - cmd.append('shell') - - if fsid: - cmd.extend(['--fsid', fsid]) - - return cmd - - -def build_base_cmd_orch(module: "AnsibleModule") -> List[str]: - cmd = build_base_cmd(module) - cmd.extend(['ceph', 'orch']) - - return cmd - - -def exit_module(module, out, rc, cmd, err, startd, changed=False, diff=dict(before="", after="")): # noqa: E501 - endd = datetime.datetime.now() - delta = endd - startd - - result = dict( - cmd=cmd, - start=str(startd), - end=str(endd), - delta=str(delta), - rc=rc, - stdout=out.rstrip("\r\n"), - stderr=err.rstrip("\r\n"), - changed=changed, - diff=diff - ) - module.exit_json(**result) - - -def fatal(message, module): - ''' - Report a fatal error and exit - ''' - - if module: - module.fail_json(msg=message, rc=1) - else: - raise Exception(message) diff --git a/plugins/module_utils/ceph_common.py b/plugins/module_utils/ceph_common.py index b40933b..6929f14 100644 --- a/plugins/module_utils/ceph_common.py +++ b/plugins/module_utils/ceph_common.py @@ -10,6 +10,97 @@ ExceptionType = TypeVar('ExceptionType', bound=BaseException) +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 user_key is None: + user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user) + + cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501 + + base_cmd = [ + '-n', + user, + '-k', + user_key, + '--cluster', + cluster + ] + + 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 + + +def container_exec(binary, container_image, interactive=False): + ''' + Build the docker CLI to run a command inside a container + ''' + + container_binary = os.getenv('CEPH_CONTAINER_BINARY') + command_exec = [container_binary, 'run'] + + if interactive: + command_exec.extend(['--interactive']) + + command_exec.extend(['--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=' + binary, container_image]) + return command_exec + + +def is_containerized(): + ''' + Check if we are running on a containerized cluster + ''' + + if 'CEPH_CONTAINER_IMAGE' in os.environ: + container_image = os.getenv('CEPH_CONTAINER_IMAGE') + else: + container_image = None + + return container_image + + +def pre_generate_cmd(cmd, container_image=None, interactive=False): + ''' + Generate ceph prefix command + ''' + if container_image: + cmd = container_exec(cmd, container_image, interactive=interactive) + else: + cmd = [cmd] + + return cmd + + +def exec_command(module, cmd, stdin=None, check_rc=False): + ''' + Execute command(s) + ''' + + binary_data = False + if stdin: + binary_data = True + rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501 + + return rc, cmd, out, err + def retry(exceptions: Type[ExceptionType], module: "AnsibleModule", retries: int = 20, delay: int = 1) -> Callable: def decorator(f: Callable) -> Callable: diff --git a/plugins/modules/ceph_authtool.py b/plugins/modules/ceph_authtool.py index e469bcb..bfdc379 100644 --- a/plugins/modules/ceph_authtool.py +++ b/plugins/modules/ceph_authtool.py @@ -3,7 +3,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import container_exec, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import container_exec, \ is_containerized except ImportError: from module_utils.ca_common import container_exec, \ diff --git a/plugins/modules/ceph_crush.py b/plugins/modules/ceph_crush.py index 8b2e2a9..38cb5eb 100644 --- a/plugins/modules/ceph_crush.py +++ b/plugins/modules/ceph_crush.py @@ -9,7 +9,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import fatal + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import fatal except ImportError: from module_utils.ca_common import fatal import datetime diff --git a/plugins/modules/ceph_crush_rule.py b/plugins/modules/ceph_crush_rule.py index a84d7ca..b75c24d 100644 --- a/plugins/modules/ceph_crush_rule.py +++ b/plugins/modules/ceph_crush_rule.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module, \ generate_cmd, \ is_containerized, \ exec_command diff --git a/plugins/modules/ceph_dashboard_user.py b/plugins/modules/ceph_dashboard_user.py index 89fdaff..037a3b9 100644 --- a/plugins/modules/ceph_dashboard_user.py +++ b/plugins/modules/ceph_dashboard_user.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import generate_cmd, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import generate_cmd, \ is_containerized, \ exec_command, \ exit_module, \ diff --git a/plugins/modules/ceph_ec_profile.py b/plugins/modules/ceph_ec_profile.py index 778a7a7..a854560 100644 --- a/plugins/modules/ceph_ec_profile.py +++ b/plugins/modules/ceph_ec_profile.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import is_containerized, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import is_containerized, \ generate_cmd, \ exec_command, \ exit_module diff --git a/plugins/modules/ceph_fs.py b/plugins/modules/ceph_fs.py index 10db2b1..ea84b31 100644 --- a/plugins/modules/ceph_fs.py +++ b/plugins/modules/ceph_fs.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import is_containerized, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import is_containerized, \ exec_command, \ generate_cmd, \ exit_module diff --git a/plugins/modules/ceph_key.py b/plugins/modules/ceph_key.py index 02bed85..12118ce 100644 --- a/plugins/modules/ceph_key.py +++ b/plugins/modules/ceph_key.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import generate_cmd, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import generate_cmd, \ is_containerized, \ container_exec, \ fatal diff --git a/plugins/modules/ceph_mgr_module.py b/plugins/modules/ceph_mgr_module.py index 0c2d177..5fd5f4b 100644 --- a/plugins/modules/ceph_mgr_module.py +++ b/plugins/modules/ceph_mgr_module.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module, \ generate_cmd, \ is_containerized except ImportError: diff --git a/plugins/modules/ceph_osd.py b/plugins/modules/ceph_osd.py index 0eadc93..65a07f9 100644 --- a/plugins/modules/ceph_osd.py +++ b/plugins/modules/ceph_osd.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module, generate_cmd, is_containerized # noqa: E501 + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module, generate_cmd, is_containerized # noqa: E501 except ImportError: from module_utils.ca_common import exit_module, generate_cmd, is_containerized # noqa: E501 import datetime diff --git a/plugins/modules/ceph_osd_flag.py b/plugins/modules/ceph_osd_flag.py index ef21eaf..9b5d030 100644 --- a/plugins/modules/ceph_osd_flag.py +++ b/plugins/modules/ceph_osd_flag.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module, \ generate_cmd, \ is_containerized except ImportError: diff --git a/plugins/modules/ceph_pool.py b/plugins/modules/ceph_pool.py index 96e66dd..ec9670f 100644 --- a/plugins/modules/ceph_pool.py +++ b/plugins/modules/ceph_pool.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import generate_cmd, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import generate_cmd, \ pre_generate_cmd, \ is_containerized, \ exec_command, \ diff --git a/plugins/modules/ceph_volume.py b/plugins/modules/ceph_volume.py index ade0d55..afa1bfc 100644 --- a/plugins/modules/ceph_volume.py +++ b/plugins/modules/ceph_volume.py @@ -2,7 +2,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exec_command, \ + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exec_command, \ is_containerized, \ fatal except ImportError: diff --git a/plugins/modules/ceph_volume_simple_activate.py b/plugins/modules/ceph_volume_simple_activate.py index 4825f0c..f3e131b 100644 --- a/plugins/modules/ceph_volume_simple_activate.py +++ b/plugins/modules/ceph_volume_simple_activate.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module except ImportError: from module_utils.ca_common import exit_module import datetime diff --git a/plugins/modules/ceph_volume_simple_scan.py b/plugins/modules/ceph_volume_simple_scan.py index e76f279..06ed70e 100644 --- a/plugins/modules/ceph_volume_simple_scan.py +++ b/plugins/modules/ceph_volume_simple_scan.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module except ImportError: from module_utils.ca_common import exit_module import datetime diff --git a/plugins/modules/cephadm_adopt.py b/plugins/modules/cephadm_adopt.py index 723e0a4..2f87f34 100644 --- a/plugins/modules/cephadm_adopt.py +++ b/plugins/modules/cephadm_adopt.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import exit_module + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import exit_module except ImportError: from module_utils.ca_common import exit_module import datetime diff --git a/plugins/modules/radosgw_caps.py b/plugins/modules/radosgw_caps.py index 1d5f6b4..dac20b8 100644 --- a/plugins/modules/radosgw_caps.py +++ b/plugins/modules/radosgw_caps.py @@ -19,7 +19,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import ( + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import ( exit_module, exec_command, is_containerized, diff --git a/plugins/modules/radosgw_zone.py b/plugins/modules/radosgw_zone.py index 8ddab13..bc38769 100644 --- a/plugins/modules/radosgw_zone.py +++ b/plugins/modules/radosgw_zone.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import fatal + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import fatal except ImportError: from module_utils.ca_common import fatal import datetime diff --git a/plugins/modules/radosgw_zonegroup.py b/plugins/modules/radosgw_zonegroup.py index 533d58f..dc99195 100644 --- a/plugins/modules/radosgw_zonegroup.py +++ b/plugins/modules/radosgw_zonegroup.py @@ -17,7 +17,7 @@ from ansible.module_utils.basic import AnsibleModule try: - from ansible.module_utils.ca_common import fatal + from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import fatal except ImportError: from module_utils.ca_common import fatal import datetime