1
0
Fork 0
mirror of https://github.com/containers/ansible-podman-collections.git synced 2026-02-04 07:11:49 +00:00

generate_systemd: implement --wants, --after and --requires (GH-431) (#432)

* generate_systemd: implement --wants, --after and --requires (GH-431)

Signed-off-by: dada513 <dada513@protonmail.com>
Signed-off-by: Sagi Shnaidman <sshnaidm@redhat.com>
Co-authored-by: Sagi Shnaidman <sshnaidm@redhat.com>
This commit is contained in:
dada513 2022-09-01 19:28:07 +02:00 committed by GitHub
parent befb381194
commit b72a026328
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 3 deletions

View file

@ -34,12 +34,13 @@ def run_podman_command(module, executable='podman', args=None, expected_rc=0, ig
return rc, out, err
def generate_systemd(module, module_params, name):
def generate_systemd(module, module_params, name, version):
"""Generate systemd unit file."""
command = [module_params['executable'], 'generate', 'systemd',
name, '--format', 'json']
sysconf = module_params['generate_systemd']
empty = {}
gt4ver = LooseVersion(version) >= LooseVersion('4.0.0')
if sysconf.get('restart_policy'):
if sysconf.get('restart_policy') not in [
"no", "on-success", "on-failure", "on-abnormal", "on-watchdog",
@ -65,6 +66,31 @@ def generate_systemd(module, module_params, name):
command.extend(['--pod-prefix=%s' % sysconf['pod_prefix']])
if sysconf.get('separator') is not None:
command.extend(['--separator=%s' % sysconf['separator']])
if sysconf.get('after') is not None:
sys_after = sysconf['after']
if isinstance(sys_after, str):
sys_after = [sys_after]
for after in sys_after:
command.extend(['--after=%s' % after])
if sysconf.get('wants') is not None:
sys_wants = sysconf['wants']
if isinstance(sys_wants, str):
sys_wants = [sys_wants]
for want in sys_wants:
command.extend(['--wants=%s' % want])
if sysconf.get('requires') is not None:
sys_req = sysconf['requires']
if isinstance(sys_req, str):
sys_req = [sys_req]
for require in sys_req:
command.extend(['--requires=%s' % require])
for param in ['after', 'wants', 'requires']:
if sysconf.get(param) is not None and not gt4ver:
module.fail_json(msg="Systemd parameter '%s' is supported from "
"podman version 4 only! Current version is %s" % (
param, version))
if module.params['debug'] or module_params['debug']:
module.log("PODMAN-CONTAINER-DEBUG: systemd command: %s" %
" ".join(command))

View file

@ -1522,7 +1522,10 @@ class PodmanManager:
if self.module.params['debug'] or self.module_params['debug']:
self.results.update({'podman_version': self.container.version})
self.results.update(
{'podman_systemd': generate_systemd(self.module, self.module_params, self.name)})
{'podman_systemd': generate_systemd(self.module,
self.module_params,
self.name,
self.container.version)})
def make_started(self):
"""Run actions if desired state is 'started'."""

View file

@ -726,7 +726,10 @@ class PodmanPodManager:
if self.module.params['debug'] or self.module_params['debug']:
self.results.update({'podman_version': self.pod.version})
self.results.update(
{'podman_systemd': generate_systemd(self.module, self.module_params, self.name)})
{'podman_systemd': generate_systemd(self.module,
self.module_params,
self.name,
self.pod.version)})
def execute(self):
"""Execute the desired action according to map of actions & states."""

View file

@ -358,6 +358,24 @@ options:
Refer to podman-generate-systemd(1) for more information.
type: bool
default: false
after:
type: list
elements: str
required: false
description:
- Add the systemd unit after (After=) option, that ordering dependencies between the list of dependencies and this service.
wants:
type: list
elements: str
required: false
description:
- Add the systemd unit wants (Wants=) option, that this service is (weak) dependent on.
requires:
type: list
elements: str
required: false
description:
- Set the systemd unit requires (Requires=) option. Similar to wants, but declares a stronger requirement dependency.
gidmap:
description:
- Run the container in a new user namespace using the supplied mapping.

View file

@ -163,6 +163,24 @@ options:
Refer to podman-generate-systemd(1) for more information.
type: bool
default: false
after:
type: list
elements: str
required: false
description:
- Add the systemd unit after (After=) option, that ordering dependencies between the list of dependencies and this service.
wants:
type: list
elements: str
required: false
description:
- Add the systemd unit wants (Wants=) option, that this service is (weak) dependent on.
requires:
type: list
elements: str
required: false
description:
- Set the systemd unit requires (Requires=) option. Similar to wants, but declares a stronger requirement dependency.
gidmap:
description:
- GID map for the user namespace. Using this flag will run the container with