Files
code-tools/terraform/scripts/port-forward.sh
2025-09-29 14:14:30 +00:00

129 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
WORKSPACE_ID="${CODER_WORKSPACE_ID:-}"
if [[ -z "${WORKSPACE_ID}" && -f /tmp/git-metadata/workspace-id ]]; then
WORKSPACE_ID="$(cat /tmp/git-metadata/workspace-id)"
fi
if [[ -z "${WORKSPACE_ID}" ]]; then
echo "Unable to determine CODER_WORKSPACE_ID; skipping port forwarding" >&2
exit 0
fi
SERVICES_ENABLED="${ENABLE_SERVICES:-false}"
PGADMIN_ENABLED="${ENABLE_PGADMIN:-false}"
MARIMO_ENABLED="${ENABLE_MARIMO:-false}"
ensure_socat() {
if command -v socat >/dev/null 2>&1; then
return 0
fi
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update -qq
sudo apt-get install -y socat >/dev/null
elif command -v apk >/dev/null 2>&1; then
sudo apk add --no-cache socat >/dev/null
else
echo "socat is required for port forwarding but could not be installed automatically" >&2
return 1
fi
}
resolve_target() {
local container_name="$1"
local wait_seconds="${2:-60}"
local sleep_interval=2
local waited=0
local ip=""
while (( waited < wait_seconds )); do
if command -v docker >/dev/null 2>&1; then
if docker inspect -f "{{.State.Running}}" "${container_name}" >/dev/null 2>&1; then
ip="$(docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "${container_name}" 2>/dev/null | tr -d " ")"
if [[ -n "${ip}" ]]; then
echo "${ip}"
return 0
fi
fi
fi
if getent hosts "${container_name}" >/dev/null 2>&1; then
echo "${container_name}"
return 0
fi
sleep "${sleep_interval}"
waited=$((waited + sleep_interval))
done
return 1
}
start_forward() {
local service_name="$1"
local listen_port="$2"
local target_port="$3"
local log_path="$4"
local label="$5"
local retries="${6:-1}"
local retry_delay="${7:-5}"
local attempt=1
local target
local wait_seconds=90
if [[ "$label" == "pgAdmin" ]]; then
wait_seconds=150
fi
while (( attempt <= retries )); do
if target="$(resolve_target "${service_name}" "$wait_seconds")"; then
echo "Forwarding ${label} to localhost:${listen_port} (target: ${target}:${target_port})"
nohup socat TCP-LISTEN:${listen_port},reuseaddr,fork TCP:${target}:${target_port} >"${log_path}" 2>&1 &
return 0
fi
if (( attempt < retries )); then
echo "Retrying ${label} forward in ${retry_delay}s..." >&2
sleep "${retry_delay}"
fi
attempt=$((attempt + 1))
done
echo "Timed out waiting for ${label} container (${service_name}); skipping forward" >&2
return 1
}
if ! ensure_socat; then
exit 0
fi
# stop previous forwards if they exist
pkill -f "socat.*5050" >/dev/null 2>&1 || true
pkill -f "socat.*6333" >/dev/null 2>&1 || true
pkill -f "socat.*marimo" >/dev/null 2>&1 || true
if [[ "${SERVICES_ENABLED}" == "true" ]]; then
if [[ "${PGADMIN_ENABLED}" == "true" ]]; then
start_forward "pgadmin-${WORKSPACE_ID}" 5050 5050 /tmp/socat-pgadmin.log "pgAdmin" 2 10 || true
else
echo "pgAdmin disabled; skipping port forward"
fi
start_forward "qdrant-${WORKSPACE_ID}" 6333 6333 /tmp/socat-qdrant.log "Qdrant" || true
else
echo "Database services disabled; skipping pgAdmin/Qdrant forwards"
fi
# Marimo runs inside workspace container, no port forwarding needed
if [[ "${MARIMO_ENABLED}" == "true" ]]; then
echo "Marimo running inside workspace container (no port forwarding needed)"
else
echo "Marimo disabled"
fi
sleep 2
ps -o pid,cmd -C socat || true