Files
soleprint/berth/ctrl/ports.sh
2026-09-14 03:57:00 -03:00

79 lines
2.6 KiB
Bash

#!/usr/bin/env bash
# The local port map, and whether it still agrees with rig.
#
# Usage:
# ./ports.sh show # DERIVED / ACTIVE / SOURCE
# ./ports.sh verify # recompute rig's formula, report drift
#
# berth recomputes rig's port formula rather than importing it, so neither
# depends on the other. See ../README.md.
set -euo pipefail
cd "$(dirname "$0")"
source ./lib/config.sh
source ./lib/estate.sh
load_config
# name<US>host<US>local_port for everything that has one.
_local_ports() {
python3 -c '
import json, sys
d = json.load(open(sys.argv[1]))
for s in d.get("services", []):
p = s.get("local_port")
if p:
print("\x1f".join([s.get("name",""), s.get("host",""), str(p)]))
' "$ESTATE_FILE"
}
show() {
local ld; ld=$(estate_get "local_domain"); : "${ld:=local.ar}"
printf '%-12s %-22s %-8s %-8s %s\n' NAME ADDRESS ACTIVE DERIVED SOURCE
local name host port base
while IFS=$'\x1f' read -r name host port; do
[ -z "$name" ] && continue
base=$(derive_port_base "$host")
if [ "$port" = "$base" ]; then
printf '%-12s %-22s %-8s %-8s %s\n' "$name" "${host}.${ld}" "$port" "$base" "derived"
else
printf '%-12s %-22s %-8s %-8s %s\n' "$name" "${host}.${ld}" "$port" "$base" "override"
fi
done < <(_local_ports)
echo
echo "DERIVED is what rig's formula gives for that name. ACTIVE is what the"
echo "estate records. 'override' is not an error — most of these were never"
echo "rigs. 'make ports verify' says which ones should have matched."
}
verify() {
local name host port base rc=0 checked=0
while IFS=$'\x1f' read -r name host port; do
[ -z "$name" ] && continue
# Only 20000-21999 is rig's to predict; anything else was never derived.
if [ "$port" -lt 20000 ] || [ "$port" -gt 21999 ]; then
continue
fi
checked=$((checked + 1))
base=$(derive_port_base "$host")
if [ "$port" != "$base" ]; then
echo "DRIFT $name (${host}): estate says $port, rig's formula gives $base"
echo " either the rig pinned HTTP_PORT in its ctrl/.env, or the"
echo " folder was renamed. The Caddy map is stale either way."
rc=1
else
echo "ok $name (${host}): $port"
fi
done < <(_local_ports)
echo
echo "checked $checked rig-shaped port(s) in 20000-21999."
[ "$rc" = 0 ] && echo "no drift." || echo "drift found — regenerate with 'make services render local'."
return 0
}
case "${1:-show}" in
show) show ;;
verify) verify ;;
*) echo "usage: $0 [show|verify]" >&2; exit 1 ;;
esac