From 93cb472162dd292784d5b41f91aff3d682669654 Mon Sep 17 00:00:00 2001 From: Rabi Mishra Date: Mon, 10 Aug 2020 11:05:52 +0530 Subject: [PATCH] Add ansible module for container image prepare This adds a new ansible module for container image prepare and changes the role to use the module instead. Change-Id: I1cfa68c74eb772ddf794c53827fd9bea1fe2e5a3 --- ...odules-tripleo_container_image_prepare.rst | 14 ++ .../tripleo_container_image_prepare.py | 171 ++++++++++++++++++ .../molecule/default/molecule.yml | 1 + .../tasks/main.yml | 67 +------ 4 files changed, 196 insertions(+), 57 deletions(-) create mode 100644 doc/source/modules/modules-tripleo_container_image_prepare.rst create mode 100644 tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py diff --git a/doc/source/modules/modules-tripleo_container_image_prepare.rst b/doc/source/modules/modules-tripleo_container_image_prepare.rst new file mode 100644 index 000000000..3c62d56ec --- /dev/null +++ b/doc/source/modules/modules-tripleo_container_image_prepare.rst @@ -0,0 +1,14 @@ +======================================== +Module - tripleo_container_image_prepare +======================================== + + +This module provides for the following ansible plugin: + + * tripleo_container_image_prepare + + +.. ansibleautoplugin:: + :module: tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py + :documentation: true + :examples: true diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py b/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py new file mode 100644 index 000000000..fca3413c7 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py @@ -0,0 +1,171 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright (c) 2018 OpenStack Foundation +# All Rights Reserved. +# +# 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. + +import yaml +import logging +import os + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.openstack import openstack_full_argument_spec +from ansible.module_utils.openstack import openstack_module_kwargs + +from tripleo_common import constants +from tripleo_common.image import image_uploader +from tripleo_common.image import kolla_builder +from tripleo_common.utils.locks import processlock + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = ''' +--- +module: tripleo_container_image_prepare + +short_description: Container Image Prepare + +version_added: "2.8" + +description: + - "Container Image Prepare." + +options: + roles_data: + description: + - Roles data to filter images + default: [] + type: list + environment: + description: + - Stack environment containing ContainerImagePrepare parameter + type: dict + default: {} + cleanup: + description: + - Cleanup behaviour + type: str + default: full + dry_run: + description: + - Flag for dry run + type: bool + default: false + log_file: + description: + - Log file + type: str + debug: + description: + - Flag to enable debug logging + type: bool + default: false +author: + - Rabi Mishra (@ramishra) +''' + +EXAMPLES = ''' +- name: Container image prepare + tripleo_container_image_prepare: + roles_data: {} + environment: {} + cleanup: full + dry_run: False +''' + + +def setup_logging(log_file, debug): + # Implements own logging + log_format = ('%(asctime)s %(process)d %(levelname)s ' + '%(name)s [ ] %(message)s') + logging.basicConfig( + datefmt='%Y-%m-%d %H:%M:%S', + format=log_format + ) + log = logging.getLogger() + if log_file: + formatter = logging.Formatter(log_format) + fh = logging.FileHandler(filename=log_file) + fh.setFormatter(formatter) + log.addHandler(fh) + if debug: + log_level = logging.DEBUG + else: + log_level = logging.INFO + log.setLevel(log_level) + return log + + +def run_module(): + result = dict( + success=False, + changed=False, + error="", + ) + + argument_spec = openstack_full_argument_spec( + **yaml.safe_load(DOCUMENTATION)['options'] + ) + + module = AnsibleModule( + argument_spec, + supports_check_mode=False, + **openstack_module_kwargs() + ) + + log_file = module.params.get('log_file') + debug = module.params.get('debug') + if not module.no_log: + log = setup_logging(log_file, debug) + + cleanup = module.params.get('cleanup') + dry_run = module.params.get('dry_run') + if cleanup not in image_uploader.CLEANUP: + raise RuntimeError('--cleanup must be one of: %s' % + ', '.join(image_uploader.CLEANUP)) + + roles_data = module.params.get('roles_data') + env = module.params.get('environment') + try: + lock = processlock.ProcessLock() + params = kolla_builder.container_images_prepare_multi( + env, roles_data, cleanup=cleanup, dry_run=dry_run, + lock=lock) + + if not module.no_log: + output = yaml.safe_dump(params, default_flow_style=False) + log.info(output) + + result['success'] = True + result['changed'] = True + except Exception as err: + result['error'] = str(err) + result['msg'] = ("Error running container image prepare: %s" % (err)) + module.fail_json(**result) + + # in the event of a successful module execution, you will want to + # simple AnsibleModule.exit_json(), passing the key/value results + module.exit_json(**result) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/tripleo_ansible/roles/tripleo_container_image_prepare/molecule/default/molecule.yml b/tripleo_ansible/roles/tripleo_container_image_prepare/molecule/default/molecule.yml index 8d3c2ab1e..0f942d4f2 100644 --- a/tripleo_ansible/roles/tripleo_container_image_prepare/molecule/default/molecule.yml +++ b/tripleo_ansible/roles/tripleo_container_image_prepare/molecule/default/molecule.yml @@ -36,6 +36,7 @@ provisioner: log: true env: ANSIBLE_STDOUT_CALLBACK: yaml + ANSIBLE_LIBRARY: "${ANSIBLE_LIBRARY:-/usr/share/ansible/plugins/modules}" scenario: test_sequence: diff --git a/tripleo_ansible/roles/tripleo_container_image_prepare/tasks/main.yml b/tripleo_ansible/roles/tripleo_container_image_prepare/tasks/main.yml index 6092db64c..38dd2dd59 100644 --- a/tripleo_ansible/roles/tripleo_container_image_prepare/tasks/main.yml +++ b/tripleo_ansible/roles/tripleo_container_image_prepare/tasks/main.yml @@ -14,63 +14,16 @@ # License for the specific language governing permissions and limitations # under the License. - -# "tripleo_container_image_prepare" will search for and load any operating system variable file - name: Container image prepare become: true - block: - - name: Create temp file for prepare parameter - tempfile: - state: file - suffix: -prepare-param - register: prepare_param - check_mode: false - - - name: Write ContainerImagePrepare parameter file - copy: - dest: "{{ prepare_param.path }}" - content: "{{ tripleo_container_image_prepare_content }}" - - - name: Create temp file for role data - tempfile: - state: file - suffix: -role-data - register: role_data - check_mode: false - - - name: Write role data file - copy: - dest: "{{ role_data.path }}" - content: "{{ tripleo_container_image_prepare_roles }}" - - - name: "Run tripleo_container_image_prepare logged to: {{ tripleo_container_image_prepare_log_file }}" - shell: >- - /usr/bin/tripleo-container-image-prepare - --roles-file {{ role_data.path }} - --environment-file {{ prepare_param.path }} - --cleanup partial - --log-file {{ tripleo_container_image_prepare_log_file }} - {% if (tripleo_container_image_prepare_debug | bool) %} - --debug - {% endif %} - >/dev/null - no_log: "{{ not tripleo_container_image_prepare_debug | bool }}" - when: - - (tripleo_container_image_prepare_content | dict2items | length) > 0 - - (tripleo_container_image_prepare_roles | length) > 0 - always: - - name: Delete param file - command: "rm -rf '{{ prepare_param.path }}'" - when: - - prepare_param is defined - - prepare_param.changed - check_mode: false - - - name: Delete role file - command: "rm -rf '{{ role_data.path }}'" - when: - - role_data is defined - - role_data.changed - check_mode: false + tripleo_container_image_prepare: + roles_data: "{{ tripleo_container_image_prepare_roles }}" + environment: "{{ tripleo_container_image_prepare_content }}" + cleanup: partial + log_file: "{{ tripleo_container_image_prepare_log_file }}" + debug: "{{ tripleo_container_image_prepare_debug | bool }}" + when: + - (tripleo_container_image_prepare_content | dict2items | length) > 0 + - (tripleo_container_image_prepare_roles | length) > 0 tags: - - container_image_prepare + - container_image_prepare