← back to Designer Wallcoverings

DW-Agents/dw-agents/QUICK-DEPLOY.sh

145 lines

#!/bin/bash
###############################################################################
# QUICK DEPLOYMENT SCRIPT
# One-command deployment of the complete DW-Agents reliability solution
###############################################################################

set -e

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

print_header() {
    echo ""
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
    echo -e "${BLUE}  $1${NC}"
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
    echo ""
}

print_step() {
    echo -e "${GREEN}➜${NC} $1"
}

print_warn() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

BASE_DIR="/root/DW-Agents"
cd "$BASE_DIR"

print_header "DW-AGENTS RELIABILITY SOLUTION - QUICK DEPLOY"

# Step 1: Make scripts executable
print_step "Making scripts executable..."
chmod +x scripts/*.sh
chmod +x QUICK-DEPLOY.sh

# Step 2: Load environment
print_step "Loading environment variables..."
if [ -f .env ]; then
    export $(grep -v '^#' .env | xargs)
    echo "  ✓ Environment loaded"
else
    print_warn ".env file not found - some agents may not work correctly"
fi

# Step 3: Install/update dependencies
print_step "Checking dependencies..."
if ! command -v jq &> /dev/null; then
    print_warn "jq not found - installing..."
    apt-get update && apt-get install -y jq
fi

# Step 4: Ensure log directory
print_step "Creating log directory..."
mkdir -p logs
chmod 755 logs

# Step 5: Deploy agents
print_step "Deploying all agents via PM2..."
./scripts/startup-all-agents.sh

# Step 6: Configure cron jobs
print_step "Configuring cron jobs..."

# Create temporary crontab
crontab -l > /tmp/current-cron 2>/dev/null || echo "" > /tmp/current-cron

# Add watchdog if not present
if ! grep -q "watchdog-service.sh" /tmp/current-cron; then
    echo "*/2 * * * * /root/DW-Agents/scripts/watchdog-service.sh >> /root/DW-Agents/logs/watchdog-cron.log 2>&1" >> /tmp/current-cron
    echo "  ✓ Added watchdog service (every 2 minutes)"
fi

# Add recovery service if not present
if ! grep -q "agent-recovery-service.sh" /tmp/current-cron; then
    echo "* * * * * /root/DW-Agents/scripts/agent-recovery-service.sh >> /root/DW-Agents/logs/recovery-cron.log 2>&1" >> /tmp/current-cron
    echo "  ✓ Added recovery service (every minute)"
fi

# Add log cleanup if not present
if ! grep -q "find /root/DW-Agents/logs" /tmp/current-cron; then
    echo "0 2 * * * find /root/DW-Agents/logs -name '*.log' -mtime +7 -delete" >> /tmp/current-cron
    echo "  ✓ Added log cleanup (daily at 2 AM)"
fi

# Install new crontab
crontab /tmp/current-cron
rm /tmp/current-cron

# Step 7: Final verification
print_header "DEPLOYMENT VERIFICATION"

sleep 5

print_step "Checking PM2 processes..."
TOTAL=$(pm2 jlist 2>/dev/null | jq '. | length')
ONLINE=$(pm2 jlist 2>/dev/null | jq '[.[] | select(.pm2_env.status == "online")] | length')

echo "  Agents online: $ONLINE/$TOTAL"

if [ "$ONLINE" -eq "$TOTAL" ]; then
    echo -e "  ${GREEN}✓ All agents are online${NC}"
else
    echo -e "  ${YELLOW}⚠ Some agents may still be starting...${NC}"
fi

# Step 8: Test watchdog
print_step "Testing watchdog service..."
if ./scripts/watchdog-service.sh; then
    echo -e "  ${GREEN}✓ Watchdog service working${NC}"
else
    echo -e "  ${YELLOW}⚠ Watchdog detected some issues (check logs)${NC}"
fi

# Step 9: Display access information
print_header "DEPLOYMENT COMPLETE"

echo -e "${GREEN}✓ DW-Agents Reliability Solution Deployed Successfully${NC}"
echo ""
echo "Key URLs:"
echo "  • Master Hub:     http://45.61.58.125:9893"
echo "  • CEO Dashboard:  http://45.61.58.125:7120"
echo "  • All Agents:     http://45.61.58.125:9111"
echo ""
echo "Management Commands:"
echo "  • View status:    pm2 list"
echo "  • View logs:      pm2 logs"
echo "  • Monitor:        pm2 monit"
echo "  • Restart all:    pm2 restart all"
echo ""
echo "Monitoring:"
echo "  • Watchdog logs:  tail -f logs/watchdog.log"
echo "  • Recovery logs:  tail -f logs/recovery.log"
echo ""
echo -e "${BLUE}For detailed documentation, see: DEPLOYMENT-GUIDE.md${NC}"
echo ""