FROM debian:bookworm

ENV DEBIAN_FRONTEND=noninteractive
ARG SOURCE_DATE_EPOCH

# ansible have problems finding async_job data when run with 'root'
# but from /home/runner.
ENV ANSIBLE_ASYNC_DIR=/

WORKDIR /

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3="*" \
    python3-pip="*" \
    python3-venv="*" \
    openssh-client="*" \
    shellcheck="*" \
    jq="*" \
    curl="*" \
    ca-certificates="*" \
    git="*" \
    python3-poetry="*" \
 && rm -rf /var/lib/apt/lists/* \
 && apt-get clean \
 && find /var/cache/ldconfig -type f -delete \
 && rm var/lib/dbus/machine-id \
 && find / -type f -name '*.log' -print -exec truncate -s0 '{}' + \
 && find / -type f -name '*.log.xz' -print -exec truncate -s0 '{}' +

 SHELL ["/bin/bash", "-o", "pipefail", "-c"]
 # Don't forget to update sha256 for binaries (not archive!) if updating
 RUN  poetry self update 2.1.2 \
    && bash -c "curl -L https://github.com/casey/just/releases/download/1.40.0/just-1.40.0-x86_64-unknown-linux-musl.tar.gz | tar xz --wildcards 'just'" \
    && echo "3fae4508b25e85bec16c2985bb76f731d35b103434c5bf264af0fa2ab07e889a  just" | sha256sum -c \
    && mv just /usr/local/bin \
    && bash -c "curl -L https://github.com/cli/cli/releases/download/v2.72.0/gh_2.72.0_linux_amd64.tar.gz | tar xz --wildcards */gh" \
    && mv gh_2.72.0_linux_amd64/bin/gh /usr/local/bin \
    && echo "9e7b2c5bd1494bd5236f589cd359e960885dd90c5b7541be3812822a21a60581 /usr/local/bin/gh" | sha256sum -c \
    && chmod +x /usr/local/bin/just \
    && rm -r root/.cache/ \
    && find /var/cache/ldconfig -type f -delete

COPY pyproject.toml poetry.lock requirements.yml /

RUN poetry config virtualenvs.in-project true \
 && poetry install --no-interaction --only main --no-root --no-cache \
 && rm -r root/.cache/ \
 && find /var/cache/ldconfig -type f -delete \
 && find / -type f -name 'RECORD' -delete \
 && find / -type f -name '*.pyc' -delete

ENV VIRTUAL_ENV="/.venv"
ENV PATH="/.venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

RUN ansible-galaxy collection install -r requirements.yml --collections-path /usr/share/ansible/collections \
 && rm -rf /root/.ansible \
 && find /var/cache/ldconfig -type f -delete \
 && find / -type f -name 'RECORD' -delete \
 && find / -type f -name '*.pyc' -delete
