diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0189274 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,20 @@ +# keep the build context lean — only backend/ and frontend/ sources matter +.git/ +.gitea/ +venv/ +wiki/ +discovering/ +log.json +CLAUDE.md +compose.yaml + +# built/installed inside the image, never copied in +frontend/node_modules/ +frontend/dist/ +frontend/tsconfig.tsbuildinfo + +# runtime state — must never leak into an image +backend/backend.db +backend/events.jsonl +backend/config.json* +**/__pycache__/ diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..77f07e7 --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,95 @@ +name: Build Art IoT image + +on: + push: + branches: [main] + paths: + - 'backend/**' + - 'frontend/**' + - 'Dockerfile' + - '.dockerignore' + - 'compose.yaml' + - '.gitea/workflows/build.yml' + workflow_dispatch: + +jobs: + build-and-publish: + runs-on: ubuntu-22.04 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + # ------------------------------------------------------------------------- + # Plain `docker build` (not buildx) so the image is visible to `docker run` + # for the smoke test below — buildx's docker-container driver exports to a + # separate buildkitd cache that `docker run` can't see. + # ------------------------------------------------------------------------- + - name: Build image + run: docker build -t artiot:ci . + + # ------------------------------------------------------------------------- + # Smoke test gate: 65 checks against the freshly built image — REST auth/ + # scopes, per-gun cards, pending-card workflow, OCPP WS round-trips via a + # fake charge point, session store, SPA serving. All in-process (fastapi + # TestClient) against a temp db; no network listeners needed. + # + # --user root only because httpx (TestClient's transport, test-only dep) + # isn't shipped in the runtime image and uid 1000 can't pip-install. + # If this fails, the workflow halts and the broken image is never pushed. + # ------------------------------------------------------------------------- + - name: Smoke test + timeout-minutes: 3 + run: | + docker run --rm --user root artiot:ci sh -c \ + "pip install --no-cache-dir -q httpx && python -m backend.smoke_test" + + # ------------------------------------------------------------------------- + # Registry login — only runs if the smoke test passed. + # ------------------------------------------------------------------------- + - name: Login to Gitea registry + uses: docker/login-action@v3 + with: + registry: git.infra.msai.al + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + # ------------------------------------------------------------------------- + # Two tags: + # : — mutable; the compose stack's default (ARTIOT_TAG=main). + # : — immutable; set ARTIOT_TAG= in Komodo for a pinned + # rollback. + # ------------------------------------------------------------------------- + - name: Tag and push image + env: + BRANCH: ${{ github.ref_name }} + SHA: ${{ github.sha }} + IMG: git.infra.msai.al/art/charging_station + run: | + docker tag artiot:ci $IMG:$BRANCH + docker tag artiot:ci $IMG:$SHA + docker push $IMG:$BRANCH + docker push $IMG:$SHA + + # ------------------------------------------------------------------------- + # Trigger the Komodo Stack redeploy. HMAC-validated webhook — same pattern + # as the polymarket-bot repo. Both secrets must be set on this repo: + # KOMODO_STACK_WEBHOOK_URL, KOMODO_WEBHOOK_SECRET + # ------------------------------------------------------------------------- + - name: Trigger Komodo Stack redeploy + if: success() + env: + URL: ${{ secrets.KOMODO_STACK_WEBHOOK_URL }} + SECRET: ${{ secrets.KOMODO_WEBHOOK_SECRET }} + run: | + if [ -z "$URL" ] || [ -z "$SECRET" ]; then + echo "Komodo webhook secrets not set — skipping redeploy trigger." + exit 0 + fi + body='{"ref":"refs/heads/${{ github.ref_name }}"}' + sig=$(printf '%s' "$body" | openssl dgst -sha256 -hmac "$SECRET" | awk '{print $2}') + curl -fsS -X POST \ + -H 'Content-Type: application/json' \ + -H "X-Hub-Signature-256: sha256=$sig" \ + -d "$body" \ + "$URL" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4b733fc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,58 @@ +# ---------------------------------------------------------------------------- +# Art IoT — one image: OCPP 1.6J CSMS + REST API + Tuya relay control, serving +# the built React panel. Two stages: +# 1. node builds frontend/ -> dist/ +# 2. python:slim runs `python -m backend.main` on :9000, with dist/ where +# backend/main.py expects it (/app/frontend/dist). +# +# All mutable state (backend.db, events.jsonl) lives on the /data volume via +# the BACKEND_DB / BACKEND_EVENTS env overrides — the container is disposable. +# +# Build: docker build -t artiot:dev . +# Run: docker run -p 9000:9000 -v artiot-data:/data artiot:dev +# ---------------------------------------------------------------------------- + +FROM node:24-alpine AS panel +WORKDIR /build +# deps first — separate layer so source edits don't bust the npm cache +COPY frontend/package.json frontend/package-lock.json ./ +RUN npm ci +COPY frontend/ ./ +RUN npm run build + + +FROM python:3.13-slim + +# curl for the compose healthcheck; tini reaps zombies and forwards SIGTERM so +# `docker stop` shuts uvicorn down cleanly (capped at 3s by main.py, because +# the charger holds its WebSocket open forever). +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + ca-certificates \ + curl \ + tini \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +# Python deps first — separate layer, pinned exactly to requirements.txt. +COPY backend/requirements.txt /app/backend/requirements.txt +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir -r /app/backend/requirements.txt + +COPY backend/ /app/backend/ +COPY --from=panel /build/dist/ /app/frontend/dist/ + +# Persistent state. Owned by UID 1000 so the non-root process can write it. +RUN mkdir -p /data && chown -R 1000:1000 /data +ENV BACKEND_DB=/data/backend.db \ + BACKEND_EVENTS=/data/events.jsonl \ + PYTHONUNBUFFERED=1 +VOLUME ["/data"] + +# 9000 is load-bearing: hardcoded in the charger's touchscreen URL. +EXPOSE 9000 + +USER 1000:1000 +ENTRYPOINT ["/usr/bin/tini", "--"] +CMD ["python", "-m", "backend.main"] diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..ae55029 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,62 @@ +# ---------------------------------------------------------------------------- +# Art IoT production stack — one service, deployed by Komodo, panel routed +# through Traefik. +# +# Two front doors, same container port: +# * ws://:9000/ — the charger dials this DIRECTLY +# (published port below). The URL is hardcoded in the charger's touch- +# screen and changing it needs a power-cycle, so the Docker host must be +# reachable at that exact IP:port (DHCP reservation!). No TLS — the +# JC-6620 speaks plain ws://. +# * https://${ARTIOT_HOST} — the human-facing panel via Traefik. +# +# Komodo env (all optional, defaults below): +# ARTIOT_TAG image tag — `main` for rolling, a commit SHA to pin +# ARTIOT_HOST panel hostname routed by Traefik +# TRAEFIK_NETWORK name of the external Traefik docker network +# ARTIOT_ENTRYPOINT Traefik entrypoint (default websecure) +# ARTIOT_CERTRESOLVER Traefik cert resolver (default letsencrypt) +# +# First deploy on a new host: copy the existing backend.db into the volume — +# docker compose cp ./backend/backend.db artiot:/data/backend.db (then restart) +# or bootstrap fresh: docker compose exec artiot python -m backend.users add --scopes admin +# +# The relays are controlled over the LAN (tinytuya, TCP to the device IPs) — +# the Docker host must sit on a subnet that reaches them. +# ---------------------------------------------------------------------------- + +services: + artiot: + image: ${ARTIOT_REGISTRY:-git.infra.msai.al/art/charging_station}:${ARTIOT_TAG:-main} + container_name: artiot + restart: unless-stopped + ports: + # OCPP — the charger's hardcoded dial-in. Keep 9000:9000, always. + - "9000:9000" + volumes: + # backend.db + events.jsonl; survives `docker compose down`, only + # `down -v` wipes it. + - artiot-data:/data + networks: + - traefik-net + healthcheck: + test: ["CMD", "curl", "-fsS", "http://localhost:9000/api/health"] + interval: 15s + timeout: 3s + retries: 5 + start_period: 10s + labels: + - traefik.enable=true + - traefik.http.routers.artiot.rule=Host(`${ARTIOT_HOST:-artiot.infra.msai.al}`) + - traefik.http.routers.artiot.entrypoints=${ARTIOT_ENTRYPOINT:-websecure} + - traefik.http.routers.artiot.tls.certresolver=${ARTIOT_CERTRESOLVER:-letsencrypt} + - traefik.http.services.artiot.loadbalancer.server.port=9000 + +volumes: + artiot-data: + name: artiot-data + +networks: + traefik-net: + external: true + name: ${TRAEFIK_NETWORK:-traefik}