diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index d5b286e..c503d14 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,10 +1,11 @@ # CI (SPEC 15.5, ADR 0003/0004): only run steps with git, dotnet and docker, no external actions. -# Publishing to the registry on v* tags arrives in phase 9 (issue #70). +# The image is published only on v* tags, after the tests and the drill (SPEC 15.5, ADR 0002 and 0020); never on fase-N. name: ci on: push: branches: ["**"] + tags: ["v*"] pull_request: jobs: @@ -13,7 +14,8 @@ jobs: container: image: mcr.microsoft.com/dotnet/sdk:10.0 # The databases of the tests live in memory: the disk of the runner stops for seconds under load (ADR 0019). - options: --tmpfs /test-tmp:size=4g + # exec: Docker mounts a tmpfs noexec by default, and the tests run the fake git and docker they write there. + options: --tmpfs /test-tmp:size=4g,exec strategy: fail-fast: false matrix: @@ -46,10 +48,15 @@ jobs: TESTINGPLATFORM_TELEMETRY_OPTOUT: "1" steps: - name: Checkout + # The token goes to git through its environment, so it is on no command line. + env: + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraHeader + GIT_CONFIG_VALUE_0: "Authorization: token ${{ github.token }}" run: | git init -q . git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" - git -c http.extraHeader="Authorization: token ${{ github.token }}" fetch -q --depth 1 origin "${{ github.sha }}" + git fetch -q --depth 1 origin "${{ github.sha }}" git checkout -q FETCH_HEAD - name: Check run: | @@ -63,20 +70,42 @@ jobs: image: needs: test runs-on: ubuntu-latest - # Registry settings for phase 9 (issue #70): declared here, not used before then. env: - REGISTRY: gitea.encke-hake.ts.net - REGISTRY_USER: ${{ github.repository_owner }} - REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} + # The Docker daemon of the runner is shared between runs: a name of this run only, so what is published is what was tried. + IMAGE: anubis:ci-${{ github.run_id }}-${{ github.run_attempt }} steps: - name: Checkout + # The token goes to git through its environment, so it is on no command line. + env: + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraHeader + GIT_CONFIG_VALUE_0: "Authorization: token ${{ github.token }}" run: | git init -q . git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" - git -c http.extraHeader="Authorization: token ${{ github.token }}" fetch -q --depth 1 origin "${{ github.sha }}" + git fetch -q --depth 1 origin "${{ github.sha }}" git checkout -q FETCH_HEAD - - name: Build image (no publish) - run: docker build -f deploy/docker/Dockerfile -t anubis:ci . + - name: Build image + env: + REF: ${{ github.ref }} + REF_NAME: ${{ github.ref_name }} + run: | + case "$REF" in refs/tags/v*) version=${REF_NAME#v} ;; *) version=dev ;; esac + docker build -f deploy/docker/Dockerfile --build-arg "ANUBIS_VERSION=$version" --build-arg "ANUBIS_COMMIT=$(git rev-parse HEAD)" -t "$IMAGE" . # The guard drill inside the image (issue #48): offline, its own SQLite database in a temporary folder. - name: Guard drill in the image - run: docker run --rm anubis:ci drill + run: docker run --rm "$IMAGE" drill + - name: Publish image + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + env: + REGISTRY: ${{ vars.REGISTRY || 'gitea.encke-hake.ts.net' }} + REGISTRY_USER: ${{ vars.REGISTRY_USER || github.actor }} + REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} + TAG: ${{ github.ref_name }} + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraHeader + GIT_CONFIG_VALUE_0: "Authorization: token ${{ github.token }}" + run: sh ./deploy/docker/publish.sh "$IMAGE" "$TAG" + - name: Remove the image of this run + if: always() + run: docker image rm -f "$IMAGE" || true diff --git a/CLAUDE.md b/CLAUDE.md index fad0d07..7b9be82 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -46,8 +46,11 @@ Un pacchetto nuovo rompe `DependencyPolicyTests` finché non è autorizzato (ADR - `docs/adr/0003-csharp-dotnet.md` — stack, struttura del repository, strumenti - `docs/adr/0004-componenti-esterni.md` — pacchetti ammessi e come chiederne altri - `docs/adr/0007-scelte-fase-1.md` — schema per i due motori, interfaccia statica, immagine Docker +- `docs/MANUALE.md`, `docs/RUNBOOK.md`, `docs/GO-LIVE.md` — manuali per l'utente: si aggiornano insieme al codice +- `docs/adr/0020-scelte-fase-9.md` — consegna, ripristino, dichiarazione, revisione trimestrale - `docs/design/` — screen di riferimento (SPEC 13.1); `docs/design/logo/` — logo - `docs/ROADMAP.md` — fasi, issue, criteri di accettazione +- `docs/piani/2026-12-02-nuova-rotta.md` — nuova rotta decisa il 2 dicembre 2026 (fasi 10–24, solo Docker); `docs/ricerca/` — il documento da cui viene - `docs/STATE.md` — stato corrente - `docs/SVILUPPO.md` — flusso di lavoro e regole - `deploy/unraid/anubis.xml` — template Unraid diff --git a/Directory.Build.props b/Directory.Build.props index 8bd1fc2..0d197c7 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -13,7 +13,7 @@ it;en BalboSoft Anubis - 0.7.0 + 0.9.0 diff --git a/README.md b/README.md index 8e78e1c..8ca6c53 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Bot di trading personale: azioni USA su **Alpaca** e crypto spot su **OKX Europe Si installa in due modi, dallo stesso codice: - **Docker**, un solo container pensato per un server Unraid sempre acceso. Immagine: `gitea.encke-hake.ts.net/balbosoft/anubis` (sezione Packages del repository). -- **Applicazione desktop** per Windows, con installatore (sezione Release). +- **Applicazione desktop** per Windows, con installatore (Fase 10, non ancora disponibile). Database a scelta: PostgreSQL oppure SQLite. Scritto in C# su .NET 10, con interfaccia Blazor: nessun runtime da installare, né su Windows né su Unraid (ADR 0003). @@ -15,7 +15,7 @@ Database a scelta: PostgreSQL oppure SQLite. Scritto in C# su .NET 10, con inter Nell'antico Egitto Anubis, il dio con la testa di sciacallo, pesava il cuore dei defunti contro la piuma di Maat, la verità e l'equilibrio, e custodiva le tombe. Il bot fa lo stesso con i soldi: **pesa** ogni operazione contro il rischio prima di farla e **protegge** il capitale. Il logo è quella bilancia, con il cuore e la piuma in equilibrio (ADR 0005). -> Stato: **progettazione completata, sviluppo non ancora iniziato.** Il piano è nelle [milestone](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/milestones) del repository. +> Stato: **in simulazione, pronto da installare in Docker.** Il piano è nelle [milestone](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/milestones) del repository. ![Panoramica](docs/design/01-panoramica.jpg) @@ -23,13 +23,16 @@ Nell'antico Egitto Anubis, il dio con la testa di sciacallo, pesava il cuore dei | Documento | Contenuto | |---|---| +| [docs/MANUALE.md](docs/MANUALE.md) | Installare, usare, salvare, ripristinare | +| [docs/RUNBOOK.md](docs/RUNBOOK.md) | Cosa fare negli incidenti | +| [docs/GO-LIVE.md](docs/GO-LIVE.md) | Cosa deve essere vero prima dei soldi veri (questa versione non li usa) | | [docs/SPEC.md](docs/SPEC.md) | Specifica completa: architettura, strategie, rischio, interfaccia, piano a fasi | | [docs/ROADMAP.md](docs/ROADMAP.md) | Le fasi con i lavori su Gitea e i criteri di accettazione | | [docs/STATE.md](docs/STATE.md) | A che punto siamo e cosa serve dall'utente | | [docs/SVILUPPO.md](docs/SVILUPPO.md) | Come si lavora: flusso su Gitea, regole, prove | | [docs/adr/](docs/adr/) | Decisioni prese: nome (0001), Gitea (0002), linguaggio C# (0003), componenti esterni (0004), logo (0005) | | [docs/design/](docs/design/) | Schermate di riferimento e logo | -| [deploy/unraid/anubis.xml](deploy/unraid/anubis.xml) | Template Docker per Unraid (bozza) | +| [deploy/unraid/anubis.xml](deploy/unraid/anubis.xml) | Template Docker per Unraid | ## Schermate di riferimento diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 6c2f15d..511c295 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -17,9 +17,11 @@ COPY docs/design/logo/ docs/design/logo/ # The backtest outcomes are read at the first start (SPEC 12.3); .dockerignore lets only their JSON files through. COPY docs/backtests/ docs/backtests/ COPY docs/learning/ docs/learning/ -RUN dotnet publish src/Anubis.Server/Anubis.Server.csproj -c Release --no-restore -o /app +# The commit goes into the version shown by the diagnostics report; empty when not given. +ARG ANUBIS_COMMIT= +RUN dotnet publish src/Anubis.Server/Anubis.Server.csproj -c Release --no-restore -o /app ${ANUBIS_COMMIT:+-p:SourceRevisionId=$ANUBIS_COMMIT} # The guard (SPEC 9.3, ADR 0012) is the main process and starts the engine next to it, from the same folder. -RUN dotnet publish src/Anubis.Guard/Anubis.Guard.csproj -c Release --no-restore -o /app +RUN dotnet publish src/Anubis.Guard/Anubis.Guard.csproj -c Release --no-restore -o /app ${ANUBIS_COMMIT:+-p:SourceRevisionId=$ANUBIS_COMMIT} FROM mcr.microsoft.com/dotnet/aspnet:10.0 WORKDIR /app @@ -35,7 +37,16 @@ EXPOSE 8787 VOLUME /data # Starts as root only to give /data to PUID:PGID, then runs the guard as that user (entrypoint.sh); the guard starts # the engine. "docker run --rm drill" runs the guard drill instead (issue #48). -# Health through bash's /dev/tcp: no curl or wget in the image. 503 (database down) counts as unhealthy. -HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \ +# Health through bash's /dev/tcp: no curl or wget in the image. 503 (database down, loops stopped, no prices) counts as unhealthy. +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD ["bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/${PORT:-8787} && printf 'GET /health HTTP/1.0\r\nHost: localhost\r\n\r\n' >&3 && head -n 1 <&3 | grep -q ' 200 '"] ENTRYPOINT ["anubis-entrypoint"] +ARG ANUBIS_VERSION=dev +ARG ANUBIS_COMMIT= +LABEL org.opencontainers.image.title="Anubis" \ + org.opencontainers.image.description="Bot di trading personale: azioni USA e crypto spot, senza leva" \ + org.opencontainers.image.vendor="BalboSoft" \ + org.opencontainers.image.url="https://gitea.encke-hake.ts.net/BalboSoft/Anubis" \ + org.opencontainers.image.source="https://gitea.encke-hake.ts.net/BalboSoft/Anubis" \ + org.opencontainers.image.version="$ANUBIS_VERSION" \ + org.opencontainers.image.revision="$ANUBIS_COMMIT" diff --git a/deploy/docker/entrypoint.sh b/deploy/docker/entrypoint.sh index ec68e96..02ff837 100644 --- a/deploy/docker/entrypoint.sh +++ b/deploy/docker/entrypoint.sh @@ -4,8 +4,18 @@ # The process is the guard (SPEC 9.3, ADR 0012): it starts the engine, watches it and passes the stop on to it. set -eu +# What the application writes (database, backups, logs) is readable only by its own user. +umask 077 + app="/app/Anubis.Guard.dll" +# A command ("docker run db import ...") runs once and ends: without the guard, which would start it again. +case "${1:-}" in + db|access|data|diagnostics|soak) + app="/app/Anubis.Server.dll" + ;; +esac + if [ "$(id -u)" != "0" ]; then exec dotnet "$app" "$@" fi @@ -19,6 +29,14 @@ case "$uid$gid" in ;; esac +case "$uid" in + *[!0]*) ;; + *) + echo "PUID=0 farebbe girare Anubis come amministratore: usa un utente normale (99 su Unraid)." >&2 + exit 64 + ;; +esac + mkdir -p /data for folder in db models research import backups logs web-keys guard; do mkdir -p "/data/$folder" @@ -28,4 +46,4 @@ find /data -xdev \( ! -user "$uid" -o ! -group "$gid" \) -exec chown -h "$uid:$g chmod 700 /data export HOME=/tmp -exec setpriv --reuid="$uid" --regid="$gid" --clear-groups -- dotnet "$app" "$@" +exec setpriv --reuid="$uid" --regid="$gid" --clear-groups --no-new-privs -- dotnet "$app" "$@" diff --git a/deploy/docker/publish.sh b/deploy/docker/publish.sh new file mode 100644 index 0000000..c1d92b5 --- /dev/null +++ b/deploy/docker/publish.sh @@ -0,0 +1,63 @@ +#!/bin/sh +# Publishes an image already built and tried (SPEC 15.5, ADR 0002 and 0020). +# Usage: publish.sh +# From the environment: REGISTRY, REGISTRY_USER, REGISTRY_TOKEN (write:package). The token reaches docker only on its +# input and stays in a temporary folder removed at the end. +set -eu + +image="$1" +tag="$2" +root=$(cd "$(dirname "$0")/../.." && pwd) +fail() { + echo "Pubblicazione non riuscita: $1" >&2 + exit 1 +} + +printf '%s\n' "$tag" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$' || fail "il tag '$tag' non è una versione (vX.Y.Z)." +version=${tag#v} +expected=$(sed -n 's:.*\(.*\).*:\1:p' "$root/Directory.Build.props") +[ "${version%%-*}" = "$expected" ] || fail "il tag $tag non corrisponde alla versione $expected di Directory.Build.props." +[ -n "${REGISTRY:-}" ] || fail "manca REGISTRY." +[ -n "${REGISTRY_USER:-}" ] || fail "manca REGISTRY_USER." +[ -n "${REGISTRY_TOKEN:-}" ] || fail "manca il segreto REGISTRY_TOKEN (token Gitea con il permesso write:package)." +name="$REGISTRY/balbosoft/anubis" + +# The image says what it was made from (its labels): only what comes from the commit of the tag is published as that tag. +# Without the tag here, as in the CI, the repository on Gitea says its commit: the line with ^{} for an annotated tag. +commit=$(git -C "$root" rev-parse --verify --quiet "refs/tags/$tag^{commit}") || { + found=$(git -C "$root" ls-remote origin "refs/tags/$tag" "refs/tags/$tag^{}") || fail "non riesco a leggere i tag del repository." + commit=$(printf '%s\n' "$found" | sort -k 2 | tail -n 1 | cut -f 1) +} +[ -n "$commit" ] || fail "il tag $tag non c'è, né qui né nel repository: crealo prima di pubblicare." +label() { + docker image inspect --format "{{ index .Config.Labels \"org.opencontainers.image.$1\" }}" "$image" +} +built=$(label version) || fail "non trovo l'immagine $image." +# The image built by hand for vX.Y.Z-something says X.Y.Z, the version of Directory.Build.props: the commit tells them apart. +[ "$built" = "$version" ] || [ "$built" = "$expected" ] || fail "l'immagine $image dice di essere la versione '$built', non $version: ricostruiscila." +source=$(label revision) || fail "non trovo l'immagine $image." +[ "$source" = "$commit" ] || fail "l'immagine $image viene dal commit '$source', non da $commit: ricostruiscila dal commit del tag, senza modifiche in sospeso." + +# latest moves only for the highest plain version among the tags of the repository. +latest="" +case "$version" in + *-*) ;; + *) + tags=$(git -C "$root" ls-remote --tags --refs origin 'v*') || fail "non riesco a leggere i tag del repository." + newest=$(printf '%s\n' "$tags" "x refs/tags/$tag" | sed -n 's:.*refs/tags/v\([0-9]*\.[0-9]*\.[0-9]*\)$:\1:p' | sort -t. -k1,1n -k2,2n -k3,3n | tail -n 1) + if [ "$newest" = "$version" ]; then latest=1; fi + ;; +esac + +DOCKER_CONFIG=$(mktemp -d) +export DOCKER_CONFIG +trap 'rm -rf "$DOCKER_CONFIG"' EXIT +printf '%s' "$REGISTRY_TOKEN" | docker login "$REGISTRY" --username "$REGISTRY_USER" --password-stdin +docker tag "$image" "$name:$version" +docker push "$name:$version" +if [ -n "$latest" ]; then + docker tag "$image" "$name:latest" + docker push "$name:latest" +fi + +echo "Pubblicata $name:$version${latest:+ (e latest)}." diff --git a/deploy/unraid/anubis.xml b/deploy/unraid/anubis.xml index 7c1e7d0..8d974d1 100644 --- a/deploy/unraid/anubis.xml +++ b/deploy/unraid/anubis.xml @@ -1,6 +1,6 @@