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_image
|
|
|
|
short_description: Manage the container images on the atomic host platform
|
|
|
|
description:
|
|
|
|
- Manage the container images on the atomic host platform.
|
|
|
|
- Allows to execute the commands specified by the RUN label in the container image when present.
|
|
|
|
author:
|
|
|
|
- Saravanan KR (@krsacme)
|
|
|
|
notes:
|
|
|
|
- Host should support C(atomic) command.
|
|
|
|
requirements:
|
|
|
|
- atomic
|
|
|
|
- python >= 2.6
|
|
|
|
options:
|
|
|
|
backend:
|
|
|
|
description:
|
|
|
|
- Define the backend where the image is pulled.
|
2020-07-31 10:57:57 +02:00
|
|
|
choices: [ 'docker', 'ostree' ]
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the container image.
|
|
|
|
required: True
|
2020-07-31 10:57:57 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The state of the container image.
|
|
|
|
- The state C(latest) will ensure container image is upgraded to the latest version and forcefully restart container, if running.
|
2020-07-31 10:57:57 +02:00
|
|
|
choices: [ 'absent', 'latest', 'present' ]
|
|
|
|
default: 'latest'
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
started:
|
|
|
|
description:
|
|
|
|
- Start or Stop the container.
|
|
|
|
type: bool
|
|
|
|
default: 'yes'
|
|
|
|
'''
|
|
|
|
|
2020-07-31 10:57:57 +02:00
|
|
|
EXAMPLES = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: Execute the run command on rsyslog container image (atomic run rhel7/rsyslog)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.atomic_image:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: rhel7/rsyslog
|
|
|
|
state: latest
|
|
|
|
|
|
|
|
- name: Pull busybox to the OSTree backend
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.atomic_image:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: busybox
|
|
|
|
state: latest
|
|
|
|
backend: ostree
|
|
|
|
'''
|
|
|
|
|
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 traceback
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-27 09:39:18 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def do_upgrade(module, image):
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
|
|
|
args = [atomic_bin, 'update', '--force', image]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc != 0: # something went wrong emit the msg
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
elif 'Image is up to date' in out:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def core(module):
|
|
|
|
image = module.params['name']
|
|
|
|
state = module.params['state']
|
|
|
|
started = module.params['started']
|
|
|
|
backend = module.params['backend']
|
|
|
|
is_upgraded = False
|
|
|
|
|
|
|
|
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
|
2021-04-02 22:06:07 +02:00
|
|
|
atomic_bin = module.get_bin_path('atomic')
|
2020-03-09 10:11:07 +01:00
|
|
|
out = {}
|
|
|
|
err = {}
|
|
|
|
rc = 0
|
|
|
|
|
|
|
|
if backend:
|
|
|
|
if state == 'present' or state == 'latest':
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'pull', "--storage=%s" % backend, 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:
|
|
|
|
out_run = ""
|
|
|
|
if started:
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'run', "--storage=%s" % backend, image]
|
2020-03-09 10:11:07 +01:00
|
|
|
rc, out_run, err = module.run_command(args, check_rc=False)
|
|
|
|
if rc < 0:
|
|
|
|
module.fail_json(rc=rc, msg=err)
|
|
|
|
|
|
|
|
changed = "Extracting" in out or "Copying blob" in out
|
|
|
|
module.exit_json(msg=(out + out_run), changed=changed)
|
|
|
|
elif state == 'absent':
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'images', 'delete', "--storage=%s" % backend, 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 = "Unable to find" not in out
|
|
|
|
module.exit_json(msg=out, changed=changed)
|
|
|
|
return
|
|
|
|
|
|
|
|
if state == 'present' or state == 'latest':
|
|
|
|
if state == 'latest':
|
|
|
|
is_upgraded = do_upgrade(module, image)
|
|
|
|
|
|
|
|
if started:
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'run', image]
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'install', image]
|
2020-03-09 10:11:07 +01:00
|
|
|
elif state == 'absent':
|
2021-04-02 22:06:07 +02:00
|
|
|
args = [atomic_bin, 'uninstall', 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)
|
|
|
|
elif rc == 1 and 'already present' in err:
|
|
|
|
module.exit_json(restult=err, changed=is_upgraded)
|
|
|
|
elif started and 'Container is running' in out:
|
|
|
|
module.exit_json(result=out, changed=is_upgraded)
|
|
|
|
else:
|
|
|
|
module.exit_json(msg=out, changed=True)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
backend=dict(type='str', choices=['docker', 'ostree']),
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
state=dict(type='str', default='latest', choices=['absent', 'latest', 'present']),
|
|
|
|
started=dict(type='bool', default=True),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|