Phase 1, PR 1: skeleton (config, source clients, DB, monitor loop, health, CI, deploy) #5

Merged
jared merged 4 commits from skeleton into main 2026-11-25 22:51:39 -05:00
Owner

First of six phase 1 PRs from the plan on #1. No checks yet: this is the frame every check runs in. Already running on CT 173 (bootstrapped from this branch), with all four sources answering.

What's here

  • Settings (thor/config.py): non-secret /etc/thor/thor.json plus secrets in /etc/thor/*.env (root:thor 0640, never in git). Every check defaults to shadow.
  • Source clients (thor/clients/), all read-only. A source that can't answer raises SourceError, which means "unknown", never "nothing found".
    • Prometheus: instant and range queries.
    • Loki: lifted from Gandalf's reboots.py (gandalf@1c250d6), now keeping stream labels and refusing windows over 30 days.
    • Proxmox: token thor@pve!monitor (PVEAuditor). Tries the nodes in turn and remembers the one that answered. A refused token fails at once instead of trying every node. ceph_flags() includes pause.
    • Gandalf: machine key from gandalf#68, suppressions with include_ended.
    • Tinker: adapted from Gandalf's TicketClient; every create sends dedup_key (tinker_tickets#123). Nothing calls it yet.
  • Database (thor/db.py): schema thor on CT 149, versioned migrations, UTC times. Every failure is a DatabaseError the caller survives.
  • Monitor loop (thor/monitor.py), 120 s, following the Area 1 rules:
    • Each cycle reviews the window since the last good one; after downtime it catches up for at most 24 h.
    • A source that can't answer opens a blind period; it doesn't fail the cycle.
    • Writes are buffered in memory while CT 149 is away (bounded), and the loop keeps running.
    • A failing check leaves its window to be reviewed again next cycle; other checks still run.
  • Web (thor/web/): /health checks the DB, monitor freshness and the deployed commit, and returns 503 when stale or when the DB is away. /metrics exposes thor_last_good_cycle_timestamp_seconds, thor_source_up, thor_cycle_duration_seconds and thor_build_info.
  • CI (.gitea/workflows/ci.yml), modelled on Pulse's:
    • flake8 and the em/en dash check
    • pytest with a 90% coverage floor
    • MariaDB integration tests
    • bandit, pip-audit
    • gitleaks, actionlint, shellcheck, zizmor, semgrep
    • Pinned actions and a pinned image digest (node:22-trixie, so Python 3.13 like prod).
    • main deploys through a signed webhook, then waits for /health to report the commit.
  • Deploy (deploy/):
    • thor_deploy.sh, Pulse's pattern: exact sha, timestamp and replay checks, installs units and the nginx site only when changed, rolls back unless /health reports the sha with a working DB. Sandbox-tested.
    • bootstrap.sh: idempotent first install.
    • Hardened systemd units.
    • The webhook runs as its own user, with a sudoers entry for the deploy script only.

Verification

  • 51 unit tests pass, plus 4 DB tests against a real MariaDB. Coverage is 95% without the DB.
  • flake8, bandit, shellcheck, actionlint, zizmor and semgrep (238 rules) are clean locally. No em or en dashes.
  • On CT 173: migration 1 applied; /health returns {"db":true,"monitor":"ok","sha":"b0e240a...","status":"ok"}; first cycle took 0.2 s with no blind sources.

Not in this PR

The engine, the memory check and the T1 replay (PR 2), reboots and known conditions with the T2 to T4 replays (PR 3), /covering and the UI (PR 4, comes with screenshots), MCP (PR 5), and Kuma/Prometheus/Grafana watching Thor (PR 6).

🤖 Generated with Claude Code

First of six phase 1 PRs from the plan on #1. No checks yet: this is the frame every check runs in. Already running on CT 173 (bootstrapped from this branch), with all four sources answering. ## What's here - **Settings** (`thor/config.py`): non-secret `/etc/thor/thor.json` plus secrets in `/etc/thor/*.env` (root:thor 0640, never in git). Every check defaults to `shadow`. - **Source clients** (`thor/clients/`), all read-only. A source that can't answer raises `SourceError`, which means "unknown", never "nothing found". - Prometheus: instant and range queries. - Loki: lifted from Gandalf's `reboots.py` (gandalf@1c250d6), now keeping stream labels and refusing windows over 30 days. - Proxmox: token `thor@pve!monitor` (PVEAuditor). Tries the nodes in turn and remembers the one that answered. A refused token fails at once instead of trying every node. `ceph_flags()` includes `pause`. - Gandalf: machine key from gandalf#68, suppressions with `include_ended`. - Tinker: adapted from Gandalf's `TicketClient`; every create sends `dedup_key` (tinker_tickets#123). Nothing calls it yet. - **Database** (`thor/db.py`): schema `thor` on CT 149, versioned migrations, UTC times. Every failure is a `DatabaseError` the caller survives. - **Monitor loop** (`thor/monitor.py`), 120 s, following the Area 1 rules: - Each cycle reviews the window since the last good one; after downtime it catches up for at most 24 h. - A source that can't answer opens a blind period; it doesn't fail the cycle. - Writes are buffered in memory while CT 149 is away (bounded), and the loop keeps running. - A failing check leaves its window to be reviewed again next cycle; other checks still run. - **Web** (`thor/web/`): `/health` checks the DB, monitor freshness and the deployed commit, and returns 503 when stale or when the DB is away. `/metrics` exposes `thor_last_good_cycle_timestamp_seconds`, `thor_source_up`, `thor_cycle_duration_seconds` and `thor_build_info`. - **CI** (`.gitea/workflows/ci.yml`), modelled on Pulse's: - flake8 and the em/en dash check - pytest with a 90% coverage floor - MariaDB integration tests - bandit, pip-audit - gitleaks, actionlint, shellcheck, zizmor, semgrep - Pinned actions and a pinned image digest (`node:22-trixie`, so Python 3.13 like prod). - `main` deploys through a signed webhook, then waits for `/health` to report the commit. - **Deploy** (`deploy/`): - `thor_deploy.sh`, Pulse's pattern: exact sha, timestamp and replay checks, installs units and the nginx site only when changed, rolls back unless `/health` reports the sha with a working DB. Sandbox-tested. - `bootstrap.sh`: idempotent first install. - Hardened systemd units. - The webhook runs as its own user, with a sudoers entry for the deploy script only. ## Verification - 51 unit tests pass, plus 4 DB tests against a real MariaDB. Coverage is 95% without the DB. - flake8, bandit, shellcheck, actionlint, zizmor and semgrep (238 rules) are clean locally. No em or en dashes. - **On CT 173:** migration 1 applied; `/health` returns `{"db":true,"monitor":"ok","sha":"b0e240a...","status":"ok"}`; first cycle took 0.2 s with no blind sources. ## Not in this PR The engine, the memory check and the T1 replay (PR 2), reboots and known conditions with the T2 to T4 replays (PR 3), `/covering` and the UI (PR 4, comes with screenshots), MCP (PR 5), and Kuma/Prometheus/Grafana watching Thor (PR 6). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jared added 3 commits 2026-11-25 22:37:05 -05:00
Work in progress for thor#1 phase 1 (PR 1 of 6). Not yet pushed: CI workflow and deploy files still to add.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- .gitea/workflows/ci.yml: flake8 + dash check, pytest (90% coverage floor), MariaDB
  integration tests, bandit, pip-audit, gitleaks, actionlint, shellcheck, zizmor, semgrep;
  main deploys through a signed webhook and waits for /health to report the commit.
- deploy/thor_deploy.sh: deploys an exact sha, installs units and nginx site when changed,
  rolls back unless /health reports the sha with a working database. Sandbox-tested.
- deploy/bootstrap.sh: idempotent first install on CT 173.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Unit tests for the DB connection layer and Tinker error paths (coverage 95% without MariaDB)
CI / Lint (flake8, house style) (pull_request) Successful in 2m14s
CI / Test (pytest) (pull_request) Canceled after 0s
CI / Integration (MariaDB) (pull_request) Canceled after 0s
CI / Security (bandit, pip-audit) (pull_request) Canceled after 0s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Canceled after 0s
CI / Deploy (pull_request) Canceled after 0s
b0e240af50
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared added 1 commit 2026-11-25 22:40:36 -05:00
Test: restart while the database is away still catches up once it answers (T2 order)
CI / Test (pytest) (pull_request) Successful in 52s
CI / Integration (MariaDB) (pull_request) Successful in 1m50s
CI / Security (bandit, pip-audit) (pull_request) Successful in 1m4s
CI / Lint (flake8, house style) (pull_request) Successful in 58s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Successful in 2m57s
CI / Deploy (pull_request) Skipped
f3d0b5e11a
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared merged commit e676a8e658 into main 2026-11-25 22:51:39 -05:00
Author
Owner

Merged and deployed through CI (run 4019: lint, test, integration, security, scan, deploy all green).

Deploy verified on CT 173:

  • Prod HEAD: e676a8e6587de77d8e22a503fd9ad529aacd15c6 (the merge commit); /opt/thor/revision.env matches.
  • thor, thor-monitor and webhook are active.
  • /health: {"db":true,"monitor":"ok","sha":"e676a8e...","status":"ok"}, HTTP 200.
  • /var/log/thor-deploy.log: deploying e676a8e... (previous b0e240a...), then deployed e676a8e...: healthy, 3 s later.
  • No tracebacks in journalctl -u thor -u thor-monitor.

Also set up for this deploy: repo secret WEBHOOK_SECRET (copied from the CT without printing it); thor.lotusguild.org in NPM (proxy host 60, cloned from Gandalf's host 50, wildcard cert) behind the Authelia rule two_factor, group admin.

Merged and deployed through CI (run 4019: lint, test, integration, security, scan, deploy all green). **Deploy verified on CT 173:** - Prod HEAD: `e676a8e6587de77d8e22a503fd9ad529aacd15c6` (the merge commit); `/opt/thor/revision.env` matches. - `thor`, `thor-monitor` and `webhook` are active. - `/health`: `{"db":true,"monitor":"ok","sha":"e676a8e...","status":"ok"}`, HTTP 200. - `/var/log/thor-deploy.log`: `deploying e676a8e... (previous b0e240a...)`, then `deployed e676a8e...: healthy`, 3 s later. - No tracebacks in `journalctl -u thor -u thor-monitor`. Also set up for this deploy: repo secret `WEBHOOK_SECRET` (copied from the CT without printing it); `thor.lotusguild.org` in NPM (proxy host 60, cloned from Gandalf's host 50, wildcard cert) behind the Authelia rule `two_factor`, group admin.
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/thor#5