Scenario 1: container install and update (all-in-one and per-component)
- 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>
This commit is contained in:
1 parent
e556d6d4b9
commit
1c49d0b9c2
17 files changed
+1930
-50
No files matched your search
@@ -0,0 +1,70 @@
|
||||
# OpenVPN Monitor & Profiler: ALL components in ONE container (scenario 1, variant A)
|
||||
#
|
||||
# Build context: the repository root (see docker-compose.allinone.yml):
|
||||
# docker build -f deploy/allinone/Dockerfile .
|
||||
#
|
||||
# Processes (supervisord): nginx (UI + reverse proxy), gunicorn (monitoring API, APP_CORE),
|
||||
# gatherer (APP_CORE), uvicorn (profiler API, APP_PROFILER). OpenVPN is started by the profiler
|
||||
# API itself (direct start inside containers, see APP_PROFILER/services/process.py).
|
||||
#
|
||||
# Layout: code in /app/APP_CORE and /app/APP_PROFILER; data in the same volume paths as the
|
||||
# per-component compose file: /app/db, /app/easy-rsa, /app/client-config, /etc/openvpn,
|
||||
# /var/log/openvpn. The profiler runs with working directory /app, so its relative paths
|
||||
# (easy-rsa, client-config) resolve to the volumes exactly as in variant B.
|
||||
#
|
||||
# Supported ENV: JWT_SECRET or OVPMON_API_SECRET_KEY (required), OVPMON_INITIAL_ADMIN_USER,
|
||||
# OVPMON_INITIAL_ADMIN_PASSWORD, OVPMON_CORS_ORIGINS, OVPMON_* overrides of the components.
|
||||
|
||||
# Stage 1: build the UI
|
||||
FROM node:20-alpine AS ui-build
|
||||
WORKDIR /app
|
||||
COPY APP_UI/package*.json ./
|
||||
RUN npm install
|
||||
COPY APP_UI/ ./
|
||||
RUN npm run build
|
||||
|
||||
# Stage 2: runtime
|
||||
FROM python:3.12-alpine
|
||||
|
||||
# openvpn/easy-rsa/iptables/iproute2: like APP_PROFILER/Dockerfile (OpenRC is not needed: direct start)
|
||||
# nginx, supervisor: the process tree; gettext: envsubst for the nginx template
|
||||
RUN apk add --no-cache openvpn easy-rsa iproute2 iptables bash nginx supervisor gettext \
|
||||
&& (command -v envsubst >/dev/null || apk add --no-cache gettext-envsubst) \
|
||||
&& command -v envsubst >/dev/null
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Python dependencies of both components (+ gunicorn for the Flask API). Layers are cached
|
||||
# until a requirements file changes.
|
||||
COPY APP_CORE/requirements.txt /tmp/requirements-core.txt
|
||||
COPY APP_PROFILER/requirements.txt /tmp/requirements-profiler.txt
|
||||
RUN pip install --no-cache-dir -r /tmp/requirements-core.txt -r /tmp/requirements-profiler.txt gunicorn \
|
||||
&& rm -f /tmp/requirements-*.txt
|
||||
|
||||
COPY APP_CORE /app/APP_CORE
|
||||
COPY APP_PROFILER /app/APP_PROFILER
|
||||
|
||||
# UI and nginx: the template is rendered at container start (entrypoint.sh)
|
||||
COPY --from=ui-build /app/dist /usr/share/nginx/html
|
||||
COPY APP_UI/default.conf.template /etc/ovpmon/default.conf.template
|
||||
RUN rm -f /etc/nginx/http.d/default.conf \
|
||||
&& mkdir -p /run/nginx /var/log/nginx \
|
||||
&& ln -sf /dev/stdout /var/log/nginx/access.log \
|
||||
&& ln -sf /dev/stderr /var/log/nginx/error.log
|
||||
|
||||
COPY deploy/allinone/supervisord.conf /etc/supervisord.conf
|
||||
COPY deploy/allinone/entrypoint.sh /usr/local/bin/ovpmon-entrypoint.sh
|
||||
|
||||
# Mount points of the five named volumes
|
||||
RUN chmod +x /usr/local/bin/ovpmon-entrypoint.sh \
|
||||
&& mkdir -p /app/db /app/easy-rsa /app/client-config /etc/openvpn /var/log/openvpn
|
||||
|
||||
# Default database locations (the compose file sets the same values)
|
||||
ENV OVPMON_PROFILER_DB_PATH=/app/db/ovpn_profiler.db \
|
||||
OVPMON_OPENVPN_MONITOR_DB_PATH=/app/db/openvpn_monitor.db \
|
||||
OVPMON_OPENVPN_MONITOR_LOG_PATH=/var/log/openvpn/openvpn-status.log
|
||||
|
||||
EXPOSE 80
|
||||
EXPOSE 1194/udp
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/ovpmon-entrypoint.sh"]
|
||||
Executable
+92
@@ -0,0 +1,92 @@
|
||||
#!/bin/sh
|
||||
# Entrypoint of the all-in-one container (variant A).
|
||||
#
|
||||
# Order: environment -> TUN device -> workspace (easy-rsa) -> NAT -> nginx config ->
|
||||
# DATABASE MIGRATION (explicit step, container does not start on failure) -> supervisord.
|
||||
#
|
||||
# Re-running (container restart) is safe: every step is idempotent.
|
||||
set -eu
|
||||
|
||||
log() { echo "[ovpmon] $*"; }
|
||||
warn() { echo "[ovpmon] WARNING: $*" >&2; }
|
||||
die() { echo "[ovpmon] FATAL: $*" >&2; exit 1; }
|
||||
|
||||
# --- 1. Secret ----------------------------------------------------------------------------
|
||||
# Both APIs read OVPMON_API_SECRET_KEY; JWT_SECRET is accepted as an alias (same name as in .env).
|
||||
if [ -z "${OVPMON_API_SECRET_KEY:-}" ] && [ -n "${JWT_SECRET:-}" ]; then
|
||||
OVPMON_API_SECRET_KEY="$JWT_SECRET"
|
||||
fi
|
||||
[ -n "${OVPMON_API_SECRET_KEY:-}" ] || die "JWT_SECRET (or OVPMON_API_SECRET_KEY) is not set. Generate one: openssl rand -hex 32"
|
||||
export OVPMON_API_SECRET_KEY
|
||||
|
||||
# Database locations (same defaults as in the image and in docker-compose.allinone.yml)
|
||||
export OVPMON_PROFILER_DB_PATH="${OVPMON_PROFILER_DB_PATH:-/app/db/ovpn_profiler.db}"
|
||||
export OVPMON_OPENVPN_MONITOR_DB_PATH="${OVPMON_OPENVPN_MONITOR_DB_PATH:-/app/db/openvpn_monitor.db}"
|
||||
|
||||
# --- 2. TUN device ------------------------------------------------------------------------
|
||||
if [ ! -c /dev/net/tun ]; then
|
||||
log "Creating /dev/net/tun"
|
||||
mkdir -p /dev/net
|
||||
mknod /dev/net/tun c 10 200 || warn "cannot create /dev/net/tun (add devices: /dev/net/tun to the container)"
|
||||
chmod 600 /dev/net/tun 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# IP forwarding is set by the container runtime (sysctls: net.ipv4.ip_forward=1 in compose)
|
||||
|
||||
mkdir -p /run /run/nginx /app/db /app/client-config /etc/openvpn /var/log/openvpn
|
||||
|
||||
# --- 3. Easy-RSA workspace ----------------------------------------------------------------
|
||||
# /app/easy-rsa is the PKI volume; the profiler (working directory /app) uses /app/easy-rsa/pki.
|
||||
# Copy the easy-rsa scripts only if the volume does not have them yet (never touches pki/).
|
||||
if [ ! -f /app/easy-rsa/easyrsa ]; then
|
||||
log "Initializing Easy-RSA workspace in /app/easy-rsa"
|
||||
mkdir -p /app/easy-rsa
|
||||
cp -r /usr/share/easy-rsa/* /app/easy-rsa/
|
||||
fi
|
||||
|
||||
# --- 4. NAT and MSS clamping (as in APP_PROFILER/entrypoint.sh, without duplicates) --------
|
||||
# The rules live in the container's network namespace and survive a plain container restart,
|
||||
# so each rule is added only if it is not there yet (-C checks for it).
|
||||
NAT_IFACE="${OVPMON_NAT_IFACE:-eth0}"
|
||||
ensure_rule() {
|
||||
table="$1"; shift
|
||||
if iptables -t "$table" -C "$@" 2>/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
iptables -t "$table" -A "$@" || warn "iptables rule not added: -t $table $* (NET_ADMIN missing? VPN traffic will not be routed)"
|
||||
}
|
||||
ensure_rule nat POSTROUTING -o "$NAT_IFACE" -j MASQUERADE
|
||||
ensure_rule mangle FORWARD -o "$NAT_IFACE" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
|
||||
ensure_rule mangle FORWARD -i "$NAT_IFACE" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
|
||||
|
||||
# --- 5. Nginx configuration ---------------------------------------------------------------
|
||||
# The same template as the UI image of variant B; all upstreams are local. Only the four
|
||||
# OVP_* variables are substituted, nginx's own $variables stay intact.
|
||||
export OVP_API_HOST=127.0.0.1 OVP_API_PORT=5001
|
||||
export OVP_PROFILER_HOST=127.0.0.1 OVP_PROFILER_PORT=8000
|
||||
envsubst '${OVP_API_HOST} ${OVP_API_PORT} ${OVP_PROFILER_HOST} ${OVP_PROFILER_PORT}' \
|
||||
< /etc/ovpmon/default.conf.template > /etc/nginx/http.d/ovpmon.conf
|
||||
nginx -t || die "generated nginx configuration is invalid (see above)"
|
||||
|
||||
# --- 6. Database migration (explicit step, before any service starts) ----------------------
|
||||
# Same command as the update scripts run. Pending migrations are applied after a consistent
|
||||
# backup (<db>.bak-<UTC timestamp> next to the database). A database that is newer than this
|
||||
# image, or a failed migration, stops the container here: nothing is started on a bad database.
|
||||
# See DOCS/General/Database_Migrations.md
|
||||
run_migration() {
|
||||
name="$1"; dir="$2"
|
||||
log "Database migration: $name"
|
||||
if ! ( cd "$dir" && python migrate.py ); then
|
||||
die "database migration of $name failed (see the report above). Nothing was started. Restore the volumes from the last backup or use the previous image; see DOCS/Deployment/Scenario1_Containers.md"
|
||||
fi
|
||||
}
|
||||
run_migration "profiler" /app/APP_PROFILER
|
||||
run_migration "monitoring" /app/APP_CORE
|
||||
|
||||
# The migration has been done: the services only verify the schema and never migrate on their own
|
||||
# (two gunicorn workers and the gatherer must not race for it).
|
||||
export OVPMON_AUTO_MIGRATE=0
|
||||
|
||||
# --- 7. Services --------------------------------------------------------------------------
|
||||
log "Starting services (supervisord)"
|
||||
exec supervisord -n -c /etc/supervisord.conf
|
||||
@@ -0,0 +1,86 @@
|
||||
; Process tree of the all-in-one container (variant A). PID 1 is supervisord (started by
|
||||
; entrypoint.sh after the explicit database migration step). All logs go to the container's
|
||||
; stdout/stderr (docker logs). Environment variables of the container are inherited by the programs.
|
||||
|
||||
[supervisord]
|
||||
nodaemon=true
|
||||
user=root
|
||||
logfile=/dev/null
|
||||
logfile_maxbytes=0
|
||||
pidfile=/run/supervisord.pid
|
||||
loglevel=info
|
||||
|
||||
[unix_http_server]
|
||||
file=/run/supervisor.sock
|
||||
|
||||
[rpcinterface:supervisor]
|
||||
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
|
||||
|
||||
[supervisorctl]
|
||||
serverurl=unix:///run/supervisor.sock
|
||||
|
||||
; Start order (priority, lower first): gatherer, then the two APIs, nginx last.
|
||||
; The explicit migration already ran in entrypoint.sh; the programs only check the schema
|
||||
; (OVPMON_AUTO_MIGRATE=0 is exported there).
|
||||
|
||||
[program:gatherer]
|
||||
command=python openvpn_gatherer_v3.py
|
||||
directory=/app/APP_CORE
|
||||
priority=10
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startsecs=5
|
||||
startretries=5
|
||||
stopsignal=TERM
|
||||
stopwaitsecs=10
|
||||
stdout_logfile=/dev/stdout
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/stderr
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
[program:api]
|
||||
; Flask monitoring API; "app" is the module-level Flask object of openvpn_api_v3.py
|
||||
command=gunicorn -w 2 -b 127.0.0.1:5001 --access-logfile - --error-logfile - openvpn_api_v3:app
|
||||
directory=/app/APP_CORE
|
||||
priority=20
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startsecs=5
|
||||
startretries=5
|
||||
stopsignal=TERM
|
||||
stopwaitsecs=15
|
||||
stdout_logfile=/dev/stdout
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/stderr
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
[program:profiler]
|
||||
; Working directory /app on purpose: the profiler resolves easy-rsa/ and client-config/ relative
|
||||
; to it, and in this layout /app/easy-rsa and /app/client-config are the volumes (as in variant B).
|
||||
command=uvicorn --app-dir /app/APP_PROFILER main:app --host 127.0.0.1 --port 8000
|
||||
directory=/app
|
||||
priority=20
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startsecs=5
|
||||
startretries=5
|
||||
stopsignal=TERM
|
||||
stopwaitsecs=15
|
||||
stdout_logfile=/dev/stdout
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/stderr
|
||||
stderr_logfile_maxbytes=0
|
||||
|
||||
[program:nginx]
|
||||
command=nginx -g "daemon off;"
|
||||
priority=30
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startsecs=3
|
||||
startretries=5
|
||||
stopsignal=QUIT
|
||||
stopwaitsecs=10
|
||||
stdout_logfile=/dev/stdout
|
||||
stdout_logfile_maxbytes=0
|
||||
stderr_logfile=/dev/stderr
|
||||
stderr_logfile_maxbytes=0
|
||||
Reference in new issue
Block a user