# SPDX-License-Identifier: AGPL-3.0-only
# Image `ameesh-executor` : l'exécuteur médié d'un appareil prêté (L114).
# Interface figée : docs/IMAGE-EXECUTEUR.md. La publication (CI, ghcr.io,
# digest pour la signature O4) est décrite au §9 du même document et
# exécutée par .github/workflows/image-executor.yml.
#
#   docker build --platform linux/amd64 \
#       -f deploy/executor-image/Containerfile \
#       --build-arg VERSION=<version> --build-arg REVISION=<sha> \
#       -t ameesh-executor:dev .
#
# Contenu : Node (pour dsh), 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.
#
# Deux épinglages des dépendances mouvantes du binaire ; les paquets apt
# (python3, git, tini) suivent les dépôts Debian bookworm au moment du build,
# et c'est le digest publié qui fait référence :
#   - l'image de base Node est épinglée par le digest de son manifeste
#     linux/amd64 (et non par le tag mobile `22-bookworm-slim`) ;
#   - dsh est installé par `npm ci` depuis package.json + package-lock.json
#     versionnés dans ce dossier : les paquets de l'arbre sont fixés par
#     version et intégrité sha512, et non résolus à la construction.
# L'architecture est linux/amd64 : c'est ce manifeste que le bureau signe
# (§9) ; un hôte d'une autre architecture ouvrira un lot dédié.

ARG NODE_IMAGE=node:22-bookworm-slim@sha256:ea11b74b986d520137cdef9a5346e447a00f275ecc4abc0055b7151226231e09
ARG DSH_VERSION=0.2.0-rc.2

# --- roue ameesh (le dépôt ne va pas dans l'image finale) --------------------
FROM ${NODE_IMAGE} AS build

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

WORKDIR /src
COPY pyproject.toml README.md LICENSE ./
COPY src ./src
# #9681 : `[tool.setuptools.data-files]` de pyproject.toml liste des fichiers
# sous ces trois répertoires ; sans eux, `pip wheel .` échoue sur un checkout
# propre (« doesn't exist or not a regular file »). `.dockerignore` les
# réintègre explicitement (#9679), pas tout `deploy`.
COPY deploy/mise-a-jour ./deploy/mise-a-jour
COPY deploy/postgres ./deploy/postgres
COPY deploy/sql ./deploy/sql
RUN set -eux; \
    python3 -m venv /opt/build; \
    /opt/build/bin/pip wheel --no-cache-dir --no-deps --wheel-dir /wheels .

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

ARG VERSION=0.0.0
ARG REVISION=unknown
ARG DSH_VERSION=0.2.0-rc.2

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.opencontainers.image.version="${VERSION}" \
      org.opencontainers.image.revision="${REVISION}" \
      org.opencontainers.image.source="https://github.com/manaty/ameesh" \
      org.ameesh.image-interface="ameesh-executor/1"

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

COPY --from=build /wheels /wheels
RUN set -eux; \
    python3 -m venv /opt/ameesh; \
    /opt/ameesh/bin/pip install --no-cache-dir /wheels/ameesh-*.whl 'cryptography>=41'; \
    rm -rf /wheels

# dsh (DeepSeek Harness) : seul harnais de l'image, il passe par le relais de
# modèle du serveur du mesh (L111). L'arbre npm est figé par le lockfile.
COPY deploy/executor-image/package.json deploy/executor-image/package-lock.json /opt/dsh/
RUN set -eux; \
    cd /opt/dsh; \
    npm ci --omit=dev --ignore-scripts --no-audit --no-fund; \
    npm cache clean --force; \
    test "$(node -p 'require("/opt/dsh/node_modules/@deepseek-ai/dsh/package.json").version')" = "$DSH_VERSION"; \
    ln -s /opt/dsh/node_modules/@deepseek-ai/dsh/lib/bin.js /usr/local/bin/dsh; \
    dsh --version

COPY deploy/executor-image/ameesh-executor /usr/local/bin/ameesh-executor

RUN set -eux; \
    chmod 0755 /usr/local/bin/ameesh-executor; \
    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 PATH=/opt/ameesh/bin:$PATH \
    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"]
