#!/usr/bin/env bash # Observer hookup: let tools/observer/observer.mjs watch one cloud node so /live on the site shows the 20-node network. # The RPC never leaves the node's loopback, so the observer reaches it through an ssh tunnel (from the Mac) or runs # on the node itself (remote mode, keeps the overloaded Mac out of it). # # ./experiments/observer.sh tunnel [node] ssh -L 28610 -> the node's wRPC JSON; prints the observer command # ./experiments/observer.sh remote on [node] install Node 22 on the node, copy observer.mjs and DATABASE_URL, start a unit # ./experiments/observer.sh remote off [node] stop and remove that unit (the DATABASE_URL file is deleted too) # # Tables: with LIVE_TABLE_PREFIX=cloud_ the observer writes cloud_live_* and the public page keeps showing the Mac's # devnet (site/api/live.mjs reads the unprefixed tables; it has no prefix switch, so a side-by-side page needs that # one-line change in site/). Without the prefix the cloud network REPLACES the Mac's devnet on /live: stop the # Mac's observer first, or two writers fight over live_state. . "$(dirname "$0")/../lib/common.sh" require_nodes mode="${1:-}"; node="${3:-${2:-}}" case "$mode" in tunnel) node="${2:-$(node_name 1)}"; ip=$(node_ip "$node") log "tunnel to $node ($ip): local ws://127.0.0.1:${LOCAL_PORT:-28620} -> node 127.0.0.1:$RPC_JSON_PORT (Ctrl+C ends it)" log "in another shell, from the repo root:" log " IGNEUM_RPC=ws://127.0.0.1:${LOCAL_PORT:-28620} LIVE_TABLE_PREFIX=cloud_ node tools/observer/observer.mjs # parallel record" log " IGNEUM_RPC=ws://127.0.0.1:${LOCAL_PORT:-28620} node tools/observer/observer.mjs # takes over /live (stop the Mac's observer first)" exec ssh "${SSH_OPTS[@]}" -N -L "${LOCAL_PORT:-28620}:127.0.0.1:$RPC_JSON_PORT" "$SSH_USER@$ip" ;; remote) sub="${2:-}"; node="${3:-$(node_name 1)}"; ip=$(node_ip "$node") if [ "$sub" = on ]; then envf="$HOME/.config/igneum/env"; [ -f "$envf" ] || die "no $envf (DATABASE_URL)" dburl=$(grep -E '^DATABASE_URL=' "$envf" | head -1) [ -n "$dburl" ] || die "DATABASE_URL not in $envf" log "installing Node 22 (NodeSource apt repository) and the observer on $node" nssh "$ip" 'command -v node >/dev/null && node -v | grep -q "^v2[2-9]" || { curl -fsSL https://deb.nodesource.com/setup_22.x | bash - >/dev/null && apt-get install -y -qq nodejs >/dev/null; }; node -v' nscp "$REPO/tools/observer/observer.mjs" "$SSH_USER@$ip:/opt/igneum/observer.mjs" printf '%s\nIGNEUM_RPC=ws://127.0.0.1:%s\nLIVE_TABLE_PREFIX=%s\n' "$dburl" "$RPC_JSON_PORT" "${LIVE_TABLE_PREFIX:-cloud_}" | nssh "$ip" 'umask 077; cat > /etc/igneum/observer.env' nssh "$ip" 'cat > /etc/systemd/system/igneum-observer.service </dev/null; rm -f /etc/systemd/system/igneum-observer.service /etc/igneum/observer.env; systemctl daemon-reload; echo removed' else die "usage: observer.sh remote on|off [node]"; fi ;; *) die "usage: observer.sh tunnel [node] | observer.sh remote on|off [node]" ;; esac