From 5198b726fb5c243755777db0894629b039082cc4 Mon Sep 17 00:00:00 2001 From: Sergey <6213510+sshnaidm@users.noreply.github.com> Date: Thu, 10 Nov 2022 10:13:45 -0500 Subject: [PATCH] Delete systemd files when container/pod is deleted (#508) Fix #489 Signed-off-by: Sagi Shnaidman Signed-off-by: Sagi Shnaidman --- plugins/module_utils/podman/common.py | 35 +++++++++++++++++-- .../podman/podman_container_lib.py | 5 +++ plugins/module_utils/podman/podman_pod_lib.py | 6 ++++ .../targets/podman_container/tasks/main.yml | 28 +++++++++++++++ .../targets/podman_pod/tasks/main.yml | 26 +++++++++++++- 5 files changed, 97 insertions(+), 3 deletions(-) diff --git a/plugins/module_utils/podman/common.py b/plugins/module_utils/podman/common.py index 588110a..a8f901e 100644 --- a/plugins/module_utils/podman/common.py +++ b/plugins/module_utils/podman/common.py @@ -34,12 +34,11 @@ def run_podman_command(module, executable='podman', args=None, expected_rc=0, ig return rc, out, err -def generate_systemd(module, module_params, name, version): +def run_generate_systemd_command(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 [ @@ -95,6 +94,13 @@ def generate_systemd(module, module_params, name, version): module.log("PODMAN-CONTAINER-DEBUG: systemd command: %s" % " ".join(command)) rc, systemd, err = module.run_command(command) + return rc, systemd, err + + +def generate_systemd(module, module_params, name, version): + empty = {} + sysconf = module_params['generate_systemd'] + rc, systemd, err = run_generate_systemd_command(module, module_params, name, version) if rc != 0: module.log( "PODMAN-CONTAINER-DEBUG: Error generating systemd: %s" % err) @@ -120,6 +126,31 @@ def generate_systemd(module, module_params, name, version): return empty +def delete_systemd(module, module_params, name, version): + sysconf = module_params['generate_systemd'] + if not sysconf.get('path'): + # We don't know where systemd files are located, nothing to delete + module.log( + "PODMAN-CONTAINER-DEBUG: Not deleting systemd file - no path!") + return + rc, systemd, err = run_generate_systemd_command(module, module_params, name, version) + if rc != 0: + module.log( + "PODMAN-CONTAINER-DEBUG: Error generating systemd: %s" % err) + return + else: + try: + data = json.loads(systemd) + for file_name in data.keys(): + file_name += ".service" + os.unlink(os.path.join(sysconf['path'], file_name)) + return + except Exception as e: + module.log( + "PODMAN-CONTAINER-DEBUG: Error deleting systemd: %s" % e) + return + + def lower_keys(x): if isinstance(x, list): return [lower_keys(v) for v in x] diff --git a/plugins/module_utils/podman/podman_container_lib.py b/plugins/module_utils/podman/podman_container_lib.py index b9a85e7..b1f161d 100644 --- a/plugins/module_utils/podman/podman_container_lib.py +++ b/plugins/module_utils/podman/podman_container_lib.py @@ -7,6 +7,7 @@ from ansible.module_utils._text import to_bytes, to_native # noqa: F402 from ansible_collections.containers.podman.plugins.module_utils.podman.common import LooseVersion from ansible_collections.containers.podman.plugins.module_utils.podman.common import lower_keys from ansible_collections.containers.podman.plugins.module_utils.podman.common import generate_systemd +from ansible_collections.containers.podman.plugins.module_utils.podman.common import delete_systemd from ansible_collections.containers.podman.plugins.module_utils.podman.common import normalize_signal __metaclass__ = type @@ -1659,6 +1660,10 @@ class PodmanManager: if not self.container.exists: self.results.update({'changed': False}) elif self.container.exists: + delete_systemd(self.module, + self.module_params, + self.name, + self.container.version) self.container.delete() self.results['actions'].append('deleted %s' % self.container.name) self.results.update({'changed': True}) diff --git a/plugins/module_utils/podman/podman_pod_lib.py b/plugins/module_utils/podman/podman_pod_lib.py index 9c8977e..4d7bdcb 100644 --- a/plugins/module_utils/podman/podman_pod_lib.py +++ b/plugins/module_utils/podman/podman_pod_lib.py @@ -5,6 +5,8 @@ from ansible.module_utils._text import to_bytes, to_native from ansible_collections.containers.podman.plugins.module_utils.podman.common import LooseVersion from ansible_collections.containers.podman.plugins.module_utils.podman.common import lower_keys from ansible_collections.containers.podman.plugins.module_utils.podman.common import generate_systemd +from ansible_collections.containers.podman.plugins.module_utils.podman.common import delete_systemd + __metaclass__ = type @@ -852,6 +854,10 @@ class PodmanPodManager: if not self.pod.exists: self.results.update({'changed': False}) elif self.pod.exists: + delete_systemd(self.module, + self.module_params, + self.name, + self.pod.version) self.pod.delete() self.results['actions'].append('deleted %s' % self.pod.name) self.results.update({'changed': True}) diff --git a/tests/integration/targets/podman_container/tasks/main.yml b/tests/integration/targets/podman_container/tasks/main.yml index a448914..992a279 100644 --- a/tests/integration/targets/podman_container/tasks/main.yml +++ b/tests/integration/targets/podman_container/tasks/main.yml @@ -613,6 +613,34 @@ - "'Restart=always' in system1.podman_systemd.values() | list | first" - "'autogenerated by Podman' not in system1.podman_systemd.values() | list | first" + - name: Delete container with systemd generation parameters + containers.podman.podman_container: + executable: "{{ test_executable | default('podman') }}" + name: container1 + image: alpine + state: absent + command: sleep 20m + generate_systemd: + path: /tmp/ + restart_policy: always + time: 120 + no_header: true + names: true + pod_prefix: whocares + separator: zzzz + container_prefix: contain + register: system1 + + - name: Check service file doesn't present + stat: + path: /tmp/containzzzzcontainer1.service + register: service2_file + + - name: Check that service file was deleted + assert: + that: + - not service2_file.stat.exists | bool + - name: Create temporary rootfs directory ansible.builtin.tempfile: state: directory diff --git a/tests/integration/targets/podman_pod/tasks/main.yml b/tests/integration/targets/podman_pod/tasks/main.yml index 000191e..990f727 100644 --- a/tests/integration/targets/podman_pod/tasks/main.yml +++ b/tests/integration/targets/podman_pod/tasks/main.yml @@ -720,6 +720,11 @@ container_prefix: ainer register: system1 + - name: Check systemd file exists + stat: + path: /tmp/dir1/poditto-pod2.service + register: podsys_stat + - name: Check that all settings from systemd are correct assert: that: @@ -727,12 +732,31 @@ - "'stop -t 120 ' in system1.podman_systemd['poditto-pod2']" - "'Restart=always' in system1.podman_systemd['poditto-pod2']" - "'autogenerated by Podman' not in system1.podman_systemd['poditto-pod2']" + - podsys_stat.stat.exists | bool - - name: Remove pod2 + - name: Delete pod2 containers.podman.podman_pod: executable: "{{ test_executable | default('podman') }}" name: pod2 state: absent + generate_systemd: + path: /tmp/dir1 + restart_policy: always + time: 120 + no_header: true + names: true + pod_prefix: poditto + container_prefix: ainer + + - name: Check if systemd file exists (should not) + stat: + path: /tmp/dir1/poditto-pod2.service + register: podsys2_stat + + - name: Check that systemd file was removed + assert: + that: + - not podsys2_stat.stat.exists|bool - name: Run pod2 with network slirp4netns containers.podman.podman_pod: