PMFlow/devops/tunnel/setup.sh

88 lines
2.4 KiB
Bash
Executable File
Raw 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 domain name (e.g., dev.pmflow.com): " DOMAIN_NAME
read -p "Enter a name for your tunnel (e.g., pmflow-dev): " TUNNEL_NAME
if [ -z "$DOMAIN_NAME" ] || [ -z "$TUNNEL_NAME" ]; then
echo "❌ Domain name 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
cloudflared tunnel route dns "$TUNNEL_NAME" "$DOMAIN_NAME"
# 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
cat > devops/tunnel/config.yml <<EOF
tunnel: $TUNNEL_ID
credentials-file: $HOME/.cloudflared/$TUNNEL_ID.json
ingress:
- hostname: $DOMAIN_NAME
service: http://localhost:5174
- 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 or add VITE_APP_DOMAIN
if grep -q "VITE_APP_DOMAIN=" "$ENV_FILE"; then
# Use sed based on OS (macOS vs Linux)
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s|VITE_APP_DOMAIN=.*|VITE_APP_DOMAIN=$DOMAIN_NAME|" "$ENV_FILE"
else
sed -i "s|VITE_APP_DOMAIN=.*|VITE_APP_DOMAIN=$DOMAIN_NAME|" "$ENV_FILE"
fi
else
echo "VITE_APP_DOMAIN=$DOMAIN_NAME" >> "$ENV_FILE"
fi
echo "✅ Updated $ENV_FILE with VITE_APP_DOMAIN=$DOMAIN_NAME"
echo "🎉 Setup complete! You can now run the tunnel with:"
echo " cloudflared tunnel run --config devops/tunnel/config.yml $TUNNEL_NAME"