# Caelum reduction sidecar.
#
# Multi-stage because not every package in the astronomy stack ships a wheel
# for every architecture. photutils has C extensions and publishes no
# linux/aarch64 wheel, so on an arm64 host pip falls back to building from
# source and needs a compiler. Putting that compiler in a build stage keeps it
# out of the runtime image and makes the build work on amd64 and arm64 alike,
# rather than only on whichever one happened to have wheels the day it was
# written.
#
# Python 3.13 rather than 3.14: the stack's wheel coverage is better there, and
# a base image whose wheels are still catching up turns every install into a
# source build.

# ---------- build stage ----------
FROM python:3.13-slim AS build

# Compilers and the headers numpy's build needs. None of this reaches the
# runtime image.
# hadolint ignore=DL3008
# Debian mirrors over https: the build machines cannot open port 80 to the
# mirror CDN. python:3.13-slim already carries ca-certificates.
RUN sed -i 's|http://deb.debian.org|https://deb.debian.org|g' /etc/apt/sources.list.d/debian.sources \
  && apt-get update \
  && apt-get install --no-install-recommends -y build-essential \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /build
COPY requirements.txt ./

# Resolve everything to wheels once. Anything that already has one is
# downloaded; anything that does not is compiled here, in this stage.
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt

# ---------- runtime stage ----------
FROM python:3.13-slim

# curl for the container healthcheck, ca-certificates so the worker can reach
# an API over TLS. Nothing else: this process talks to one HTTP endpoint and
# does arithmetic.
#
# The plate solver (ASTAP or astrometry.net) is deliberately NOT installed.
# Its index files run to tens of GB and belong on a mounted volume rather than
# in a layer, so it arrives with the astrometry stage in phase 3.
# hadolint ignore=DL3008
# Debian mirrors over https: the build machines cannot open port 80 to the
# mirror CDN. python:3.13-slim already carries ca-certificates.
RUN sed -i 's|http://deb.debian.org|https://deb.debian.org|g' /etc/apt/sources.list.d/debian.sources \
  && apt-get update \
  && apt-get install --no-install-recommends -y curl ca-certificates \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install from the wheels built above, with the index switched off so a
# network hiccup cannot silently pull a different version than the one this
# image was built against.
COPY requirements.txt ./
COPY --from=build /wheels /wheels
RUN pip install --no-cache-dir --no-index --find-links=/wheels -r requirements.txt \
  && rm -rf /wheels

COPY caelum_reduce/ ./caelum_reduce/

# Unprivileged. The worker writes nothing to disk: frames and masters stream
# through memory, so it needs no writable mount and should not have one.
# An explicit numeric uid, so the identity is resolvable on any host and stays
# predictable if a volume is ever mounted here.
RUN useradd --create-home --uid 10001 --shell /usr/sbin/nologin reduce \
  && chown -R 10001:10001 /app
USER 10001

# Unbuffered so container logs appear as they happen rather than in blocks,
# and no .pyc files since this user cannot write to /app anyway.
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# The worker has no HTTP surface of its own, so "healthy" means the process can
# still import its stack. A failed claim is not unhealthy: the API being
# briefly down is a thing the worker is built to survive.
#
# JSON form, so there is no shell in the healthcheck. Python already exits
# non-zero on an uncaught ImportError, which is the failure this detects.
HEALTHCHECK --interval=60s --timeout=15s --start-period=20s --retries=3 \
  CMD ["python", "-c", "import astropy, numpy, photutils, caelum_reduce"]

ENTRYPOINT ["python", "-m", "caelum_reduce"]
