2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright: Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
2020-07-31 10:57:57 +02:00
|
|
|
DOCUMENTATION = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
---
|
|
|
|
module: atomic_container
|
|
|
|
short_description: Manage the containers on the atomic host platform
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- Manage the containers on the atomic host platform.
|
|
|
|
- Allows to manage the lifecycle of a container on the atomic host platform.
|
2020-03-09 10:11:07 +01:00
|
|
|
author: "Giuseppe Scrivano (@giuseppe)"
|
|
|
|
notes:
|
|
|
|
- Host should support C(atomic) command
|
|
|
|
requirements:
|
|
|
|
- atomic
|
|
|
|
- "python >= 2.6"
|
|
|
|
options:
|
|
|
|
backend:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- Define the backend to use for the container.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: True
|
|
|
|
choices: ["docker", "ostree"]
|
2020-07-31 10:57:57 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- Name of the container.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: True
|
2020-07-31 10:57:57 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
image:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- The image to use to install the container.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: True
|
2020-07-31 10:57:57 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
rootfs:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- Define the rootfs of the image.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- State of the container.
|
|
|
|
choices: ["absent", "latest", "present", "rollback"]
|
2020-03-09 10:11:07 +01:00
|
|
|
default: "latest"
|
2020-07-31 10:57:57 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
mode:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- Define if it is an user or a system container.
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: ["user", "system"]
|
2020-07-31 10:57:57 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
values:
|
|
|
|
description:
|
2020-07-31 10:57:57 +02:00
|
|
|
- Values for the installation of the container.
|
|
|
|
- This option is permitted only with mode 'user' or 'system'.
|
|
|
|
- The values specified here will be used at installation time as --set arguments for atomic install.
|
|
|
|
type: list
|
|
|
|
elements: str
|
2022-11-01 20:09:31 +01:00
|
|
|
default: []
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
2020-07-31 10:57:57 +02:00
|
|
|
EXAMPLES = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Install the etcd system container
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.atomic_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: etcd
|
|
|
|
image: rhel/etcd
|
|
|
|
backend: ostree
|
|
|
|
state: latest
|
|
|
|
mode: system
|
|
|
|
values:
|
|
|
|
- ETCD_NAME=etcd.server
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Uninstall the etcd system container
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.atomic_container:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: etcd
|
|
|
|
image: rhel/etcd
|
|
|
|
backend: ostree
|
|
|
|
state: absent
|
|
|
|
mode: system
|
|
|
|
'''
|
|
|
|
|
2020-07-31 10:57:57 +02:00
|
|
|
RETURN = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
msg:
|
|
|
|
description: The command standard output
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: [u'Using default tag: latest ...']
|
|
|
|
'''
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def do_install(module, mode, rootfs, container, image, values_list, backend):
|
|
|
|
system_list = ["--system"] if mode == 'system' else []
|
|
|
|
user_list = ["--user"] if mode == 'user' else []
|
|
|
|
rootfs_list = ["--rootfs=%s" % rootfs] if rootfs else []
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
|
|
|
args = [atomic_bin, 'install', "--storage=%s" % backend, '--name=%s' % container] + system_list + user_list + rootfs_list + values_list + [image]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
else:
|
|
|
|
changed = "Extracting" in out or "Copying blob" in out
|
|
|
|
module.exit_json(msg=out, changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def do_update(module, container, image, values_list):
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
|
|
|
args = [atomic_bin, 'containers', 'update', "--rebase=%s" % image] + values_list + [container]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
else:
|
|
|
|
changed = "Extracting" in out or "Copying blob" in out
|
|
|
|
module.exit_json(msg=out, changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def do_uninstall(module, name, backend):
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
|
|
|
args = [atomic_bin, 'uninstall', "--storage=%s" % backend, name]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
module.exit_json(msg=out, changed=True)
|
|
|
|
|
|
|
|
|
|
|
|
def do_rollback(module, name):
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
|
|
|
args = [atomic_bin, 'containers', 'rollback', name]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
else:
|
|
|
|
changed = "Rolling back" in out
|
|
|
|
module.exit_json(msg=out, changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def core(module):
|
|
|
|
mode = module.params['mode']
|
|
|
|
name = module.params['name']
|
|
|
|
image = module.params['image']
|
|
|
|
rootfs = module.params['rootfs']
|
|
|
|
values = module.params['values']
|
|
|
|
backend = module.params['backend']
|
|
|
|
state = module.params['state']
|
|
|
|
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
2020-03-09 10:11:07 +01:00
|
|
|
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
|
|
|
|
|
|
|
|
values_list = ["--set=%s" % x for x in values] if values else []
|
|
|
|
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'containers', 'list', '--no-trunc', '-n', '--all', '-f', 'backend=%s' % backend, '-f', 'container=%s' % name]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
return
|
|
|
|
present = name in out
|
|
|
|
|
|
|
|
if state == 'present' and present:
|
|
|
|
module.exit_json(msg=out, changed=False)
|
|
|
|
elif (state in ['latest', 'present']) and not present:
|
|
|
|
do_install(module, mode, rootfs, name, image, values_list, backend)
|
|
|
|
elif state == 'latest':
|
|
|
|
do_update(module, name, image, values_list)
|
|
|
|
elif state == 'absent':
|
|
|
|
if not present:
|
|
|
|
module.exit_json(msg="The container is not present", changed=False)
|
|
|
|
else:
|
|
|
|
do_uninstall(module, name, backend)
|
|
|
|
elif state == 'rollback':
|
|
|
|
do_rollback(module, name)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2022-04-24 10:49:29 +02:00
|
|
|
mode=dict(choices=['user', 'system']),
|
2020-07-31 10:57:57 +02:00
|
|
|
name=dict(required=True),
|
|
|
|
image=dict(required=True),
|
2022-04-24 10:49:29 +02:00
|
|
|
rootfs=dict(),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(default='latest', choices=['present', 'absent', 'latest', 'rollback']),
|
2020-07-31 10:57:57 +02:00
|
|
|
backend=dict(required=True, choices=['docker', 'ostree']),
|
|
|
|
values=dict(type='list', default=[], elements='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
if module.params['values'] is not None and module.params['mode'] == 'default':
|
|
|
|
module.fail_json(msg="values is supported only with user or system mode")
|
|
|
|
|
|
|
|
# Verify that the platform supports atomic command
|
2021-04-02 22:06:07 +02:00
|
|
|
dummy = module.get_bin_path('atomic', required=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
core(module)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='Unanticipated error running atomic: %s' % to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|