#!/bin/bash # Game Tracker Deployment Script with improved error handling set -e # Exit on error # Bold text function bold() { echo -e "\033[1m$1\033[0m" } # Print section header header() { echo bold "=== $1 ===" } # Print error message and exit error() { echo -e "\033[31mERROR: $1\033[0m" >&2 exit 1 } # Check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } header "Game Tracker Deployment" # Check requirements echo "Checking requirements..." if ! command_exists docker; then error "Docker is not installed. Please install Docker first." fi if ! command_exists docker-compose; then error "Docker Compose is not installed. Please install Docker Compose first." fi # Default settings DEPLOY_MODE=${DEPLOY_MODE:-local} # Check if .env file exists echo "Checking environment..." if [ ! -f .env ]; then error "'.env' file not found! Please create a .env file with your IGDB credentials:\nIGDB_CLIENT_ID=your_client_id_here\nIGDB_CLIENT_SECRET=your_client_secret_here" fi # Make sure necessary directories exist echo "Creating required directories..." mkdir -p instance mkdir -p nginx # Check deployment mode if [ "$1" = "--ssl" ] || [ "$DEPLOY_MODE" = "ssl" ]; then header "SSL Deployment (Production Mode)" # Check for domain name if [ -z "$DOMAIN" ]; then read -p "Enter your domain name (e.g., gametracker.example.com): " DOMAIN export DOMAIN fi # Check for email if [ -z "$EMAIL" ]; then read -p "Enter your email (for Let's Encrypt notifications): " EMAIL export EMAIL fi # Verify domain echo "Verifying domain '$DOMAIN' is properly configured..." host_ip=$(dig +short "$DOMAIN" 2>/dev/null || echo "") if [ -z "$host_ip" ]; then echo "WARNING: Could not resolve domain '$DOMAIN'. DNS might not be configured correctly." read -p "Continue anyway? (y/n): " answer if [[ "$answer" != [Yy]* ]]; then error "Deployment cancelled. Please configure DNS correctly and try again." fi else echo "Domain '$DOMAIN' resolves to IP: $host_ip" fi # Run the SSL setup script echo "Running SSL setup script..." ./init-letsencrypt.sh header "SSL Deployment Complete" echo "Your Game Tracker is now running at: https://$DOMAIN" echo "" echo "SSL certificates will auto-renew every 90 days" echo "To view logs: docker-compose logs -f" echo "To stop the server: docker-compose down" else header "Local Deployment (Development Mode)" # Create a simpler nginx config for local deployment echo "Generating local nginx configuration..." cat > nginx/app.conf << EOF server { listen 80; server_name localhost; location / { proxy_pass http://web:8000; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; } } EOF # Build and start Docker containers echo "Building and starting Docker containers..." docker-compose down 2>/dev/null || true # Ignore errors if no containers are running docker-compose up --build -d # Check if containers started successfully if ! docker-compose ps | grep -q "Up"; then error "Failed to start containers. Check logs with 'docker-compose logs'." fi header "Local Deployment Complete" echo "Your Game Tracker is now running at: http://localhost" echo "" echo "To view logs: docker-compose logs -f" echo "To stop the server: docker-compose down" echo "To deploy with SSL: ./deploy.sh --ssl" fi