#!/usr/bin/env bash # node-stats: one-command agent install + cluster join. # # Generated for you by the admin panel (Admin → Nodes → Raft cluster sync → # "One-shot join token"); the command looks like: # # NODE_STATS_JOIN_URL="http://10.0.0.2:9090" \ # NODE_STATS_JOIN_KEY="<64-hex one-shot token>" \ # bash -c "$(curl -fsSL https://git.example.com/owner/node-page/releases/download//agent-join.sh)" # # (the release asset has the release sources filled in; the in-repo copy needs # NODE_STATS_REPO and/or NODE_STATS_UPDATE_MIRROR) # # What it does on a fresh machine: # 1. installs the Docker stack via scripts/install.sh (docker + compose v2 # checks, stack dir, gen-compose, `docker compose up -d`), # 2. waits for the app to come up, # 3. joins the cluster through the same API the setup wizard uses # (POST /setup/join-raft-cluster) — the node id, bind and advertise # addresses are derived automatically, # 4. waits until the leader's snapshot has replicated. # # Optional env knobs: # NODE_STATS_REPO GitHub owner/name for the raw scripts and releases # NODE_STATS_UPDATE_MIRROR Gitea/Forgejo repository URL (https://host/owner/name) # NODE_STATS_IMAGE container image override # NODE_STATS_CHANNEL=beta follow the beta release line (passed through to install.sh) # NODE_STATS_PORT HTTP port (default 9090) # NODE_STATS_RAFT_PORT Raft TCP port (default 7000) # NODE_STATS_DIR stack directory override # NODE_STATS_HOSTNAME display name for this machine # NODE_STATS_ADVERTISE_ADDR host:port other nodes use to reach this node's # Raft (default: primary IPv4 + RAFT_PORT) # NODE_STATS_SKIP_INSTALL=1 stack already running — only join set -euo pipefail # Release sources: filled in by CI in the release asset, from the env otherwise. REPO="${NODE_STATS_REPO-LastSkywalkerER/node-page}" MIRROR="${NODE_STATS_UPDATE_MIRROR-https://gitea.sky-apps.uk/LastSkywalker/node-page}" case "$REPO" in __*__) REPO="" ;; esac case "$MIRROR" in __*__ | off | none) MIRROR="" ;; esac MIRROR="${MIRROR%/}"; MIRROR="${MIRROR%.git}" # install.sh (fetched from main below) reads the same two variables. export NODE_STATS_REPO="$REPO" NODE_STATS_UPDATE_MIRROR="${MIRROR:-off}" PORT="${NODE_STATS_PORT:-9090}" RAFT_PORT="${NODE_STATS_RAFT_PORT:-7000}" API="http://127.0.0.1:${PORT}/api/v1" log() { printf '\033[1;36m[node-stats]\033[0m %s\n' "$*"; } fail() { printf '\033[1;31m[node-stats]\033[0m %s\n' "$*" >&2; exit 1; } [ -n "${NODE_STATS_JOIN_URL:-}" ] || fail "NODE_STATS_JOIN_URL is required (the main node's URL, e.g. http://10.0.0.2:9090)" [ -n "${NODE_STATS_JOIN_KEY:-}" ] || fail "NODE_STATS_JOIN_KEY is required (one-shot token from the main node's admin panel)" command -v curl >/dev/null 2>&1 || fail "curl is required" [ -n "$REPO" ] || [ -n "$MIRROR" ] || [ "${NODE_STATS_SKIP_INSTALL:-0}" = "1" ] || fail "no release source: use the agent-join.sh attached to a release, or set NODE_STATS_REPO=owner/name and/or NODE_STATS_UPDATE_MIRROR=https://git.example.com/owner/name" # json_int — best-effort flat-JSON integer extraction without jq. json_int() { grep -o "\"$1\":[0-9]*" | head -1 | grep -o '[0-9]*$' || true; } json_bool_true() { grep -q "\"$1\":true"; } detect_ipv4() { if [ -n "${NODE_STATS_IPV4:-}" ]; then printf '%s' "$NODE_STATS_IPV4"; return; fi case "$(uname -s)" in Linux) ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i/dev/null | awk '/interface:/{print $2}')" 2>/dev/null ;; esac } # 1. Install the stack (reuses the canonical installer so there is exactly # one source of truth for docker checks / compose generation). if [ "${NODE_STATS_SKIP_INSTALL:-0}" != "1" ]; then log "Installing the node-stats Docker stack…" tmp_install="$(mktemp)" # GitHub first, then the mirror (same files). { [ -n "$REPO" ] && curl -fsSL "https://raw.githubusercontent.com/${REPO}/main/scripts/install.sh" -o "$tmp_install" 2>/dev/null; } \ || { [ -n "$MIRROR" ] && curl -fsSL "${MIRROR}/raw/branch/main/scripts/install.sh" -o "$tmp_install"; } \ || fail "cannot download install.sh from ${REPO:+github.com/${REPO} }${MIRROR}" bash "$tmp_install" install rm -f "$tmp_install" fi # 1b. The installer may have auto-picked non-default ports when 9090/7000 # were taken — the stack's .env is the source of truth from here on. stack_dir="${NODE_STATS_DIR:-}" if [ -z "$stack_dir" ]; then if [ "$(id -u)" = "0" ]; then stack_dir="/opt/node-stats"; else stack_dir="$HOME/.node-stats"; fi fi if [ -f "${stack_dir}/.env" ]; then env_port="$(sed -n 's/^NODE_STATS_PORT=//p' "${stack_dir}/.env" | head -1)" env_raft="$(sed -n 's/^NODE_STATS_RAFT_PORT=//p' "${stack_dir}/.env" | head -1)" [ -n "$env_port" ] && PORT="$env_port" && API="http://127.0.0.1:${PORT}/api/v1" [ -n "$env_raft" ] && RAFT_PORT="$env_raft" fi # 2. Wait for the app. log "Waiting for the app on :${PORT}…" status_json="" for _ in $(seq 1 60); do status_json="$(curl -fsS -m 3 "${API}/setup/status" 2>/dev/null)" && break sleep 2 done [ -n "$status_json" ] || fail "the app did not come up on :${PORT} — check 'docker compose logs' in the stack dir" # 3. Join — only meaningful on a fresh (setup-mode) node. if ! printf '%s' "$status_json" | json_bool_true "setup_needed"; then progress="$(curl -fsS -m 5 "${API}/setup/raft-progress" 2>/dev/null || true)" if printf '%s' "$progress" | json_bool_true "enabled"; then log "This node is already part of a cluster — nothing to do." exit 0 fi fail "this node is already set up; joining replaces local data and must be confirmed from Admin → Nodes → Cluster sync → Join" fi advertise="${NODE_STATS_ADVERTISE_ADDR:-}" if [ -z "$advertise" ]; then ip4="$(detect_ipv4)" [ -n "$ip4" ] || fail "could not auto-detect this machine's IPv4 — set NODE_STATS_ADVERTISE_ADDR=host:${RAFT_PORT}" advertise="${ip4}:${RAFT_PORT}" fi log "Joining cluster at ${NODE_STATS_JOIN_URL} (this node advertises ${advertise})…" # advertise_url is this node's EXTERNALLY reachable HTTP base URL — host from the # Raft advertise addr, port = the PUBLISHED app port (not the container-internal # :8080). Peers ship the off-Raft metric stream to this URL, so it must be the # host-published port or cross-node metrics silently never arrive. The leader # persists it as RAFT_ADVERTISE_PUBLIC_URL so the node re-advertises it on boot. adv_host="${advertise%%:*}" advertise_url="http://${adv_host}:${PORT}" join_body="$(printf '{"peer_url":"%s","token":"%s","advertise_addr":"%s","bind_addr":":%s","advertise_url":"%s"}' \ "$NODE_STATS_JOIN_URL" "$NODE_STATS_JOIN_KEY" "$advertise" "$RAFT_PORT" "$advertise_url")" join_resp="$(curl -sS -m 60 -X POST "${API}/setup/join-raft-cluster" \ -H 'Content-Type: application/json' -d "$join_body" -w '\n%{http_code}')" join_code="$(printf '%s' "$join_resp" | tail -1)" if [ "$join_code" != "200" ]; then printf '%s\n' "$join_resp" | head -n -1 >&2 fail "join was rejected (HTTP ${join_code}). Token expired or already used? Generate a fresh one on the main node." fi # 4. Wait for the snapshot to replicate (applied catches up with commit). log "Join accepted — waiting for the cluster snapshot to replicate…" for _ in $(seq 1 90); do progress="$(curl -fsS -m 5 "${API}/setup/raft-progress" 2>/dev/null || true)" applied="$(printf '%s' "$progress" | json_int "applied_index")" commit="$(printf '%s' "$progress" | json_int "commit_index")" if printf '%s' "$progress" | json_bool_true "enabled" \ && [ -n "$applied" ] && [ -n "$commit" ] && [ "$commit" -gt 0 ] && [ "$applied" -ge "$commit" ]; then log "Done. This machine is now a cluster node." log "Open http://${advertise%%:*}:${PORT} and sign in with your existing cluster credentials." exit 0 fi sleep 2 done fail "snapshot replication did not finish in time — check Admin → Nodes → Raft on the main node (the voter may still catch up on its own)"