PMFlow/devops/tunnel/setup.sh

106 lines
3.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Cloudflare Tunnel Setup Script for PMFlow
set -e
echo "🚀 Starting Cloudflare Tunnel Setup..."
# Check if cloudflared is installed
if ! command -v cloudflared &> /dev/null; then
echo "❌ cloudflared is not installed. Please install it first:"
echo " brew install cloudflared"
exit 1
fi
# Ask for configuration
read -p "Enter your Frontend domain (e.g., pmflow.com): " FRONTEND_DOMAIN
read -p "Enter your Backend domain (e.g., api.pmflow.com): " BACKEND_DOMAIN
read -p "Enter a name for your tunnel (e.g., pmflow-tunnel): " TUNNEL_NAME
if [ -z "$FRONTEND_DOMAIN" ] || [ -z "$BACKEND_DOMAIN" ] || [ -z "$TUNNEL_NAME" ]; then
echo "❌ Frontend domain, Backend domain, and Tunnel name are required."
exit 1
fi
echo "Checking authentication..."
# Check if cert.pem exists in default location
if [ ! -f "$HOME/.cloudflared/cert.pem" ]; then
echo "⚠️ You need to login to Cloudflare first."
cloudflared tunnel login
fi
echo "Creating tunnel '$TUNNEL_NAME'..."
# Check if tunnel exists, if not create it
if ! cloudflared tunnel list | grep -q "$TUNNEL_NAME"; then
cloudflared tunnel create "$TUNNEL_NAME"
else
echo "ℹ️ Tunnel '$TUNNEL_NAME' already exists."
fi
echo "Routing DNS..."
# Route DNS for both domains
cloudflared tunnel route dns "$TUNNEL_NAME" "$FRONTEND_DOMAIN"
cloudflared tunnel route dns "$TUNNEL_NAME" "$BACKEND_DOMAIN"
# Get Tunnel ID
TUNNEL_ID=$(cloudflared tunnel list | grep "$TUNNEL_NAME" | awk '{print $1}')
if [ -z "$TUNNEL_ID" ]; then
echo "❌ Could not find Tunnel ID for '$TUNNEL_NAME'"
exit 1
fi
echo "Generating config file..."
# Create config.yml
mkdir -p devops/tunnel
cat > devops/tunnel/config.yml <<EOF
tunnel: $TUNNEL_ID
credentials-file: $HOME/.cloudflared/$TUNNEL_ID.json
ingress:
- hostname: $FRONTEND_DOMAIN
service: http://localhost:5174
- hostname: $BACKEND_DOMAIN
service: http://localhost:8000
- service: http_status:404
EOF
echo "✅ Tunnel configuration created at devops/tunnel/config.yml"
# Update .env file in frontend
ENV_FILE="frontend/.env"
if [ ! -f "$ENV_FILE" ]; then
touch "$ENV_FILE"
fi
# Update VITE_APP_DOMAIN and VITE_API_BASE_URL
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s|VITE_APP_DOMAIN=.*|VITE_APP_DOMAIN=$FRONTEND_DOMAIN|" "$ENV_FILE"
# Check if VITE_API_BASE_URL exists, if so replace it, else append
if grep -q "VITE_API_BASE_URL=" "$ENV_FILE"; then
sed -i '' "s|VITE_API_BASE_URL=.*|VITE_API_BASE_URL=https://$BACKEND_DOMAIN|" "$ENV_FILE"
else
echo "VITE_API_BASE_URL=https://$BACKEND_DOMAIN" >> "$ENV_FILE"
fi
else
sed -i "s|VITE_APP_DOMAIN=.*|VITE_APP_DOMAIN=$FRONTEND_DOMAIN|" "$ENV_FILE"
if grep -q "VITE_API_BASE_URL=" "$ENV_FILE"; then
sed -i "s|VITE_API_BASE_URL=.*|VITE_API_BASE_URL=https://$BACKEND_DOMAIN|" "$ENV_FILE"
else
echo "VITE_API_BASE_URL=https://$BACKEND_DOMAIN" >> "$ENV_FILE"
fi
fi
if ! grep -q "VITE_APP_DOMAIN=" "$ENV_FILE"; then
echo "VITE_APP_DOMAIN=$FRONTEND_DOMAIN" >> "$ENV_FILE"
fi
echo "✅ Updated $ENV_FILE with:"
echo " VITE_APP_DOMAIN=$FRONTEND_DOMAIN"
echo " VITE_API_BASE_URL=https://$BACKEND_DOMAIN"
echo "🎉 Setup complete! You can now run the tunnel with:"
echo " ./devops/tunnel/start.sh"