Skip to content

Commit 390f8cf

Browse files
committed
POC add option to run with podman-py API
Add Podman API support for podman_container and podman_container_info modules using podman-py module.
1 parent 559c4e2 commit 390f8cf

10 files changed

Lines changed: 743 additions & 55 deletions

File tree

.github/workflows/podman_container.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ jobs:
170170
ansible --version
171171
python3 -m pip install --user --force-reinstall --upgrade .
172172
173-
174173
- name: Run collection tests for podman container
175174
run: |
176175
export PATH=~/.local/bin:$PATH
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Podman API container
2+
3+
on:
4+
push:
5+
paths:
6+
- '.github/workflows/podman_container_api.yml'
7+
- 'ci/*.yml'
8+
- 'ci/run_containers_tests.sh'
9+
- 'ci/playbooks/containers/podman_container_api.yml'
10+
#- 'plugins/modules/podman_container.py'
11+
- 'plugins/module_utils/podman/podman_container_lib.py'
12+
- 'plugins/module_utils/podman/podman_api.py'
13+
- 'plugins/module_utils/podman/common.py'
14+
- 'tests/integration/targets/podman_container_api/**'
15+
branches:
16+
- master
17+
pull_request:
18+
paths:
19+
- '.github/workflows/podman_container_api.yml'
20+
- 'ci/*.yml'
21+
- 'ci/run_containers_tests.sh'
22+
- 'ci/playbooks/containers/podman_container_api.yml'
23+
#- 'plugins/modules/podman_container.py'
24+
- 'plugins/module_utils/podman/podman_container_lib.py'
25+
- 'plugins/module_utils/podman/podman_api.py'
26+
- 'plugins/module_utils/podman/common.py'
27+
- 'tests/integration/targets/podman_container_api/**'
28+
schedule:
29+
- cron: 4 0 * * * # Run daily at 0:03 UTC
30+
31+
jobs:
32+
33+
test_podman_container_api:
34+
name: Podman API container ${{ matrix.ansible-version }}-${{ matrix.os || 'ubuntu-latest' }}
35+
runs-on: ${{ matrix.os || 'ubuntu-latest' }}
36+
defaults:
37+
run:
38+
shell: bash
39+
strategy:
40+
fail-fast: false
41+
matrix:
42+
ansible-version:
43+
- ansible<2.10
44+
- git+https://github.com/ansible/ansible.git@stable-2.11
45+
- git+https://github.com/ansible/ansible.git@devel
46+
os:
47+
- ubuntu-20.04
48+
python-version:
49+
- 3.7
50+
51+
steps:
52+
53+
- name: Check out repository
54+
uses: actions/checkout@v2
55+
56+
- name: Set up Python ${{ matrix.python-version }}
57+
uses: actions/setup-python@v2
58+
with:
59+
python-version: ${{ matrix.python-version }}
60+
61+
- name: Upgrade pip and display Python and PIP versions
62+
run: |
63+
sudo apt-get update
64+
sudo apt-get install -y python*-wheel python*-yaml
65+
python -m pip install --upgrade pip
66+
python -V
67+
pip --version
68+
69+
- name: Set up pip cache
70+
uses: actions/cache@v1
71+
with:
72+
path: ~/.cache/pip
73+
key: ${{ runner.os }}-pip-${{ github.ref }}-units-VMs
74+
restore-keys: |
75+
${{ runner.os }}-pip-
76+
${{ runner.os }}-
77+
78+
- name: Install Ansible ${{ matrix.ansible-version }}
79+
run: python3 -m pip install --user --force-reinstall --upgrade '${{ matrix.ansible-version }}'
80+
81+
- name: Build and install the collection tarball
82+
run: |
83+
export PATH=~/.local/bin:$PATH
84+
85+
echo "Run ansible version"
86+
command -v ansible
87+
ansible --version
88+
rm -rf /tmp/just_new_collection
89+
~/.local/bin/ansible-galaxy collection build --output-path /tmp/just_new_collection --force
90+
~/.local/bin/ansible-galaxy collection install -vvv --force /tmp/just_new_collection/*.tar.gz
91+
92+
- name: Run collection tests for podman container API
93+
run: |
94+
export PATH=~/.local/bin:$PATH
95+
96+
if [[ '${{ matrix.ansible-version }}' == 'git+https://github.com/ansible/ansible.git@devel' ]]; then
97+
export ANSIBLE_CONFIG=$(pwd)/ci/ansible-dev.cfg
98+
elif [[ '${{ matrix.ansible-version }}' == 'ansible<2.10' ]]; then
99+
export ANSIBLE_CONFIG=$(pwd)/ci/ansible-2.9.cfg
100+
fi
101+
python3 -m pip install --user requests
102+
podman system service --time=0 unix:///tmp/podman.sock &
103+
104+
echo $ANSIBLE_CONFIG
105+
command -v ansible-playbook
106+
pip --version
107+
python --version
108+
ansible-playbook --version
109+
110+
ansible-playbook -vv ci/playbooks/pre.yml \
111+
-e host=localhost \
112+
-i localhost, \
113+
-e ansible_connection=local \
114+
-e setup_python=false
115+
116+
TEST2RUN=podman_container_api ./ci/run_containers_tests.sh
117+
shell: bash
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- hosts: all
3+
gather_facts: true
4+
module_defaults:
5+
containers.podman.podman_container:
6+
podman_socket: unix:///tmp/podman.sock
7+
tasks:
8+
- include_role:
9+
name: podman_container_api
10+
vars:
11+
idem_image: idempotency_test
12+
ansible_python_interpreter: "{{ _ansible_python_interpreter }}"

plugins/module_utils/podman/common.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
from __future__ import absolute_import, division, print_function
55
__metaclass__ = type
66

7+
try:
8+
import requests
9+
from ansible_collections.containers.podman.plugins.module_utils.podman.podman_api import PodmanAPIClient
10+
HAS_REQUESTS = True
11+
except ImportError:
12+
PodmanAPIClient = object
13+
HAS_REQUESTS = False
14+
715

816
def run_podman_command(module, executable='podman', args=None, expected_rc=0, ignore_errors=False):
917
if not isinstance(executable, list):
@@ -25,3 +33,15 @@ def lower_keys(x):
2533
return dict((k.lower(), lower_keys(v)) for k, v in x.items())
2634
else:
2735
return x
36+
37+
38+
class PodmanAPI:
39+
def __init__(self, module):
40+
if module.params.get('podman_socket') and not HAS_REQUESTS:
41+
module.fail_json(
42+
"Requests module is not installed while socket was provided!")
43+
self.client = PodmanAPIClient(module.params.get('podman_socket'))
44+
try:
45+
self.client.version()
46+
except Exception as api_error:
47+
module.fail_json("Podman API error: %s" % str(api_error))

0 commit comments

Comments
 (0)