58 lines
1.9 KiB
Bash
Executable File
58 lines
1.9 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}"
|
|
JUPYTER_ENABLED="${ENABLE_JUPYTER:-false}"
|
|
|
|
if ! command -v socat >/dev/null 2>&1; then
|
|
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
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# stop previous forwards if they exist
|
|
pkill -f "socat.*pgadmin" >/dev/null 2>&1 || true
|
|
pkill -f "socat.*qdrant" >/dev/null 2>&1 || true
|
|
pkill -f "socat.*jupyter" >/dev/null 2>&1 || true
|
|
|
|
if [[ "${SERVICES_ENABLED}" == "true" ]]; then
|
|
if [[ "${PGADMIN_ENABLED}" == "true" ]]; then
|
|
echo "Forwarding pgAdmin to localhost:5050"
|
|
nohup socat TCP-LISTEN:5050,reuseaddr,fork TCP:pgadmin-${WORKSPACE_ID}:80 >/tmp/socat-pgadmin.log 2>&1 &
|
|
else
|
|
echo "pgAdmin disabled; skipping port forward"
|
|
fi
|
|
|
|
echo "Forwarding Qdrant to localhost:6333"
|
|
nohup socat TCP-LISTEN:6333,reuseaddr,fork TCP:qdrant-${WORKSPACE_ID}:6333 >/tmp/socat-qdrant.log 2>&1 &
|
|
else
|
|
echo "Database services disabled; skipping pgAdmin/Qdrant forwards"
|
|
fi
|
|
|
|
if [[ "${JUPYTER_ENABLED}" == "true" ]]; then
|
|
echo "Forwarding JupyterLab to localhost:8888"
|
|
nohup socat TCP-LISTEN:8888,reuseaddr,fork TCP:jupyter-${WORKSPACE_ID}:8888 >/tmp/socat-jupyter.log 2>&1 &
|
|
else
|
|
echo "JupyterLab disabled; skipping port forward"
|
|
fi
|
|
|
|
sleep 2
|
|
ps -o pid,cmd -C socat || true
|