Docs restructure, code review, gatherer reliability, JWT key fail-closed

Documentation
- Three levels: DOCS/General (overview, configuration), DOCS/Deployment
  (choose a method, first steps), details per method in Containers/ and
  System_Services/; new root README and DOCS/README map.
- Remove superseded pages (Deployment.md, Deployment_Docker/Native.md, Index.md);
  split the two scenario guides by section.
- Isolate the Timeweb/Hysteria2 records in DEPLOYMENTS/timeweb-hysteria2/
  (not linked from DOCS, listed in .graphifyignore).
- Add DOCS/code_review/ with a time-stamped analysis of the code base.

Gatherer (BUG-02, BUG-03)
- Session key (CN, address, connected_since_ts), baseline from the
  active_sessions snapshot, grace period for sessions missing from the status.
- Status file integrity (TITLE/TIME/END) and freshness; a torn read skips the tick.
- Cache applied after commit; main loop survives exceptions, backs off, and
  stops cleanly on SIGTERM/SIGINT; new client rows start with the first
  frame's totals; five tests.

JWT signing key (SEC-01)
- No built-in default: Core and Profiler refuse to start with a missing,
  placeholder or short (< 32) key; shipped config.ini has an empty value.
- Same rule in install.sh, update.sh (issues a new key when missing or weak),
  docker lib/entrypoint; five tests, conftest.py for the test key.

Each change has a plan and a summary under DOCS/Changes/.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-05 20:30:45 +03:00
1 parent f95a0b63e6
commit ad6995bb63
85 files changed
+2805 -1280

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
# Containers: update, backup, restore, rollback
Day-2 operations of the container installation. Every update takes a verified backup first and rolls back automatically on failure. Install: [Containers](README.md).
## Update
```bash
deploy/docker/update.sh # variant and project from .ovpmon-deploy
deploy/docker/update.sh --yes # no questions
deploy/docker/update.sh --no-pull # sources already in place / offline (no git pull, no base image refresh)
deploy/docker/update.sh --rollback # manual rollback to the newest backup
```
Each phase is logged separately:
| Phase | What happens | If it fails |
|---|---|---|
| 0 Preflight | state file, `.env`, compose configuration, running stack, the other variant not running, `db_data` volume exists, free space | nothing was changed |
| 1 **Backup** | all five volumes → `backups/<UTC timestamp>/*.tar.gz` through a throwaway `alpine` container (volumes mounted read-only), copy of `.env` (mode 600), record of the current image IDs, `SHA256SUMS`; every archive is verified (`tar -tzf`) | the update is **refused**, nothing was changed |
| 2 Sources | `git pull --ff-only` when the directory is a git checkout and `--no-pull` was not given | nothing was changed |
| 3 Build | current images are tagged `<name>:previous`, then the new images are built while the old stack keeps running | nothing was changed, the old version still runs |
| 4 Stop | the application containers are stopped (volumes are kept); a second, final backup of the volumes is taken now that nothing writes to the databases | automatic rollback |
| 5 **Database migration** | a separate step, see below | automatic rollback, nothing is started |
| 6 Start | `up -d` with the new images, wait for the health checks | automatic rollback |
Old backups: the newest 5 are kept; older ones are pruned only after a successful update. Volumes are never deleted and `down -v` is never used. The new `deploy/docker/*.sh` take effect on the next run (the running script is not replaced).
### Database migration as its own step
After the containers are stopped and before any new component starts, `update.sh` runs, for each database, a one-off container **from the new image with the same volumes**:
| Variant | Profiler database | Monitoring database |
|---|---|---|
| B | `app-profiler` image: `cd /app && python migrate.py` | `app-api` image: `cd /app && python migrate.py` |
| A | `ovpmon` image: `cd /app/APP_PROFILER && python migrate.py` | `ovpmon` image: `cd /app/APP_CORE && python migrate.py` |
For each database it runs `python migrate.py --check` (exit 0 up to date, 2 pending, 1 error or database newer than the code), applies the pending migrations with `python migrate.py` and checks again. The reports are printed. A failure or a "too new" database aborts **before anything is started** and triggers the rollback.
What the migration does (details: [Database migrations](../../General/Database_Migrations.md)): versioned, additive-only changes (`CREATE ... IF NOT EXISTS`, `ADD COLUMN` with defaults), never a drop or rewrite; before any change an integrity check and a consistent copy `<db>.bak-<UTC timestamp>` next to the database (newest 5 kept); after the change an integrity check and a comparison of row counts; on a mismatch everything is rolled back; a database newer than the application is not touched.
The migration also runs by itself when a service starts (variant B: profiler and API; variant A: the entrypoint, before `supervisord`), so a plain `docker compose up -d --build` is safe too. `update.sh` only makes it a visible, separate, controlled step.
### Automatic rollback
On any failure in phases 4 to 6: the stack is stopped, all five volumes are restored from the backup of this run, the `:previous` images are started (through a generated `compose.previous.yml` in the backup directory) and the result is reported with the backup path. If the rollback itself fails, the message says where the backup is and how to retry (`update.sh --rollback`, or `restore.sh`).
After a rollback the stack runs on the `:previous` images; the next `update.sh` run builds again and recreates the containers on the new images.
## Backup, restore, rollback
### Backup
```bash
deploy/docker/backup.sh # live backup of the five volumes -> backups/<UTC timestamp>/
deploy/docker/backup.sh --stop # stops the containers during the copy (consistent SQLite files), starts them again
deploy/docker/backup.sh --keep 14 # afterwards keep the newest 14
```
Cron example (nightly, consistent, 14 kept): `30 3 * * * /opt/ovpmon/deploy/docker/backup.sh --stop --keep 14 >> /var/log/ovpmon-backup.log 2>&1`.
A live backup copies the PKI and configuration reliably; a database file that is written at that very moment may be caught half-way, which `--stop` avoids (and `update.sh` always backs up a stopped stack for its final snapshot). The backup holds private keys, password hashes and the secret: the directory is mode 700, the files mode 600. Store copies off the host and protect them.
Manual equivalent for one volume (project `ovpmon`):
```bash
mkdir -p backups/manual
docker run --rm -v ovpmon_db_data:/data:ro -v "$PWD/backups/manual":/backup alpine \
tar -czf /backup/db_data.tar.gz -C /data .
```
### Restore
```bash
deploy/docker/restore.sh backups/20261005T101500Z # asks, stops the stack, restores, prints the start command
deploy/docker/restore.sh backups/20261005T101500Z --yes --start
```
It verifies the archives first, saves the current content to `backups/<timestamp>-pre-restore`, empties each volume and fills it from the archive. Volumes are not removed. `.env` is not touched (a difference to the copy in the backup is reported). Manual equivalent:
```bash
docker compose stop # or: -f docker-compose.allinone.yml
docker run --rm -v ovpmon_db_data:/data -v "$PWD/backups/20261005T101500Z":/backup:ro alpine \
sh -c 'rm -rf /data/* /data/.[!.]* /data/..?* && tar -xzf /backup/db_data.tar.gz -C /data'
docker compose up -d
```
A single database can also be put back from its migration backup in `db_data`: stop the stack, copy `<db>.bak-<timestamp>` over the database file, start the OLD release (see [Database migrations](../../General/Database_Migrations.md)).
### Rollback to the previous version
```bash
deploy/docker/update.sh --rollback
```
After confirmation: stop, restore all volumes from the newest complete backup (current content saved first), start the images recorded in that backup. Use it when an update completed but the new version misbehaves. Data created after that backup is lost (it is in the `-pre-restore` copy).
## Switching between variants
Both variants use the same volumes, so with the same project name the data can be used by the other variant: stop the running one (`docker compose [-f docker-compose.allinone.yml] stop`), take a backup, run `install.sh --variant <other> --project <same name>`. This path has not been tested in a real run; keep the backup. Starting both at once is refused.
## What is preserved on update
| Item | On update |
|---|---|
| Databases (`db_data`) | Kept. Backed up twice (before and after the stop), migrated additively with their own `<db>.bak-*` copy; restored automatically if the update fails after the stop |
| PKI (`ovp_pki`): CA, keys, certificates, CRL | Kept, backed up. The easy-rsa scripts are copied only when the volume does not have them yet |
| Client profiles (`ovp_client_config`) | Kept, backed up |
| Server configuration (`ovp_config`) | Kept, backed up (the new image does not overwrite it) |
| OpenVPN logs and status file (`ovp_logs`) | Kept |
| `.env` (secret, ports) | Kept, copied into every backup |
| User sessions | Stay valid while `JWT_SECRET` is unchanged |
| Images | Replaced; the previous ones stay tagged `<name>:previous` |
| Containers | Recreated; running VPN sessions are interrupted and clients reconnect; the OpenVPN process has to be started again (see troubleshooting) |
| Container file system, `profiler.log`, `openvpn_gatherer.log` | Not kept (use `docker logs`) |
| Backups in `backups/` | The newest 5 kept, older ones pruned after a successful update; `-pre-restore` copies are never pruned automatically |
| Volumes | Never deleted by any script; `down -v` is never used |
Related: [Database migrations](../../General/Database_Migrations.md), [Deployment plan](../../Changes/2026-10-05_Deployment_Scenarios_PLAN.md).