network-monitor/container/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

81 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Pull the latest source from the remote repo in .env (REPO_URL, REPO_REF and credentials)
# into the running container and restart the app on it. No rebuild.
# docker exec <container> update # to the tip of REPO_REF
# docker exec <container> update <ref> # to a tag, branch or commit
# docker exec <container> update --check # show what an update would bring in
# docker exec <container> update --rollback # back to the commit before the last update
# Steps: fetch, check out, sync requirements, import check, restart, wait for /healthz to
# report the new commit. If any step fails, it puts the previous commit back and exits 1.
set -euo pipefail
. /usr/local/lib/app/lib.sh
usage() { sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit "${1:-0}"; }
mode=update force=0 ref=
while [ $# -gt 0 ]; do
case $1 in
--check) mode=check ;;
--rollback) mode=rollback ;;
--force) force=1 ;;
-h|--help) usage ;;
-*) usage 1 ;;
*) ref=$1 ;;
esac
shift
done
load_env
[ "${DEV:-}" = 1 ] && die "dev mode: the source is bind-mounted from the host; update it with git there"
[ -d "$SRC/.git" ] || die "no checkout at $SRC"
exec 9>/tmp/app-update.lock
flock -n 9 || die "another update is running"
cd "$SRC"
old=$(git rev-parse HEAD)
if [ "$mode" = rollback ]; then
new=$(cat "$STATE/previous" 2>/dev/null) || die "no previous commit recorded"
else
sync_remote
git fetch --quiet --prune --tags origin
target=${ref:-${REPO_REF:-main}}
# A branch resolves on the remote; a tag or commit as given.
new=$(git rev-parse --verify --quiet "origin/$target^{commit}" || git rev-parse --verify --quiet "$target^{commit}") \
|| die "unknown ref: $target"
fi
short() { git rev-parse --short "$1"; }
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; }
if [ "$force" = 0 ] && [ -n "$(git status --porcelain --untracked-files=no)" ]; then
die "the checkout has local changes (inspect with: docker exec ${CONTAINER_NAME:-<container>} git -C $SRC status); --force discards them"
fi
restart_app() { kill -HUP "$(cat "$PIDFILE")" || return 1; sleep 1; }
apply() { git reset --quiet --hard "$1" && sync_deps && check_import; }
restarted=0
log "updating $(short "$old") -> $(short "$new")"
if apply "$new" && restarted=1 && restart_app && wait_healthy "$(short "$new")"; then
echo "$old" > "$STATE/previous"
git log --oneline "$old..$new" 2>/dev/null | head -20 || true
if [ -n "$(git diff --name-only "$old" "$new" -- container/)" ]; then
log "note: container/ changed; rebuild the image to pick that up (docker compose up -d --build)"
fi
echo "updated to $(short "$new")"
exit 0
fi
log "update failed; going back to $(short "$old")"
apply "$old" || die "could not restore $(short "$old"): the app may be down, check: docker logs ${CONTAINER_NAME:-<container>}"
[ "$restarted" = 1 ] || { log "the running app was never touched"; exit 1; }
restart_app && wait_healthy "$(short "$old")" || die "restored $(short "$old") but it is not healthy; check: docker logs ${CONTAINER_NAME:-<container>}"
log "running $(short "$old") again"
exit 1