From c342035fb6ed0efb312d94cca4d35f94f0d043b2 Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Fri, 4 Aug 2023 09:56:27 +0200 Subject: [PATCH] library: add ceph_authtool module This adds the module `ceph_authtool`. Signed-off-by: Guillaume Abrioux --- library/ceph_authtool.py | 137 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 library/ceph_authtool.py diff --git a/library/ceph_authtool.py b/library/ceph_authtool.py new file mode 100644 index 000000000..b9ff4ba0e --- /dev/null +++ b/library/ceph_authtool.py @@ -0,0 +1,137 @@ +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible.module_utils.basic import AnsibleModule +try: + from ansible.module_utils.ca_common import container_exec, \ + is_containerized, \ + fatal +except ImportError: + from module_utils.ca_common import container_exec, \ + is_containerized, \ + fatal +import datetime +import json +import os +import struct +import time +import base64 +import socket + +class KeyringExists(Exception): + pass + +def build_cmd(create_keyring=False, + gen_key=False, + add_key=False, + import_keyring=None, + caps={}, + name=None, + path=None, + container_image=None, + **a): + + auth_tool_binary: str = 'ceph-authtool' + + if container_image: + c = container_exec(auth_tool_binary, + container_image) + else: + c = [auth_tool_binary] + + + if name: + c.extend(['-n', name]) + if create_keyring: + if os.path.exists(path): + raise KeyringExists + c.append('-C') + if gen_key: + c.append('-g') + if caps: + for k, v in caps.items(): + c.extend(['--cap'] + [k] + [v]) + + c.append(path) + + if import_keyring: + c.extend(['--import-keyring', import_keyring]) + + return c + +def run_module(): + module_args = dict( + name=dict(type='str', required=False), + create_keyring=dict(type='bool', required=False, default=False), + gen_key=dict(type='bool', required=False, default=False), + add_key=dict(type='str', required=False, default=None), + import_keyring=dict(type='str', required=False, default=None), + caps=dict(type='dict', required=False, default=None), + path=dict(type='str', required=True) + ) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True, + add_file_common_args=True, + ) + + + + + cmd = [] + changed = False + + result = dict( + changed=changed, + stdout='', + stderr='', + rc=0, + start='', + end='', + delta='', + ) + + if module.check_mode: + module.exit_json(**result) + + startd = datetime.datetime.now() + + # will return either the image name or None + container_image = is_containerized() + try: + cmd = build_cmd(**module.params, container_image=container_image) + except KeyringExists: + rc = 0 + out = f"{module.params['path']} already exists. Skipping" + err = "" + else: + rc, out, err = module.run_command(cmd) + if rc == 0: + changed = True + + 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, + ) + if rc != 0: + module.fail_json(msg='non-zero return code', **result) + + #file_args = module.load_file_common_arguments(module.params) + #module.set_fs_attributes_if_different(file_args, False) + module.exit_json(**result) + +def main(): + run_module() + +if __name__ == '__main__': + main() -- 2.39.5