From d780244273bb7fbdc6b0f90fb3f658216f2ec151 Mon Sep 17 00:00:00 2001 From: NikolineLykPedersen <88713323+NikolineLykPedersen@users.noreply.github.com> Date: Fri, 20 Aug 2021 12:32:12 +0200 Subject: [PATCH] Backport: Allow podman executable override with env (#53) --- README.rst | 12 ++++++++++++ lib/molecule_podman/driver.py | 18 +++++++++++++++--- lib/molecule_podman/playbooks/create.yml | 19 ++++++++++++++++--- lib/molecule_podman/playbooks/destroy.yml | 4 +++- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 88b411f..36f7838 100644 --- a/README.rst +++ b/README.rst @@ -22,6 +22,18 @@ provisioning test resources. Please note that this driver is currently in its early stage of development. +Change podman executable +======================== + +To change the podman executable from the standard podman, export environment +variable ``MOLECULE_PODMAN_EXECUTABLE``. For instance, if you wish to run +molecule with ``podman-remote`` instead of ordinary ``podman``, the variable +can be exported as: + +.. code-block:: console + + $ export MOLECULE_PODMAN_EXECUTABLE=podman-remote + .. _get-involved: Get Involved diff --git a/lib/molecule_podman/driver.py b/lib/molecule_podman/driver.py index 7c2b074..4e4450c 100644 --- a/lib/molecule_podman/driver.py +++ b/lib/molecule_podman/driver.py @@ -21,9 +21,10 @@ from __future__ import absolute_import +import distutils.spawn import os -from molecule import logger +from molecule import logger, util from molecule.api import Driver from molecule.util import lru_cache @@ -150,6 +151,14 @@ def __init__(self, config=None): """Construct Podman.""" super(Podman, self).__init__(config) self._name = "podman" + # To change the podman executable, set environment variable + # MOLECULE_PODMAN_EXECUTABLE + # An example could be MOLECULE_PODMAN_EXECUTABLE=podman-remote + self.podman_exec = os.environ.get("MOLECULE_PODMAN_EXECUTABLE", "podman") + self.podman_cmd = distutils.spawn.find_executable(self.podman_exec) + if not self.podman_cmd: + msg = f"command not found in PATH {self.podman_exec}" + util.sysexit_with_message(msg) @property def name(self): @@ -162,7 +171,7 @@ def name(self, value): @property def login_cmd_template(self): return ( - "podman exec " + f"{self.podman_cmd} exec " "-e COLUMNS={columns} " "-e LINES={lines} " "-e TERM=bash " @@ -182,7 +191,10 @@ def login_options(self, instance_name): return {"instance": instance_name} def ansible_connection_options(self, instance_name): - return {"ansible_connection": "podman"} + return { + "ansible_connection": "podman", + "ansible_podman_executable": f"{self.podman_exec}", + } @lru_cache() def sanity_checks(self): diff --git a/lib/molecule_podman/playbooks/create.yml b/lib/molecule_podman/playbooks/create.yml index 219a757..4f6481f 100644 --- a/lib/molecule_podman/playbooks/create.yml +++ b/lib/molecule_podman/playbooks/create.yml @@ -4,10 +4,22 @@ gather_facts: false no_log: "{{ molecule_no_log }}" become: "{{ not (item.rootless|default(true)) }}" + vars: + podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}" tasks: + - name: get podman executable path + command: which {{ podman_exec }} + register: podman_path + environment: + PATH: "{{ ansible_env.PATH }}:/sbin:/usr/sbin" + changed_when: false + - name: save path to executable as fact + set_fact: + podman_cmd: "{{ podman_path.stdout }}" + - name: Log into a container registry command: > - podman login + {{ podman_cmd }} login --username {{ item.registry.credentials.username }} --password {{ item.registry.credentials.password }} --tls-verify={{ item.tls_verify | default(lookup('env', 'DOCKER_TLS_VERIFY')) or false }} @@ -44,6 +56,7 @@ - name: Discover local Podman images podman_image_info: name: "molecule_local/{{ item.item.name }}" + executable: "{{ podman_exec }}" with_items: "{{ platforms.results }}" when: - not item.pre_build_image | default(false) @@ -51,7 +64,7 @@ - name: Build an Ansible compatible image command: > - podman build + {{ podman_cmd }} build -f {{ item.dest }} -t molecule_local/{{ item.item.image }} {% if item.item.buildargs is defined %}{% for i,k in item.item.buildargs.items() %}--build-arg={{ i }}={{ k }} {% endfor %}{% endif %} @@ -77,7 +90,7 @@ - name: Create molecule instance(s) command: > - podman run + {{ podman_cmd }} run -d --name "{{ item.name }}" {% if item.pid_mode is defined %}--pid={{ item.pid_mode }}{% endif %} diff --git a/lib/molecule_podman/playbooks/destroy.yml b/lib/molecule_podman/playbooks/destroy.yml index 71389dd..1f3e1b7 100644 --- a/lib/molecule_podman/playbooks/destroy.yml +++ b/lib/molecule_podman/playbooks/destroy.yml @@ -5,9 +5,11 @@ gather_facts: false no_log: "{{ molecule_no_log }}" become: "{{ not (item.rootless|default(true)) }}" + vars: + podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE')|default('podman',true) }}" tasks: - name: Destroy molecule instance(s) - shell: podman container exists {{ item.name }} && podman rm -f {{ item.name }} || true + shell: "{{ podman_exec }} container exists {{ item.name }} && {{ podman_exec }} rm -f {{ item.name }} || true" register: server with_items: "{{ molecule_yml.platforms }}" async: 7200