All checks were successful
PR build (required check) / changes (pull_request) Successful in 6s
secrets-guard / encrypted (pull_request) Successful in 5s
PR build (required check) / build-backend (pull_request) Has been skipped
shell-lint / shellcheck (pull_request) Successful in 7s
PR build (required check) / build-frontend (pull_request) Has been skipped
PR build (required check) / validate-observability (pull_request) Successful in 18s
PR build (required check) / gate (pull_request) Successful in 2s
Environments stop being machines. Until now each box WAS an environment --
"beta" named both a deploy target and a host, "the desktop" named both the
operator's computer and the dev server -- so every path could assume one
environment per host and hardcode /opt/thermograph, /etc/thermograph.env and
ports 8137/8080. That assumption ends here:
vps1 75.119.132.91 Forgejo, Grafana/Loki, the portfolio site, and DEV
(own Postgres, mesh-only on 10.10.0.2:8137)
vps2 169.58.46.181 PROD and BETA as two Swarm stacks sharing one
TimescaleDB instance, plus Centralis, Postfix, backups
desktop AI model hosting + flex Swarm capacity, no environment
Nothing live has moved; the ordered cutover is in
infra/deploy/RUNBOOK-vps1-vps2-cutover.md.
deploy/env-topology.sh is the single source of truth: env -> host, checkout,
branch, deploy mode, stack name, env file, LB ports, DB role/database, service
prefix. THERMOGRAPH_ENV is the input, and on vps2 it is the only thing
distinguishing a beta deploy from a prod one -- deploy.sh refuses to run when it
disagrees with the checkout it was invoked from. The host-wide secrets-env and
deploy-mode markers survive only as a fallback for a single-environment box.
Beta gets its own stack file rather than an overlay (a merge cannot REMOVE the
db service, and beta having no database of its own is the point). Its services
are prefixed beta-*: Swarm registers a service's short name as a DNS alias on
every network it joins, so two stacks both calling a service `web` on the shared
data network would let prod's frontend resolve a beta task. Prod's stack, env
and LB config are untouched.
One Postgres, two databases with two roles: deploy/db/provision-env-db.sh
creates thermograph_beta (NOSUPERUSER, owns only its own database, CONNECT
revoked from PUBLIC) plus a <role>_ro for ad-hoc queries, and refuses to run for
the environment that owns the instance -- doing so would demote prod's bootstrap
superuser.
Fixes that co-residency would otherwise have broken silently:
- CI secrets are keyed by host (VPS1_SSH_*, VPS2_SSH_*). SSH_* meant "beta" and
also "the Forgejo box" because those shared a machine; that conflation is what
once had the prod backup dumping beta.
- The nightly backup dumps BOTH application databases to separate off-box
prefixes, and fails loudly on a missing one instead of skipping it.
- Alloy stopped deriving the `host` label from the node -- on vps2 that would
have filed every beta line as prod, feeding prod's alert rules with beta's
traffic. It is now derived per source, with a new `node` label for the machine,
and beta's log volume is mounted via a vps2-only overlay.
- ops/dbq.sh, ops/iceberg.sh and the secrets seed scripts derived their SSH
target and env-file path from the topology instead of hardcoding beta to
75.119.132.91 -- which is vps1's address now.
- Dev's overlay binds DEV_BIND_ADDR (loopback by default, the mesh address on
vps1) instead of 0.0.0.0: correct for a home LAN box, a public exposure of
unreviewed branches on a VPS.
Terraform's hosts variable is keyed by machine with a nested environments map,
and main.tf flattens (host, environment) pairs so two environments on one box
cannot share a checkout. Caddy's single reference config is split per host.
Docs, onboarding and the runbooks are updated throughout, including the security
rationales that co-residency makes false: separate SSH credentials no longer put
a host boundary between beta and prod, and the boundary that remains is the
database and the filesystem.
115 lines
4.3 KiB
Bash
Executable file
115 lines
4.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Sets up this node's side of a full-mesh WireGuard tunnel across all of
|
|
# vps2, vps1, and the desktop (three nodes, not two — see
|
|
# docs/runbooks/implementation-handoff.md Track B step 2). Docker Swarm's
|
|
# control plane (2377/tcp) is TLS-encrypted by default, but the overlay data
|
|
# plane (VXLAN, 4789/udp) is NOT — and it should never face the public
|
|
# internet. Swarm joins over these private WireGuard IPs instead.
|
|
#
|
|
# A full mesh means every node peers directly with every other node (not a
|
|
# hub-and-spoke through one box) — with three nodes that's 2 [Peer] blocks per
|
|
# node. Run this on EACH of the three nodes, in any order, using a small peer
|
|
# list file so the script isn't hardcoded to any particular node count.
|
|
#
|
|
# --- Peer list format --------------------------------------------------
|
|
# A text file, one line per OTHER node (not including the one you're running
|
|
# on), each line: <wg_ip> <public_ip> <pubkey-or-dash>
|
|
# 10.10.0.1 169.58.46.181 <vps2's pubkey, or - if not yet known>
|
|
# 10.10.0.2 75.119.132.91 <vps1's pubkey, or ->
|
|
# 10.10.0.3 <desktop's public/reachable IP or a DDNS name> <desktop's pubkey, or ->
|
|
#
|
|
# First pass on any node: peer pubkeys you don't have yet are "-". Run this
|
|
# script on all three nodes once (prints each node's own pubkey), fill those
|
|
# into everyone's peer-list files, then re-run on all three once more to
|
|
# actually establish the tunnels. Re-running is safe (idempotent) at any point.
|
|
#
|
|
# bash setup-wireguard.sh <my_wg_ip> <peers_file>
|
|
set -euo pipefail
|
|
|
|
MY_WG_IP="${1:?usage: $0 <my_wg_ip> <peers_file>}"
|
|
PEERS_FILE="${2:?}"
|
|
WG_PORT="${WG_PORT:-51820}"
|
|
WG_DIR=/etc/wireguard
|
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo "Run as root (or via the agent user's sudo)." >&2
|
|
exit 1
|
|
fi
|
|
if [ ! -f "$PEERS_FILE" ]; then
|
|
echo "Peer list not found: $PEERS_FILE (see this script's header for the format)" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "==> Installing WireGuard if needed"
|
|
command -v wg >/dev/null 2>&1 || { apt-get update -y -q && apt-get install -y -q wireguard; }
|
|
|
|
install -d -m 700 "$WG_DIR"
|
|
if [ ! -f "$WG_DIR/privatekey" ]; then
|
|
echo "==> Generating this node's WireGuard keypair"
|
|
umask 077
|
|
wg genkey | tee "$WG_DIR/privatekey" | wg pubkey > "$WG_DIR/publickey"
|
|
fi
|
|
MY_PRIVKEY="$(cat "$WG_DIR/privatekey")"
|
|
MY_PUBKEY="$(cat "$WG_DIR/publickey")"
|
|
|
|
echo
|
|
echo "==> This node's WireGuard public key (put this in the OTHER two nodes'"
|
|
echo " peer-list files, replacing the '-' for this node's line):"
|
|
echo " $MY_PUBKEY"
|
|
echo
|
|
|
|
missing=0
|
|
{
|
|
echo "[Interface]"
|
|
echo "Address = ${MY_WG_IP}/24"
|
|
echo "PrivateKey = ${MY_PRIVKEY}"
|
|
echo "ListenPort = ${WG_PORT}"
|
|
echo
|
|
|
|
while read -r peer_wg_ip peer_public_ip peer_pubkey; do
|
|
[ -z "${peer_wg_ip:-}" ] && continue
|
|
case "$peer_wg_ip" in \#*) continue ;; esac
|
|
if [ "$peer_pubkey" = "-" ] || [ -z "$peer_pubkey" ]; then
|
|
echo "# SKIPPED (no pubkey yet) — peer at ${peer_public_ip}" >&2
|
|
missing=$((missing + 1))
|
|
continue
|
|
fi
|
|
echo "[Peer]"
|
|
echo "PublicKey = ${peer_pubkey}"
|
|
echo "Endpoint = ${peer_public_ip}:${WG_PORT}"
|
|
echo "AllowedIPs = ${peer_wg_ip}/32"
|
|
echo "PersistentKeepalive = 25"
|
|
echo
|
|
done < "$PEERS_FILE"
|
|
} > "$WG_DIR/wg0.conf"
|
|
chmod 600 "$WG_DIR/wg0.conf"
|
|
|
|
echo "==> Bringing up wg0"
|
|
systemctl enable --now wg-quick@wg0 2>/dev/null || (wg-quick down wg0 2>/dev/null; wg-quick up wg0)
|
|
wg syncconf wg0 <(wg-quick strip wg0) 2>/dev/null || true
|
|
|
|
echo "==> Firewall: only let listed peers' public IPs reach the WireGuard port"
|
|
if command -v ufw >/dev/null 2>&1; then
|
|
while read -r _wg_ip peer_public_ip _pubkey; do
|
|
[ -z "${peer_public_ip:-}" ] && continue
|
|
case "$peer_public_ip" in \#*) continue ;; esac
|
|
ufw allow from "$peer_public_ip" to any port "$WG_PORT" proto udp comment "wireguard peer"
|
|
done < "$PEERS_FILE"
|
|
fi
|
|
|
|
echo
|
|
echo "wg0 status:"
|
|
wg show wg0 || true
|
|
echo
|
|
if [ "$missing" -gt 0 ]; then
|
|
echo "NOTE: $missing peer(s) skipped (no pubkey yet in $PEERS_FILE). Fill them in"
|
|
echo "and re-run on this node once all three nodes have printed their pubkey."
|
|
else
|
|
echo "==> All peers configured. Verify once every node has run this:"
|
|
while read -r peer_wg_ip _peer_public_ip peer_pubkey; do
|
|
[ -z "${peer_wg_ip:-}" ] && continue
|
|
case "$peer_wg_ip" in \#*) continue ;; esac
|
|
[ "$peer_pubkey" = "-" ] && continue
|
|
echo " ping -c2 ${peer_wg_ip}"
|
|
done < "$PEERS_FILE"
|
|
fi
|