Skip to content

Commit

Permalink
Add unstable podman to jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Sagi Shnaidman <[email protected]>
  • Loading branch information
sshnaidm committed Jun 12, 2024
1 parent ac63c58 commit 2ae7b05
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 76 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/podman_container_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/[email protected]
python-version: "3.10"
podman-version: stable

steps:

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions plugins/module_utils/podman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 33 additions & 7 deletions plugins/module_utils/podman/podman_container_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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']
Expand All @@ -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:
Expand Down Expand Up @@ -1920,13 +1939,20 @@ 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
)
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
Expand Down
7 changes: 7 additions & 0 deletions plugins/modules/podman_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,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"""
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/targets/podman_container/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@
- label=type:spc_t
- label=filetype:container_share_t
- seccomp=unconfined
podman_socket: "{{ podman_socket | default(omit) }}"

- name: Recreate container with same security_opt flags
containers.podman.podman_container:
Expand All @@ -439,6 +440,7 @@
- label=type:spc_t
- label=filetype:container_share_t
- seccomp=unconfined
podman_socket: "{{ podman_socket | default(omit) }}"
register: recreate_security_opt

- name: Check if output is correct
Expand All @@ -455,6 +457,7 @@
executable: "{{ test_executable | default('podman') }}"
name: container
state: absent
podman_socket: "{{ podman_socket | default(omit) }}"

- name: Recreate container with parameters
containers.podman.podman_container:
Expand Down Expand Up @@ -866,6 +869,7 @@
assert:
that:
- envfile.stdout == "foo\n"
when: podman_socket is not defined

- name: Create container with multiple environment variables files
containers.podman.podman_container:
Expand All @@ -890,6 +894,7 @@
assert:
that:
- envfile2.stdout == "qwerty\n"
when: podman_socket is not defined

- name: Delete container with environment variables file
containers.podman.podman_container:
Expand All @@ -903,6 +908,7 @@
state: directory
suffix: container-rootfs
register: container_tempdir

- name: Debug container_tempdir
ansible.builtin.debug:
var: container_tempdir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,73 +222,6 @@
that:
- info9 is not changed

- name: Run container with mounted /dev/fuse
containers.podman.podman_container:
executable: "{{ test_executable | default('podman') }}"
image: "{{ idem_image }}"
name: idempotency
state: started
command: 1h
device:
- /dev/fuse
podman_socket: "{{ podman_socket | default(omit) }}"
register: test15

- name: Run container with mounted /dev/fuse again
containers.podman.podman_container:
executable: "{{ test_executable | default('podman') }}"
image: "{{ idem_image }}"
name: idempotency
state: started
command: 1h
device:
- /dev/fuse
podman_socket: "{{ podman_socket | default(omit) }}"
register: test16

- name: Run container with mounted /dev/fuse:/dev/fuse
containers.podman.podman_container:
executable: "{{ test_executable | default('podman') }}"
image: "{{ idem_image }}"
name: idempotency
state: started
command: 1h
device:
- /dev/fuse:/dev/fuse
podman_socket: "{{ podman_socket | default(omit) }}"
register: test17

- name: Run container with mounted /dev/fuse third time
containers.podman.podman_container:
executable: "{{ test_executable | default('podman') }}"
image: "{{ idem_image }}"
name: idempotency
state: started
command: 1h
device:
- /dev/fuse
podman_socket: "{{ podman_socket | default(omit) }}"
register: test18

- name: Run container without mounted device
containers.podman.podman_container:
executable: "{{ test_executable | default('podman') }}"
image: "{{ idem_image }}"
name: idempotency
state: started
command: 1h
podman_socket: "{{ podman_socket | default(omit) }}"
register: test19

- name: Check info with mounted devices
assert:
that:
- test15 is changed
- test16 is not changed
- test17 is not changed
- test18 is not changed
- test19 is changed

- name: Make sure container doesn't exist
containers.podman.podman_container:
executable: "{{ test_executable | default('podman') }}"
Expand Down

0 comments on commit 2ae7b05

Please sign in to comment.