mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-11 14:35:06 +00:00
Initial commit
This commit is contained in:
commit
aebc1b03fd
4861 changed files with 812621 additions and 0 deletions
206
plugins/modules/cloud/atomic/atomic_container.py
Normal file
206
plugins/modules/cloud/atomic/atomic_container.py
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
#!/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
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: atomic_container
|
||||
short_description: Manage the containers on the atomic host platform
|
||||
description:
|
||||
- Manage the containers on the atomic host platform
|
||||
- Allows to manage the lifecycle of a container on the atomic host platform
|
||||
author: "Giuseppe Scrivano (@giuseppe)"
|
||||
notes:
|
||||
- Host should support C(atomic) command
|
||||
requirements:
|
||||
- atomic
|
||||
- "python >= 2.6"
|
||||
options:
|
||||
backend:
|
||||
description:
|
||||
- Define the backend to use for the container
|
||||
required: True
|
||||
choices: ["docker", "ostree"]
|
||||
name:
|
||||
description:
|
||||
- Name of the container
|
||||
required: True
|
||||
image:
|
||||
description:
|
||||
- The image to use to install the container
|
||||
required: True
|
||||
rootfs:
|
||||
description:
|
||||
- Define the rootfs of the image
|
||||
state:
|
||||
description:
|
||||
- State of the container
|
||||
required: True
|
||||
choices: ["latest", "present", "absent", "rollback"]
|
||||
default: "latest"
|
||||
mode:
|
||||
description:
|
||||
- Define if it is an user or a system container
|
||||
required: True
|
||||
choices: ["user", "system"]
|
||||
values:
|
||||
description:
|
||||
- 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.
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
|
||||
# Install the etcd system container
|
||||
- atomic_container:
|
||||
name: etcd
|
||||
image: rhel/etcd
|
||||
backend: ostree
|
||||
state: latest
|
||||
mode: system
|
||||
values:
|
||||
- ETCD_NAME=etcd.server
|
||||
|
||||
# Uninstall the etcd system container
|
||||
- atomic_container:
|
||||
name: etcd
|
||||
image: rhel/etcd
|
||||
backend: ostree
|
||||
state: absent
|
||||
mode: system
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
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
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
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 []
|
||||
args = ['atomic', 'install', "--storage=%s" % backend, '--name=%s' % container] + system_list + user_list + rootfs_list + values_list + [image]
|
||||
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):
|
||||
args = ['atomic', 'containers', 'update', "--rebase=%s" % image] + values_list + [container]
|
||||
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):
|
||||
args = ['atomic', 'uninstall', "--storage=%s" % backend, name]
|
||||
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):
|
||||
args = ['atomic', 'containers', 'rollback', name]
|
||||
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']
|
||||
|
||||
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
|
||||
out = {}
|
||||
err = {}
|
||||
rc = 0
|
||||
|
||||
values_list = ["--set=%s" % x for x in values] if values else []
|
||||
|
||||
args = ['atomic', 'containers', 'list', '--no-trunc', '-n', '--all', '-f', 'backend=%s' % backend, '-f', 'container=%s' % name]
|
||||
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(
|
||||
mode=dict(default=None, choices=['user', 'system']),
|
||||
name=dict(default=None, required=True),
|
||||
image=dict(default=None, required=True),
|
||||
rootfs=dict(default=None),
|
||||
state=dict(default='latest', choices=['present', 'absent', 'latest', 'rollback']),
|
||||
backend=dict(default=None, required=True, choices=['docker', 'ostree']),
|
||||
values=dict(type='list', default=[]),
|
||||
),
|
||||
)
|
||||
|
||||
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
|
||||
rc, out, err = module.run_command('atomic -v', check_rc=False)
|
||||
if rc != 0:
|
||||
module.fail_json(msg="Error in running atomic command", err=err)
|
||||
|
||||
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()
|
||||
103
plugins/modules/cloud/atomic/atomic_host.py
Normal file
103
plugins/modules/cloud/atomic/atomic_host.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#!/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
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: atomic_host
|
||||
short_description: Manage the atomic host platform
|
||||
description:
|
||||
- Manage the atomic host platform.
|
||||
- Rebooting of Atomic host platform should be done outside this module.
|
||||
author:
|
||||
- Saravanan KR (@krsacme)
|
||||
notes:
|
||||
- Host should be an atomic platform (verified by existence of '/run/ostree-booted' file).
|
||||
requirements:
|
||||
- atomic
|
||||
- python >= 2.6
|
||||
options:
|
||||
revision:
|
||||
description:
|
||||
- The version number of the atomic host to be deployed. Providing C(latest) will upgrade to the latest available version.
|
||||
default: latest
|
||||
aliases: [ version ]
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Upgrade the atomic host platform to the latest version (atomic host upgrade)
|
||||
atomic_host:
|
||||
revision: latest
|
||||
|
||||
- name: Deploy a specific revision as the atomic host (atomic host deploy 23.130)
|
||||
atomic_host:
|
||||
revision: 23.130
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
msg:
|
||||
description: The command standard output
|
||||
returned: always
|
||||
type: str
|
||||
sample: 'Already on latest'
|
||||
'''
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
def core(module):
|
||||
revision = module.params['revision']
|
||||
args = []
|
||||
|
||||
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
|
||||
|
||||
if revision == 'latest':
|
||||
args = ['atomic', 'host', 'upgrade']
|
||||
else:
|
||||
args = ['atomic', 'host', 'deploy', revision]
|
||||
|
||||
out = {}
|
||||
err = {}
|
||||
rc = 0
|
||||
|
||||
rc, out, err = module.run_command(args, check_rc=False)
|
||||
|
||||
if rc == 77 and revision == 'latest':
|
||||
module.exit_json(msg="Already on latest", changed=False)
|
||||
elif rc != 0:
|
||||
module.fail_json(rc=rc, msg=err)
|
||||
else:
|
||||
module.exit_json(msg=out, changed=True)
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
revision=dict(type='str', default='latest', aliases=["version"]),
|
||||
),
|
||||
)
|
||||
|
||||
# Verify that the platform is atomic host
|
||||
if not os.path.exists("/run/ostree-booted"):
|
||||
module.fail_json(msg="Module atomic_host is applicable for Atomic Host Platforms only")
|
||||
|
||||
try:
|
||||
core(module)
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
170
plugins/modules/cloud/atomic/atomic_image.py
Normal file
170
plugins/modules/cloud/atomic/atomic_image.py
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
#!/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
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
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.
|
||||
choices: [ docker, ostree ]
|
||||
name:
|
||||
description:
|
||||
- Name of the container image.
|
||||
required: True
|
||||
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.
|
||||
choices: [ absent, latest, present ]
|
||||
default: latest
|
||||
started:
|
||||
description:
|
||||
- Start or Stop the container.
|
||||
type: bool
|
||||
default: 'yes'
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Execute the run command on rsyslog container image (atomic run rhel7/rsyslog)
|
||||
atomic_image:
|
||||
name: rhel7/rsyslog
|
||||
state: latest
|
||||
|
||||
- name: Pull busybox to the OSTree backend
|
||||
atomic_image:
|
||||
name: busybox
|
||||
state: latest
|
||||
backend: ostree
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
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
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
def do_upgrade(module, image):
|
||||
args = ['atomic', 'update', '--force', image]
|
||||
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')
|
||||
out = {}
|
||||
err = {}
|
||||
rc = 0
|
||||
|
||||
if backend:
|
||||
if state == 'present' or state == 'latest':
|
||||
args = ['atomic', 'pull', "--storage=%s" % backend, image]
|
||||
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:
|
||||
args = ['atomic', 'run', "--storage=%s" % backend, image]
|
||||
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':
|
||||
args = ['atomic', 'images', 'delete', "--storage=%s" % backend, image]
|
||||
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:
|
||||
args = ['atomic', 'run', image]
|
||||
else:
|
||||
args = ['atomic', 'install', image]
|
||||
elif state == 'absent':
|
||||
args = ['atomic', 'uninstall', image]
|
||||
|
||||
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
|
||||
rc, out, err = module.run_command('atomic -v', check_rc=False)
|
||||
if rc != 0:
|
||||
module.fail_json(msg="Error in running atomic command", err=err)
|
||||
|
||||
try:
|
||||
core(module)
|
||||
except Exception as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue