install.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/bin/sh
  2. # =============================================================================
  3. # NetPulse — one-liner installer (Linux server)
  4. #
  5. # Read-only PWA dashboard for monitoring OpenWrt/GL.iNet home networks.
  6. # Installs the single static Go binary (frontend embedded) as a sandboxed
  7. # systemd service.
  8. #
  9. # Usage:
  10. # curl -fsSL https://raw.githubusercontent.com/gnacho/netpulse/main/install.sh | sh
  11. # sh install.sh --version=1.0.0 --unattended
  12. # sh install.sh --dry-run # describe every step, touches nothing
  13. # sh install.sh --uninstall # keeps /var/lib/netpulse (data + .env)
  14. # sh install.sh --uninstall --purge
  15. #
  16. # Requirements: Linux with systemd (Debian/Ubuntu/Fedora/Arch/...),
  17. # amd64 / arm64 / armv7. Verifies sha256 of every download (checksums.txt).
  18. #
  19. # Layout (capistrano-less: single binary + state dir):
  20. # /usr/local/bin/netpulse binary
  21. # /var/lib/netpulse working dir: .env, data/ (SQLite), .ssh/
  22. # =============================================================================
  23. set -eu
  24. APP_NAME="netpulse"
  25. GH_REPO="gnacho/netpulse"
  26. BIN_NAME="netpulse"
  27. DEFAULT_PORT="3000"
  28. INSTALL_DIR="/usr/local/bin"
  29. STATE_DIR="/var/lib/$APP_NAME"
  30. SERVICE_NAME="$APP_NAME"
  31. VERSION=""; UNATTENDED=0; DRY_RUN=0; UNINSTALL=0; PURGE=0
  32. # ---------------------------------------------------------------- logging ---
  33. if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
  34. 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')
  35. else C_G=""; C_R=""; C_Y=""; C_B=""; C_0=""; fi
  36. info() { printf '%s--%s %s\n' "$C_B" "$C_0" "$*"; }
  37. ok() { printf '%s✓%s %s\n' "$C_G" "$C_0" "$*"; }
  38. warn() { printf '%s!%s %s\n' "$C_Y" "$C_0" "$*" >&2; }
  39. err() { printf '%s✗ %s%s\n' "$C_R" "$*" "$C_0" >&2; }
  40. fatal() { _c=$1; shift; err "$*"; exit "$_c"; }
  41. run() { if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] $*"; else "$@"; fi; }
  42. usage() {
  43. cat <<EOF
  44. NetPulse — installer (network monitoring dashboard for OpenWrt/GL.iNet)
  45. Usage: sh install.sh [options]
  46. --version=X.Y.Z version to install (default: latest stable release)
  47. --unattended no questions (automatic when there's no TTY)
  48. --dry-run describe each step without touching the system
  49. --uninstall remove service, binary and user (keeps $STATE_DIR)
  50. --purge with --uninstall: also remove data and configuration
  51. -h, --help this help
  52. Update = re-run this script (your data and .env are preserved).
  53. Repo: https://github.com/$GH_REPO
  54. EOF
  55. exit 0
  56. }
  57. for arg in "$@"; do
  58. case "$arg" in
  59. --version=*) VERSION="${arg#*=}" ;;
  60. --unattended) UNATTENDED=1 ;;
  61. --dry-run) DRY_RUN=1 ;;
  62. --uninstall) UNINSTALL=1 ;;
  63. --purge) PURGE=1 ;;
  64. -h|--help) usage ;;
  65. *) fatal 10 "unknown option: $arg (try --help)" ;;
  66. esac
  67. done
  68. [ -t 0 ] || UNATTENDED=1 # pipe-to-shell: never prompt
  69. # -------------------------------------------------------------- elevation ---
  70. if [ "$(id -u)" -eq 0 ]; then SUDO=""
  71. elif command -v sudo >/dev/null 2>&1; then SUDO="sudo -E"
  72. elif command -v doas >/dev/null 2>&1; then SUDO="doas"
  73. else fatal 22 "I need root (or sudo/doas). Download the script and run it as root: su -c 'sh install.sh'"
  74. fi
  75. # --------------------------------------------------------------- uninstall --
  76. if [ "$UNINSTALL" -eq 1 ]; then
  77. info "uninstalling $APP_NAME"
  78. if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
  79. run $SUDO systemctl stop "$SERVICE_NAME" 2>/dev/null || true
  80. run $SUDO systemctl disable "$SERVICE_NAME" 2>/dev/null || true
  81. run $SUDO rm -f "/etc/systemd/system/$SERVICE_NAME.service"
  82. run $SUDO systemctl daemon-reload
  83. ok "systemd unit removed"
  84. fi
  85. run $SUDO rm -f "$INSTALL_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME.bak"
  86. ok "binary removed from $INSTALL_DIR"
  87. if id "$APP_NAME" >/dev/null 2>&1; then
  88. run $SUDO userdel "$APP_NAME" 2>/dev/null || warn "could not delete user $APP_NAME"
  89. ok "system user removed"
  90. fi
  91. if [ "$PURGE" -eq 1 ]; then
  92. run $SUDO rm -rf "$STATE_DIR"
  93. ok "data and configuration removed (--purge)"
  94. else
  95. info "data kept in $STATE_DIR (remove with --purge)"
  96. fi
  97. ok "$APP_NAME uninstalled"
  98. exit 0
  99. fi
  100. # --------------------------------------------------------------- detection --
  101. . /etc/os-release 2>/dev/null || true
  102. OS_PRETTY="${PRETTY_NAME:-$(uname -s)}"
  103. ARCH=$(uname -m)
  104. case "$ARCH" in
  105. x86_64|amd64) GOARCH=amd64 ;;
  106. aarch64|arm64) GOARCH=arm64 ;;
  107. armv7l|armv7) GOARCH=armv7 ;;
  108. *) fatal 20 "unsupported architecture: $ARCH (released: amd64, arm64, armv7)"
  109. esac
  110. if [ ! -d /run/systemd/system ] || ! command -v systemctl >/dev/null 2>&1; then
  111. fatal 23 "NetPulse needs systemd (this machine doesn't run it). See https://github.com/$GH_REPO for manual setup"
  112. fi
  113. info "detected: $OS_PRETTY · linux/$GOARCH · systemd"
  114. if command -v curl >/dev/null 2>&1; then FETCH="curl -fsSL --retry 3 --connect-timeout 10"
  115. elif command -v wget >/dev/null 2>&1; then FETCH="wget -q -O-"
  116. else fatal 21 "I need curl or wget (install it with your package manager)"
  117. fi
  118. fetch_to() { $FETCH "$1" > "$2"; }
  119. command -v sha256sum >/dev/null 2>&1 || fatal 21 "missing sha256sum (coreutils package)"
  120. # --------------------------------------------------------- resolve version --
  121. if [ -z "$VERSION" ]; then
  122. info "resolving latest stable version"
  123. VERSION=$($FETCH "https://api.github.com/repos/$GH_REPO/releases/latest" \
  124. | grep '"tag_name"' | head -1 | cut -d'"' -f4) \
  125. || fatal 31 "could not resolve the latest version (GitHub rate-limit?). Use --version=X.Y.Z"
  126. [ -n "$VERSION" ] || fatal 31 "no stable release found yet. Use --version=X.Y.Z"
  127. fi
  128. VERSION_NORM=$(echo "$VERSION" | sed 's/^v//')
  129. ASSET="${BIN_NAME}_${VERSION_NORM}_linux_${GOARCH}.tar.gz"
  130. BASE_URL="https://github.com/$GH_REPO/releases/download/$VERSION"
  131. info "version: $VERSION"
  132. # connectivity pre-flight BEFORE touching the system
  133. $FETCH "https://github.com/$GH_REPO" >/dev/null \
  134. || fatal 30 "no access to github.com (proxy? DNS? firewall?)"
  135. UPGRADING=0
  136. if [ -x "$INSTALL_DIR/$BIN_NAME" ]; then
  137. UPGRADING=1
  138. info "previous install detected: upgrade mode (data and .env are preserved)"
  139. fi
  140. # ---------------------------------------------------------- download+verify --
  141. TMP=$(mktemp -d) || fatal 34 "mktemp failed"
  142. cleanup() { rm -rf "$TMP"; }
  143. trap cleanup EXIT INT TERM
  144. info "downloading $ASSET"
  145. fetch_to "$BASE_URL/$ASSET" "$TMP/$ASSET" || fatal 32 "download failed: $BASE_URL/$ASSET"
  146. fetch_to "$BASE_URL/checksums.txt" "$TMP/checksums.txt" || fatal 32 "checksums.txt not found in release $VERSION"
  147. [ -s "$TMP/$ASSET" ] || fatal 32 "downloaded asset is empty"
  148. SUM_FILE=$(sha256sum "$TMP/$ASSET" | awk '{print $1}')
  149. SUM_REF=$(grep " $ASSET\$" "$TMP/checksums.txt" | awk '{print $1}')
  150. [ -n "$SUM_REF" ] || fatal 33 "$ASSET not listed in checksums.txt"
  151. [ "$SUM_FILE" = "$SUM_REF" ] || fatal 33 "sha256 MISMATCH for $ASSET — corrupt or tampered download"
  152. ok "sha256 verified"
  153. tar -tzf "$TMP/$ASSET" >/dev/null 2>&1 || fatal 34 "tarball is corrupt"
  154. tar -xzf "$TMP/$ASSET" -C "$TMP"
  155. [ -s "$TMP/$BIN_NAME" ] || fatal 34 "tarball does not contain $BIN_NAME"
  156. # ------------------------------------------------------------------ install --
  157. run $SUDO install -d -m 0755 "$INSTALL_DIR"
  158. if [ "$UPGRADING" -eq 1 ]; then
  159. run $SUDO cp -a "$INSTALL_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME.bak"
  160. info "previous binary backed up: $INSTALL_DIR/$BIN_NAME.bak"
  161. fi
  162. run $SUDO install -T -m 0755 "$TMP/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME"
  163. ok "binary installed at $INSTALL_DIR/$BIN_NAME"
  164. if ! id "$APP_NAME" >/dev/null 2>&1; then
  165. run $SUDO useradd --system --home-dir "$STATE_DIR" --shell /usr/sbin/nologin "$APP_NAME"
  166. ok "system user $APP_NAME created"
  167. fi
  168. run $SUDO install -d -m 0750 -o "$APP_NAME" -g "$APP_NAME" "$STATE_DIR"
  169. # SELinux: context fix, degrade to warning
  170. if command -v getenforce >/dev/null 2>&1 && [ "$(getenforce)" = "Enforcing" ]; then
  171. run $SUDO chcon -t bin_t "$INSTALL_DIR/$BIN_NAME" 2>/dev/null \
  172. || warn "SELinux Enforcing: if the service fails, check 'ausearch -m avc -ts recent'"
  173. fi
  174. # Initial config ONLY on fresh install (upgrades never touch .env or data)
  175. if [ ! -f "$STATE_DIR/.env" ]; then
  176. ADMIN_PASS=$(head -c 18 /dev/urandom | base64 | tr -d '/+=' | head -c 16)
  177. if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] would generate $STATE_DIR/.env (0600, random admin password)"; else
  178. $SUDO tee "$STATE_DIR/.env" >/dev/null <<EOF
  179. PORT=$DEFAULT_PORT
  180. DATA_DIR=./data
  181. AUTH_USER=admin
  182. AUTH_PASS=$ADMIN_PASS
  183. DEMO_MODE=0
  184. SSH_KEY_PATH=$STATE_DIR/.ssh/id_ed25519
  185. EOF
  186. $SUDO chmod 0600 "$STATE_DIR/.env"
  187. $SUDO chown "$APP_NAME:$APP_NAME" "$STATE_DIR/.env"
  188. fi
  189. FRESH_CREDENTIALS=1
  190. else
  191. FRESH_CREDENTIALS=0
  192. info "existing config kept ($STATE_DIR/.env)"
  193. fi
  194. # ----------------------------------------------------------------- service --
  195. if [ "$DRY_RUN" -eq 1 ]; then info "[dry-run] would write systemd unit + enable --now"; else
  196. $SUDO tee "/etc/systemd/system/$SERVICE_NAME.service" >/dev/null <<EOF
  197. [Unit]
  198. Description=NetPulse — network monitoring dashboard (OpenWrt/GL.iNet)
  199. After=network-online.target
  200. Wants=network-online.target
  201. [Service]
  202. Type=simple
  203. User=$APP_NAME
  204. Group=$APP_NAME
  205. WorkingDirectory=$STATE_DIR
  206. ExecStart=$INSTALL_DIR/$BIN_NAME
  207. Restart=on-failure
  208. RestartSec=5
  209. NoNewPrivileges=true
  210. ProtectSystem=strict
  211. ProtectHome=read-only
  212. PrivateTmp=true
  213. LockPersonality=true
  214. RemoveIPC=true
  215. StateDirectory=$APP_NAME
  216. UMask=007
  217. [Install]
  218. WantedBy=multi-user.target
  219. EOF
  220. fi
  221. run $SUDO systemctl daemon-reload
  222. if [ "$UPGRADING" -eq 1 ]; then run $SUDO systemctl restart "$SERVICE_NAME"
  223. else run $SUDO systemctl enable --now "$SERVICE_NAME"; fi
  224. if [ "$DRY_RUN" -eq 0 ]; then
  225. sleep 3
  226. if ! $SUDO systemctl is-active --quiet "$SERVICE_NAME"; then
  227. err "service failed to start; diagnostics:"
  228. $SUDO systemctl status "$SERVICE_NAME" --no-pager || true
  229. $SUDO journalctl -u "$SERVICE_NAME" -n 50 --no-pager || true
  230. exit 40
  231. fi
  232. ok "service $SERVICE_NAME active"
  233. if command -v curl >/dev/null 2>&1; then
  234. curl -fsS --max-time 5 "http://127.0.0.1:$DEFAULT_PORT/api/health" >/dev/null 2>&1 \
  235. && ok "HTTP health check OK on :$DEFAULT_PORT" \
  236. || warn "service is up but http://127.0.0.1:$DEFAULT_PORT didn't answer yet (give it a few seconds)"
  237. fi
  238. fi
  239. # ------------------------------------------------------------------ summary --
  240. printf '\n%s================ %s installed ================%s\n' "$C_G" "$APP_NAME" "$C_0"
  241. printf 'Version: %s%s\n' "$VERSION" "$( [ "$UPGRADING" -eq 1 ] && echo " (upgrade — previous binary at $INSTALL_DIR/$BIN_NAME.bak)" || true)"
  242. printf 'Binary: %s\n' "$INSTALL_DIR/$BIN_NAME"
  243. printf 'Data: %s (SQLite, .env, SSH keypair)\n' "$STATE_DIR"
  244. printf 'Access: http://<this-machine-ip>:%s\n' "$DEFAULT_PORT"
  245. if [ "${FRESH_CREDENTIALS:-0}" -eq 1 ] && [ "$DRY_RUN" -eq 0 ]; then
  246. printf '\nInitial credentials (shown ONCE — change them after logging in):\n'
  247. printf ' user: admin\n password: %s\n' "$ADMIN_PASS"
  248. fi
  249. printf '\nUseful commands:\n'
  250. printf ' systemctl status %s\n journalctl -u %s -f\n' "$SERVICE_NAME" "$SERVICE_NAME"
  251. printf ' sh install.sh # update to the latest stable version\n'
  252. printf ' sh install.sh --uninstall\n'
  253. printf '\nNotes:\n'
  254. printf ' - No firewall port was opened. If you need one:\n'
  255. printf ' firewall-cmd --permanent --add-port=%s/tcp && firewall-cmd --reload\n' "$DEFAULT_PORT"
  256. printf ' ufw allow %s/tcp\n' "$DEFAULT_PORT"
  257. printf ' - Live mode: authorize the server SSH public key on each router\n'
  258. printf ' (Settings shows it; append to /etc/dropbear/authorized_keys).\n'
  259. printf ' - The in-app updater follows rolling builds from main; for stable\n'
  260. printf ' updates re-run this script.\n'
  261. printf '%s================================================%s\n\n' "$C_G" "$C_0"