Files
code-tools/tf/scripts/port-forward.sh

68 lines
2.3 KiB
Bash

#!/bin/bash
# Manual port forwarding script for Coder services
echo "🔌 Setting up port forwarding for services..."
# Install socat if not available
if ! command -v socat >/dev/null 2>&1; then
echo "📦 Installing socat..."
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update -qq && sudo apt-get install -y socat
elif command -v apk >/dev/null 2>&1; then
sudo apk add --no-cache socat
else
echo "❌ Cannot install socat automatically. Please install it manually."
exit 1
fi
fi
# Kill any existing socat processes
echo "🔄 Stopping existing port forwards..."
pkill -f "socat.*5050" 2>/dev/null || true
pkill -f "socat.*6333" 2>/dev/null || true
# Get workspace ID from Coder metadata or environment
if [ -n "$CODER_WORKSPACE_ID" ]; then
WORKSPACE_ID="$CODER_WORKSPACE_ID"
elif [ -f /tmp/git-metadata/workspace-id ]; then
WORKSPACE_ID=$(cat /tmp/git-metadata/workspace-id)
else
# Try to extract from container names
WORKSPACE_ID=$(docker ps --format '{{.Names}}' 2>/dev/null | grep -E 'postgres-|redis-|qdrant-' | head -1 | sed 's/.*-//')
if [ -z "$WORKSPACE_ID" ]; then
echo "❌ Cannot determine workspace ID. Please set CODER_WORKSPACE_ID environment variable."
exit 1
fi
fi
echo "📍 Workspace ID: $WORKSPACE_ID"
# Start port forwarding
echo "🚀 Starting port forwarding..."
# Forward pgAdmin (port 5050 -> pgadmin container port 80)
if [ "${ENABLE_PGADMIN:-true}" = "true" ]; then
echo " - pgAdmin: localhost:5050 -> pgadmin-$WORKSPACE_ID:80"
nohup socat TCP-LISTEN:5050,reuseaddr,fork TCP:pgadmin-$WORKSPACE_ID:80 > /tmp/socat-pgadmin.log 2>&1 &
fi
# Forward Qdrant (port 6333 -> qdrant container port 6333)
echo " - Qdrant: localhost:6333 -> qdrant-$WORKSPACE_ID:6333"
nohup socat TCP-LISTEN:6333,reuseaddr,fork TCP:qdrant-$WORKSPACE_ID:6333 > /tmp/socat-qdrant.log 2>&1 &
# Give processes time to start
sleep 2
# Check status
echo ""
echo "✅ Port forwarding status:"
ps aux | grep -E "socat.*(5050|6333)" | grep -v grep || echo "❌ No port forwarding processes found"
echo ""
echo "📝 Logs available at:"
echo " - /tmp/socat-pgadmin.log"
echo " - /tmp/socat-qdrant.log"
echo ""
echo "🌐 Access services at:"
echo " - pgAdmin: http://localhost:5050"
echo " - Qdrant: http://localhost:6333/dashboard"