Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Allow podman executable override with env #53

Merged
merged 21 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ This plugin requires `containers.podman` collection to be present:

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 src/molecule_podman/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@

from __future__ import absolute_import

import distutils.spawn
import os
from typing import Dict

from molecule import logger
from molecule import logger, util
from molecule.api import Driver
from molecule.util import lru_cache

Expand Down Expand Up @@ -152,6 +153,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 @@ -164,7 +173,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 @@ -184,7 +193,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
23 changes: 19 additions & 4 deletions src/molecule_podman/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,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 @@ -54,6 +66,7 @@
- name: Discover local Podman images
containers.podman.podman_image_info:
name: "molecule_local/{{ item.item.name }}"
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
executable: "{{ podman_exec }}"
with_items: "{{ platforms.results }}"
loop_control:
label: "{{ item.item.name | default('None specified') }}"
Expand All @@ -63,7 +76,7 @@

- name: Build an Ansible compatible image # noqa: no-handler
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 Down Expand Up @@ -96,14 +109,15 @@
# https://github.com/ansible-community/molecule-podman/issues/22
- name: Remove possible pre-existing containers
command: >
podman rm -f -i -v {% for key in molecule_yml.platforms %}{{ key.name }} {% endfor %}
{{ podman_cmd }} rm -f -i -v {% for key in molecule_yml.platforms %}{{ key.name }} {% endfor %}
register: result
changed_when: true
failed_when: false

- name: Discover local podman networks
containers.podman.podman_network_info:
name: "{{ item.network }}"
executable: "{{ podman_exec }}"
loop: "{{ molecule_yml.platforms | flatten(levels=1) }}"
loop_control:
extended: true
Expand All @@ -117,6 +131,7 @@
- name: Create podman network dedicated to this scenario
containers.podman.podman_network:
name: "{{ podman_network.results[0].ansible_loop.allitems[0].network }}"
executable: "{{ podman_exec }}"
subnet:
"{{ podman_network.results[0].ansible_loop.allitems[0].subnet }}"
when:
Expand All @@ -126,7 +141,7 @@

- name: Create molecule instance(s)
command: >
podman
{{ podman_cmd }}
{% if item.cgroup_manager is defined %}--cgroup-manager={{ item.cgroup_manager }}{% endif %}
{% if item.storage_opt is defined %}--storage-opt={{ item.storage_opt }}{% endif %}
{% if item.storage_driver is defined %}--storage-driver={{ item.storage_driver }}{% endif %}
Expand Down
4 changes: 3 additions & 1 deletion src/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