prole/etc/init_supabase_ports.sh
chrisfu b03efa8f69 Kong API gateway, docker-import preload, OpenTofu graceful fallback, milestone fix
Kong API Gateway (replacing prole nginx):
- Add etc/init_kong.sh provisioning script (DB-less mode, prole-db namespace)
- Add kong-deployment.yaml and kong-service.yaml manifests
- Rewire ingress rules (svc/git/api.prole.org) to prole-db-kong:8000
- Update kustomization.yaml to reference kong manifests

PostgREST & DB Manager in prole-db namespace:
- Add etc/init_postgrest.sh and etc/init_db_manager.sh scripts
- Add postgrest/db-manager deployment and service manifests
- Add src/db-manager/ Node.js REST endpoint for backup triggers
- Default NAMESPACE changed to prole-db in both scripts

Docker image pre-load from PROLE_DATA/docker-import:
- Add _preload_docker_images() to init_common_services.sh
- Scan for .tar files exported by final_deployment.sh
- Import via k3d image import (k3d) or ctr (k3s) before deployments
- Increase rollout timeouts to 300s (configurable via ROLLOUT_TIMEOUT) in init_openbao.sh, init_opentofu.sh, init_garage_store.sh, init_registry.sh

OpenTofu password resolution fix:
- Add Kubernetes secret fallback in resolve_admin_password()
- Change hard exit 1 to graceful return 1 with warning
- Wrap call in if-guard so set -e doesn't abort the script chain

Milestone fix (init scripts not running):
- Add init_kong.sh, init_postgrest.sh, init_db_manager.sh to InitializationScriptsMilestone.execute() script list and arg branches
- Previously only actions.py had these; milestones.py was missing them

Installer integration:
- Add Kong/PostgREST/DB Manager to silent installer _step_init_scripts
- Add corresponding tabs and execution blocks in UI services.py
2026-02-22 00:57:49 -08:00

244 lines
7.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# init_supabase_ports.sh
# Purpose:
# - Ensure the supabase namespace front-door (svc/db on port 5432)
# points to prole-db-rw in the provided namespace.
# - Ensure the Supabase reference Postgres service is on port 15432
# and not host-exposed.
PROG="init_supabase_ports"
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Load environment and config via prole_cfg.sh
# shellcheck disable=SC1090
source "$SCRIPT_DIR/prole_cfg.sh"
if [[ "${1:-}" == "--mode" || "${1:-}" == "-m" ]]; then
prole_set_mode "${2:-}"
shift 2
elif [[ "${1:-}" == --mode=* || "${1:-}" == -m=* ]]; then
prole_set_mode "${1#*=}"
shift
fi
SUPABASE_NAMESPACE="${SUPABASE_NAMESPACE:-supabase}"
PROLE_DB_SERVICE="${PROLE_DB_SERVICE:-prole-db-rw}"
SUPABASE_DB_SERVICE="${SUPABASE_DB_SERVICE:-db}"
SUPABASE_POSTGRES_SERVICE="${SUPABASE_POSTGRES_SERVICE:-supabase-postgres}"
SUPABASE_POSTGRES_PORT="${SUPABASE_POSTGRES_PORT:-15432}"
DB_PORT="${DB_PORT:-5432}"
RESTART_ON_CHANGE=1
usage() {
cat <<EOF
Usage:
$PROG -n <namespace> [options]
Required:
-n, --namespace <namespace> Namespace that contains svc/$PROLE_DB_SERVICE
Options:
--supabase-namespace <ns> Supabase namespace (default: $SUPABASE_NAMESPACE)
--no-restart Do not restart Supabase services on change
-h, --help Show this help
Notes:
- This script creates/updates svc/$SUPABASE_DB_SERVICE in the Supabase namespace
as an ExternalName pointing to $PROLE_DB_SERVICE.<namespace>.svc.cluster.local:5432.
- When the target namespace changes, Supabase services are restarted automatically.
EOF
}
log() { printf '%s\n' "$*"; }
warn() { printf '[warn] %s\n' "$*"; }
err() { printf '[error] %s\n' "$*" >&2; }
TARGET_NAMESPACE=""
while [[ $# -gt 0 ]]; do
case "$1" in
-n|--namespace)
TARGET_NAMESPACE="${2:-}"
shift 2
;;
--supabase-namespace)
SUPABASE_NAMESPACE="${2:-}"
shift 2
;;
--no-restart)
RESTART_ON_CHANGE=0
shift
;;
-h|--help)
usage
exit 0
;;
*)
err "Unknown argument: $1"
usage
exit 2
;;
esac
done
if [[ -z "$TARGET_NAMESPACE" ]]; then
err "-n|--namespace is required"
usage
exit 1
fi
ensure_tools() {
for t in kubectl; do
command -v "$t" >/dev/null || { err "Missing required tool: $t"; exit 1; }
done
}
ensure_namespace() {
if ! kubectl get namespace "$SUPABASE_NAMESPACE" >/dev/null 2>&1; then
log "Creating namespace '$SUPABASE_NAMESPACE' ..."
kubectl create namespace "$SUPABASE_NAMESPACE" >/dev/null 2>&1 || true
fi
}
selector_pairs_to_yaml() {
local selector="$1"
local out=""
local pair key val
IFS=',' read -ra pairs <<<"$selector"
for pair in "${pairs[@]}"; do
pair="$(printf '%s' "$pair" | xargs)"
[[ -z "$pair" ]] && continue
key="${pair%%=*}"
val="${pair#*=}"
[[ -z "$key" || -z "$val" ]] && continue
out+=" ${key}: ${val}\n"
done
printf '%b' "$out"
}
get_selector_pairs() {
local out
out=$(kubectl -n "$SUPABASE_NAMESPACE" get svc "$1" \
-o jsonpath='{range $k,$v := .spec.selector}{$k}={$v},{end}' 2>/dev/null || true)
printf '%s' "$out" | sed 's/,$//'
}
restart_supabase() {
log "Restarting Supabase workloads in namespace '$SUPABASE_NAMESPACE' ..."
local restarted=0
if kubectl -n "$SUPABASE_NAMESPACE" get deploy >/dev/null 2>&1; then
if kubectl -n "$SUPABASE_NAMESPACE" rollout restart deploy --all >/dev/null 2>&1; then
restarted=1
fi
fi
if kubectl -n "$SUPABASE_NAMESPACE" get sts >/dev/null 2>&1; then
if kubectl -n "$SUPABASE_NAMESPACE" rollout restart statefulset --all >/dev/null 2>&1; then
restarted=1
fi
fi
if [[ "$restarted" -eq 0 ]]; then
warn "Rollout restart not supported; deleting Supabase pods instead."
kubectl -n "$SUPABASE_NAMESPACE" delete pod --all --ignore-not-found >/dev/null 2>&1 || true
fi
}
ensure_tools
ensure_namespace
if ! kubectl -n "$TARGET_NAMESPACE" get svc "$PROLE_DB_SERVICE" >/dev/null 2>&1; then
warn "Service not found: $PROLE_DB_SERVICE in namespace $TARGET_NAMESPACE"
fi
DESIRED_EXTERNAL="${PROLE_DB_SERVICE}.${TARGET_NAMESPACE}.svc.cluster.local"
service_exists=0
current_type=""
current_external=""
current_selector=""
if kubectl -n "$SUPABASE_NAMESPACE" get svc "$SUPABASE_DB_SERVICE" >/dev/null 2>&1; then
service_exists=1
current_type=$(kubectl -n "$SUPABASE_NAMESPACE" get svc "$SUPABASE_DB_SERVICE" -o jsonpath='{.spec.type}' 2>/dev/null || true)
current_external=$(kubectl -n "$SUPABASE_NAMESPACE" get svc "$SUPABASE_DB_SERVICE" -o jsonpath='{.spec.externalName}' 2>/dev/null || true)
current_selector=$(get_selector_pairs "$SUPABASE_DB_SERVICE")
fi
namespace_changed=1
if [[ "$service_exists" -eq 1 && "$current_type" == "ExternalName" && "$current_external" == "$DESIRED_EXTERNAL" ]]; then
namespace_changed=0
fi
# Preserve selectors from the previous db service for the reference Postgres service.
DB_SELECTOR="$current_selector"
if [[ -z "$DB_SELECTOR" ]]; then
DB_SELECTOR="${SUPABASE_DB_SELECTOR:-io.kompose.service=db}"
fi
if [[ "$service_exists" -eq 1 && "$current_type" != "ExternalName" ]]; then
log "Replacing service $SUPABASE_NAMESPACE/$SUPABASE_DB_SERVICE with ExternalName -> $DESIRED_EXTERNAL"
kubectl -n "$SUPABASE_NAMESPACE" delete svc "$SUPABASE_DB_SERVICE" --ignore-not-found >/dev/null 2>&1 || true
fi
log "Ensuring $SUPABASE_NAMESPACE/$SUPABASE_DB_SERVICE points to $DESIRED_EXTERNAL:$DB_PORT"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: ${SUPABASE_DB_SERVICE}
namespace: ${SUPABASE_NAMESPACE}
annotations:
prole.org/db-namespace: "${TARGET_NAMESPACE}"
prole.org/db-service: "${PROLE_DB_SERVICE}"
spec:
type: ExternalName
externalName: ${DESIRED_EXTERNAL}
ports:
- name: postgres
port: ${DB_PORT}
targetPort: ${DB_PORT}
EOF
# Ensure Supabase reference Postgres service is on 15432 and ClusterIP
supabase_pg_exists=0
supabase_pg_type=""
if kubectl -n "$SUPABASE_NAMESPACE" get svc "$SUPABASE_POSTGRES_SERVICE" >/dev/null 2>&1; then
supabase_pg_exists=1
supabase_pg_type=$(kubectl -n "$SUPABASE_NAMESPACE" get svc "$SUPABASE_POSTGRES_SERVICE" -o jsonpath='{.spec.type}' 2>/dev/null || true)
fi
if [[ "$supabase_pg_exists" -eq 1 && "$supabase_pg_type" != "ExternalName" ]]; then
log "Replacing service $SUPABASE_NAMESPACE/$SUPABASE_POSTGRES_SERVICE with ExternalName on ${SUPABASE_POSTGRES_PORT}"
kubectl -n "$SUPABASE_NAMESPACE" delete svc "$SUPABASE_POSTGRES_SERVICE" --ignore-not-found >/dev/null 2>&1 || true
supabase_pg_exists=0
fi
log "Ensuring $SUPABASE_NAMESPACE/$SUPABASE_POSTGRES_SERVICE uses port ${SUPABASE_POSTGRES_PORT}"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: ${SUPABASE_POSTGRES_SERVICE}
namespace: ${SUPABASE_NAMESPACE}
labels:
app: supabase-postgres
spec:
type: ExternalName
externalName: ${DESIRED_EXTERNAL}
ports:
- name: postgres
port: ${SUPABASE_POSTGRES_PORT}
targetPort: ${DB_PORT}
EOF
if [[ "$namespace_changed" -eq 1 && "$RESTART_ON_CHANGE" -eq 1 ]]; then
restart_supabase
fi
log "Supabase port wiring complete."