#!/usr/bin/env bash # Native (no Docker) setup, run by install.sh / install-probe.sh after they clone the repo # and write .env. Idempotent. Linux: run as root, needs systemd. macOS: run as the user # who will own the install (uses sudo for the launchd daemon and the update command). # NAME=network-probe ROLE=probe INSTALL_DIR=/opt/network-probe bash native/setup.sh # AVAHI=1 also announce .local with Avahi (Linux; set by the installer in LXCs) set -euo pipefail : "${NAME:?}" "${ROLE:?}" "${INSTALL_DIR:?}" AVAHI=${AVAHI:-0} . "$INSTALL_DIR/native/lib.sh" SVC_USER_LINUX=netmon MIN_PY=11 as_root() { if [ "$(id -u)" = 0 ]; then "$@"; else sudo "$@"; fi; } find_python() { local c v for c in python3.14 python3.13 python3.12 python3.11 python3 \ /opt/homebrew/bin/python3 /usr/local/bin/python3 /opt/homebrew/opt/python@3.13/bin/python3.13; do command -v "$c" >/dev/null 2>&1 || continue v=$("$c" -c 'import sys; print(sys.version_info[0] == 3 and sys.version_info[1] >= '"$MIN_PY"')' 2>/dev/null) || continue if [ "$v" = True ] && "$c" -c 'import venv, ensurepip' >/dev/null 2>&1; then command -v "$c"; return 0; fi done return 1 } ensure_python() { PYTHON=$(find_python) && return 0 if [ "$PLATFORM" = launchd ]; then if command -v brew >/dev/null 2>&1; then native_log "installing Python 3.13 with Homebrew" brew install python@3.13 >/dev/null else native_die "needs Python 3.$MIN_PY+. Install Homebrew (https://brew.sh) and re-run, or install Python from python.org" fi elif command -v apt-get >/dev/null 2>&1; then native_log "installing python3 and python3-venv" DEBIAN_FRONTEND=noninteractive apt-get update -qq DEBIAN_FRONTEND=noninteractive apt-get install -y -qq python3 python3-venv >/dev/null elif command -v dnf >/dev/null 2>&1; then dnf install -y -q python3 elif command -v apk >/dev/null 2>&1; then apk add -q python3 fi PYTHON=$(find_python) || native_die "needs Python 3.$MIN_PY+ with venv; this system's python3 is $(python3 --version 2>&1 || echo missing). Use a newer distro (Debian 12+, Ubuntu 24.04+) or INSTALL_MODE=docker" } if [ "$(uname -s)" = Darwin ]; then PLATFORM=launchd SERVICE="dev.neilbantoc.$NAME" SVC_USER=$(id -un) # owns the checkout and runs -update; the daemon itself runs as root [ "$SVC_USER" != root ] || native_die "on macOS, run the installer as your own user, not with sudo" else PLATFORM=systemd SERVICE=$NAME SVC_USER=$SVC_USER_LINUX [ "$(id -u)" = 0 ] || native_die "native mode on Linux runs as root" [ -d /run/systemd/system ] || native_die "native mode on Linux needs systemd (this system has none). Use INSTALL_MODE=docker" fi native_log "python" ensure_python native_log "using $PYTHON ($("$PYTHON" --version 2>&1))" sync_venv "$INSTALL_DIR" "$PYTHON" || native_die "installing requirements failed" mkdir -p "$INSTALL_DIR/data" "$INSTALL_DIR/.native-state" if [ "$PLATFORM" = systemd ]; then id "$SVC_USER" >/dev/null 2>&1 || useradd --system --user-group --home-dir "$INSTALL_DIR/data" --shell /usr/sbin/nologin "$SVC_USER" chown -R "$SVC_USER:$SVC_USER" "$INSTALL_DIR/data" chown root:"$SVC_USER" "$INSTALL_DIR/.env" chmod 640 "$INSTALL_DIR/.env" else chmod 600 "$INSTALL_DIR/.env" fi cat > "$INSTALL_DIR/.native" <-update command runs a copy of update.sh (update replaces the checkout under it). cp "$INSTALL_DIR/native/update.sh" "$INSTALL_DIR/.native-update.sh" chmod 755 "$INSTALL_DIR/.native-update.sh" tmp=$(mktemp) printf '#!/bin/sh\n# Deploy a new commit of %s (native install). See: %s-update --help\nNETMON_DIR=%s exec /bin/bash %s/.native-update.sh "$@"\n' \ "$NAME" "$NAME" "$INSTALL_DIR" "$INSTALL_DIR" > "$tmp" as_root mkdir -p /usr/local/bin as_root install -m 755 "$tmp" "/usr/local/bin/$NAME-update" rm -f "$tmp" if [ "$PLATFORM" = systemd ]; then # Also in /usr/bin, which is on every PATH: `pct exec -- -update` from a Proxmox # node runs without a login shell and its PATH may not include /usr/local/bin. as_root ln -sfn "/usr/local/bin/$NAME-update" "/usr/bin/$NAME-update" fi native_log "service" if [ "$PLATFORM" = systemd ]; then cat > "/etc/systemd/system/$SERVICE.service" <.local) without running as root. AmbientCapabilities=CAP_NET_BIND_SERVICE [Install] WantedBy=multi-user.target UNIT systemctl daemon-reload systemctl enable "$SERVICE" >/dev/null 2>&1 systemctl restart "$SERVICE" else plist=$(mktemp) cat > "$plist" < Label$SERVICE ProgramArguments /bin/bash$INSTALL_DIR/native/run.sh WorkingDirectory$INSTALL_DIR EnvironmentVariables PATH/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin RunAtLoad KeepAlive ThrottleInterval5 StandardOutPath$INSTALL_DIR/data/service.log StandardErrorPath$INSTALL_DIR/data/service.log PLIST as_root install -m 644 -o root -g wheel "$plist" "/Library/LaunchDaemons/$SERVICE.plist" rm -f "$plist" plutil -lint "/Library/LaunchDaemons/$SERVICE.plist" >/dev/null || native_die "the generated plist is invalid: plutil -lint /Library/LaunchDaemons/$SERVICE.plist" # Unload the old job and wait until it is really gone: bootout returns while the job is # still stopping, and bootstrapping then fails with "5: Input/output error". as_root launchctl bootout "system/$SERVICE" >/dev/null 2>&1 || true i=0 while as_root launchctl print "system/$SERVICE" >/dev/null 2>&1 && [ "$i" -lt 30 ]; do sleep 0.5; i=$((i + 1)); done # A job marked disabled (launchctl disable) can't be bootstrapped either. as_root launchctl enable "system/$SERVICE" >/dev/null 2>&1 || true if ! as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist"; then sleep 2 # one retry, for a job that took longer to stop as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist" \ || native_die "launchctl bootstrap failed. Check: sudo launchctl print system/$SERVICE; sudo launchctl print-disabled system | grep $NAME" fi fi if [ "$PLATFORM" = systemd ] && [ "$AVAHI" = 1 ]; then native_log "avahi: announcing $(hostname -s).local" if ! command -v avahi-daemon >/dev/null 2>&1; then if command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y -qq avahi-daemon >/dev/null elif command -v dnf >/dev/null 2>&1; then dnf install -y -q avahi elif command -v apk >/dev/null 2>&1; then apk add -q avahi fi fi if [ -f /etc/avahi/avahi-daemon.conf ]; then # The stock rlimit-nproc=3 stops avahi-daemon from starting in unprivileged LXC containers. sed -i 's/^rlimit-nproc=/#rlimit-nproc=/' /etc/avahi/avahi-daemon.conf port=$(. "$INSTALL_DIR/native/lib.sh"; load_dotenv "$INSTALL_DIR/.env"; ROLE=$ROLE app_port) mkdir -p /etc/avahi/services # Also list the web UI in Bonjour/DNS-SD browsers. cat > "/etc/avahi/services/$NAME.service" < Network Monitor ($ROLE) on %h _http._tcp$port SVC systemctl enable avahi-daemon >/dev/null 2>&1 || true systemctl restart avahi-daemon || native_log "warning: avahi-daemon did not start; check: journalctl -u avahi-daemon" else native_log "warning: could not install avahi-daemon; .local will not be announced" fi fi native_log "setup done ($PLATFORM service $SERVICE, user $SVC_USER)"