Files
OpenVPN-Monitoring-Simple/deploy/allinone/entrypoint.sh
T
ayurishchevandClaude Sonnet 5.5 1c49d0b9c2 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>
2026-10-05 09:24:55 +03:00

93 lines
4.7 KiB
Bash
Executable File

#!/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