- Variant B: docker-compose.yml gets configurable host ports and health checks. Variant A: deploy/allinone image (nginx, supervisord, gunicorn, uvicorn, gatherer, OpenVPN) with an explicit database migration step before any service; docker-compose.allinone.yml. Same five volumes and paths in both variants. - deploy/docker: install.sh, update.sh (backup, build, stop, separate DB migration phase, start, automatic rollback), backup.sh, restore.sh, lib.sh. - Verified on Docker: install A/B, update with pre-versioned databases, rollback after an injected failure, backup/restore. - Docs: Scenario1_Containers guide, change summary, README, Index; .dockerignore, .gitignore additions. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
262 lines
20 KiB
Markdown
262 lines
20 KiB
Markdown
# Scenario 1: installation in containers
|
|
|
|
Two variants, both installed and updated by the scripts in `deploy/docker/`:
|
|
|
|
| | Variant A: one container | Variant B: one container per component |
|
|
|---|---|---|
|
|
| Compose file | `docker-compose.allinone.yml` | `docker-compose.yml` |
|
|
| Containers | `ovp-allinone` (nginx, gunicorn, gatherer, uvicorn, OpenVPN under `supervisord`) | `ovp-ui`, `ovp-api`, `ovp-gatherer`, `ovp-profiler` |
|
|
| Image build | `deploy/allinone/Dockerfile` (build context: repository root) | one Dockerfile per component (`APP_UI`, `APP_CORE`, `APP_PROFILER`) |
|
|
| Choose it when | one host, simplest operation, one thing to start/stop/back up | you want to restart, scale or inspect components separately, or follow the usual one-process-per-container practice |
|
|
| Trade-off | a failing process is restarted by `supervisord`, the container stays up; one image to rebuild for any change | more moving parts, internal network `ovp-net` |
|
|
|
|
Both variants use the **same five named volumes with the same in-container paths**, the same environment variables and the same published ports. Backups look identical.
|
|
|
|
> **Only one variant can run per host.** Both publish the same host ports (`80/tcp`, `1194/udp` by default) and use fixed container names (`ovp-*`). The scripts refuse to start a variant while a container of the other one is running.
|
|
|
|
## Requirements
|
|
|
|
- Docker Engine with the compose plugin (`docker compose`, v2, preferred) or `docker-compose`.
|
|
- The Docker daemon running and usable by the user that runs the scripts (root, or a member of the `docker` group).
|
|
- Access to `/dev/net/tun` on the host and the `NET_ADMIN` capability (already set in the compose files).
|
|
- Free disk space: about 2 GB for images and builds, plus room for backups (the scripts check both).
|
|
- Internet access for the first build (base images, npm, pip) and for the backup helper image (`alpine:3`). For offline hosts see *Troubleshooting*.
|
|
- `openssl` is optional (random secret otherwise comes from `/dev/urandom`).
|
|
|
|
## Install
|
|
|
|
Clone the repository, change into it and run the script (as root or a docker user):
|
|
|
|
```bash
|
|
# Variant B: one container per component
|
|
deploy/docker/install.sh --variant b
|
|
|
|
# Variant A: everything in one container
|
|
deploy/docker/install.sh --variant a
|
|
|
|
# Options
|
|
deploy/docker/install.sh --variant b --project ovpmon --http-port 8080 --vpn-port 1194 --admin-user admin
|
|
```
|
|
|
|
| Option | Meaning |
|
|
|---|---|
|
|
| `--variant a\|b` | required |
|
|
| `--project NAME` | compose project name = prefix of the volume names (default `ovpmon`: volumes `ovpmon_db_data`, ...) |
|
|
| `--http-port N` | host port of the UI (written to `.env` as `OVPMON_HTTP_PORT`, default 80) |
|
|
| `--vpn-port N` | host UDP port of OpenVPN (`OVPMON_VPN_PORT`, default 1194) |
|
|
| `--admin-user NAME` | login of the first administrator (default `admin`) |
|
|
|
|
What `install.sh` does:
|
|
|
|
1. **Preflight:** docker and compose, running daemon, the other variant is not running, no foreign container with the same names, ports free, free disk space.
|
|
2. **`.env`** next to the compose files, only if it does not exist: mode 600, random `JWT_SECRET`, initial administrator with a generated 24-character password. An existing `.env` is never overwritten (`--http-port` / `--vpn-port` only set their own keys in it).
|
|
3. **Build, start** (`docker compose up -d`), write the state file `.ovpmon-deploy` (variant and project; used by `update.sh`, `backup.sh`, `restore.sh`), wait up to 3 minutes for the health checks.
|
|
4. **Initial administrator:** when the users table is not empty, `OVPMON_INITIAL_ADMIN_*` are removed from `.env` and the container that reads them is recreated (variant B: `app-api`, variant A: `ovpmon`). If the table cannot be read or is empty, the lines stay in `.env` and a warning says so.
|
|
5. **Result:** URL, ports, and the administrator login and password. **The password is shown once** and stored nowhere else. `JWT_SECRET` is never printed.
|
|
|
|
Running it again is safe: it rebuilds and starts, keeps `.env`, and makes no new administrator.
|
|
|
|
Then in the UI: sign in, **PKI Configuration → Initialize PKI**, generate the server configuration and start the VPN server. Put TLS in front of the UI (see *Security*).
|
|
|
|
### Without the script
|
|
|
|
```bash
|
|
cat > .env <<EOT
|
|
JWT_SECRET=$(openssl rand -hex 32)
|
|
OVPMON_INITIAL_ADMIN_USER=admin
|
|
OVPMON_INITIAL_ADMIN_PASSWORD=<strong password>
|
|
EOT
|
|
chmod 600 .env
|
|
docker compose up -d --build # variant B
|
|
docker compose -f docker-compose.allinone.yml up -d --build # variant A
|
|
```
|
|
|
|
After the first start remove the two `OVPMON_INITIAL_ADMIN_*` lines from `.env` and recreate `app-api` (B) or `ovpmon` (A).
|
|
|
|
## `.env` reference
|
|
|
|
| Variable | Required | Default | Meaning |
|
|
|---|---|---|---|
|
|
| `JWT_SECRET` | yes | none (compose refuses to start) | Signing key of both APIs (`OVPMON_API_SECRET_KEY`). Changing it logs every user out. Keep the same value across updates |
|
|
| `OVPMON_INITIAL_ADMIN_USER`, `OVPMON_INITIAL_ADMIN_PASSWORD` | first start only | empty | Seed for the first user, used only while the users table is empty. Removed by `install.sh` afterwards |
|
|
| `OVPMON_CORS_ORIGINS` | no | empty (off) | Comma-separated extra origins; the UI is same-origin behind nginx |
|
|
| `OVPMON_HTTP_PORT` | no | `80` | Host port of the UI |
|
|
| `OVPMON_VPN_PORT` | no | `1194` | Host UDP port of OpenVPN (the container side is always 1194) |
|
|
|
|
Variables of the scripts (environment of the shell, not `.env`): `OVPMON_BACKUP_DIR` (backup root, default `./backups`), `OVPMON_BACKUP_IMAGE` (helper image, default `alpine:3`), `OVPMON_HEALTH_TIMEOUT` (seconds, default 180), `OVPMON_MIN_FREE_MB` (docker data, default 2048), `OVPMON_MIN_FREE_BACKUP_MB` (backups, default 1024).
|
|
|
|
`OVPMON_VPN_PORT` changes only the host side of the mapping. If you change the OpenVPN port in the server settings of the UI, the client profiles carry that port; keep the two consistent.
|
|
|
|
## Ports
|
|
|
|
| Port | Published | Purpose |
|
|
|---|---|---|
|
|
| `${OVPMON_HTTP_PORT:-80}/tcp` | yes | UI and reverse proxy to both APIs (plain HTTP) |
|
|
| `${OVPMON_VPN_PORT:-1194}/udp` | yes | OpenVPN |
|
|
| `5001` (monitoring API), `8000` (profiler API) | no | variant B: `expose`d on `ovp-net` only; variant A: listen on `127.0.0.1` inside the container |
|
|
|
|
## Volumes: what is stored where
|
|
|
|
| Volume | Path in the container | Content | Mounted by (B) |
|
|
|---|---|---|---|
|
|
| `ovp_logs` | `/var/log/openvpn` | `openvpn-status.log` and OpenVPN logs | gatherer, profiler |
|
|
| `ovp_config` | `/etc/openvpn` | `server.conf` generated by the profiler | profiler |
|
|
| `ovp_pki` | `/app/easy-rsa` | easy-rsa scripts and `pki/` (CA, server and client keys and certificates, CRL, `ta.key`, `dh.pem`) | profiler |
|
|
| `ovp_client_config` | `/app/client-config` | generated client `.ovpn` files | profiler |
|
|
| `db_data` | `/app/db` | `ovpn_profiler.db`, `openvpn_monitor.db`, migration backups `*.db.bak-*` | gatherer, api, profiler |
|
|
|
|
Not stored in volumes (lost when the container is recreated): the container file system, `profiler.log` and `openvpn_gatherer.log` (written into the working directory), supervisor state. Application logs go to `docker logs`.
|
|
|
|
Volume names are `<project>_<volume>`; the scripts find them by the compose labels, so any project name works.
|
|
|
|
### Layout of variant A
|
|
|
|
The code is in `/app/APP_CORE` and `/app/APP_PROFILER`; the volumes are mounted at the same paths as in variant B (`/app/db`, `/app/easy-rsa`, `/app/client-config`, `/etc/openvpn`, `/var/log/openvpn`).
|
|
|
|
The profiler resolves `easy-rsa/` and `client-config/` relative to its working directory (`services/pki.py`, `routers/profiles.py`), and stores `client-config/<name>.ovpn` as a relative path in its database. In variant B that directory is `/app`. Variant A keeps it that way: **`supervisord` starts the profiler with working directory `/app`** (`uvicorn --app-dir /app/APP_PROFILER main:app`), so `/app/easy-rsa` and `/app/client-config` are the volumes. No symlinks are used, and the database paths are identical in both variants (so a volume set can be used by either variant). The monitoring API and the gatherer run with working directory `/app/APP_CORE`, where `config.ini` lives; their database path comes from `OVPMON_OPENVPN_MONITOR_DB_PATH=/app/db/openvpn_monitor.db`.
|
|
|
|
Processes in `ovp-allinone` (`deploy/allinone/supervisord.conf`, started in this order, all with `autorestart`, logs to stdout/stderr): `gatherer`, `api` (`gunicorn -w 2 -b 127.0.0.1:5001 openvpn_api_v3:app`), `profiler` (`uvicorn` on `127.0.0.1:8000`, no reload), `nginx`. OpenVPN is started by the profiler API on request (direct start inside containers), as in variant B.
|
|
|
|
`deploy/allinone/entrypoint.sh` runs before `supervisord`: secret mapping (`JWT_SECRET` → `OVPMON_API_SECRET_KEY` when only the first is set), `/dev/net/tun`, easy-rsa workspace (copied only when the volume does not have it yet), NAT and MSS rules without duplicates, the nginx configuration rendered from `APP_UI/default.conf.template` for `127.0.0.1`, then the **database migration as an explicit step**: the container does not start if it fails. The services then run with `OVPMON_AUTO_MIGRATE=0` (they only verify the schema).
|
|
|
|
## 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.
|
|
|
|
## Troubleshooting
|
|
|
|
| Symptom | Cause and action |
|
|
|---|---|
|
|
| `port 80/tcp is already in use` | Another service uses the port. Install with `--http-port 8080` or set `OVPMON_HTTP_PORT` in `.env` (same for `OVPMON_VPN_PORT`) |
|
|
| `container 'ovp-...' of the OTHER variant is running` | Variants A and B cannot run together. Stop the other stack first |
|
|
| `container 'ovp-ui' ... belongs to compose project 'x'` | A stack of another project uses the fixed names. Use `--project x` or remove that stack |
|
|
| Health check does not turn healthy | `docker compose ps`, then `docker compose logs --tail 100 <service>` (A: `docker logs ovp-allinone`). The scripts print the last log lines of failing services. The profiler allows 40 s and the all-in-one container 60 s before failures count |
|
|
| `FATAL: ... newer than this version` / migration "too new" | The database was migrated by a newer release. Do not start the old version on it: update to the newer sources, or restore a matching backup (`update.sh --rollback` or `restore.sh`) |
|
|
| Migration failed | Nothing was started and the volumes are restored by the automatic rollback. The report names the migration; the database copy `<db>.bak-*` is in `db_data`. Send the report with the problem |
|
|
| `JWT_SECRET must be set in .env` | `.env` missing or empty value; run `install.sh` or add `JWT_SECRET=$(openssl rand -hex 32)` |
|
|
| `permission denied` on docker | Run as root or add the user to the `docker` group. Backups belong to the user that runs the scripts |
|
|
| Permission errors on bind mounts on SELinux hosts | Backup and restore bind-mount `backups/` into a container; label the directory for containers (`chcon -Rt container_file_t backups`) |
|
|
| Cannot pull `alpine:3` (offline) | Make an image with `sh`, `tar` and `sha256sum` available locally and set `OVPMON_BACKUP_IMAGE`; use `--no-pull` with `update.sh` |
|
|
| `/dev/net/tun` missing | Load the module on the host (`modprobe tun`); the compose files map the device |
|
|
| VPN does not start after an update | The containers were recreated and the profiler starts OpenVPN only on request: start the server in the UI. Check that the server configuration exists (`ovp_config` volume) |
|
|
| The UI answers 502 right after start | The APIs are still starting; wait for the health checks |
|
|
|
|
## Security
|
|
|
|
- Put **TLS in front** of the UI (reverse proxy or a certificate in nginx): the container serves plain HTTP. See [Nginx configuration](../General/Nginx_Configuration.md).
|
|
- `.env` holds the signing secret: mode 600, owned by the operator, never committed (`.gitignore` lists it). The scripts never print `JWT_SECRET`; the generated administrator password is shown once and removed from `.env` after the first start.
|
|
- Backups contain the PKI (CA and server private keys), the databases (password hashes, 2FA secrets) and a copy of `.env`: keep them mode 700/600 and store off-host copies encrypted.
|
|
- The profiler container is privileged for networking (`NET_ADMIN`, TUN). Variant A runs everything in that one container; its APIs listen on `127.0.0.1` only, so only nginx (80) and OpenVPN (1194/udp) are reachable.
|
|
- No secrets in logs: the scripts only log names and paths; the services log the source of the secret, not its value.
|
|
|
|
## 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).
|