| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #!/bin/sh
- # =============================================================================
- # NetPulse — one-liner installer (Linux server)
- #
- # Read-only PWA dashboard for monitoring OpenWrt/GL.iNet home networks.
- # Installs the single static Go binary (frontend embedded) as a sandboxed
- # systemd service.
- #
- # Usage:
- # curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install.sh | sh
- # sh install.sh --version=1.0.0 --unattended
- # sh install.sh --dry-run # describe every step, touches nothing
- # sh install.sh --uninstall # keeps /var/lib/netpulse (data + .env)
- # sh install.sh --uninstall --purge
- #
- # Requirements: Linux with systemd (Debian/Ubuntu/Fedora/Arch/...),
- # amd64 / arm64 / armv7. Verifies sha256 of every download (checksums.txt).
- #
- # Layout (capistrano-less: single binary + state dir):
- # /usr/local/bin/netpulse binary
- # /var/lib/netpulse working dir: .env, data/ (SQLite), .ssh/
- # =============================================================================
- set -eu
- APP_NAME="netpulse"
- GH_REPO="gnacho/netpulse"
- BIN_NAME="netpulse"
- DEFAULT_PORT="3000"
- INSTALL_DIR="/usr/local/bin"
- STATE_DIR="/var/lib/$APP_NAME"
- SERVICE_NAME="$APP_NAME"
- VERSION=""; UNATTENDED=0; DRY_RUN=0; UNINSTALL=0; PURGE=0
- # ---------------------------------------------------------------- logging ---
- if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
- C_G=$(printf '\033[32m'); C_R=$(printf '\033[31m'); C_Y=$(printf '\033[33m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
- else C_G=""; C_R=""; C_Y=""; C_B=""; C_0=""; fi
- info() { printf '%s--%s %s\n' "$C_B" "$C_0" "$*"; }
- ok() { printf '%s✓%s %s\n' "$C_G" "$C_0" "$*"; }
- warn() { printf '%s!%s %s\n' "$C_Y" "$C_0" "$*" >&2; }
- err() { printf '%s✗ %s%s\n' "$C_R" "$*" "$C_0" >&2; }
- fatal() { _c=$1; shift; err "$*"; exit "$_c"; }
- run() { if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] $*"; else "$@"; fi; }
- usage() {
- cat <<EOF
- NetPulse — installer (network monitoring dashboard for OpenWrt/GL.iNet)
- Usage: sh install.sh [options]
- --version=X.Y.Z version to install (default: latest stable release)
- --unattended no questions (automatic when there's no TTY)
- --dry-run describe each step without touching the system
- --uninstall remove service, binary and user (keeps $STATE_DIR)
- --purge with --uninstall: also remove data and configuration
- -h, --help this help
- Update = re-run this script (your data and .env are preserved).
- Repo: https://github.com/$GH_REPO
- EOF
- exit 0
- }
- for arg in "$@"; do
- case "$arg" in
- --version=*) VERSION="${arg#*=}" ;;
- --unattended) UNATTENDED=1 ;;
- --dry-run) DRY_RUN=1 ;;
- --uninstall) UNINSTALL=1 ;;
- --purge) PURGE=1 ;;
- -h|--help) usage ;;
- *) fatal 10 "unknown option: $arg (try --help)" ;;
- esac
- done
- [ -t 0 ] || UNATTENDED=1 # pipe-to-shell: never prompt
- # -------------------------------------------------------------- elevation ---
- if [ "$(id -u)" -eq 0 ]; then SUDO=""
- elif command -v sudo >/dev/null 2>&1; then SUDO="sudo -E"
- elif command -v doas >/dev/null 2>&1; then SUDO="doas"
- else fatal 22 "I need root (or sudo/doas). Download the script and run it as root: su -c 'sh install.sh'"
- fi
- # --------------------------------------------------------------- uninstall --
- if [ "$UNINSTALL" -eq 1 ]; then
- info "uninstalling $APP_NAME"
- if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
- run $SUDO systemctl stop "$SERVICE_NAME" 2>/dev/null || true
- run $SUDO systemctl disable "$SERVICE_NAME" 2>/dev/null || true
- run $SUDO rm -f "/etc/systemd/system/$SERVICE_NAME.service"
- run $SUDO systemctl daemon-reload
- ok "systemd unit removed"
- fi
- run $SUDO rm -f "$INSTALL_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME.bak"
- ok "binary removed from $INSTALL_DIR"
- if id "$APP_NAME" >/dev/null 2>&1; then
- run $SUDO userdel "$APP_NAME" 2>/dev/null || warn "could not delete user $APP_NAME"
- ok "system user removed"
- fi
- if [ "$PURGE" -eq 1 ]; then
- run $SUDO rm -rf "$STATE_DIR"
- ok "data and configuration removed (--purge)"
- else
- info "data kept in $STATE_DIR (remove with --purge)"
- fi
- ok "$APP_NAME uninstalled"
- exit 0
- fi
- # --------------------------------------------------------------- detection --
- . /etc/os-release 2>/dev/null || true
- OS_PRETTY="${PRETTY_NAME:-$(uname -s)}"
- ARCH=$(uname -m)
- case "$ARCH" in
- x86_64|amd64) GOARCH=amd64 ;;
- aarch64|arm64) GOARCH=arm64 ;;
- armv7l|armv7) GOARCH=armv7 ;;
- *) fatal 20 "unsupported architecture: $ARCH (released: amd64, arm64, armv7)"
- esac
- if [ ! -d /run/systemd/system ] || ! command -v systemctl >/dev/null 2>&1; then
- fatal 23 "NetPulse needs systemd (this machine doesn't run it). See https://github.com/$GH_REPO for manual setup"
- fi
- info "detected: $OS_PRETTY · linux/$GOARCH · systemd"
- if command -v curl >/dev/null 2>&1; then FETCH="curl -fsSL --retry 3 --connect-timeout 10"
- elif command -v wget >/dev/null 2>&1; then FETCH="wget -q -O-"
- else fatal 21 "I need curl or wget (install it with your package manager)"
- fi
- fetch_to() { $FETCH "$1" > "$2"; }
- command -v sha256sum >/dev/null 2>&1 || fatal 21 "missing sha256sum (coreutils package)"
- # --------------------------------------------------------- resolve version --
- if [ -z "$VERSION" ]; then
- info "resolving latest stable version"
- VERSION=$($FETCH "https://api.github.com/repos/$GH_REPO/releases/latest" \
- | grep '"tag_name"' | head -1 | cut -d'"' -f4) \
- || fatal 31 "could not resolve the latest version (GitHub rate-limit?). Use --version=X.Y.Z"
- [ -n "$VERSION" ] || fatal 31 "no stable release found yet. Use --version=X.Y.Z"
- fi
- VERSION_NORM=$(echo "$VERSION" | sed 's/^v//')
- ASSET="${BIN_NAME}_${VERSION_NORM}_linux_${GOARCH}.tar.gz"
- BASE_URL="https://github.com/$GH_REPO/releases/download/$VERSION"
- info "version: $VERSION"
- # connectivity pre-flight BEFORE touching the system
- $FETCH "https://github.com/$GH_REPO" >/dev/null \
- || fatal 30 "no access to github.com (proxy? DNS? firewall?)"
- UPGRADING=0
- if [ -x "$INSTALL_DIR/$BIN_NAME" ]; then
- UPGRADING=1
- info "previous install detected: upgrade mode (data and .env are preserved)"
- fi
- # ---------------------------------------------------------- download+verify --
- TMP=$(mktemp -d) || fatal 34 "mktemp failed"
- cleanup() { rm -rf "$TMP"; }
- trap cleanup EXIT INT TERM
- info "downloading $ASSET"
- fetch_to "$BASE_URL/$ASSET" "$TMP/$ASSET" || fatal 32 "download failed: $BASE_URL/$ASSET"
- fetch_to "$BASE_URL/checksums.txt" "$TMP/checksums.txt" || fatal 32 "checksums.txt not found in release $VERSION"
- [ -s "$TMP/$ASSET" ] || fatal 32 "downloaded asset is empty"
- SUM_FILE=$(sha256sum "$TMP/$ASSET" | awk '{print $1}')
- SUM_REF=$(grep " $ASSET\$" "$TMP/checksums.txt" | awk '{print $1}')
- [ -n "$SUM_REF" ] || fatal 33 "$ASSET not listed in checksums.txt"
- [ "$SUM_FILE" = "$SUM_REF" ] || fatal 33 "sha256 MISMATCH for $ASSET — corrupt or tampered download"
- ok "sha256 verified"
- tar -tzf "$TMP/$ASSET" >/dev/null 2>&1 || fatal 34 "tarball is corrupt"
- tar -xzf "$TMP/$ASSET" -C "$TMP"
- [ -s "$TMP/$BIN_NAME" ] || fatal 34 "tarball does not contain $BIN_NAME"
- # ------------------------------------------------------------------ install --
- run $SUDO install -d -m 0755 "$INSTALL_DIR"
- if [ "$UPGRADING" -eq 1 ]; then
- run $SUDO cp -a "$INSTALL_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME.bak"
- info "previous binary backed up: $INSTALL_DIR/$BIN_NAME.bak"
- fi
- run $SUDO install -T -m 0755 "$TMP/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME"
- ok "binary installed at $INSTALL_DIR/$BIN_NAME"
- if ! id "$APP_NAME" >/dev/null 2>&1; then
- run $SUDO useradd --system --home-dir "$STATE_DIR" --shell /usr/sbin/nologin "$APP_NAME"
- ok "system user $APP_NAME created"
- fi
- run $SUDO install -d -m 0750 -o "$APP_NAME" -g "$APP_NAME" "$STATE_DIR"
- # SELinux: context fix, degrade to warning
- if command -v getenforce >/dev/null 2>&1 && [ "$(getenforce)" = "Enforcing" ]; then
- run $SUDO chcon -t bin_t "$INSTALL_DIR/$BIN_NAME" 2>/dev/null \
- || warn "SELinux Enforcing: if the service fails, check 'ausearch -m avc -ts recent'"
- fi
- # Initial config ONLY on fresh install (upgrades never touch .env or data)
- if [ ! -f "$STATE_DIR/.env" ]; then
- ADMIN_PASS=$(head -c 18 /dev/urandom | base64 | tr -d '/+=' | head -c 16)
- if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] would generate $STATE_DIR/.env (0600, random admin password)"; else
- $SUDO tee "$STATE_DIR/.env" >/dev/null <<EOF
- PORT=$DEFAULT_PORT
- DATA_DIR=./data
- AUTH_USER=admin
- AUTH_PASS=$ADMIN_PASS
- DEMO_MODE=0
- SSH_KEY_PATH=$STATE_DIR/.ssh/id_ed25519
- EOF
- $SUDO chmod 0600 "$STATE_DIR/.env"
- $SUDO chown "$APP_NAME:$APP_NAME" "$STATE_DIR/.env"
- fi
- FRESH_CREDENTIALS=1
- else
- FRESH_CREDENTIALS=0
- info "existing config kept ($STATE_DIR/.env)"
- fi
- # ----------------------------------------------------------------- service --
- if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] would write systemd unit + enable --now"; else
- $SUDO tee "/etc/systemd/system/$SERVICE_NAME.service" >/dev/null <<EOF
- [Unit]
- Description=NetPulse — network monitoring dashboard (OpenWrt/GL.iNet)
- After=network-online.target
- Wants=network-online.target
- [Service]
- Type=simple
- User=$APP_NAME
- Group=$APP_NAME
- WorkingDirectory=$STATE_DIR
- ExecStart=$INSTALL_DIR/$BIN_NAME
- Restart=on-failure
- RestartSec=5
- NoNewPrivileges=true
- ProtectSystem=strict
- ProtectHome=read-only
- PrivateTmp=true
- LockPersonality=true
- RemoveIPC=true
- StateDirectory=$APP_NAME
- UMask=007
- [Install]
- WantedBy=multi-user.target
- EOF
- fi
- run $SUDO systemctl daemon-reload
- if [ "$UPGRADING" -eq 1 ]; then run $SUDO systemctl restart "$SERVICE_NAME"
- else run $SUDO systemctl enable --now "$SERVICE_NAME"; fi
- if [ "$DRY_RUN" -eq 0 ]; then
- sleep 3
- if ! $SUDO systemctl is-active --quiet "$SERVICE_NAME"; then
- err "service failed to start; diagnostics:"
- $SUDO systemctl status "$SERVICE_NAME" --no-pager || true
- $SUDO journalctl -u "$SERVICE_NAME" -n 50 --no-pager || true
- exit 40
- fi
- ok "service $SERVICE_NAME active"
- if command -v curl >/dev/null 2>&1; then
- curl -fsS --max-time 5 "http://127.0.0.1:$DEFAULT_PORT/api/health" >/dev/null 2>&1 \
- && ok "HTTP health check OK on :$DEFAULT_PORT" \
- || warn "service is up but http://127.0.0.1:$DEFAULT_PORT didn't answer yet (give it a few seconds)"
- fi
- fi
- # ------------------------------------------------------------------ summary --
- printf '\n%s================ %s installed ================%s\n' "$C_G" "$APP_NAME" "$C_0"
- printf 'Version: %s%s\n' "$VERSION" "$( [ "$UPGRADING" -eq 1 ] && echo " (upgrade — previous binary at $INSTALL_DIR/$BIN_NAME.bak)" || true)"
- printf 'Binary: %s\n' "$INSTALL_DIR/$BIN_NAME"
- printf 'Data: %s (SQLite, .env, SSH keypair)\n' "$STATE_DIR"
- printf 'Access: http://<this-machine-ip>:%s\n' "$DEFAULT_PORT"
- if [ "${FRESH_CREDENTIALS:-0}" -eq 1 ] && [ "$DRY_RUN" -eq 0 ]; then
- printf '\nInitial credentials (shown ONCE — change them after logging in):\n'
- printf ' user: admin\n password: %s\n' "$ADMIN_PASS"
- fi
- printf '\nUseful commands:\n'
- printf ' systemctl status %s\n journalctl -u %s -f\n' "$SERVICE_NAME" "$SERVICE_NAME"
- printf ' sh install.sh # update to the latest stable version\n'
- printf ' sh install.sh --uninstall\n'
- printf '\nNotes:\n'
- printf ' - No firewall port was opened. If you need one:\n'
- printf ' firewall-cmd --permanent --add-port=%s/tcp && firewall-cmd --reload\n' "$DEFAULT_PORT"
- printf ' ufw allow %s/tcp\n' "$DEFAULT_PORT"
- printf ' - Live mode: authorize the server SSH public key on each router\n'
- printf ' (Settings shows it; append to /etc/dropbear/authorized_keys).\n'
- printf ' - The in-app updater follows rolling builds from main; for stable\n'
- printf ' updates re-run this script.\n'
- printf '%s================================================%s\n\n' "$C_G" "$C_0"
|