FROM python:3.12-slim-bookworm

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install \
        --yes \
        --no-install-recommends \
        bash \
        ca-certificates \
        coreutils \
        curl \
        git \
        gzip \
        jq \
        openssh-client \
        podman \
        rsync \
        tar \
    && rm -rf /var/lib/apt/lists/*

RUN python -m venv /opt/venv

ENV PATH="/opt/venv/bin:${PATH}"
ENV PIP_DISABLE_PIP_VERSION_CHECK="1"
ENV ANSIBLE_FORCE_COLOR="true"
ENV ANSIBLE_NOCOWS="1"
ENV ANSIBLE_DEPRECATION_WARNINGS="true"

COPY requirements-ci.txt \
     /tmp/requirements-ci.txt

RUN python -m pip install \
      --no-cache-dir \
      --requirement /tmp/requirements-ci.txt \
    && rm -f /tmp/requirements-ci.txt

COPY extensions/molecule/container/collections.yml \
     /tmp/collections.yml

RUN set -eu; \
    install -d -m 0755 /opt/ansible/collections; \
    attempt=1; \
    while [ "$attempt" -le 4 ]; do \
      if ansible-galaxy collection install \
        --requirements-file /tmp/collections.yml \
        --collections-path /opt/ansible/collections \
        --timeout 180 \
        --no-deps \
        --force; then \
        break; \
      fi; \
      if [ "$attempt" -eq 4 ]; then \
        exit 1; \
      fi; \
      attempt=$((attempt + 1)); \
      sleep 20; \
    done; \
    rm -f /tmp/collections.yml

ENV ANSIBLE_COLLECTIONS_PATH="/opt/ansible/collections"

RUN python -m pip check \
    && ansible --version \
    && ansible-lint --version \
    && yamllint --version \
    && molecule --version \
    && podman --version \
    && ansible-galaxy collection list \
         --collections-path /opt/ansible/collections

WORKDIR /workspace
