# syntax=docker/dockerfile:1
# Build context is the repository root.

FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
# The version (README, Versioning): X.Y.Z for a release, X.Y.Z.N between
# releases; CI passes it. 0.0.0.0, the development version, otherwise (the
# image build has no git to read one from).
ARG UNIFIED_VERSION=0.0.0.0
WORKDIR /source

# Restore layer: copy only project/props files first for build caching.
COPY global.json Directory.Build.props Directory.Packages.props ./
COPY build/Version.targets build/
COPY src/Shared.Contracts/Shared.Contracts.csproj src/Shared.Contracts/
COPY src/Shared.Infrastructure/Shared.Infrastructure.csproj src/Shared.Infrastructure/
COPY src/Backend/Backend.csproj src/Backend/
RUN dotnet restore src/Backend/Backend.csproj

COPY src/Shared.Contracts/ src/Shared.Contracts/
COPY src/Shared.Infrastructure/ src/Shared.Infrastructure/
COPY src/Backend/ src/Backend/
# The short commit goes into the informational version (X.Y.Z+<commit>).
ARG GIT_COMMIT_SHORT=""
RUN dotnet publish src/Backend/Backend.csproj -c Release -o /app --no-restore \
    -p:UnifiedVersion=${UNIFIED_VERSION} -p:GitCommitShort=${GIT_COMMIT_SHORT}

# The object store the server runs for built-in S3, as its project published it:
# build/object-store.json pins the release and each archive's SHA-256.
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS object-store
COPY build/object-store.json /tmp/pin.json
RUN set -eu; \
    case "$(uname -m)" in \
      x86_64) rid=linux-x64 ;; \
      aarch64) rid=linux-arm64 ;; \
      *) echo "No object store is pinned for $(uname -m)" >&2; exit 1 ;; \
    esac; \
    top() { sed -n "s/^ *\"$1\": *\"\([^\"]*\)\".*/\1/p" /tmp/pin.json; }; \
    file=$(sed -n "s/.*\"$rid\": *{ *\"file\": *\"\([^\"]*\)\".*/\1/p" /tmp/pin.json); \
    sha=$(sed -n "s/.*\"$rid\": *{.*\"sha256\": *\"\([0-9a-f]*\)\".*/\1/p" /tmp/pin.json); \
    test -n "$file" && test -n "$sha"; \
    curl -fsSL -o /tmp/store.tar.gz "$(top downloads)/$file"; \
    echo "$sha  /tmp/store.tar.gz" | sha256sum -c -; \
    mkdir -p /tmp/unpacked /object-store; \
    tar -xzf /tmp/store.tar.gz -C /tmp/unpacked; \
    for name in silo LICENSE NOTICE; do \
      cp "$(find /tmp/unpacked -type f -name "$name" | head -n 1)" "/object-store/$name"; \
    done; \
    chmod 755 /object-store/silo; \
    printf '%s %s\nAn unmodified copy, licensed under the GNU AGPL version 3 (see LICENSE and NOTICE), run as a separate program.\nSource for exactly this build, commit %s:\n  %s\n  %s\nPublished archive: %s/%s\nSHA-256: %s\n' \
      "$(top name)" "$(top version)" "$(top commit)" "$(top sourceArchive)" "$(top source)" "$(top downloads)" "$file" "$sha" > /object-store/SOURCE.txt

FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime
ARG UNIFIED_VERSION=0.0.0.0
# CI supplies these at image build time (empty on plain local builds).
ARG GIT_COMMIT=""
ARG GIT_COMMIT_SHORT=""
ARG BUILD_DATE=""
ARG SOURCE_URL="https://prod.git.gracesolution.info/gsadmin/ExecuteR"
LABEL org.opencontainers.image.title="ExecuteR Control Plane Backend" \
      org.opencontainers.image.description="ExecuteR control plane: REST API, enrollment, messaging workers and job dispatch" \
      org.opencontainers.image.version="${UNIFIED_VERSION}" \
      org.opencontainers.image.revision="${GIT_COMMIT}" \
      executer.revision.short="${GIT_COMMIT_SHORT}" \
      org.opencontainers.image.created="${BUILD_DATE}" \
      org.opencontainers.image.source="${SOURCE_URL}" \
      org.opencontainers.image.url="${SOURCE_URL}" \
      org.opencontainers.image.documentation="${SOURCE_URL}/wiki" \
      org.opencontainers.image.vendor="ExecuteR" \
      org.opencontainers.image.licenses="LicenseRef-EndexaSolutions-Proprietary"

# curl is used by the compose health check. pg_dump and pg_restore take
# Postgres backups from Settings › Database: the PostgreSQL client of the
# bundled server's major (IaC/compose POSTGRES_VERSION; pg_dump must be that
# major or newer), from the PostgreSQL project's own apt repository, since
# Ubuntu 24.04 ships only 16. PostgreSQL Licence (permissive, see
# THIRD-PARTY-NOTICES.md); the client needs perl for its version wrapper,
# which PG_DUMP_PATH bypasses.
ARG POSTGRES_CLIENT_MAJOR=18
RUN set -eu; \
    apt-get update; \
    apt-get install -y --no-install-recommends curl ca-certificates; \
    install -d /usr/share/postgresql-common/pgdg; \
    curl -fsSL -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc; \
    . /etc/os-release; \
    echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt ${VERSION_CODENAME}-pgdg main" \
        > /etc/apt/sources.list.d/pgdg.list; \
    apt-get update; \
    apt-get install -y --no-install-recommends "postgresql-client-${POSTGRES_CLIENT_MAJOR}"; \
    "/usr/lib/postgresql/${POSTGRES_CLIENT_MAJOR}/bin/pg_dump" --version; \
    rm -rf /var/lib/apt/lists/*; \
    mkdir -p /data; \
    chown app:app /data

WORKDIR /app
COPY --from=build /app .
COPY --from=object-store /object-store /app/object-store

# Agent installer baked in by CI so the URL-install flow works out of the box.
# CI stages the Windows-built package into ./artifacts/agent-package before
# building this image; the .dockerignore keeps it empty otherwise, and the
# backend falls back to /data/agent (or S3 offload) when it is absent.
COPY src/Infra/backend/agent-package/ /opt/executer/agent/

ENV ASPNETCORE_URLS=http://0.0.0.0:8080 \
    DATABASE_PATH=/data/app.db \
    PG_DUMP_PATH=/usr/lib/postgresql/${POSTGRES_CLIENT_MAJOR}/bin/pg_dump

VOLUME /data
EXPOSE 8080
USER app

ENTRYPOINT ["dotnet", "Backend.dll"]
