Skip to content

Commit

Permalink
Backport: Allow podman executable override with env (ansible-communit…
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolineLykPedersen authored and gs-kamnas committed Jan 20, 2022
1 parent 3c14914 commit d780244
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions lib/molecule_podman/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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 "
Expand All @@ -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):
Expand Down
19 changes: 16 additions & 3 deletions lib/molecule_podman/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -44,14 +56,15 @@
- 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)
register: podman_images

- 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 %}
Expand All @@ -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 %}
Expand Down
4 changes: 3 additions & 1 deletion lib/molecule_podman/playbooks/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d780244

Please sign in to comment.