From d171f4068d5dccdcfbf970607342d36550b011b0 Mon Sep 17 00:00:00 2001 From: Dimitri Savineau Date: Thu, 6 Aug 2020 09:48:58 -0400 Subject: [PATCH] library: add radosgw_realm module This adds radosgw_realm ansible module for replacing the command module usage with the radosgw-admin realm command. Signed-off-by: Dimitri Savineau --- library/radosgw_realm.py | 288 ++++++++++++++++++++++ roles/ceph-rgw/tasks/multisite/checks.yml | 11 - roles/ceph-rgw/tasks/multisite/master.yml | 15 +- tests/library/test_radosgw_realm.py | 105 ++++++++ 4 files changed, 402 insertions(+), 17 deletions(-) create mode 100644 library/radosgw_realm.py create mode 100644 tests/library/test_radosgw_realm.py diff --git a/library/radosgw_realm.py b/library/radosgw_realm.py new file mode 100644 index 000000000..cc05bfcd0 --- /dev/null +++ b/library/radosgw_realm.py @@ -0,0 +1,288 @@ +# Copyright 2020, Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = ''' +--- +module: radosgw_realm + +short_description: Manage RADOS Gateway Realm + +version_added: "2.8" + +description: + - Manage RADOS Gateway realm(s) creation, deletion and updates. +options: + cluster: + description: + - The ceph cluster name. + required: false + default: ceph + name: + description: + - name of the RADOS Gateway realm. + required: true + state: + description: + If 'present' is used, the module creates a realm if it doesn't + exist or update it if it already exists. + If 'absent' is used, the module will simply delete the realm. + If 'info' is used, the module will return all details about the + existing realm (json formatted). + required: false + choices: ['present', 'absent', 'info'] + default: present + default: + description: + - set the default flag on the realm. + required: false + default: false + +author: + - Dimitri Savineau +''' + +EXAMPLES = ''' +- name: create a RADOS Gateway default realm + radosgw_realm: + name: foo + default: true + +- name: get a RADOS Gateway realm information + radosgw_realm: + name: foo + state: info + +- name: delete a RADOS Gateway realm + radosgw_realm: + name: foo + state: absent +''' + +RETURN = '''# ''' + +from ansible.module_utils.basic import AnsibleModule # noqa E402 +import datetime # noqa E402 +import json # noqa E402 +import os # noqa E402 +import stat # noqa E402 +import time # noqa E402 + + +def container_exec(binary, container_image): + ''' + Build the docker CLI to run a command inside a container + ''' + + container_binary = os.getenv('CEPH_CONTAINER_BINARY') + command_exec = [container_binary, + '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=' + 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_radosgw_cmd(container_image=None): + ''' + Generate radosgw-admin prefix comaand + ''' + if container_image: + cmd = container_exec('radosgw-admin', container_image) + else: + cmd = ['radosgw-admin'] + + return cmd + + +def generate_radosgw_cmd(cluster, args, container_image=None): + ''' + Generate 'radosgw' command line to execute + ''' + + cmd = pre_generate_radosgw_cmd(container_image=container_image) + + base_cmd = [ + '--cluster', + cluster, + 'realm' + ] + + cmd.extend(base_cmd + args) + + return cmd + + +def exec_commands(module, cmd): + ''' + Execute command(s) + ''' + + rc, out, err = module.run_command(cmd) + + return rc, cmd, out, err + + +def create_realm(module, container_image=None): + ''' + Create a new realm + ''' + + cluster = module.params.get('cluster') + name = module.params.get('name') + default = module.params.get('default', False) + + args = ['create', '--rgw-realm=' + name] + + if default: + args.append('--default') + + cmd = generate_radosgw_cmd(cluster=cluster, args=args, container_image=container_image) + + return cmd + + +def get_realm(module, container_image=None): + ''' + Get existing realm + ''' + + cluster = module.params.get('cluster') + name = module.params.get('name') + + args = ['get', '--rgw-realm=' + name, '--format=json'] + + cmd = generate_radosgw_cmd(cluster=cluster, args=args, container_image=container_image) + + return cmd + + +def remove_realm(module, container_image=None): + ''' + Remove a realm + ''' + + cluster = module.params.get('cluster') + name = module.params.get('name') + + args = ['delete', '--rgw-realm=' + name] + + cmd = generate_radosgw_cmd(cluster=cluster, args=args, container_image=container_image) + + return cmd + + +def exit_module(module, out, rc, cmd, err, startd, changed=False): + 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, + ) + module.exit_json(**result) + + +def run_module(): + module_args = dict( + cluster=dict(type='str', required=False, default='ceph'), + name=dict(type='str', required=True), + state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), + default=dict(type='bool', required=False, default=False), + ) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True, + ) + + # Gather module parameters in variables + name = module.params.get('name') + state = module.params.get('state') + + if module.check_mode: + module.exit_json( + changed=False, + stdout='', + stderr='', + rc=0, + start='', + end='', + delta='', + ) + + startd = datetime.datetime.now() + changed = False + + # will return either the image name or None + container_image = is_containerized() + + if state == "present": + rc, cmd, out, err = exec_commands(module, get_realm(module, container_image=container_image)) + if rc != 0: + rc, cmd, out, err = exec_commands(module, create_realm(module, container_image=container_image)) + changed = True + + elif state == "absent": + rc, cmd, out, err = exec_commands(module, get_realm(module, container_image=container_image)) + if rc == 0: + rc, cmd, out, err = exec_commands(module, remove_realm(module, container_image=container_image)) + changed = True + else: + rc = 0 + out = "Realm {} doesn't exist".format(name) + + elif state == "info": + rc, cmd, out, err = exec_commands(module, get_realm(module, container_image=container_image)) + + exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/roles/ceph-rgw/tasks/multisite/checks.yml b/roles/ceph-rgw/tasks/multisite/checks.yml index a9fc55f63..9742d713e 100644 --- a/roles/ceph-rgw/tasks/multisite/checks.yml +++ b/roles/ceph-rgw/tasks/multisite/checks.yml @@ -1,15 +1,4 @@ --- -- name: check if the realm already exists - command: "{{ container_exec_cmd }} radosgw-admin realm get --cluster={{ cluster }} --rgw-realm={{ item }}" - delegate_to: "{{ groups[mon_group_name][0] }}" - register: realmcheck - failed_when: False - changed_when: False - check_mode: no - run_once: True - loop: "{{ realms }}" - when: realms is defined - - name: check if the zonegroup already exists command: "{{ container_exec_cmd }} radosgw-admin zonegroup get --cluster={{ cluster }} --rgw-realm={{ item.realm }} --rgw-zonegroup={{ item.zonegroup }}" delegate_to: "{{ groups[mon_group_name][0] }}" diff --git a/roles/ceph-rgw/tasks/multisite/master.yml b/roles/ceph-rgw/tasks/multisite/master.yml index 269681287..43f05fefc 100644 --- a/roles/ceph-rgw/tasks/multisite/master.yml +++ b/roles/ceph-rgw/tasks/multisite/master.yml @@ -1,13 +1,16 @@ --- - name: create the realm(s) - command: "{{ container_exec_cmd }} radosgw-admin realm create --cluster={{ cluster }} --rgw-realm={{ item.item }} {{ '--default' if realms | length == 1 else '' }}" + radosgw_realm: + name: "{{ item }}" + cluster: "{{ cluster }}" + default: "{{ true if realms | length == 1 else false }}" delegate_to: "{{ groups[mon_group_name][0] }}" run_once: true - loop: "{{ realmcheck.results }}" - when: - - realms is defined - - realms | length > 0 - - "'No such file or directory' in item.stderr" + loop: "{{ realms }}" + when: realms is defined + environment: + CEPH_CONTAINER_IMAGE: "{{ ceph_docker_registry + '/' + ceph_docker_image + ':' + ceph_docker_image_tag if containerized_deployment | bool else None }}" + CEPH_CONTAINER_BINARY: "{{ container_binary }}" - name: create zonegroup(s) command: "{{ container_exec_cmd }} radosgw-admin zonegroup create --cluster={{ cluster }} --rgw-realm={{ item.item.realm }} --rgw-zonegroup={{ item.item.zonegroup }} {{ '--default' if zonegroups | length == 1 else '' }} {{ '--master' if item.item.is_master | bool else '' }}" diff --git a/tests/library/test_radosgw_realm.py b/tests/library/test_radosgw_realm.py new file mode 100644 index 000000000..4a4e51214 --- /dev/null +++ b/tests/library/test_radosgw_realm.py @@ -0,0 +1,105 @@ +import os +import sys +from mock.mock import patch, MagicMock +import pytest +sys.path.append('./library') +import radosgw_realm # noqa: E402 + + +fake_binary = 'radosgw-admin' +fake_cluster = 'ceph' +fake_container_binary = 'podman' +fake_container_image = 'docker.io/ceph/daemon:latest' +fake_container_cmd = [ + fake_container_binary, + '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=' + fake_binary, + fake_container_image +] +fake_realm = 'foo' +fake_params = {'cluster': fake_cluster, + 'name': fake_realm, + 'default': True} + + +class TestRadosgwRealmModule(object): + + @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) + def test_container_exec(self): + cmd = radosgw_realm.container_exec(fake_binary, fake_container_image) + assert cmd == fake_container_cmd + + def test_not_is_containerized(self): + assert radosgw_realm.is_containerized() is None + + @patch.dict(os.environ, {'CEPH_CONTAINER_IMAGE': fake_container_image}) + def test_is_containerized(self): + assert radosgw_realm.is_containerized() == fake_container_image + + @pytest.mark.parametrize('image', [None, fake_container_image]) + @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) + def test_pre_generate_radosgw_cmd(self, image): + if image: + expected_cmd = fake_container_cmd + else: + expected_cmd = [fake_binary] + + assert radosgw_realm.pre_generate_radosgw_cmd(image) == expected_cmd + + @pytest.mark.parametrize('image', [None, fake_container_image]) + @patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary}) + def test_generate_radosgw_cmd(self, image): + if image: + expected_cmd = fake_container_cmd + else: + expected_cmd = [fake_binary] + + expected_cmd.extend([ + '--cluster', + fake_cluster, + 'realm' + ]) + assert radosgw_realm.generate_radosgw_cmd(fake_cluster, [], image) == expected_cmd + + def test_create_realm(self): + fake_module = MagicMock() + fake_module.params = fake_params + expected_cmd = [ + fake_binary, + '--cluster', fake_cluster, + 'realm', 'create', + '--rgw-realm=' + fake_realm, + '--default' + ] + + assert radosgw_realm.create_realm(fake_module) == expected_cmd + + def test_get_realm(self): + fake_module = MagicMock() + fake_module.params = fake_params + expected_cmd = [ + fake_binary, + '--cluster', fake_cluster, + 'realm', 'get', + '--rgw-realm=' + fake_realm, + '--format=json' + ] + + assert radosgw_realm.get_realm(fake_module) == expected_cmd + + def test_remove_realm(self): + fake_module = MagicMock() + fake_module.params = fake_params + expected_cmd = [ + fake_binary, + '--cluster', fake_cluster, + 'realm', 'delete', + '--rgw-realm=' + fake_realm + ] + + assert radosgw_realm.remove_realm(fake_module) == expected_cmd