From e03f6bb595f97ac62e7a66d94dbc53484555d906 Mon Sep 17 00:00:00 2001 From: Rabi Mishra Date: Mon, 17 Aug 2020 11:56:42 +0530 Subject: [PATCH] Add new playbook for container image prepare cli This adds a new playbook for container image prepare command which will be used in tripleoclient and can also be used elsewhere. Change-Id: I8a1b9794723627bd458b5354155787bb1a2e26bc --- ...modules-tripleo_build_heat_environment.rst | 14 +++ .../modules/tripleo_build_heat_environment.py | 91 +++++++++++++++++++ .../tripleo_container_image_prepare.py | 2 + .../cli-container-image-prepare.yaml | 72 +++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 doc/source/modules/modules-tripleo_build_heat_environment.rst create mode 100644 tripleo_ansible/ansible_plugins/modules/tripleo_build_heat_environment.py create mode 100644 tripleo_ansible/playbooks/cli-container-image-prepare.yaml diff --git a/doc/source/modules/modules-tripleo_build_heat_environment.rst b/doc/source/modules/modules-tripleo_build_heat_environment.rst new file mode 100644 index 000000000..55a5bbba5 --- /dev/null +++ b/doc/source/modules/modules-tripleo_build_heat_environment.rst @@ -0,0 +1,14 @@ +======================================= +Module - tripleo_build_heat_environment +======================================= + + +This module provides for the following ansible plugin: + + * tripleo_build_heat_environment + + +.. ansibleautoplugin:: + :module: tripleo_ansible/ansible_plugins/modules/tripleo_build_heat_environment.py + :documentation: true + :examples: true diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_build_heat_environment.py b/tripleo_ansible/ansible_plugins/modules/tripleo_build_heat_environment.py new file mode 100644 index 000000000..4c740731b --- /dev/null +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_build_heat_environment.py @@ -0,0 +1,91 @@ +# 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 + +from ansible.module_utils import tripleo_common_utils as tc +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 ansible.module_utils.openstack import openstack_cloud_from_module + +from heatclient.common import template_utils + +DOCUMENTATION = """ +--- +module: tripleo_build_heat_environment +short_description: Build heat stack environment +author: + - "Rabi Mishra (@ramishra)" +version_added: "2.10" +description: + - Builds heat stack environment from environment files. +options: + container: + description: + - Name of plan / container + type: str + env_files: + description: + - List of environment files and directories + type: list + default: [] +requirements: ["tripleo-common"] +""" + +EXAMPLES = """ +- name: Build heat environment + tripleo_build_heat_environment: + container: overcloud + env_files: [] +""" + + +def main(): + result = dict( + success=False, + changed=False, + error=None, + environment={} + ) + module = AnsibleModule( + openstack_full_argument_spec( + **yaml.safe_load(DOCUMENTATION)['options'] + ), + **openstack_module_kwargs() + ) + container = module.params.get('container') + env_files = module.params.get('env_files') + try: + if container: + _, conn = openstack_cloud_from_module(module) + tripleo = tc.TripleOCommon(session=conn.session) + heat = tripleo.get_orchestration_client() + env = heat.environment(container) + else: + _, env = template_utils.process_multiple_environments_and_files( + env_paths=env_files) + result['environment'] = env + result['changed'] = True + result['success'] = True + except Exception as ex: + result['error'] = str(ex) + result['msg'] = 'Error buiding environment: {}'.format( + ex) + module.fail_json(**result) + + module.exit_json(**result) + + +if __name__ == "__main__": + main() diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py b/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py index fca3413c7..4a75a1ab0 100644 --- a/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py @@ -116,6 +116,7 @@ def run_module(): success=False, changed=False, error="", + params={} ) argument_spec = openstack_full_argument_spec( @@ -153,6 +154,7 @@ def run_module(): result['success'] = True result['changed'] = True + result['params'] = {"parameter_defaults": params} except Exception as err: result['error'] = str(err) result['msg'] = ("Error running container image prepare: %s" % (err)) diff --git a/tripleo_ansible/playbooks/cli-container-image-prepare.yaml b/tripleo_ansible/playbooks/cli-container-image-prepare.yaml new file mode 100644 index 000000000..46faf6a58 --- /dev/null +++ b/tripleo_ansible/playbooks/cli-container-image-prepare.yaml @@ -0,0 +1,72 @@ +--- +# Copyright 2019 Red Hat, Inc. +# 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. + +- name: Container Image Prepare + connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}" + hosts: "{{ tripleo_target_host | default('localhost') }}" + remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}" + gather_facts: "{{ (tripleo_target_host is defined) | ternary(true, false) }}" + any_errors_fatal: true + vars: + roles_file: /usr/share/openstack-tripleo-heat-templates/roles_data.yaml + environment_files: [] + environment_directories: [] + cleanup: full + dry_run: false + debug: false + log_file: container_image_prepare.log + + tasks: + - name: Fetch roles_data + slurp: + src: "{{ roles_file }}" + register: roles_data + - name: Set fact for log file + set_fact: + cip_log_file: "{{ lookup('env', 'HOME') ~ '/' ~ log_file }}" + - name: Get all files in directories + find: + paths: "{{ environment_directories }}" + patterns: '*.yaml' + when: environment_directories + register: out_env_files + - name: Build environment_files + set_fact: + env_files: "{{ out_env_files.files | map(attribute='path') | list + environment_files }}" + - name: Build heat stack environment + tripleo_build_heat_environment: + env_files: "{{ env_files }}" + register: env_result + - name: Prepare container images, check logs in {{ cip_log_file }} + become: true + tripleo_container_image_prepare: + roles_data: "{{ roles_data['content'] | b64decode | from_yaml}}" + environment: "{{ env_result.environment }}" + cleanup: "{{ cleanup }}" + dry_run: "{{ dry_run }}" + log_file: "{{ cip_log_file }}" + debug: "{{ debug }}" + register: prepare_result + - name: Write output to file to {{ output_env_file }} + copy: + content: "{{ prepare_result.params | to_nice_yaml }}" + backup: true + dest: "{{ output_env_file }}" + when: output_env_file is defined + - name: Write output to console + debug: + msg: "{{ prepare_result.params | to_nice_yaml }}" + when: output_env_file is not defined