network-monitor/container/entrypoint.sh
neil 54a765510a 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>
2026-11-04 18:10:29 -08:00

58 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Container entrypoint (runs under tini). Gets the source and venv ready, then supervises
# uvicorn: SIGHUP restarts the app on the current checkout (that's how `update` swaps code
# in without restarting the container); SIGTERM stops it. If the app dies on its own, the
# container exits and Docker's restart policy takes over.
#
# With arguments, it runs them in the source dir after setup instead, e.g.
# docker compose run --rm app pytest
set -uo pipefail
. /usr/local/lib/app/lib.sh
load_env
ensure_source || exit 1
sync_deps || die "installing requirements failed"
cd "$SRC"
if [ $# -gt 0 ]; then
export PATH="$VENV/bin:$PATH"
exec "$@"
fi
child=
stopping=0
restart=0
trap 'stopping=1; [ -n "$child" ] && kill -TERM "$child" 2>/dev/null' TERM INT
trap 'restart=1; [ -n "$child" ] && kill -TERM "$child" 2>/dev/null' HUP
echo $$ > "$PIDFILE"
start_app() {
local args=(app.main:app --host 0.0.0.0 --port "$PORT"
--proxy-headers --forwarded-allow-ips "${FORWARDED_ALLOW_IPS:-127.0.0.1}"
--no-server-header --timeout-graceful-shutdown 10)
[ "${DEV:-}" = 1 ] && args+=(--reload)
export APP_VERSION
APP_VERSION=$(current_version)
log "starting app at $APP_VERSION"
env "${APP_ENV_UNSET[@]}" "$VENV/bin/uvicorn" "${args[@]}" &
child=$!
}
while :; do
start_app
wait "$child"; rc=$?
# A trapped signal interrupts `wait` while the child is still shutting down.
while kill -0 "$child" 2>/dev/null; do wait "$child"; rc=$?; done
[ "$stopping" = 1 ] && exit 0
if [ "$restart" = 1 ]; then restart=0; continue; fi
log "app exited with status $rc"
# New code that crashes at startup must not take the container down mid-update, or
# `update` dies with it and can't roll back. Wait for its restart signal instead.
if ! flock -n /tmp/app-update.lock true; then
log "an update is running; waiting for it to restart the app"
while [ "$restart" = 0 ] && [ "$stopping" = 0 ]; do sleep 1 & wait $!; done
[ "$stopping" = 1 ] && exit 0
restart=0; continue
fi
exit "$rc"
done