Database migrations with backup, integrity checks and downgrade protection
- Profiler and monitoring: schema_migrations table, additive ordered
migrations, single write lock, integrity check, consistent backup via
the SQLite backup API (newest 5 kept), row-count verification before
commit, refusal to start on a database newer than the code.
- Profiler baseline migration is generated from the models and adds every
missing column (replaces ensure_columns and the manual add_*.py scripts).
- migrate.py in each component (--check exit 0/2/1, --db); services migrate
at start, OVPMON_AUTO_MIGRATE=0 only checks.
- Tests: test_migrations.py per component; verified on databases created by
the code of commit 3836049 and on the live stand.
- Docs: Database_Migrations reference, change summary, deployment
scenarios plan, README, Index.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
56dae90ff5
commit
e556d6d4b9
15 files changed
+1729
-127
No files matched your search
@@ -0,0 +1,77 @@
|
||||
# Deployment scenarios and safe upgrade: plan (2026-10-05)
|
||||
|
||||
Goal: two documented, scripted scenarios.
|
||||
- **Scenario 1, containers:** A) all components in one container; B) one container per component.
|
||||
- **Scenario 2, system services, install and update:** A) Alpine, B) Debian, C) Ubuntu.
|
||||
|
||||
Priority: **an update must not damage the databases; a database must survive the migration** that a new version needs.
|
||||
|
||||
(The request lists "variant A" three times for scenario 2; this plan reads them as A, B, C.)
|
||||
|
||||
## What exists today
|
||||
- Containers: `docker-compose.yml` (four services, variant B) and `DOCS/General/Deployment_Docker.md`. No single-container variant.
|
||||
- System services: manual guide `Deployment_Native.md` (tested on Alpine only), systemd units, OpenRC scripts, privilege-separation helper with `doas`. No install or update script.
|
||||
- Database migration is incomplete:
|
||||
- Profiler: `create_all` plus `ensure_columns()` for the single column `note`. The other columns that models have (`public_ip`, `tun_mtu`, `mssfix`, `is_revoked`, `is_expired`, `expiration_date`, ...) are added only by standalone manual scripts in `APP_PROFILER/scripts` and `add_columns.py`. **A database from an older version breaks after an update unless someone runs them.**
|
||||
- Monitoring (`APP_CORE/db.py`): `CREATE TABLE IF NOT EXISTS` plus `_ensure_column`. No schema version, no backup, no integrity check.
|
||||
- Found blockers for Debian/Ubuntu:
|
||||
1. `ovpmon-helper` calls `/sbin/rc-service openvpn` only; `process.py` uses `systemctl <action> openvpn` for non-Alpine, but on Debian/Ubuntu the real unit is `openvpn-server@server` and the config is expected in `/etc/openvpn/server/`.
|
||||
2. `doas` is not installed by default (`opendoas` package; Ubuntu and Debian have it, version differs).
|
||||
3. The UI build needs Node 20.19+ (Vite 7); Debian 12 and Ubuntu 22.04 ship older Node.
|
||||
4. Nginx paths and Python versions differ (`/etc/nginx/http.d` on Alpine, `conf.d` or `sites-available` on Debian/Ubuntu).
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Database migration (foundation, done first)
|
||||
- One migration mechanism for both databases: table `schema_migrations(version, applied_at)`, ordered idempotent migrations in code (`APP_PROFILER/migrations.py`, `APP_CORE/migrations.py`), applied in a transaction at startup and by the CLI `migrate` used in the update script.
|
||||
- Migration 1 for the Profiler adds every column that the current models need and a pre-versioned database lacks (generated from the models, not hand-written); `note` moves into it. Migration for `APP_CORE` stamps the baseline after checking the existing schema.
|
||||
- Safety rules:
|
||||
- Before any migration: `PRAGMA integrity_check`, then a consistent backup through the SQLite backup API (`<db>.bak-<timestamp>`), kept.
|
||||
- The migration never drops or rewrites data; only `ADD COLUMN` and `CREATE ... IF NOT EXISTS`, with defaults.
|
||||
- A database **newer** than the application is not touched: the service refuses to start with a clear message (downgrade protection).
|
||||
- After the migration: integrity check and row counts compared with the backup; on failure the backup is restored automatically by the update script.
|
||||
- One `VERSION` file is written on install and update and logged on start.
|
||||
- Test: one pytest builds a database with the **old** schema (taken from the baseline commit `3836049`), puts data into it, migrates, checks that the data is intact, the columns exist and a second run changes nothing.
|
||||
|
||||
### 2. Scenario 1: containers
|
||||
- **B (distributed):** existing `docker-compose.yml`, hardened: named volumes kept on update, `.env` template, health checks, documented `docker compose pull/build && up -d`; the migration runs at the start of the Profiler and the API; pre-update volume backup command (`docker run ... tar`).
|
||||
- **A (one container):** new `deploy/allinone/Dockerfile` and `docker-compose.allinone.yml`: Alpine, `supervisord` (or OpenRC-free supervisor) running nginx, gunicorn, gatherer, uvicorn and OpenVPN; same environment variables, one data volume (databases, PKI, client config, `/etc/openvpn`), ports 80 and 1194/udp.
|
||||
- Script `deploy/docker/install.sh` and `update.sh` (idempotent: generates the secret, asks for the admin once, creates the backup, rebuilds, waits for health, prints the result).
|
||||
- Document: `DOCS/Deployment/Scenario1_Containers.md` (A and B, install, update, backup and restore, rollback).
|
||||
|
||||
### 3. Scenario 2: system services (Alpine, Debian, Ubuntu)
|
||||
- `deploy/native/install.sh` and `update.sh` with an OS adapter (detects Alpine / Debian / Ubuntu; package names, service manager, nginx paths, doas package):
|
||||
- install: packages, `ovpmon` user, directories, venvs, environment file (random secret), services (OpenRC or systemd), root helper and doas rules, Nginx with a self-signed certificate, firewall hints;
|
||||
- update: preflight (free space, version, services state) → backup (databases through the backup API, `/etc/ovpmon`, PKI, client config, `server.conf`, code version) → stop → new code → dependencies → **database migration** → UI → start → health check → on failure automatic rollback (code and databases) with a log.
|
||||
- Debian/Ubuntu support in the code: `ovpmon-helper` and `process.py` choose the service manager (OpenRC `openvpn`, systemd `openvpn-server@server`) and the config location; the helper rules are checked on both.
|
||||
- UI build: the script builds `dist` in a Node 20 container (or uses a prebuilt archive) so that the host does not need a recent Node.
|
||||
- Document: `DOCS/Deployment/Scenario2_System_Services.md` with one section per OS, the update procedure, backup, rollback and a "what is preserved" table.
|
||||
|
||||
### 4. Verification (containers, no real host is touched)
|
||||
- Scenario 1: build and start A and B, log in, initialize PKI, create a client; then **update from the old version** (baseline `3836049` data volume) to the new one and check that data and PKI survive.
|
||||
- Scenario 2: run `install.sh` and then `update.sh` inside Alpine 3.23, Debian 12 and Ubuntu 24.04 containers with an init system (OpenRC / systemd); seed an old database and PKI before the update; check migration, health check and rollback (forced failure).
|
||||
- Limit to state in the report: a container is not a full host (no real firewall, `tun` and service-manager details can differ); a final run on a VM of each OS is recommended before production.
|
||||
|
||||
### 5. Documentation
|
||||
- The two scenario documents, links from `README.md`, `DOCS/General/Index.md`; the old `Deployment_Docker.md` and `Deployment_Native.md` become short pointers to the new documents; summary `DOCS/Changes/2026-10-05_Deployment_Scenarios.md`.
|
||||
|
||||
## Decisions for the owner
|
||||
1. **Form:** scripts (`install.sh`, `update.sh`) plus documents (recommended), or documents only.
|
||||
2. **Order of work:** database migration first, then Scenario 1, then Scenario 2 (recommended), or all in one step.
|
||||
3. **Debian/Ubuntu service manager:** use `openvpn-server@server` and `/etc/openvpn/server/server.conf` (the distribution convention, recommended) instead of the Alpine-style `openvpn.conf`.
|
||||
4. **UI build:** in a Node 20 container (recommended) or require Node 20+ on the host.
|
||||
|
||||
## Out of scope
|
||||
High availability, PostgreSQL instead of SQLite, automatic OS package upgrades, Windows hosts, a real-host run (needs a VM).
|
||||
|
||||
## Decisions of the owner (2026-10-05)
|
||||
1. Scripts `install.sh` and `update.sh` plus documents.
|
||||
2. **The database migration phase is separated from the component update.** Phase 1 (this stage): migration mechanism, standalone `migrate` command, backup, checks, tests; it also runs by itself at service start. Phase 2: scenario 1 (containers). Phase 3: scenario 2 (system services). The update scripts call the migration as its own, separate step before the components are replaced or restarted.
|
||||
3. Debian and Ubuntu: `openvpn-server@server` and `/etc/openvpn/server/server.conf`.
|
||||
4. The UI is built in a Node 20 container.
|
||||
|
||||
## Phase 1 details (migration)
|
||||
- Each component has its own build context and virtualenv, so the small migration helper is a separate copy in `APP_PROFILER/` and `APP_CORE/` (same behaviour).
|
||||
- Service start applies pending migrations automatically (after the backup); `OVPMON_AUTO_MIGRATE=0` makes the service only check and refuse to start while migrations are pending (used by the update scripts). The standalone command: `migrate.py [--check]` in each component, exit codes 0 / 2 (pending) / 1 (error).
|
||||
- Old standalone scripts (`add_columns.py`, `scripts/add_*.py`) stay in place and are marked as superseded in the documentation.
|
||||
- Tests: one file per component, with the oldest and the baseline (`3836049`) schemas embedded as SQL.
|
||||
Reference in new issue
Block a user