# SPDX-License-Identifier: AGPL-3.0-only
# Image `ameesh-executor` : l'exécuteur médié d'un appareil prêté (L114, BROUILLON).
# Interface figée : docs/IMAGE-EXECUTEUR.md. Ni construite ni publiée ici.
#
#   podman build -f deploy/executor-image/Containerfile \
#       --build-arg DSH_SHA256=<sha256 du binaire> -t ameesh-executor:dev .
#
# Contenu : Python, roue ameesh (+ cryptography), git, dsh, tini. AUCUN
# SECRET : ni clé de fournisseur, ni identifiant de forge, ni DSN, ni clé de
# release. La clé de l'exécuteur naît dans le volume, à l'enrôlement.

ARG PYTHON_IMAGE=python:3.12-slim-bookworm

# --- roue ameesh (le dépôt ne va pas dans l'image finale) --------------------
FROM ${PYTHON_IMAGE} AS build
WORKDIR /src
COPY pyproject.toml README.md LICENSE ./
COPY src ./src
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /wheels .

# --- image d'exécution -------------------------------------------------------
FROM ${PYTHON_IMAGE}

# dsh (DeepSeek Harness) : binaire fourni au contexte de construction, version
# épinglée, empreinte vérifiée. Seul harnais de l'image : il passe par le
# relais de modèle du serveur du mesh (L111).
ARG DSH_SHA256

LABEL org.opencontainers.image.title="ameesh-executor" \
      org.opencontainers.image.description="Exécuteur ameesh médié (appareil prêté, /api/exec/v1)" \
      org.opencontainers.image.licenses="AGPL-3.0-only" \
      org.ameesh.image-interface="ameesh-executor/1"

RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends git tini ca-certificates; \
    rm -rf /var/lib/apt/lists/*

COPY --from=build /wheels /wheels
RUN pip install --no-cache-dir /wheels/ameesh-*.whl 'cryptography>=41' && rm -rf /wheels

COPY deploy/executor-image/dsh /usr/local/bin/dsh
RUN test -n "$DSH_SHA256" \
    && echo "$DSH_SHA256  /usr/local/bin/dsh" | sha256sum -c - \
    && chmod 0755 /usr/local/bin/dsh
COPY deploy/executor-image/ameesh-executor /usr/local/bin/ameesh-executor

RUN useradd --uid 10001 --user-group --no-create-home --home-dir /var/lib/ameesh-exec \
        --shell /usr/sbin/nologin ameesh \
    && install -d -o 10001 -g 10001 -m 0700 /var/lib/ameesh-exec

# Interface (docs/IMAGE-EXECUTEUR.md §2) : valeurs par défaut de l'image. Le
# lanceur ne pose que AMEESH_EXEC_URL, et au besoin AMEESH_EXEC_MESH et
# AMEESH_EXEC_HOST.
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    HOME=/var/lib/ameesh-exec \
    AMEESH_EXEC_HOME=/var/lib/ameesh-exec \
    AMEESH_STATE=/var/lib/ameesh-exec/state \
    DSH_HOME=/var/lib/ameesh-exec/harness/dsh \
    AMEESH_DSH_BIN=/usr/local/bin/dsh \
    AMEESH_BACKEND=mediated \
    AMEESH_EXEC_SIGNER=cryptography \
    AMEESH_HOST_GATE=file \
    AMEESH_HOST_GATE_PATH=/run/ameesh-gate/state/state.json \
    AMEESH_HOST_GATE_ACK=/run/ameesh-gate/ack/ack.json \
    AMEESH_HOST_GATE_FALLBACK=stopped \
    AMEESH_HOST_GATE_DRAIN=90 \
    AMEESH_EXEC_ENROLL_DIR=/run/ameesh-enroll

VOLUME ["/var/lib/ameesh-exec"]
USER 10001:10001
WORKDIR /var/lib/ameesh-exec
STOPSIGNAL SIGTERM
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
    CMD ["/usr/local/bin/ameesh-executor", "health"]

ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/ameesh-executor"]
CMD ["run"]
