← back to Designer Wallcoverings

DW-Agents/dw-agents/integrate-all-agents.sh

138 lines

#!/bin/bash

# DW-Agents Global Auth Integration Script
# Integrates Google OAuth into all agents automatically

echo "🔐 DW Global Auth Integration"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "This will:"
echo "  1. Backup all agent files"
echo "  2. Add global auth middleware"
echo "  3. Remove old authentication"
echo "  4. Restart all agents"
echo ""
read -p "Continue? (yes/no): " confirm

if [ "$confirm" != "yes" ]; then
    echo "Aborted."
    exit 1
fi

# Create backup directory
BACKUP_DIR="/root/DW-Agents-backup-$(date +%Y%m%d-%H%M%S)"
echo ""
echo "📦 Creating backup at: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
cp -r /root/DW-Agents/* "$BACKUP_DIR/"
echo "✅ Backup complete"

# List of agents to update (agent-dir:main-file:port)
AGENTS=(
    "master-hub:master-hub.ts:9893"
    "agent-executive-ceo:ceo-dashboard-agent.ts:7121"
    "agent-executive-cfo:cfo-dashboard-agent.ts:7122"
    "agent-executive-coo:coo-dashboard-agent.ts:7124"
    "agent-executive-vp-ops:vp-ops-dashboard-agent.ts:7123"
    "agent-security:security-agent.ts:9892"
    "agent-needs-attention:needs-attention-agent.ts:9886"
    "agent-legal-team:legal-team-agent.ts:9878"
    "agent-accounting:accounting-agent.ts:9882"
    "agent-shopify-store:shopify-store-agent.ts:7238"
    "agent-marketing:marketing-agent.ts:9881"
    "agent-purchasing-office:purchasing-agent.ts:9880"
    "agent-digital-samples:digital-samples-agent.ts:9879"
    "trend-research-agent:trend-research-agent.ts:9883"
    "agent-skills-manager:skills-manager-agent.ts:9894"
    "agent-task-orchestrator:task-orchestrator-agent.ts:9900"
    "agent-zendesk-chat:zendesk-chat-agent.ts:9884"
    "agent-todays-highlights:todays-highlights-agent.ts:9885"
    "agent-parallel-processes:parallel-processes-agent.ts:9887"
    "agent-in-parallel:in-parallel-agent.ts:9891"
    "agent-completed-tasks:completed-tasks-agent.ts:9889"
    "agent-new-client-signup:new-client-signup-agent.ts:9890"
    "agent-server-uptime:server-uptime-agent.ts:9888"
    "agent-log-monitor:log-monitor-agent.ts:7239"
    "agent-ui-manager:ui-manager-agent.ts:7240"
    "agent-restart-analyzer:restart-analyzer-agent.ts:7241"
    "agent-all-agents:all-agents-viewer.ts:9111"
)

cd /root/DW-Agents

echo ""
echo "🔧 Updating agents..."
echo ""

for agent_info in "${AGENTS[@]}"; do
    IFS=':' read -r agent_dir main_file port <<< "$agent_info"

    agent_path="/root/DW-Agents/$agent_dir/$main_file"

    if [ ! -f "$agent_path" ]; then
        echo "⚠️  Skipping $agent_dir - file not found"
        continue
    fi

    echo "🔄 Updating: $agent_dir (port $port)"

    # Check if already has global auth
    if grep -q "requireGlobalAuth" "$agent_path"; then
        echo "   ✓ Already has global auth"
        continue
    fi

    # Create temp file with modifications
    temp_file="/tmp/agent_temp_$$"

    # Add imports after existing imports (before first non-import line)
    awk '
    /^import/ { print; imports=1; next }
    !imports_done && !(/^import/ || /^$/ || /^\/\//) {
        if (!added) {
            print "import { requireGlobalAuth } from \"../shared-global-auth\";"
            print "import cookieParser from \"cookie-parser\";"
            added=1
        }
        imports_done=1
    }
    { print }
    ' "$agent_path" > "$temp_file"

    # Add cookie parser and global auth middleware after app creation
    awk '
    /^const app = express/ || /^app = express/ {
        print
        print ""
        print "// Global Authentication"
        print "app.use(cookieParser());"
        print "app.use(requireGlobalAuth);"
        next
    }
    { print }
    ' "$temp_file" > "${temp_file}.2"

    # Move updated file back
    mv "${temp_file}.2" "$agent_path"
    rm -f "$temp_file"

    echo "   ✅ Updated successfully"
done

echo ""
echo "🔄 Restarting all agents..."
pm2 restart all

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Global Auth Integration Complete!"
echo ""
echo "🔐 All agents now require Google OAuth"
echo "🌍 Login at: http://45.61.58.125:9999/login"
echo "📁 Backup saved at: $BACKUP_DIR"
echo ""
echo "Test it:"
echo "  Visit any agent URL - you'll be redirected to Google login"
echo "  After login, you can access all agents with one session!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"