Browse Source

Merge "Add ansible module for container image prepare"

tags/1.7.0
Zuul 1 month ago
committed by Gerrit Code Review
parent
commit
2980f52f1b
4 changed files with 196 additions and 57 deletions
  1. +14
    -0
      doc/source/modules/modules-tripleo_container_image_prepare.rst
  2. +171
    -0
      tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py
  3. +1
    -0
      tripleo_ansible/roles/tripleo_container_image_prepare/molecule/default/molecule.yml
  4. +10
    -57
      tripleo_ansible/roles/tripleo_container_image_prepare/tasks/main.yml

+ 14
- 0
doc/source/modules/modules-tripleo_container_image_prepare.rst View File

@@ -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

+ 171
- 0
tripleo_ansible/ansible_plugins/modules/tripleo_container_image_prepare.py View File

@@ -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()

+ 1
- 0
tripleo_ansible/roles/tripleo_container_image_prepare/molecule/default/molecule.yml View File

@@ -36,6 +36,7 @@ provisioner:
log: true
env:
ANSIBLE_STDOUT_CALLBACK: yaml
ANSIBLE_LIBRARY: "${ANSIBLE_LIBRARY:-/usr/share/ansible/plugins/modules}"

scenario:
test_sequence:


+ 10
- 57
tripleo_ansible/roles/tripleo_container_image_prepare/tasks/main.yml View File

@@ -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

Loading…
Cancel
Save