network-monitor/native/update.sh
neil c9738358de Link <name>-update into /usr/bin so pct exec finds it without the full path
pct exec runs without a login shell and its PATH may lack /usr/local/bin.
setup.sh adds the link on Linux; update.sh adds it to older installs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 21:05:20 -08:00

90 lines
4.1 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" = systemd ] && [ ! -e "/usr/bin/$NAME-update" ]; then # installs older than the /usr/bin link
ln -sfn "/usr/local/bin/$NAME-update" "/usr/bin/$NAME-update" 2>/dev/null || true
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