← back to Designer Wallcoverings

DW-Agents/dw-agents/manage-agents.sh

161 lines

#!/bin/bash
# DW-Agents Management Script
# Quick commands for managing all DW agents

set -e

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

function print_header() {
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${BLUE}🤖  DW-Agents Management${NC}"
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}

function show_status() {
    print_header
    echo ""
    pm2 list | grep -E "dw-|NAME" || echo "No DW agents running"
    echo ""
    echo -e "${GREEN}🌍 Agent URLs:${NC}"
    echo "  Master Hub:        http://45.61.58.125:9712"
    echo "  Digital Samples:   http://45.61.58.125:9879"
    echo "  Legal Team:        http://45.61.58.125:9878"
    echo ""
}

function start_all() {
    print_header
    echo -e "${GREEN}🚀 Starting all DW agents...${NC}"
    pm2 start ecosystem.config.js
    pm2 save
    echo ""
    show_status
}

function stop_all() {
    print_header
    echo -e "${YELLOW}⏸️  Stopping all DW agents...${NC}"
    pm2 stop dw-master-hub dw-digital-samples dw-legal-team
    echo ""
    show_status
}

function restart_all() {
    print_header
    echo -e "${YELLOW}🔄 Restarting all DW agents...${NC}"
    pm2 restart dw-master-hub dw-digital-samples dw-legal-team
    echo ""
    show_status
}

function view_logs() {
    print_header
    echo -e "${BLUE}📋 Select agent to view logs:${NC}"
    echo "  1) Master Hub"
    echo "  2) Digital Samples"
    echo "  3) Legal Team"
    echo "  4) All (combined)"
    echo ""
    read -p "Enter choice (1-4): " choice

    case $choice in
        1) pm2 logs dw-master-hub ;;
        2) pm2 logs dw-digital-samples ;;
        3) pm2 logs dw-legal-team ;;
        4) pm2 logs --lines 50 ;;
        *) echo -e "${RED}Invalid choice${NC}" ;;
    esac
}

function monitor() {
    print_header
    echo -e "${GREEN}📊 Opening PM2 monitor...${NC}"
    pm2 monit
}

function update() {
    print_header
    echo -e "${YELLOW}📦 Updating dependencies...${NC}"
    cd /root/DW-Agents
    npm install
    echo -e "${GREEN}✅ Update complete. Restart agents if needed.${NC}"
}

function test_dig() {
    print_header
    echo -e "${YELLOW}🧪 Testing DIG-series order workflow...${NC}"
    echo ""
    echo "This will create a test DIG order in the Digital Samples agent."
    echo "You'll need to be logged in to approve the session cookie."
    echo ""
    read -p "Press Enter to continue or Ctrl+C to cancel..."

    response=$(curl -s -X POST http://45.61.58.125:9879/api/test/dig-order)
    echo ""
    echo -e "${GREEN}Response: $response${NC}"
    echo ""
    echo "Check the Digital Samples dashboard at: http://45.61.58.125:9879"
}

function show_menu() {
    print_header
    echo ""
    echo "Select an action:"
    echo "  1) Show Status"
    echo "  2) Start All Agents"
    echo "  3) Stop All Agents"
    echo "  4) Restart All Agents"
    echo "  5) View Logs"
    echo "  6) Monitor (PM2 Dashboard)"
    echo "  7) Update Dependencies"
    echo "  8) Test DIG Order Workflow"
    echo "  9) Exit"
    echo ""
    read -p "Enter choice (1-9): " choice

    case $choice in
        1) show_status ;;
        2) start_all ;;
        3) stop_all ;;
        4) restart_all ;;
        5) view_logs ;;
        6) monitor ;;
        7) update ;;
        8) test_dig ;;
        9) exit 0 ;;
        *) echo -e "${RED}Invalid choice${NC}" ; sleep 1 ;;
    esac

    echo ""
    read -p "Press Enter to continue..."
    show_menu
}

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}Please run with sudo or as root${NC}"
    exit 1
fi

# Main execution
if [ $# -eq 0 ]; then
    show_menu
else
    case $1 in
        status) show_status ;;
        start) start_all ;;
        stop) stop_all ;;
        restart) restart_all ;;
        logs) view_logs ;;
        monitor) monitor ;;
        update) update ;;
        test) test_dig ;;
        *) echo "Usage: $0 {status|start|stop|restart|logs|monitor|update|test}" ; exit 1 ;;
    esac
fi