# Controller image of the Molecule workflow of this Collection (ADR 0003): molecule, its Docker
# driver and the collections the scenarios call are baked in, and nothing Lint uses, so a tool
# bump that only Lint needs leaves this image, and the scenarios it selects, alone.
#
# Built by .gitea/workflows/image.yml with kaniko (repository root as build context, because
# the pin files below are copied in) and pushed, never on pull requests, to
# git.rkdr.net/mortis/molecule-controller.
#
# What is baked in, and from where:
# - /opt/venv: requirements-molecule.lock, the hashed lock of requirements-molecule.txt
#   (ansible-core, molecule, molecule-plugins[docker] with the docker Python SDK), on the Python
#   of .python-version (PYTHON_VERSION below), the one the lock was resolved on.
# - /opt/collections: the collections of requirements.yml (the ones the roles call) and of
#   extensions/molecule/requirements.yml (community.docker, which the Docker driver's own
#   playbooks call), found through ANSIBLE_COLLECTIONS_PATH.
# - /opt/molecule-pins.sha256: the checksums of the four pin files this image was built from. A
#   job runs `sha256sum -c /opt/molecule-pins.sha256` in its checkout; when a pull request changes
#   a pin the check fails and the job installs the pinned tools and collections itself.
#
# No image-smoke scenario: the last RUN checks the tools and the pin record, and a full Molecule
# run on the image's pin pull request proves the rest (images/AGENTS.md).
#
# Layers: each COPY / RUN below is one layer, and a layer is pushed as one request, so it must
# stay below Cloudflare's 100 MB request-body limit on git.rkdr.net. The build stage prints the
# compressed size of each directory it copies.

FROM ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db AS os

ENV DEBIAN_FRONTEND=noninteractive

# python3 and python3-venv only bootstrap uv in the build stage; ca-certificates serve the
# install a job runs when a pull request changes a pin. apt lists are removed in the same layer.
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates python3 python3-venv \
    && rm -rf /var/lib/apt/lists/*


FROM os AS build

# The Python of /opt/venv: the minor version of .python-version (check_image_versions.py pins fails
# when an ARG PYTHON_VERSION line, or a lock header, differs from it).
ARG PYTHON_VERSION=3.14

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1 \
    UV_PYTHON_INSTALL_DIR=/opt/python \
    UV_PYTHON_BIN_DIR=/opt/python-bin

WORKDIR /src
COPY requirements-molecule.txt requirements-molecule.lock requirements.yml ./
# Kept at its checkout path: its name matches the root requirements.yml.
COPY extensions/molecule/requirements.yml extensions/molecule/

# uv lives only in this stage; the final image keeps the interpreter it downloaded. libpython is
# removed when the binary does not link it (about 10 MB compressed nothing here loads), as in ci.
RUN set -eu; \
    python3 -m venv /opt/uv-venv; \
    /opt/uv-venv/bin/pip install uv==0.13.9; \
    /opt/uv-venv/bin/uv python install "${PYTHON_VERSION}"; \
    d="$(echo /opt/python/cpython-${PYTHON_VERSION}.*/)"; \
    if ldd "${d}bin/python${PYTHON_VERSION}" | grep -q libpython; then \
      echo "python${PYTHON_VERSION} links libpython: keeping it"; \
    else \
      rm -f "${d}"lib/libpython*.so*; \
    fi

RUN "$(echo /opt/python/cpython-${PYTHON_VERSION}.*/bin/python${PYTHON_VERSION})" -m venv /opt/venv \
    && /opt/venv/bin/pip install --require-hashes -r requirements-molecule.lock -r requirements-molecule.txt

# ANSIBLE_COLLECTIONS_PATH is the target for the install only: a collection found anywhere else on
# the path would be skipped as "already installed" and not copied here.
RUN set -eu; \
    for req in requirements.yml extensions/molecule/requirements.yml; do \
      ANSIBLE_COLLECTIONS_PATH=/opt/collections \
        /opt/venv/bin/ansible-galaxy collection install -r "$req" -p /opt/collections; \
    done; \
    gz() { tar -C "$(dirname "$1")" -cf - "$(basename "$1")" | gzip -6 | wc -c \
      | awk -v d="$1" '{printf "%7.1f MB compressed  %s\n", $1 / 1048576, d}'; }; \
    for d in /opt/venv /opt/python /opt/collections; do gz "$d"; done


FROM os AS final

ARG PYTHON_VERSION=3.14

LABEL org.opencontainers.image.title="molecule-controller" \
      org.opencontainers.image.description="Molecule controller image for the mortis.kamino Ansible Collection: the Molecule workflow runs molecule in it, so a tool only Lint uses does not change it. It bakes in ansible-core, molecule, molecule-plugins with the docker Python SDK, the collections of requirements.yml and extensions/molecule/requirements.yml, and the Python of .python-version (the tools in /opt/venv run on it). Pin it by tag and digest and start it with docker run; it has no Docker CLI, so mount the host one." \
      org.opencontainers.image.source="https://git.rkdr.net/mortis/kamino" \
      org.opencontainers.image.url="https://git.rkdr.net/mortis/kamino" \
      org.opencontainers.image.documentation="https://git.rkdr.net/mortis/kamino/src/branch/main/docs/images/molecule-controller.md" \
      org.opencontainers.image.licenses="GPL-3.0-or-later" \
      org.opencontainers.image.authors="Defame1297 and contributors" \
      org.opencontainers.image.base.name="ubuntu:26.04" \
      net.rkdr.mortis.purpose="Molecule controller image for the mortis.kamino Ansible Collection: the Molecule workflow runs molecule in it, so a tool only Lint uses does not change it." \
      net.rkdr.mortis.contents="Ubuntu 26.04, /opt/venv (ansible-core, molecule, molecule-plugins[docker]), /opt/collections (community.general, ansible.posix, community.proxmox, community.docker), python<PYTHON_VERSION> (the one of /opt/venv), python3 (distribution), /opt/molecule-pins.sha256" \
      net.rkdr.mortis.usage="Molecule (host executor, kamino-molecule runner): docker run as the runner user, with the Docker socket, the host Docker CLI and the checkout mounted at the same path. Run sha256sum -c /opt/molecule-pins.sha256 in the checkout root and install the pinned tools and collections when a pull request changed them, else use /opt/venv/bin and ANSIBLE_COLLECTIONS_PATH=/opt/collections. See the documentation label."

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
    ANSIBLE_COLLECTIONS_PATH=/opt/collections \
    PATH=/opt/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

COPY --from=build /opt/venv /opt/venv
COPY --from=build /opt/collections /opt/collections
COPY --from=build /opt/python /opt/python

# The pin record, under checkout paths so `sha256sum -c` in a checkout root finds the files.
COPY --from=build /src/requirements-molecule.txt /src/requirements-molecule.lock /src/requirements.yml /opt/molecule-pins/
COPY --from=build /src/extensions/molecule/requirements.yml /opt/molecule-pins/extensions/molecule/
# Self-checks: the tools run on the pinned Python, every pinned collection is installed and the pin
# record verifies against the files it was made from. A failure fails the build, not a later job.
RUN set -eu; \
    cd /opt/molecule-pins; \
    sha256sum requirements-molecule.txt requirements-molecule.lock requirements.yml \
      extensions/molecule/requirements.yml > /opt/molecule-pins.sha256; \
    sha256sum --quiet -c /opt/molecule-pins.sha256; \
    python -c "import sys; assert '%d.%d' % sys.version_info[:2] == '${PYTHON_VERSION}', sys.version"; \
    core="$(sed -n 's/^ansible-core==\([^ ]*\).*/\1/p' requirements-molecule.lock)"; \
    test -n "$core"; \
    ansible --version | head -n1 | grep -F "core ${core}]"; \
    mol="$(sed -n 's/^molecule==\([^ ]*\).*/\1/p' requirements-molecule.lock)"; \
    test -n "$mol"; \
    molecule --version | grep -F "molecule ${mol}"; \
    molecule drivers | grep -qw docker; \
    python -c "import docker"; \
    collections="$(ansible-galaxy collection list --format json)"; \
    for c in $(sed -n 's/^ *- name: *//p' requirements.yml extensions/molecule/requirements.yml); do \
      printf '%s' "$collections" | python -c "import json, sys; d = json.load(sys.stdin); sys.exit(0 if any('$c' in v for v in d.values()) else 1)" \
        || { echo "collection $c is not installed" >&2; exit 1; }; \
    done; \
    ansible-galaxy collection list
