# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
# Cantabile Edge Agent: one image for both wrappers (the HA add-on and the
# Cantabile Hub's own service): the agent (Python) and the site's link (Go,
# edge/link: the site's key and NATS connection). State in /data (the
# add-on's persistent directory as well), runs as non-root.
# Multi-arch (edge/build.sh): amd64 and arm64 (Raspberry Pi 4). The link is
# cross-compiled on the build machine; amd64 at the x86-64 baseline (v1),
# because Hub hardware includes CPUs without SSE4 (AMD G-T40N bench).
FROM --platform=$BUILDPLATFORM golang:1.26.9-bookworm AS link
WORKDIR /src
COPY link/go.mod link/go.sum ./
RUN go mod download
COPY link/*.go ./
ARG VERSION=0.0.0-dev
ARG TARGETARCH
RUN CGO_ENABLED=0 GOARCH=$TARGETARCH GOAMD64=v1 go build -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /cantabile-link .

FROM python:3.12.12-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
# LightGBM's OpenMP runtime: the box trains its own models (boxtrain.py,
# methods.md B10), as the cloud's trainer image does (building/Dockerfile)
RUN apt-get update && apt-get install -y --no-install-recommends libgomp1 && rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY pyproject.toml ./
COPY cantabile_edge ./cantabile_edge
RUN pip install --no-cache-dir . && rm -rf /src \
 && useradd --system --uid 10001 --home /data cantabile && mkdir -p /data /run/cantabile \
 && chown cantabile /data /run/cantabile
COPY --from=link /cantabile-link /usr/local/bin/cantabile-link
COPY run.sh /usr/local/bin/cantabile-run
USER 10001
WORKDIR /data
# cantabile-run: the link and the agent together (CANTABILE_LINK=0: the
# agent alone, e.g. with the file or NATS sink in tests).
ENTRYPOINT ["cantabile-run"]
CMD ["--config", "/data/config.yaml", "run"]
