Network monitor: hub, probes, Docker and native installers
Hub (FastAPI + no-build web UI): - Network health: live probe/target matrix, status strips, incidents - Probes: add (with install command), rename, remove, latency and throughput charts - Clients merged from AdGuard, DHCP, query log and probe ARP tables; names, categories, suspicious flag, notes, per-client DNS requests - Query log, domain detail (clients, related subdomains), blocked domains via the AdGuard Home /control API Probe: tests peers (ping, HTTP, throughput), gateway, LAN and public DNS, internet URLs and an optional VPS; buffers results while the hub is down. Gateway and ARP from /proc/net on Linux, route/arp on macOS. Install: install.sh (hub) and install-probe.sh (generated from it), each in docker mode (compose; default on Linux hosts) or native mode (venv + systemd or launchd; default on macOS and in LXC). Both modes deploy with an update command that checks the new commit and rolls back on failure. CLAUDE.md carries shared context for Claude Code sessions on each machine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
54a765510a
63 changed files with 6464 additions and 0 deletions
122
container/lib.sh
Normal file
122
container/lib.sh
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
# Shared by entrypoint and update. Installed at /usr/local/lib/app/lib.sh.
|
||||
# Config: the project .env, mounted at /run/config/app.env, read by load_env.
|
||||
# Layout inside the container:
|
||||
# /srv/src git checkout of the app (cloned from REPO_URL on first start, or bind-mounted in dev)
|
||||
# /srv/venv virtualenv built from requirements.txt (rebuilt when requirements or Python change)
|
||||
# /srv/state update bookkeeping (previous commit, ssh known_hosts)
|
||||
# /data the app's own persistent data
|
||||
# /srv is one named volume, so source, venv and state survive container recreation.
|
||||
|
||||
SRC=${SRC:-/srv/src}
|
||||
VENV=${VENV:-/srv/venv}
|
||||
STATE=${STATE:-/srv/state}
|
||||
PORT=${PORT:-8000}
|
||||
PIDFILE=/tmp/app-supervisor.pid
|
||||
ENV_FILE=${ENV_FILE:-/run/config/app.env}
|
||||
|
||||
log() { echo "[$(basename "$0")] $*" >&2; }
|
||||
die() { log "error: $*"; exit 1; }
|
||||
|
||||
# Read the project's .env (bind-mounted read-only at $ENV_FILE). It runs on every start
|
||||
# and every update, so a new URL or rotated token takes effect without a rebuild.
|
||||
# Plain KEY=value lines only; it is parsed, not executed. Values never get printed.
|
||||
load_env() {
|
||||
[ -e "$ENV_FILE" ] || return 0
|
||||
[ -f "$ENV_FILE" ] || die "$ENV_FILE is not a file (create .env before 'docker compose up'; Docker made a directory in its place)"
|
||||
[ -r "$ENV_FILE" ] || die "$ENV_FILE is not readable by uid $(id -u): chown $(id -u) .env && chmod 600 .env on the host"
|
||||
local line key val
|
||||
while IFS= read -r line || [ -n "$line" ]; do
|
||||
line=${line%$'\r'}
|
||||
[[ $line =~ ^[[:space:]]*(export[[:space:]]+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]] || continue
|
||||
key=${BASH_REMATCH[2]} val=${BASH_REMATCH[3]}
|
||||
if [[ $val =~ ^\"(.*)\"$ || $val =~ ^\'(.*)\'$ ]]; then val=${BASH_REMATCH[1]}; fi
|
||||
export "$key=$val"
|
||||
done < "$ENV_FILE"
|
||||
setup_git_auth
|
||||
}
|
||||
|
||||
# Credentials for REPO_URL, from the environment only. Nothing is written into the checkout:
|
||||
# https: GIT_TOKEN (+ GIT_USERNAME, default x-access-token) answered by a credential helper
|
||||
# ssh: GIT_SSH_KEY, the private key base64-encoded (base64 -w0 deploy_key)
|
||||
setup_git_auth() {
|
||||
if [ -n "${GIT_TOKEN:-}" ]; then
|
||||
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=credential.helper
|
||||
export GIT_CONFIG_VALUE_0='!f() { [ "$1" = get ] || exit 0; echo "username=${GIT_USERNAME:-x-access-token}"; echo "password=$GIT_TOKEN"; }; f'
|
||||
fi
|
||||
local ssh="ssh -o UserKnownHostsFile=$STATE/known_hosts -o StrictHostKeyChecking=accept-new"
|
||||
if [ -n "${GIT_SSH_KEY:-}" ]; then
|
||||
local key=/tmp/git-deploy-key
|
||||
(umask 077; printf '%s' "$GIT_SSH_KEY" | base64 -d > "$key") || die "GIT_SSH_KEY is not valid base64"
|
||||
ssh="$ssh -i $key -o IdentitiesOnly=yes"
|
||||
fi
|
||||
export GIT_SSH_COMMAND="$ssh" GIT_TERMINAL_PROMPT=0
|
||||
}
|
||||
|
||||
# Hide user:password@ if someone put credentials in the URL anyway.
|
||||
redact() { printf '%s' "$1" | sed -E 's#://[^/@]+@#://***@#'; }
|
||||
|
||||
# The app gets the .env settings but not the repo credentials.
|
||||
APP_ENV_UNSET=(-u GIT_TOKEN -u GIT_USERNAME -u GIT_SSH_KEY -u GIT_CONFIG_COUNT -u GIT_CONFIG_KEY_0 -u GIT_CONFIG_VALUE_0)
|
||||
|
||||
# Clone the source on first start. A bind-mounted checkout (dev) already has .git.
|
||||
ensure_source() {
|
||||
mkdir -p "$STATE"
|
||||
[ -d "$SRC/.git" ] && return 0
|
||||
[ -n "${REPO_URL:-}" ] || die "no source at $SRC and REPO_URL is not set in .env"
|
||||
log "cloning $(redact "$REPO_URL") (${REPO_REF:-main}) into $SRC"
|
||||
# $SRC may exist as an empty dir; clone into it.
|
||||
git clone --quiet --branch "${REPO_REF:-main}" "$REPO_URL" "$SRC"
|
||||
}
|
||||
|
||||
# Follow REPO_URL if .env changed it. Credentials are never part of the stored URL.
|
||||
sync_remote() {
|
||||
[ -n "${REPO_URL:-}" ] || return 0
|
||||
[ "$(git -C "$SRC" remote get-url origin 2>/dev/null)" = "$REPO_URL" ] || git -C "$SRC" remote set-url origin "$REPO_URL"
|
||||
}
|
||||
|
||||
requirement_files() {
|
||||
echo "$SRC/requirements.txt"
|
||||
if [ "${DEV:-}" = 1 ] && [ -f "$SRC/requirements-dev.txt" ]; then echo "$SRC/requirements-dev.txt"; fi
|
||||
}
|
||||
|
||||
# Bring the venv in line with the checkout. A no-op when nothing changed.
|
||||
sync_deps() {
|
||||
local py want files
|
||||
py=$(python3 -c 'import sys; print("%d.%d.%d" % sys.version_info[:3])')
|
||||
mapfile -t files < <(requirement_files)
|
||||
want="$py $(cat "${files[@]}" | sha256sum | cut -d' ' -f1)"
|
||||
if [ "$(cat "$VENV/.python" 2>/dev/null)" != "$py" ]; then
|
||||
log "creating venv for Python $py"
|
||||
rm -rf "$VENV"
|
||||
python3 -m venv "$VENV"
|
||||
echo "$py" > "$VENV/.python"
|
||||
fi
|
||||
[ "$(cat "$VENV/.synced" 2>/dev/null)" = "$want" ] && return 0
|
||||
log "installing requirements"
|
||||
local args=()
|
||||
for f in "${files[@]}"; do args+=(-r "$f"); done
|
||||
"$VENV/bin/pip" install --quiet --disable-pip-version-check --no-cache-dir "${args[@]}" || return 1
|
||||
echo "$want" > "$VENV/.synced"
|
||||
}
|
||||
|
||||
# The new code must at least import before it replaces the running app.
|
||||
check_import() {
|
||||
(cd "$SRC" && "$VENV/bin/python" -c 'import app.main') || return 1
|
||||
}
|
||||
|
||||
current_version() { git -C "$SRC" rev-parse --short HEAD 2>/dev/null || echo dev; }
|
||||
|
||||
# Wait until /healthz answers with the expected version (or any version if none given).
|
||||
wait_healthy() {
|
||||
local want=${1:-} timeout=${HEALTH_TIMEOUT:-30} got
|
||||
for _ in $(seq 1 "$timeout"); do
|
||||
got=$(python3 - "$PORT" <<'PY' 2>/dev/null
|
||||
import json, sys, urllib.request
|
||||
r = urllib.request.urlopen(f"http://127.0.0.1:{sys.argv[1]}/healthz", timeout=2)
|
||||
print(json.load(r).get("version", ""))
|
||||
PY
|
||||
) && { [ -z "$want" ] || [ "$got" = "$want" ]; } && return 0
|
||||
sleep 1
|
||||
done
|
||||
return 1
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue