network-monitor/native/update.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

87 lines
3.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Deploy a new commit to a native install, the same way `docker exec <name> update` does in
# a container: fetch, check out, sync requirements, check that app.main imports, restart the
# service, wait for /healthz to report the new commit; on any failure, go back.
# <name>-update to the tip of REPO_REF
# <name>-update <ref> to a tag, branch or commit
# <name>-update --check what an update would bring in
# <name>-update --rollback back to the commit before the last update
# Linux: run with sudo (the checkout is root-owned). macOS: run as the installing user.
set -euo pipefail
INSTALL_DIR=${NETMON_DIR:?run this through the <name>-update command}
. "$INSTALL_DIR/native/lib.sh"
. "$INSTALL_DIR/.native"
usage() { sed -n '2,10p' "$0" | sed 's/^# \{0,1\}//'; exit "${1:-0}"; }
mode=update ref=
while [ $# -gt 0 ]; do
case $1 in
--check) mode=check ;;
--rollback) mode=rollback ;;
-h|--help) usage ;;
-*) usage 1 ;;
*) ref=$1 ;;
esac
shift
done
if [ "$PLATFORM" = systemd ] && [ "$(id -u)" != 0 ]; then native_die "run it with sudo: sudo $NAME-update $*"; fi
if [ "$PLATFORM" = launchd ] && [ "$(id -u)" = 0 ] && [ "$SVC_USER" != root ]; then
exec sudo -u "$SVC_USER" NETMON_DIR="$INSTALL_DIR" /bin/bash "$0" "$@"
fi
load_dotenv "$INSTALL_DIR/.env"
setup_git_auth
cd "$INSTALL_DIR"
STATE="$INSTALL_DIR/.native-state"
mkdir -p "$STATE"
LOCK="$STATE/update.lock"
if ! mkdir "$LOCK" 2>/dev/null; then native_die "another update is running (or remove $LOCK)"; fi
trap 'rmdir "$LOCK" 2>/dev/null; [ -n "${NATIVE_KEYFILE:-}" ] && rm -f "$NATIVE_KEYFILE"' EXIT
[ -n "${REPO_URL:-}" ] && [ "$(git remote get-url origin 2>/dev/null)" != "$REPO_URL" ] && git remote set-url origin "$REPO_URL"
short() { git rev-parse --short "$1"; }
old=$(git rev-parse HEAD)
if [ "$mode" = rollback ]; then
new=$(cat "$STATE/previous" 2>/dev/null) || native_die "no previous commit recorded"
else
git fetch --quiet --prune --tags origin
target=${ref:-${REPO_REF:-main}}
new=$(git rev-parse --verify --quiet "origin/$target^{commit}" || git rev-parse --verify --quiet "$target^{commit}") \
|| native_die "unknown ref: $target"
fi
if [ "$mode" = check ]; then
if [ "$old" = "$new" ]; then echo "up to date at $(short "$old")"; else
echo "$(short "$old") -> $(short "$new")"; git log --oneline "$old..$new" 2>/dev/null || true
fi
exit 0
fi
[ "$old" = "$new" ] && { echo "already at $(short "$new")"; exit 0; }
check_import() { (cd "$INSTALL_DIR" && ROLE="$ROLE" DATA_DIR="$INSTALL_DIR/data" "$INSTALL_DIR/.venv/bin/python" -c 'import app.main'); }
apply() { git reset --quiet --hard "$1" && sync_venv "$INSTALL_DIR" "$PYTHON" && check_import; }
refresh_self() { # pick up a newer update.sh; rename keeps this running copy intact
local tmp="$INSTALL_DIR/.native-update.sh.new"
cp "$INSTALL_DIR/native/update.sh" "$tmp" && chmod 755 "$tmp" && mv -f "$tmp" "$INSTALL_DIR/.native-update.sh"
}
native_log "updating $(short "$old") -> $(short "$new")"
restarted=0
if apply "$new" && restarted=1 && restart_service && wait_health "$(short "$new")" "${HEALTH_TIMEOUT:-40}"; then
echo "$old" > "$STATE/previous"
git log --oneline "$old..$new" 2>/dev/null | head -20 || true
refresh_self || true
if [ -n "$(git diff --name-only "$old" "$new" -- native/setup.sh)" ]; then
native_log "note: native/setup.sh changed; re-run the installer to apply service changes"
fi
echo "updated to $(short "$new")"
exit 0
fi
native_log "update failed; going back to $(short "$old")"
apply "$old" || native_die "could not restore $(short "$old"); check: $(service_logs_hint)"
[ "$restarted" = 1 ] || { native_log "the running app was never touched"; exit 1; }
restart_service && wait_health "$(short "$old")" "${HEALTH_TIMEOUT:-40}" || native_die "restored $(short "$old") but it is not healthy; check: $(service_logs_hint)"
native_log "running $(short "$old") again"
exit 1