Files
OpenVPN-Monitoring-Simple/deploy/docker/install.sh
T

192 lines
8.7 KiB
Bash
Raw Normal View History

#!/bin/sh
# Install OpenVPN Monitor & Profiler in containers.
#
# deploy/docker/install.sh --variant a|b [--project NAME] [--http-port N] [--vpn-port N] [--admin-user NAME]
#
# --variant a all components in ONE container (docker-compose.allinone.yml)
# --variant b one container per component (docker-compose.yml)
# --project compose project name = prefix of the volumes (default: ovpmon)
# --http-port host port of the web UI (default 80), stored in .env as OVPMON_HTTP_PORT
# --vpn-port host UDP port of OpenVPN (default 1194), stored in .env as OVPMON_VPN_PORT
# --admin-user login of the initial administrator (default: admin)
#
# Safe to run again: .env is created only if it does not exist, an existing stack is only rebuilt
# and started. Variants A and B cannot run on the same host together (same ports and names).
# See DOCS/Deployment/Containers/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
. "$SCRIPT_DIR/lib.sh"
usage() {
sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'
}
ARG_VARIANT=""
ARG_PROJECT=""
ARG_HTTP_PORT=""
ARG_VPN_PORT=""
ADMIN_USER="admin"
while [ $# -gt 0 ]; do
case "$1" in
--variant) [ $# -ge 2 ] || die "--variant needs a value"; ARG_VARIANT="$2"; shift 2 ;;
--project) [ $# -ge 2 ] || die "--project needs a value"; ARG_PROJECT="$2"; shift 2 ;;
--http-port) [ $# -ge 2 ] || die "--http-port needs a value"; ARG_HTTP_PORT="$2"; shift 2 ;;
--vpn-port) [ $# -ge 2 ] || die "--vpn-port needs a value"; ARG_VPN_PORT="$2"; shift 2 ;;
--admin-user) [ $# -ge 2 ] || die "--admin-user needs a value"; ADMIN_USER="$2"; shift 2 ;;
-h|--help) usage; exit 0 ;;
*) usage >&2; die "unknown option: $1" ;;
esac
done
[ -n "$ARG_VARIANT" ] || { usage >&2; die "--variant a|b is required"; }
set_variant "$ARG_VARIANT"
PROJECT="${ARG_PROJECT:-$(state_get OVPMON_PROJECT)}"
PROJECT="${PROJECT:-$DEFAULT_PROJECT}"
valid_project "$PROJECT" || die "invalid project name '$PROJECT' (lowercase letters, digits, - and _)"
[ -z "$ARG_HTTP_PORT" ] || valid_port "$ARG_HTTP_PORT" || die "invalid --http-port '$ARG_HTTP_PORT'"
[ -z "$ARG_VPN_PORT" ] || valid_port "$ARG_VPN_PORT" || die "invalid --vpn-port '$ARG_VPN_PORT'"
case "$ADMIN_USER" in
""|*[!A-Za-z0-9._-]*) die "invalid --admin-user (letters, digits, . _ - only)" ;;
esac
acquire_lock
trap release_lock EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
# ---------------------------------------------------------------------------------------------
phase 0 "preflight"
need_docker
need_compose_version
log "docker compose command: $DC_BIN; variant $VARIANT; project $PROJECT"
check_other_variant_stopped
check_name_clash
HTTP_PORT="${ARG_HTTP_PORT:-$(env_get OVPMON_HTTP_PORT)}"; HTTP_PORT="${HTTP_PORT:-80}"
VPN_PORT="${ARG_VPN_PORT:-$(env_get OVPMON_VPN_PORT)}"; VPN_PORT="${VPN_PORT:-1194}"
valid_port "$HTTP_PORT" || die "invalid OVPMON_HTTP_PORT '$HTTP_PORT' in .env"
valid_port "$VPN_PORT" || die "invalid OVPMON_VPN_PORT '$VPN_PORT' in .env"
if stack_running; then
log "the stack of this variant is already running: ports are held by it, port check skipped"
else
check_port "$HTTP_PORT" tcp "UI (HTTP)"
check_port "$VPN_PORT" udp "OpenVPN"
fi
check_free "$(docker_root_dir)" "$MIN_FREE_DOCKER_MB" "images (docker data root)"
check_free "$ROOT_DIR" 256 "the installation directory"
# ---------------------------------------------------------------------------------------------
phase 1 "configuration (.env)"
if [ -f "$ENV_FILE" ]; then
log ".env exists and is kept as it is: $ENV_FILE"
if [ -n "$ARG_HTTP_PORT" ]; then env_set OVPMON_HTTP_PORT "$ARG_HTTP_PORT"; log "OVPMON_HTTP_PORT=$ARG_HTTP_PORT written to .env"; fi
if [ -n "$ARG_VPN_PORT" ]; then env_set OVPMON_VPN_PORT "$ARG_VPN_PORT"; log "OVPMON_VPN_PORT=$ARG_VPN_PORT written to .env"; fi
else
command -v openssl >/dev/null 2>&1 || log "openssl not found: the secret is generated from /dev/urandom"
JWT=$(openssl rand -hex 32 2>/dev/null || true)
if [ "${#JWT}" -ne 64 ]; then
JWT=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')
fi
[ "${#JWT}" -eq 64 ] || die "cannot generate a random secret"
ADMIN_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 24)
[ "${#ADMIN_PASSWORD}" -eq 24 ] || die "cannot generate a random password"
(
umask 077
{
echo "# Created by deploy/docker/install.sh on $(date -u '+%Y-%m-%d %H:%M:%S') UTC. Keep it private, never commit it."
echo "JWT_SECRET=$JWT"
echo "# Initial administrator: used only while the users table is empty; install.sh removes these two lines after the first start"
echo "OVPMON_INITIAL_ADMIN_USER=$ADMIN_USER"
echo "OVPMON_INITIAL_ADMIN_PASSWORD=$ADMIN_PASSWORD"
echo "# Optional settings"
if [ -n "$ARG_HTTP_PORT" ]; then echo "OVPMON_HTTP_PORT=$ARG_HTTP_PORT"; else echo "# OVPMON_HTTP_PORT=80"; fi
if [ -n "$ARG_VPN_PORT" ]; then echo "OVPMON_VPN_PORT=$ARG_VPN_PORT"; else echo "# OVPMON_VPN_PORT=1194"; fi
echo "# OVPMON_CORS_ORIGINS="
echo "# OVPMON_CSP_CONNECT_EXTRA="
} > "$ENV_FILE"
)
chmod 600 "$ENV_FILE"
unset JWT ADMIN_PASSWORD
log ".env created (mode 600): $ENV_FILE"
fi
check_env_file
dc config -q || die "compose configuration is invalid (see the message above)"
# ---------------------------------------------------------------------------------------------
phase 2 "build"
dc build
# ---------------------------------------------------------------------------------------------
phase 3 "start"
dc up -d
[ -n "$(state_get OVPMON_INSTALLED_AT)" ] || INSTALLED_AT=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
state_write "$VARIANT" "$PROJECT" "${INSTALLED_AT:-}"
log "state recorded in $STATE_FILE"
if ! wait_healthy; then
die "the stack did not become healthy. Check: $DC_BIN -p $PROJECT -f $(basename "$COMPOSE_FILE") logs"
fi
# ---------------------------------------------------------------------------------------------
phase 4 "initial administrator"
SHOW_USER=""
SHOW_PASSWORD=""
print_admin() {
[ -n "$SHOW_PASSWORD" ] || return 0
printf '\n Administrator (shown ONCE, it is not stored anywhere any more):\n'
printf ' login: %s\n' "$SHOW_USER"
printf ' password: %s\n' "$SHOW_PASSWORD"
}
if [ -n "$(env_get OVPMON_INITIAL_ADMIN_USER)$(env_get OVPMON_INITIAL_ADMIN_PASSWORD)" ]; then
COUNT=$(users_count || true)
case "$COUNT" in
""|*[!0-9]*)
warn "cannot read the users table (got '$COUNT'): OVPMON_INITIAL_ADMIN_* are LEFT in .env. Remove them by hand after you have logged in, then: $DC_BIN -p $PROJECT -f $(basename "$COMPOSE_FILE") up -d $SEED_SERVICE"
;;
0)
warn "the users table is empty: the administrator was not created. OVPMON_INITIAL_ADMIN_* stay in .env. Check the logs of $SEED_SERVICE"
;;
*)
SHOW_USER=$(env_get OVPMON_INITIAL_ADMIN_USER)
SHOW_PASSWORD=$(env_get OVPMON_INITIAL_ADMIN_PASSWORD)
env_remove_admin
log "$COUNT user(s) exist: OVPMON_INITIAL_ADMIN_* removed from .env, recreating $SEED_SERVICE without them"
dc up -d --no-deps --force-recreate "$SEED_SERVICE"
if ! wait_healthy; then
# the seed is already gone from .env: the one-time credentials must not be lost
print_admin
die "the stack did not become healthy after removing the seed. Check: $DC_BIN -p $PROJECT -f $(basename "$COMPOSE_FILE") logs"
fi
;;
esac
else
COUNT=$(users_count || true)
case "$COUNT" in
0) warn "no users exist and .env has no OVPMON_INITIAL_ADMIN_USER / OVPMON_INITIAL_ADMIN_PASSWORD: add them and run '$DC_BIN -p $PROJECT -f $(basename "$COMPOSE_FILE") up -d $SEED_SERVICE'" ;;
*) log "users already exist: nothing to seed" ;;
esac
fi
# ---------------------------------------------------------------------------------------------
HOST_IP=$(hostname -I 2>/dev/null | awk '{print $1}' || true)
HOST_IP="${HOST_IP:-<server-address>}"
URL_PORT=""
[ "$HTTP_PORT" = 80 ] || URL_PORT=":$HTTP_PORT"
printf '\n'
log "=== INSTALLED: variant $VARIANT, project $PROJECT ==="
printf ' Web UI: http://%s%s/\n' "$HOST_IP" "$URL_PORT"
printf ' OpenVPN: %s/udp on this host (open it in the firewall)\n' "$VPN_PORT"
printf ' Config: %s (mode 600; contains the secret, never commit it)\n' "$ENV_FILE"
print_admin
cat <<EOF
Next steps:
1. Open the Web UI and sign in.
2. PKI Configuration -> Initialize PKI (wait until it finishes).
3. Generate the server configuration and start the VPN server in the UI.
4. Put TLS in front of the UI (reverse proxy); the container serves plain HTTP.
Update later with: deploy/docker/update.sh Backup: deploy/docker/backup.sh
EOF