Skip to content

Commit

Permalink
POC add option to run with podman-py API
Browse files Browse the repository at this point in the history
Add Podman API support for podman_container and podman_container_info
modules using podman-py module.
  • Loading branch information
sshnaidm committed Jun 10, 2021
1 parent 559c4e2 commit 390f8cf
Show file tree
Hide file tree
Showing 10 changed files with 743 additions and 55 deletions.
1 change: 0 additions & 1 deletion .github/workflows/podman_container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ jobs:
ansible --version
python3 -m pip install --user --force-reinstall --upgrade .
- name: Run collection tests for podman container
run: |
export PATH=~/.local/bin:$PATH
Expand Down
117 changes: 117 additions & 0 deletions .github/workflows/podman_container_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: Podman API container

on:
push:
paths:
- '.github/workflows/podman_container_api.yml'
- 'ci/*.yml'
- 'ci/run_containers_tests.sh'
- 'ci/playbooks/containers/podman_container_api.yml'
#- 'plugins/modules/podman_container.py'
- 'plugins/module_utils/podman/podman_container_lib.py'
- 'plugins/module_utils/podman/podman_api.py'
- 'plugins/module_utils/podman/common.py'
- 'tests/integration/targets/podman_container_api/**'
branches:
- master
pull_request:
paths:
- '.github/workflows/podman_container_api.yml'
- 'ci/*.yml'
- 'ci/run_containers_tests.sh'
- 'ci/playbooks/containers/podman_container_api.yml'
#- 'plugins/modules/podman_container.py'
- 'plugins/module_utils/podman/podman_container_lib.py'
- 'plugins/module_utils/podman/podman_api.py'
- 'plugins/module_utils/podman/common.py'
- 'tests/integration/targets/podman_container_api/**'
schedule:
- cron: 4 0 * * * # Run daily at 0:03 UTC

jobs:

test_podman_container_api:
name: Podman API container ${{ matrix.ansible-version }}-${{ matrix.os || 'ubuntu-latest' }}
runs-on: ${{ matrix.os || 'ubuntu-latest' }}
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
ansible-version:
- ansible<2.10
- git+https://github.com/ansible/[email protected]
- git+https://github.com/ansible/ansible.git@devel
os:
- ubuntu-20.04
python-version:
- 3.7

steps:

- name: Check out repository
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Upgrade pip and display Python and PIP versions
run: |
sudo apt-get update
sudo apt-get install -y python*-wheel python*-yaml
python -m pip install --upgrade pip
python -V
pip --version
- name: Set up pip cache
uses: actions/cache@v1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ github.ref }}-units-VMs
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Install Ansible ${{ matrix.ansible-version }}
run: python3 -m pip install --user --force-reinstall --upgrade '${{ matrix.ansible-version }}'

- name: Build and install the collection tarball
run: |
export PATH=~/.local/bin:$PATH
echo "Run ansible version"
command -v ansible
ansible --version
rm -rf /tmp/just_new_collection
~/.local/bin/ansible-galaxy collection build --output-path /tmp/just_new_collection --force
~/.local/bin/ansible-galaxy collection install -vvv --force /tmp/just_new_collection/*.tar.gz
- name: Run collection tests for podman container API
run: |
export PATH=~/.local/bin:$PATH
if [[ '${{ matrix.ansible-version }}' == 'git+https://github.com/ansible/ansible.git@devel' ]]; then
export ANSIBLE_CONFIG=$(pwd)/ci/ansible-dev.cfg
elif [[ '${{ matrix.ansible-version }}' == 'ansible<2.10' ]]; then
export ANSIBLE_CONFIG=$(pwd)/ci/ansible-2.9.cfg
fi
python3 -m pip install --user requests
podman system service --time=0 unix:///tmp/podman.sock &
echo $ANSIBLE_CONFIG
command -v ansible-playbook
pip --version
python --version
ansible-playbook --version
ansible-playbook -vv ci/playbooks/pre.yml \
-e host=localhost \
-i localhost, \
-e ansible_connection=local \
-e setup_python=false
TEST2RUN=podman_container_api ./ci/run_containers_tests.sh
shell: bash
12 changes: 12 additions & 0 deletions ci/playbooks/containers/podman_container_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- hosts: all
gather_facts: true
module_defaults:
containers.podman.podman_container:
podman_socket: unix:///tmp/podman.sock
tasks:
- include_role:
name: podman_container_api
vars:
idem_image: idempotency_test
ansible_python_interpreter: "{{ _ansible_python_interpreter }}"
20 changes: 20 additions & 0 deletions plugins/module_utils/podman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type

try:
import requests
from ansible_collections.containers.podman.plugins.module_utils.podman.podman_api import PodmanAPIClient
HAS_REQUESTS = True
except ImportError:
PodmanAPIClient = object
HAS_REQUESTS = False


def run_podman_command(module, executable='podman', args=None, expected_rc=0, ignore_errors=False):
if not isinstance(executable, list):
Expand All @@ -25,3 +33,15 @@ def lower_keys(x):
return dict((k.lower(), lower_keys(v)) for k, v in x.items())
else:
return x


class PodmanAPI:
def __init__(self, module):
if module.params.get('podman_socket') and not HAS_REQUESTS:
module.fail_json(
"Requests module is not installed while socket was provided!")
self.client = PodmanAPIClient(module.params.get('podman_socket'))
try:
self.client.version()
except Exception as api_error:
module.fail_json("Podman API error: %s" % str(api_error))
Loading

0 comments on commit 390f8cf

Please sign in to comment.