From 5d9039fd96afabde3f9baef83404cf7ecf26c8f0 Mon Sep 17 00:00:00 2001 From: Sagi Shnaidman Date: Tue, 8 Aug 2023 08:08:51 +0300 Subject: [PATCH] Add unstable podman to jobs --- .github/workflows/podman_container_api.yml | 12 +++++- plugins/module_utils/podman/common.py | 2 + .../podman/podman_container_lib.py | 40 +++++++++++++++---- plugins/modules/podman_container.py | 10 ++++- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/.github/workflows/podman_container_api.yml b/.github/workflows/podman_container_api.yml index 84708d3e..97283861 100644 --- a/.github/workflows/podman_container_api.yml +++ b/.github/workflows/podman_container_api.yml @@ -31,7 +31,7 @@ on: jobs: test_podman_container_api: - name: Podman API container ${{ matrix.ansible-version }}-${{ matrix.os || 'ubuntu-22.04' }} + name: Podman API container ${{ matrix.ansible-version }}-${{ matrix.os || 'ubuntu-22.04' }}-${{ matrix.podman-version || 'unstable' }} runs-on: ${{ matrix.os || 'ubuntu-22.04' }} defaults: run: @@ -47,6 +47,13 @@ jobs: - ubuntu-22.04 python-version: - "3.10" + podman-version: + - unstable + include: + - os: ubuntu-22.04 + ansible-version: git+https://github.com/ansible/ansible.git@stable-2.15 + python-version: "3.10" + podman-version: stable steps: @@ -112,7 +119,8 @@ jobs: -e host=localhost \ -i localhost, \ -e ansible_connection=local \ - -e setup_python=false + -e setup_python=false \ + -e podman_version_ubuntu=${{ matrix.podman-version }} TEST2RUN=podman_container_api ./ci/run_containers_tests.sh shell: bash diff --git a/plugins/module_utils/podman/common.py b/plugins/module_utils/podman/common.py index 54f3b6e5..bb756b87 100644 --- a/plugins/module_utils/podman/common.py +++ b/plugins/module_utils/podman/common.py @@ -429,6 +429,8 @@ def diff_generic(params, info_config, module_arg, cmd_arg, boolean_type=False): else: before = ",".join(sorted(before)) if len(before) > 1 else before[0] return before, after + + class PodmanAPI: def __init__(self, module, module_params): if module_params.get('podman_socket') and not HAS_REQUESTS: diff --git a/plugins/module_utils/podman/podman_container_lib.py b/plugins/module_utils/podman/podman_container_lib.py index 4bee466b..f6639df3 100644 --- a/plugins/module_utils/podman/podman_container_lib.py +++ b/plugins/module_utils/podman/podman_container_lib.py @@ -9,16 +9,11 @@ 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 -<<<<<<< HEAD from ansible_collections.containers.podman.plugins.module_utils.podman.common import diff_generic from ansible_collections.containers.podman.plugins.module_utils.podman.common import createcommand from ansible_collections.containers.podman.plugins.module_utils.podman.quadlet import create_quadlet_state from ansible_collections.containers.podman.plugins.module_utils.podman.quadlet import ContainerQuadlet - -======= -from ansible_collections.containers.podman.plugins.module_utils.podman.common import normalize_signal from .common import PodmanAPI ->>>>>>> e7671a8 (Add option to run with podman-py API) __metaclass__ = type @@ -1189,6 +1184,20 @@ def _diff_update_and_compare(self, param_name, before, after): return True return False + def clean_aliases(self, params): + aliases = {} + for k, v in ARGUMENTS_SPEC_CONTAINER.items(): + if 'aliases' in v: + for alias in v['aliases']: + aliases[alias] = k + return {aliases.get(k, k): v for k, v in params.items()} + + def _get_create_command_annotation(self): + if ('annotations' in self.info['config'] + and 'ansible.podman.collection.cmd' in self.info['config']['annotations']): + return self.clean_aliases(json.loads(self.info['config']['annotations']['ansible.podman.collection.cmd'])) + return {} + def _diff_generic(self, module_arg, cmd_arg, boolean_type=False): """ Generic diff function for module arguments from CreateCommand @@ -1204,6 +1213,14 @@ def _diff_generic(self, module_arg, cmd_arg, boolean_type=False): """ info_config = self.info["config"] + if USE_API: + cmd = self._get_create_command_annotation() + if cmd: + params = self.clean_aliases(self.params) + self.module.log("PODMAN-DEBUG: cmd_arg = %s and param arg = %s" % (cmd.get(module_arg), params.get(module_arg))) + return self._diff_update_and_compare(module_arg, cmd.get(module_arg), params.get(module_arg)) + return self._diff_update_and_compare(module_arg, None, None) + before, after = diff_generic(self.params, info_config, module_arg, cmd_arg, boolean_type) return self._diff_update_and_compare(module_arg, before, after) @@ -1707,7 +1724,10 @@ def clean_volume(x): return "/" return x.replace("//", "/").rstrip("/") - before = createcommand('--volume', self.info['config']) + if USE_API: + before = self._get_create_command_annotation().get('volume') + else: + before = createcommand('--volume', self.info['config']) if before == []: before = None after = self.params['volume'] @@ -1716,7 +1736,6 @@ def clean_volume(x): [clean_volume(i) for i in v.split(":")[:2]]) for v in self.params['volume']] if before is not None: before = [":".join([clean_volume(i) for i in v.split(":")[:2]]) for v in before] - self.module.log("PODMAN Before: %s and After: %s" % (before, after)) if before is None and after is None: return self._diff_update_and_compare('volume', before, after) if after is not None: @@ -1920,6 +1939,9 @@ def _perform_action(self, action): self.client.containers.remove(self.name, force=True) elif action == 'create': new_params.pop('detach') + if not new_params.get('annotations'): + new_params['annotations'] = {} + new_params['annotations'].update({'ansible.podman.collection.cmd': json.dumps(self.module_params)}) try: container = self.client.containers.create( **new_params @@ -1927,6 +1949,10 @@ def _perform_action(self, action): except Exception as e: self.module.fail_json(msg=str(e)) elif action == 'run': + new_params.pop('detach') + if not new_params.get('annotations'): + new_params['annotations'] = {} + new_params['annotations'].update({'ansible.podman.collection.cmd': json.dumps(self.module_params)}) try: container = self.client.containers.run( **new_params diff --git a/plugins/modules/podman_container.py b/plugins/modules/podman_container.py index 4faa6e00..8cbf5c0a 100644 --- a/plugins/modules/podman_container.py +++ b/plugins/modules/podman_container.py @@ -303,8 +303,7 @@ description: - Set custom DNS search domains (Use dns_search with '' if you don't wish to set the search domain) - type: list - elements: str + type: str aliases: - dns_search_domains entrypoint: @@ -1232,6 +1231,13 @@ - | [Install] WantedBy=default.target + +- name: Run container with Podman API + containers.podman.podman_container: + name: my_api_container + image: docker.io/library/busybox:latest + state: started + podman_socket: /var/run/podman/podman.sock """ RETURN = r"""