← back to Watches

PRODUCTION_SETUP.sh

415 lines

#!/bin/bash

################################################################################
# OMEGA WATCHES - MASTER PRODUCTION SETUP SCRIPT
# One-command setup for complete production infrastructure
################################################################################

set -euo pipefail

GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

APP_DIR="/root/Projects/watches"

log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

log_info() {
    echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

log_error() {
    echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING:${NC} $1"
}

print_header() {
    clear
    echo -e "${BLUE}"
    cat << "EOF"
╔════════════════════════════════════════════════════════════════╗
║                                                                ║
║        OMEGA WATCHES - PRODUCTION INFRASTRUCTURE SETUP         ║
║                                                                ║
║                Complete DevOps Automation                      ║
║                                                                ║
╚════════════════════════════════════════════════════════════════╝
EOF
    echo -e "${NC}"
}

print_footer() {
    echo ""
    echo -e "${GREEN}"
    cat << "EOF"
╔════════════════════════════════════════════════════════════════╗
║                                                                ║
║           PRODUCTION INFRASTRUCTURE SETUP COMPLETE!            ║
║                                                                ║
╚════════════════════════════════════════════════════════════════╝
EOF
    echo -e "${NC}"
}

# Check if running as root
check_root() {
    if [ "$EUID" -ne 0 ]; then
        log_error "This script must be run as root"
        exit 1
    fi
}

# Display menu
show_menu() {
    echo ""
    log_info "Setup Options:"
    echo ""
    echo "  1) Full Setup (Recommended)"
    echo "  2) Monitoring Only (Prometheus + Grafana)"
    echo "  3) Nginx Reverse Proxy Only"
    echo "  4) Backup System Only"
    echo "  5) Cron Jobs Only"
    echo "  6) Custom Setup"
    echo "  0) Exit"
    echo ""
    read -p "Select option [1-6, 0 to exit]: " choice
    echo ""
}

# Full setup
full_setup() {
    log "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
    log "STARTING FULL PRODUCTION SETUP"
    log "${BLUE}═══════════════════════════════════════════════════════════════${NC}"

    # 1. Install dependencies
    log_info "Step 1/7: Installing system dependencies..."
    install_dependencies

    # 2. Setup monitoring stack
    log_info "Step 2/7: Setting up monitoring stack..."
    setup_monitoring

    # 3. Configure Nginx
    log_info "Step 3/7: Configuring Nginx reverse proxy..."
    setup_nginx

    # 4. Setup backup system
    log_info "Step 4/7: Setting up automated backups..."
    setup_backups

    # 5. Configure cron jobs
    log_info "Step 5/7: Configuring cron jobs..."
    setup_cron

    # 6. Open firewall ports
    log_info "Step 6/7: Configuring firewall..."
    configure_firewall

    # 7. Verify setup
    log_info "Step 7/7: Verifying installation..."
    verify_setup

    print_footer
    show_access_info
}

# Install dependencies
install_dependencies() {
    log "Installing required packages..."

    apt update -qq
    apt install -y -qq curl wget git jq htop net-tools 2>&1 | grep -v "^Reading" || true

    # Check if Docker is installed
    if ! command -v docker &> /dev/null; then
        log_warning "Docker not found. Installing Docker..."
        curl -fsSL https://get.docker.com | bash > /dev/null 2>&1
        systemctl enable docker
        systemctl start docker
    fi

    # Check if Docker Compose is installed
    if ! command -v docker-compose &> /dev/null; then
        log_warning "Docker Compose not found. Installing..."
        apt install -y docker-compose > /dev/null 2>&1
    fi

    log "Dependencies installed ✓"
}

# Setup monitoring
setup_monitoring() {
    if [ -f "$APP_DIR/scripts/setup-monitoring.sh" ]; then
        cd "$APP_DIR"
        bash scripts/setup-monitoring.sh
    else
        log_warning "Monitoring setup script not found. Skipping..."
    fi
}

# Setup Nginx
setup_nginx() {
    if [ -f "$APP_DIR/scripts/nginx-setup.sh" ]; then
        cd "$APP_DIR"
        bash scripts/nginx-setup.sh
    else
        log_warning "Nginx setup script not found. Skipping..."
    fi
}

# Setup backups
setup_backups() {
    log "Creating backup directories..."

    mkdir -p "$APP_DIR/backups"/{daily,weekly,monthly,deployments}

    # Create initial backup
    if [ -f "$APP_DIR/scripts/backup-automation.sh" ]; then
        cd "$APP_DIR"
        bash scripts/backup-automation.sh daily
        log "Initial backup created ✓"
    else
        log_warning "Backup script not found. Skipping..."
    fi
}

# Setup cron jobs
setup_cron() {
    if [ -f "$APP_DIR/scripts/cron-setup.sh" ]; then
        cd "$APP_DIR"
        bash scripts/cron-setup.sh
    else
        log_warning "Cron setup script not found. Skipping..."
    fi
}

# Configure firewall
configure_firewall() {
    log "Configuring firewall rules..."

    # Application ports
    ufw allow 7500/tcp comment "Omega Watches Main App" > /dev/null 2>&1
    ufw allow 7501/tcp comment "Omega Watches Staging" > /dev/null 2>&1
    ufw allow 7502/tcp comment "Omega Watches Green" > /dev/null 2>&1

    # Monitoring ports
    ufw allow 7510/tcp comment "Prometheus" > /dev/null 2>&1
    ufw allow 7511/tcp comment "Grafana" > /dev/null 2>&1
    ufw allow 7512/tcp comment "Node Exporter" > /dev/null 2>&1
    ufw allow 7513/tcp comment "Alertmanager" > /dev/null 2>&1
    ufw allow 7514/tcp comment "Redis" > /dev/null 2>&1

    # Web server ports
    ufw allow 80/tcp comment "HTTP" > /dev/null 2>&1
    ufw allow 443/tcp comment "HTTPS" > /dev/null 2>&1

    log "Firewall configured ✓"
}

# Verify setup
verify_setup() {
    log "Verifying installation..."

    local errors=0

    # Check application
    if pm2 list | grep -q omega-watches; then
        log "✓ Application is running"
    else
        log_error "✗ Application not running"
        ((errors++))
    fi

    # Check Nginx
    if systemctl is-active --quiet nginx; then
        log "✓ Nginx is running"
    else
        log_warning "⚠ Nginx is not running"
    fi

    # Check Docker containers
    if docker ps | grep -q prometheus; then
        log "✓ Monitoring stack is running"
    else
        log_warning "⚠ Monitoring stack not running"
    fi

    # Check backups
    if [ -d "$APP_DIR/backups/daily" ]; then
        local backup_count=$(ls -1 "$APP_DIR/backups/daily"/*.tar.gz 2>/dev/null | wc -l)
        log "✓ Backup system configured ($backup_count backups found)"
    else
        log_warning "⚠ Backup directory not found"
    fi

    # Check cron jobs
    if crontab -l | grep -q "omega-watches"; then
        log "✓ Cron jobs configured"
    else
        log_warning "⚠ Cron jobs not configured"
    fi

    if [ $errors -eq 0 ]; then
        log "${GREEN}All critical components verified ✓${NC}"
    else
        log_error "$errors critical errors found"
    fi
}

# Show access information
show_access_info() {
    echo ""
    log_info "╔════════════════════════════════════════════════════════════════╗"
    log_info "║                     ACCESS INFORMATION                         ║"
    log_info "╚════════════════════════════════════════════════════════════════╝"
    echo ""
    echo "🌐 OMEGA WATCHES APPLICATION:"
    echo "   Main Site:     http://45.61.58.125:7500"
    echo "   API:           http://45.61.58.125:7500/api/watches"
    echo "   Health Check:  http://45.61.58.125:7500/api/health"
    echo "   API Docs:      http://45.61.58.125:7500/api/docs"
    echo ""
    echo "📊 MONITORING DASHBOARDS:"
    echo "   Grafana:       http://45.61.58.125:7511"
    echo "                  (admin / omega-watches-admin)"
    echo "   Prometheus:    http://45.61.58.125:7510"
    echo "   Alertmanager:  http://45.61.58.125:7513"
    echo ""
    echo "🔧 MANAGEMENT COMMANDS:"
    echo "   Deploy:        cd $APP_DIR && ./scripts/production-deploy.sh"
    echo "   Backup:        cd $APP_DIR && ./scripts/backup-automation.sh daily"
    echo "   Logs:          pm2 logs omega-watches"
    echo "   Status:        pm2 list"
    echo "   Health:        curl http://localhost:7500/api/health | jq"
    echo ""
    echo "📚 DOCUMENTATION:"
    echo "   Deployment:    $APP_DIR/RUNBOOKS/DEPLOYMENT_RUNBOOK.md"
    echo "   DR Plan:       $APP_DIR/RUNBOOKS/DISASTER_RECOVERY.md"
    echo ""
    echo "💾 BACKUP LOCATIONS:"
    echo "   Daily:         $APP_DIR/backups/daily/"
    echo "   Weekly:        $APP_DIR/backups/weekly/"
    echo "   Monthly:       $APP_DIR/backups/monthly/"
    echo ""
}

# Custom setup
custom_setup() {
    log_info "Custom Setup - Select components to install:"
    echo ""

    read -p "Install monitoring stack? (y/n): " install_monitoring
    read -p "Setup Nginx reverse proxy? (y/n): " install_nginx
    read -p "Configure backup system? (y/n): " install_backup
    read -p "Setup cron jobs? (y/n): " install_cron

    echo ""

    [[ "$install_monitoring" =~ ^[Yy]$ ]] && setup_monitoring
    [[ "$install_nginx" =~ ^[Yy]$ ]] && setup_nginx
    [[ "$install_backup" =~ ^[Yy]$ ]] && setup_backups
    [[ "$install_cron" =~ ^[Yy]$ ]] && setup_cron

    configure_firewall
    verify_setup
    print_footer
    show_access_info
}

# Main function
main() {
    print_header

    check_root

    cd "$APP_DIR"

    if [ $# -eq 0 ]; then
        # Interactive mode
        show_menu

        case $choice in
            1)
                full_setup
                ;;
            2)
                setup_monitoring
                verify_setup
                ;;
            3)
                setup_nginx
                verify_setup
                ;;
            4)
                setup_backups
                verify_setup
                ;;
            5)
                setup_cron
                verify_setup
                ;;
            6)
                custom_setup
                ;;
            0)
                log "Exiting..."
                exit 0
                ;;
            *)
                log_error "Invalid option"
                exit 1
                ;;
        esac
    else
        # Non-interactive mode
        case "${1:-}" in
            --full|full)
                full_setup
                ;;
            --monitoring)
                setup_monitoring
                ;;
            --nginx)
                setup_nginx
                ;;
            --backup)
                setup_backups
                ;;
            --cron)
                setup_cron
                ;;
            --help|help|-h)
                echo "Usage: $0 [option]"
                echo ""
                echo "Options:"
                echo "  --full        Full production setup (recommended)"
                echo "  --monitoring  Setup monitoring stack only"
                echo "  --nginx       Setup Nginx reverse proxy only"
                echo "  --backup      Setup backup system only"
                echo "  --cron        Setup cron jobs only"
                echo "  --help        Show this help message"
                echo ""
                echo "Run without arguments for interactive mode"
                exit 0
                ;;
            *)
                log_error "Unknown option: ${1:-}"
                echo "Run '$0 --help' for usage information"
                exit 1
                ;;
        esac
    fi
}

# Run main
main "$@"