master-zapier-plan-draft/packages/verae-nats-cluster/scripts/expose-public.sh
George Lambert 123e040253
Some checks failed
offline / test (push) Has been cancelled
updates to nats cluster configuration and scripts
2026-09-19 23:52:39 -04:00

92 lines
2.9 KiB
Shell
Executable file

#!/usr/bin/env bash
# Public NATS *client* edge on NS1. Does not publish cluster :6222 or monitor :8222.
# Hub stays on vmbr1; this process binds 70.88.205.138:4222 with a token.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
export PATH="/usr/sbin:/usr/bin:/bin:/usr/local/bin:$PATH"
HOST="$(hostname -f 2>/dev/null || hostname)"
case "$HOST" in
NS1.GEORGELAMBERT.ORG|NS1|ns1.georgelambert.org|ns1) ;;
*) echo "refusing: expose-public.sh must run on NS1, got '$HOST'" >&2; exit 1 ;;
esac
PUBLIC_IP="${PUBLIC_IP:-70.88.205.138}"
TOKEN_FILE="${TOKEN_FILE:-/etc/nats/public-edge.token}"
# shellcheck disable=SC1091
. "$ROOT/cluster.env"
if [[ -f "$TOKEN_FILE" ]]; then
TOKEN="$(cat "$TOKEN_FILE")"
else
TOKEN="$(openssl rand -hex 24)"
sudo install -d -m 755 /etc/nats
echo "$TOKEN" | sudo tee "$TOKEN_FILE" >/dev/null
sudo chmod 600 "$TOKEN_FILE"
fi
# Enable leaf hub on each cluster node (vmbr1 :7422 only).
mapfile -t rows < <(printf '%s\n' "$NODES" | awk 'NF==3 {print}')
for row in "${rows[@]}"; do
# shellcheck disable=SC2086
set -- $row
vmid=$1 ip=$3
sudo pct exec "$vmid" -- bash -lc "
set -e
f=/etc/nats/nats.conf
if ! grep -q '^leafnodes' \"\$f\"; then
cat >> \"\$f\" <<EOF
leafnodes {
host: ${ip}
port: 7422
}
EOF
fi
systemctl restart nats-server
"
done
sleep 2
for row in "${rows[@]}"; do
set -- $row
sudo pct exec "$1" -- ss -lntp | grep -E ':7422|:4222' || true
done
NSBIN=/usr/local/bin/nats-server
if ! sudo test -x "$NSBIN"; then
echo "copying nats-server from LXC 511"
sudo pct pull 511 /usr/local/bin/nats-server "$NSBIN"
sudo chmod 755 "$NSBIN"
fi
sudo test -x "$NSBIN" || { echo "nats-server binary not found" >&2; exit 1; }
tmp="$(mktemp)"
sed -e "s/{{PUBLIC_IP}}/$PUBLIC_IP/" -e "s/{{TOKEN}}/$TOKEN/" \
"$ROOT/conf/public-edge.conf.tmpl" >"$tmp"
sudo install -d -m 755 /etc/nats
sudo install -m 600 "$tmp" /etc/nats/public-edge.conf
rm -f "$tmp"
sed "s|/usr/local/bin/nats-server|$NSBIN|" "$ROOT/systemd/nats-public-edge.service" \
| sudo tee /etc/systemd/system/nats-public-edge.service >/dev/null
sudo systemctl daemon-reload
sudo systemctl enable --now nats-public-edge.service
sleep 1
sudo systemctl --no-pager --full status nats-public-edge.service | head -20
ss -lntp | grep -E "4222|14422|8229" || true
# Caddy WebSocket TLS front (wss://nats.zapier.georgelambert.org)
CADDY=/etc/caddy/sites/zapier-nats.caddy
sudo tee "$CADDY" >/dev/null <<'C'
# Public NATS WebSocket. Raw TCP is 70.88.205.138:4222 (token). Do not proxy :6222/:7422.
http://nats.zapier.georgelambert.org {
redir https://nats.zapier.georgelambert.org{uri} permanent
}
nats.zapier.georgelambert.org {
encode gzip zstd
reverse_proxy 127.0.0.1:14422
}
C
sudo systemctl reload caddy || sudo service caddy reload || true
echo "PUBLIC_NATS_URL=nats://${TOKEN}@${PUBLIC_IP}:4222"
echo "PUBLIC_WS_URL=wss://nats.zapier.georgelambert.org"
echo "token file $TOKEN_FILE"