From c02c300dc384cfab2f795932c47f93b8e3a818e6 Mon Sep 17 00:00:00 2001 From: 1sonhador <1sonhador@gmail.com> Date: Tue, 24 Mar 2026 18:33:37 +0000 Subject: [PATCH] feat(deploy): version.json generation, webhook promote, verify (BIT-468) Align LXC deploy with BIT-465: write-version-json.sh, optional webhook-deploy.sh, verify.sh fails when ER_VERSION_JSON_PATH is set but missing/invalid, env.example + nginx sketch + runbook updates. Ignore deploy/lxc/runtime/ (generated on server). --- .gitignore | 2 + deploy/lxc/README.md | 4 +- deploy/lxc/env.example | 6 ++ ...itreport-error-reporting.site.conf.example | 7 ++ deploy/lxc/verify.sh | 27 +++++++ deploy/lxc/webhook-deploy.sh | 45 +++++++++++ deploy/lxc/write-version-json.sh | 77 +++++++++++++++++++ .../bitreport-error-reporting-lxc-proxmox.md | 19 ++++- 8 files changed, 185 insertions(+), 2 deletions(-) create mode 100755 deploy/lxc/webhook-deploy.sh create mode 100755 deploy/lxc/write-version-json.sh diff --git a/.gitignore b/.gitignore index 5d739cd..633586c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ node_modules/ dist/ .next/ +# Deploy-time artefact (BIT-465); generated on server by write-version-json.sh +deploy/lxc/runtime/ .env .env.local *.log diff --git a/deploy/lxc/README.md b/deploy/lxc/README.md index 7da0e85..fad131d 100644 --- a/deploy/lxc/README.md +++ b/deploy/lxc/README.md @@ -8,7 +8,9 @@ Authoritative runbook: [`docs/deployment/bitreport-error-reporting-lxc-proxmox.m |------|---------| | `env.example` | Template for `/etc/error-reporting/env` | | `run-migrate.sh` | Apply migrations + seed (after MariaDB is up) | -| `verify.sh` | `curl` ingest `/health` and optional checks | +| `write-version-json.sh` | Emit `runtime/version.json` (BIT-465); set `ER_VERSION_JSON_PATH` in env | +| `webhook-deploy.sh` | Example full promote: `git pull`, builds, `write-version-json`, `verify`, restarts | +| `verify.sh` | `curl` ingest `/health`; validates `version.json` when `ER_VERSION_JSON_PATH` is set | | `systemd/*.service` | Unit files (adjust `WorkingDirectory` if not `/opt/errors.bitreport.pt`) | | `nginx/*.conf.example` | TLS reverse proxy sketch | diff --git a/deploy/lxc/env.example b/deploy/lxc/env.example index 25353c3..0eb0cf6 100644 --- a/deploy/lxc/env.example +++ b/deploy/lxc/env.example @@ -6,6 +6,12 @@ ER_REPO_ROOT=/opt/errors.bitreport.pt # Legacy variable name (still read by run-migrate.sh if ER_REPO_ROOT is unset): # OTT_ROOT=/opt/errors.bitreport.pt +# --- BIT-465 version.json (written by deploy/lxc/write-version-json.sh; nginx can alias this path) --- +# Use the same tree as ER_REPO_ROOT (systemd EnvironmentFile= does not expand ${ER_REPO_ROOT}). +ER_VERSION_JSON_PATH=/opt/errors.bitreport.pt/deploy/lxc/runtime/version.json +# ER_VERSION_PROJECT=errors.bitreport.pt +# ER_VERSION_BUILT_BY=BitReport.pt + # --- MariaDB / MySQL (single LXC: localhost; split DB LXC: set host in URL) --- # Percent-encode special characters in user/password (RFC 3986) when building the URL. DATABASE_URL=mysql://er_app:CHANGE_ME@127.0.0.1:3306/error_reporting diff --git a/deploy/lxc/nginx/bitreport-error-reporting.site.conf.example b/deploy/lxc/nginx/bitreport-error-reporting.site.conf.example index f964544..6d054a1 100644 --- a/deploy/lxc/nginx/bitreport-error-reporting.site.conf.example +++ b/deploy/lxc/nginx/bitreport-error-reporting.site.conf.example @@ -27,6 +27,13 @@ server { # ssl_certificate ...; # ssl_certificate_key ...; + # BIT-465: static version.json from deploy (path must match ER_VERSION_JSON_PATH on disk). + # location = /version.json { + # alias /opt/errors.bitreport.pt/deploy/lxc/runtime/version.json; + # default_type application/json; + # add_header Cache-Control "no-store"; + # } + location / { proxy_pass http://127.0.0.1:3040; proxy_http_version 1.1; diff --git a/deploy/lxc/verify.sh b/deploy/lxc/verify.sh index c0c9468..a289c70 100755 --- a/deploy/lxc/verify.sh +++ b/deploy/lxc/verify.sh @@ -48,4 +48,31 @@ else echo "verify: admin-ui not responding on ${_ADMIN_PORT} (expected if service stopped or demo mode off)" fi +# BIT-465: fail deploy if version.json is missing or invalid when path is configured. +if [[ -n "${ER_VERSION_JSON_PATH:-}" ]]; then + if [[ ! -f "${ER_VERSION_JSON_PATH}" ]]; then + echo "verify: ER_VERSION_JSON_PATH missing file: ${ER_VERSION_JSON_PATH}" >&2 + exit 1 + fi + python3 - "${ER_VERSION_JSON_PATH}" <<'PY' +import json +import sys + +required = {"project", "commit", "branch", "builtAt", "builtBy"} +path = sys.argv[1] +with open(path, encoding="utf-8") as f: + data = json.load(f) +if not isinstance(data, dict): + sys.exit("verify: version.json root must be an object") +missing = required - data.keys() +if missing: + sys.exit("verify: version.json missing keys: " + ", ".join(sorted(missing))) +for k in required: + v = data[k] + if not isinstance(v, str) or not v.strip(): + sys.exit(f"verify: version.json invalid empty field {k!r}") +PY + echo "verify: version.json OK (${ER_VERSION_JSON_PATH})" +fi + echo "verify: done" diff --git a/deploy/lxc/webhook-deploy.sh b/deploy/lxc/webhook-deploy.sh new file mode 100755 index 0000000..71ca73b --- /dev/null +++ b/deploy/lxc/webhook-deploy.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Example promote script for Gitea webhook → LXC (BIT-465). Adjust branches, users, and +# restart policy for your host. Run as the user that owns the repo (often root on first setup). +set -euo pipefail + +_ENV_FILE="/etc/error-reporting/env" +if [[ ! -f "${_ENV_FILE}" ]]; then + echo "webhook-deploy: missing ${_ENV_FILE}" >&2 + exit 1 +fi + +set -a +# shellcheck disable=SC1090 +source "${_ENV_FILE}" +set +a + +_ROOT="${ER_REPO_ROOT:?ER_REPO_ROOT must be set in ${_ENV_FILE}}" +cd "${_ROOT}" + +# Match your deploy branch (repository uses master today). +_DEPLOY_BRANCH="${ER_DEPLOY_BRANCH:-master}" +git fetch origin "${_DEPLOY_BRANCH}" +git checkout "${_DEPLOY_BRANCH}" +git merge --ff-only "origin/${_DEPLOY_BRANCH}" + +echo "webhook-deploy: npm ci + build (ingest-api, admin-ui, collector-worker, api)" +(cd "${_ROOT}/ingest-api" && npm ci && npm run build) +(cd "${_ROOT}/admin-ui" && npm ci && npm run build) +(cd "${_ROOT}/collector-worker" && npm ci && npm run build) +(cd "${_ROOT}/api" && npm ci && npm run build) + +if getent passwd error-reporting >/dev/null 2>&1; then + chown -R error-reporting:error-reporting "${_ROOT}" +fi + +"${_ROOT}/deploy/lxc/write-version-json.sh" +"${_ROOT}/deploy/lxc/verify.sh" + +if [[ "${ER_WEBHOOK_RESTART_SERVICES:-1}" == "1" ]]; then + systemctl restart bitreport-error-reporting-ingest-api.service + systemctl restart bitreport-error-reporting-admin-ui.service + systemctl restart bitreport-error-reporting-collector-worker.service +fi + +echo "webhook-deploy: done" diff --git a/deploy/lxc/write-version-json.sh b/deploy/lxc/write-version-json.sh new file mode 100755 index 0000000..84df345 --- /dev/null +++ b/deploy/lxc/write-version-json.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# Generate deploy/lxc/runtime/version.json (BIT-465). Core logic matches +# paperclip tools/deploy/write_version_json.sh — keep behaviour in sync when changing. +# +# Typical: source /etc/error-reporting/env then run this script (no args). +# Or pass explicitly: ER_REPO_ROOT, ER_VERSION_JSON_PATH, ER_VERSION_PROJECT. +set -euo pipefail + +_ENV_FILE="/etc/error-reporting/env" +if [[ -f "${_ENV_FILE}" ]]; then + set -a + # shellcheck disable=SC1090 + source "${_ENV_FILE}" + set +a +fi + +_GIT_DIR="${ER_REPO_ROOT:-}" +if [[ -z "${_GIT_DIR}" ]]; then + _SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + _GIT_DIR="$(cd "${_SCRIPT_DIR}/../.." && pwd)" +fi + +_OUTPUT="${ER_VERSION_JSON_PATH:-${_GIT_DIR}/deploy/lxc/runtime/version.json}" +_PROJECT="${ER_VERSION_PROJECT:-errors.bitreport.pt}" + +if [[ ! -d "${_GIT_DIR}/.git" && ! -f "${_GIT_DIR}/.git" ]]; then + echo "write-version-json: not a git repository: ${_GIT_DIR}" >&2 + exit 1 +fi + +_COMMIT="$(git -C "${_GIT_DIR}" rev-parse HEAD)" +_BRANCH="$(git -C "${_GIT_DIR}" rev-parse --abbrev-ref HEAD)" +_BUILT_BY="${ER_VERSION_BUILT_BY:-BitReport.pt}" +_BUILT_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + +_OUT_DIR="$(dirname "${_OUTPUT}")" +mkdir -p "${_OUT_DIR}" + +_TMP="${_OUTPUT}.tmp.$$" +python3 - "${_TMP}" "${_PROJECT}" "${_COMMIT}" "${_BRANCH}" "${_BUILT_AT}" "${_BUILT_BY}" <<'PY' +import json +import sys + +path, project, commit, branch, built_at, built_by = sys.argv[1:7] +doc = { + "project": project, + "commit": commit, + "branch": branch, + "builtAt": built_at, + "builtBy": built_by, +} +with open(path, "w", encoding="utf-8") as f: + json.dump(doc, f, indent=2, sort_keys=False) + f.write("\n") +PY + +python3 - "${_TMP}" <<'PY' +import json +import sys + +required = {"project", "commit", "branch", "builtAt", "builtBy"} +path = sys.argv[1] +with open(path, encoding="utf-8") as f: + data = json.load(f) +if not isinstance(data, dict): + sys.exit("version.json: root must be an object") +missing = required - data.keys() +if missing: + sys.exit("version.json: missing keys: " + ", ".join(sorted(missing))) +for k in required: + v = data[k] + if not isinstance(v, str) or not v.strip(): + sys.exit(f"version.json: invalid or empty string for {k!r}") +PY + +mv -f "${_TMP}" "${_OUTPUT}" +echo "write-version-json: wrote ${_OUTPUT} (commit ${_COMMIT:0:12})" diff --git a/docs/deployment/bitreport-error-reporting-lxc-proxmox.md b/docs/deployment/bitreport-error-reporting-lxc-proxmox.md index 3408978..eee083a 100644 --- a/docs/deployment/bitreport-error-reporting-lxc-proxmox.md +++ b/docs/deployment/bitreport-error-reporting-lxc-proxmox.md @@ -145,6 +145,21 @@ systemctl status bitreport-error-reporting-ingest-api.service journalctl -u bitreport-error-reporting-ingest-api.service -f ``` +### 10.1 `version.json` (BIT-465) + +Gerar metadados de build/commit após cada `git pull` / promote (ficheiro **não** vai para o Git — `deploy/lxc/runtime/` está em `.gitignore`): + +```bash +set -a +source /etc/error-reporting/env +set +a +/opt/errors.bitreport.pt/deploy/lxc/write-version-json.sh +``` + +Com `ER_VERSION_JSON_PATH` definido no env (ver `deploy/lxc/env.example`), o `verify.sh` **falha** se o JSON estiver ausente ou não cumprir o schema mínimo (`project`, `commit`, `branch`, `builtAt`, `builtBy`). Fluxo completo de exemplo após push no Gitea: `deploy/lxc/webhook-deploy.sh` (ajustar ramo, utilizador e `systemctl` ao teu host). + +Para `https://errors.bitreport.pt/version.json` sem VPN, configurar o proxy (Nginx ou NPM *Advanced*) com `location = /version.json` a servir o ficheiro no disco — ver comentário em `deploy/lxc/nginx/bitreport-error-reporting.site.conf.example`. + ## 11. Fixture HTTP de demo (opcional) No Compose, o worker usa um contentor nginx com `collector-worker/fixture/errors.json`. Em LXC nativo, para reproduzir o cenário de poll HTTP: @@ -233,7 +248,8 @@ Se o domínio estiver atrás de **Cloudflare** e o utilizador vir **525 SSL hand | 1 | No LXC: `/opt/errors.bitreport.pt/deploy/lxc/verify.sh` | Sucesso local (`ingest` `/health` HTTP **200**) | | 2 | `curl -sS -o /dev/null -w '%{http_code}\n' https://ingest./health` | **200** | | 3 | `curl -sS -o /dev/null -w '%{http_code}\n' https://errors.bitreport.pt/` | **200** ou redireccionamento esperado da UI | -| 4 | Browser | Smoke de login / navegação na admin UI | +| 4 | `curl -fsS https://errors.bitreport.pt/version.json` (após configurar *location* estático no proxy) | **200**, JSON com `commit` alinhado ao deploy | +| 5 | Browser | Smoke de login / navegação na admin UI | **Nota:** tráfego público pode mostrar `server: cloudflare`; a origem continua **NPM → LXC da stack**. Até TLS/DNS estarem correctos, `curl` externo pode responder **525** — usar a tabela acima como critério após go-live. @@ -244,6 +260,7 @@ Se o domínio estiver atrás de **Cloudflare** e o utilizador vir **525 SSL hand | Env exemplo | `deploy/lxc/env.example` | | Migrações + seed | `deploy/lxc/run-migrate.sh` | | Smoke test | `deploy/lxc/verify.sh` | +| `version.json` + promote webhook | `deploy/lxc/write-version-json.sh`, `deploy/lxc/webhook-deploy.sh` | | Units systemd | `deploy/lxc/systemd/` | | Nginx exemplo | `deploy/lxc/nginx/` | | Acesso público (NPM/DNS) | Este documento — §14 |