network-monitor/native/setup.sh
neil cd2deda996 LXC installs: announce <hostname>.local with Avahi, hub on port 80
In an LXC (lxc mode on a Proxmox node, or native mode inside an LXC), the
installer now:
- installs avahi-daemon (with the rlimit-nproc fix for unprivileged
  containers), so the container answers at <hostname>.local, and lists the
  web UI as a _http._tcp service
- serves the hub on port 80 (CAP_NET_BIND_SERVICE for the netmon user) unless
  HOST_PORT is given, so http://netmon-hub.local works without a port
- runs the inner install with LANG/LC_ALL=C.UTF-8 (no locale warnings from
  the node's LANG) and prints one summary, with the .local address

When the UI is opened by a .local name, the Add probe command uses the hub's
own IP for HUB_URL, since Linux probes usually can't resolve .local.

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

177 lines
7.1 KiB
Bash
Executable file

#!/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 <hostname>.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)
[ "$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" <<CONF
NAME=$NAME
ROLE=$ROLE
PLATFORM=$PLATFORM
SERVICE=$SERVICE
SVC_USER=$SVC_USER
PYTHON=$PYTHON
CONF
# The <name>-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"
native_log "service"
if [ "$PLATFORM" = systemd ]; then
cat > "/etc/systemd/system/$SERVICE.service" <<UNIT
[Unit]
Description=Network Monitor $ROLE ($NAME)
After=network-online.target
Wants=network-online.target
[Service]
User=$SVC_USER
Group=$SVC_USER
WorkingDirectory=$INSTALL_DIR
ExecStart=/bin/bash $INSTALL_DIR/native/run.sh
Restart=always
RestartSec=5
NoNewPrivileges=yes
# Lets the hub listen on port 80 in an LXC (http://<hostname>.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" <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>$SERVICE</string>
<key>UserName</key><string>$SVC_USER</string>
<key>ProgramArguments</key>
<array><string>/bin/bash</string><string>$INSTALL_DIR/native/run.sh</string></array>
<key>WorkingDirectory</key><string>$INSTALL_DIR</string>
<key>EnvironmentVariables</key>
<dict><key>PATH</key><string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string></dict>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
<key>ThrottleInterval</key><integer>5</integer>
<key>StandardOutPath</key><string>$INSTALL_DIR/data/service.log</string>
<key>StandardErrorPath</key><string>$INSTALL_DIR/data/service.log</string>
</dict>
</plist>
PLIST
as_root install -m 644 -o root -g wheel "$plist" "/Library/LaunchDaemons/$SERVICE.plist"
rm -f "$plist"
as_root launchctl bootout "system/$SERVICE" >/dev/null 2>&1 || true
as_root launchctl bootstrap system "/Library/LaunchDaemons/$SERVICE.plist"
as_root launchctl enable "system/$SERVICE" >/dev/null 2>&1 || true
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" <<SVC
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">Network Monitor ($ROLE) on %h</name>
<service><type>_http._tcp</type><port>$port</port></service>
</service-group>
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; <hostname>.local will not be announced"
fi
fi
native_log "setup done ($PLATFORM service $SERVICE, user $SVC_USER)"