96 lines
3.1 KiB
Bash
Executable File
96 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Give each environment its own block of host ports, derived from the directory name.
|
|
# base = 20000 + (hash(slug) % 200) * 10; +0 HTTP +1 HTTPS +2 TILT +3 REGISTRY
|
|
# Usage: ports.sh show | active | derive | persist
|
|
# Notes: docs/notes/ports.md
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")"
|
|
|
|
source ./lib/config.sh
|
|
|
|
# derive_port_base lives in lib/config.sh so every script resolves the same block
|
|
# without going through this one.
|
|
derive_base() { derive_port_base "$1"; }
|
|
|
|
derive() {
|
|
load_config
|
|
local base; base=$(derive_base "$CLUSTER")
|
|
DERIVED_HTTP=$base
|
|
DERIVED_HTTPS=$((base + 1))
|
|
DERIVED_TILT=$((base + 2))
|
|
DERIVED_REGISTRY=$((base + 3))
|
|
}
|
|
|
|
# Resolved facts for consumers outside bash, space-separated, positional:
|
|
# CLUSTER KUBECONTEXT HTTP HTTPS TILT REGISTRY MANIFESTS_DIR
|
|
# Read this, not `derive` (which ignores ctrl/.env).
|
|
active() {
|
|
load_config
|
|
echo "$CLUSTER $KUBECONTEXT $HTTP_PORT $HTTPS_PORT $TILT_PORT $REGISTRY_PORT $MANIFESTS_DIR"
|
|
}
|
|
|
|
show() {
|
|
derive
|
|
echo "environment $CLUSTER"
|
|
echo "derived base $(derive_base "$CLUSTER")"
|
|
echo
|
|
printf " %-14s %-8s %-8s %s\n" KEY DERIVED ACTIVE SOURCE
|
|
_row HTTP_PORT "$DERIVED_HTTP"
|
|
_row HTTPS_PORT "$DERIVED_HTTPS"
|
|
_row TILT_PORT "$DERIVED_TILT"
|
|
_row REGISTRY_PORT "$DERIVED_REGISTRY"
|
|
}
|
|
|
|
_row() {
|
|
local key="$1" derived="$2" active="${!1:-}" src="derived"
|
|
if [ -n "$active" ] && [ "$active" != "$derived" ]; then
|
|
src="override"
|
|
elif [ -z "$active" ]; then
|
|
active="$derived"
|
|
fi
|
|
printf " %-14s %-8s %-8s %s\n" "$key" "$derived" "$active" "$src"
|
|
}
|
|
|
|
# Write the derived block into ctrl/.env, once. Existing keys are never
|
|
# rewritten — an override stays an override.
|
|
persist() {
|
|
derive
|
|
[ -f ./.env ] || cp ./.env.example ./.env
|
|
|
|
local wrote=0 key val
|
|
for key in HTTP_PORT:$DERIVED_HTTP \
|
|
HTTPS_PORT:$DERIVED_HTTPS \
|
|
TILT_PORT:$DERIVED_TILT \
|
|
REGISTRY_PORT:$DERIVED_REGISTRY; do
|
|
val="${key#*:}"; key="${key%%:*}"
|
|
if grep -qE "^${key}=[0-9]" ./.env 2>/dev/null; then
|
|
continue
|
|
fi
|
|
if [ "$wrote" -eq 0 ]; then
|
|
{
|
|
echo ""
|
|
echo "# Port block for this environment, derived from the directory name"
|
|
echo "# so copies never collide. Pinned here on first use — edit freely."
|
|
} >> ./.env
|
|
wrote=1
|
|
fi
|
|
# Replace a commented/empty placeholder if present, else append.
|
|
if grep -qE "^#?\s*${key}=" ./.env 2>/dev/null; then
|
|
sed -i "s|^#\?\s*${key}=.*|${key}=${val}|" ./.env
|
|
else
|
|
echo "${key}=${val}" >> ./.env
|
|
fi
|
|
done
|
|
|
|
[ "$wrote" -eq 1 ] && echo "pinned port block into ctrl/.env" || echo "ports already set in ctrl/.env"
|
|
return 0
|
|
}
|
|
|
|
case "${1:-show}" in
|
|
show) show ;;
|
|
derive) derive; echo "$DERIVED_HTTP $DERIVED_HTTPS $DERIVED_TILT $DERIVED_REGISTRY" ;;
|
|
active) active ;;
|
|
persist) persist ;;
|
|
*) echo "usage: $0 [show|active|derive|persist]" >&2; exit 1 ;;
|
|
esac
|