#!/usr/bin/env bash # AITBC Platform Services Deployment Script for aitbc and aitbc1 Servers # Deploys backend services and frontend to both production servers set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # No Color print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_critical() { echo -e "${RED}[CRITICAL]${NC} $1" } print_server() { echo -e "${PURPLE}[SERVER]${NC} $1" } print_deploy() { echo -e "${CYAN}[DEPLOY]${NC} $1" } # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(dirname "$SCRIPT_DIR")" SERVICES_DIR="$ROOT_DIR/apps/coordinator-api/src/app/services" FRONTEND_DIR="$ROOT_DIR/apps/marketplace-web" # Server configuration AITBC_SERVER="aitbc-cascade" AITBC1_SERVER="aitbc1-cascade" AITBC_HOST="aitbc.bubuit.net" AITBC1_HOST="aitbc1.bubuit.net" echo "🚀 AITBC Platform Services Deployment to aitbc and aitbc1 Servers" echo "==============================================================" echo "Timestamp: $(date -Iseconds)" echo "" # Pre-deployment checks check_prerequisites() { print_status "Checking prerequisites..." # Check if SSH keys are available if [[ ! -f "$HOME/.ssh/id_rsa" ]] && [[ ! -f "$HOME/.ssh/id_ed25519" ]]; then print_error "SSH keys not found. Please generate SSH keys first." exit 1 fi # Check if we can connect to servers print_status "Testing SSH connections..." if ! ssh -o ConnectTimeout=10 -o BatchMode=yes $AITBC_SERVER "echo 'Connection successful'" 2>/dev/null; then print_error "Cannot connect to $AITBC_SERVER" exit 1 fi if ! ssh -o ConnectTimeout=10 -o BatchMode=yes $AITBC1_SERVER "echo 'Connection successful'" 2>/dev/null; then print_error "Cannot connect to $AITBC1_SERVER" exit 1 fi # Check if required directories exist if [[ ! -d "$SERVICES_DIR" ]]; then print_error "Services directory not found: $SERVICES_DIR" exit 1 fi if [[ ! -d "$FRONTEND_DIR" ]]; then print_error "Frontend directory not found: $FRONTEND_DIR" exit 1 fi print_success "Prerequisites check completed" } # Deploy backend services deploy_services() { print_status "Deploying backend services..." # Deploy to aitbc server print_server "Deploying services to aitbc server..." # Copy services to aitbc scp -r "$SERVICES_DIR" $AITBC_SERVER:/tmp/ # Install dependencies and setup services on aitbc ssh $AITBC_SERVER " # Create service directory sudo mkdir -p /opt/aitbc/services # Copy services sudo cp -r /tmp/services/* /opt/aitbc/services/ # Install Python dependencies cd /opt/aitbc/services python3 -m pip install -r requirements.txt 2>/dev/null || true # Create systemd services sudo tee /etc/systemd/system/aitbc-cross-chain-reputation.service > /dev/null << 'EOF' [Unit] Description=AITBC Cross Chain Reputation Service After=network.target [Service] Type=simple User=aitbc WorkingDirectory=/opt/aitbc/services ExecStart=/usr/bin/python3 -m cross_chain_reputation Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF sudo tee /etc/systemd/system/aitbc-agent-communication.service > /dev/null << 'EOF' [Unit] Description=AITBC Agent Communication Service After=network.target [Service] Type=simple User=aitbc WorkingDirectory=/opt/aitbc/services ExecStart=/usr/bin/python3 -m agent_communication Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF sudo tee /etc/systemd/system/aitbc-advanced-learning.service > /dev/null << 'EOF' [Unit] Description=AITBC Advanced Learning Service After=network.target [Service] Type=simple User=aitbc WorkingDirectory=/opt/aitbc/services ExecStart=/usr/bin/python3 -m advanced_learning Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF # Reload systemd and start services sudo systemctl daemon-reload sudo systemctl enable aitbc-cross-chain-reputation sudo systemctl enable aitbc-agent-communication sudo systemctl enable aitbc-advanced-learning sudo systemctl start aitbc-cross-chain-reputation sudo systemctl start aitbc-agent-communication sudo systemctl start aitbc-advanced-learning echo 'Services deployed and started on aitbc' " # Deploy to aitbc1 server print_server "Deploying services to aitbc1 server..." # Copy services to aitbc1 scp -r "$SERVICES_DIR" $AITBC1_SERVER:/tmp/ # Install dependencies and setup services on aitbc1 ssh $AITBC1_SERVER " # Create service directory sudo mkdir -p /opt/aitbc/services # Copy services sudo cp -r /tmp/services/* /opt/aitbc/services/ # Install Python dependencies cd /opt/aitbc/services python3 -m pip install -r requirements.txt 2>/dev/null || true # Create systemd services sudo tee /etc/systemd/system/aitbc-cross-chain-reputation.service > /dev/null << 'EOF' [Unit] Description=AITBC Cross Chain Reputation Service After=network.target [Service] Type=simple User=aitbc WorkingDirectory=/opt/aitbc/services ExecStart=/usr/bin/python3 -m cross_chain_reputation Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF sudo tee /etc/systemd/system/aitbc-agent-communication.service > /dev/null << 'EOF' [Unit] Description=AITBC Agent Communication Service After=network.target [Service] Type=simple User=aitbc WorkingDirectory=/opt/aitbc/services ExecStart=/usr/bin/python3 -m agent_communication Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF sudo tee /etc/systemd/system/aitbc-advanced-learning.service > /dev/null << 'EOF' [Unit] Description=AITBC Advanced Learning Service After=network.target [Service] Type=simple User=aitbc WorkingDirectory=/opt/aitbc/services ExecStart=/usr/bin/python3 -m advanced_learning Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF # Reload systemd and start services sudo systemctl daemon-reload sudo systemctl enable aitbc-cross-chain-reputation sudo systemctl enable aitbc-agent-communication sudo systemctl enable aitbc-advanced-learning sudo systemctl start aitbc-cross-chain-reputation sudo systemctl start aitbc-agent-communication sudo systemctl start aitbc-advanced-learning echo 'Services deployed and started on aitbc1' " print_success "Backend services deployed to both servers" } # Deploy frontend deploy_frontend() { print_status "Building and deploying frontend..." cd "$FRONTEND_DIR" # Build frontend print_status "Building frontend application..." npm run build # Deploy to aitbc server print_server "Deploying frontend to aitbc server..." # Copy built frontend to aitbc scp -r dist/* $AITBC_SERVER:/tmp/frontend/ ssh $AITBC_SERVER " # Backup existing frontend sudo cp -r /var/www/aitbc.bubuit.net /var/www/aitbc.bubuit.net.backup 2>/dev/null || true # Deploy new frontend sudo rm -rf /var/www/aitbc.bubuit.net/* sudo cp -r /tmp/frontend/* /var/www/aitbc.bubuit.net/ # Set permissions sudo chown -R www-data:www-data /var/www/aitbc.bubuit.net/ sudo chmod -R 755 /var/www/aitbc.bubuit.net/ echo 'Frontend deployed to aitbc' " # Deploy to aitbc1 server print_server "Deploying frontend to aitbc1 server..." # Copy built frontend to aitbc1 scp -r dist/* $AITBC1_SERVER:/tmp/frontend/ ssh $AITBC1_SERVER " # Backup existing frontend sudo cp -r /var/www/aitbc.bubuit.net /var/www/aitbc.bubuit.net.backup 2>/dev/null || true # Deploy new frontend sudo rm -rf /var/www/aitbc.bubuit.net/* sudo cp -r /tmp/frontend/* /var/www/aitbc.bubuit.net/ # Set permissions sudo chown -R www-data:www-data /var/www/aitbc.bubuit.net/ sudo chmod -R 755 /var/www/aitbc.bubuit.net/ echo 'Frontend deployed to aitbc1' " print_success "Frontend deployed to both servers" } # Deploy configuration files deploy_configuration() { print_status "Deploying configuration files..." # Create nginx configuration for aitbc print_server "Deploying nginx configuration to aitbc..." ssh $AITBC_SERVER " sudo tee /etc/nginx/sites-available/aitbc-advanced.conf > /dev/null << 'EOF' server { listen 80; server_name aitbc.bubuit.net; root /var/www/aitbc.bubuit.net; index index.html; # Security headers add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection \"1; mode=block\"; add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains\" always; # Gzip compression gzip on; gzip_vary on; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json; # API routes location /api/ { proxy_pass http://localhost: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; } # Advanced features API location /api/v1/advanced/ { proxy_pass http://localhost:8001/; 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; } # Static files location / { try_files \$uri \$uri/ /index.html; expires 1y; add_header Cache-Control \"public, immutable\"; } # Health check location /health { access_log off; return 200 \"healthy\"; add_header Content-Type text/plain; } } EOF # Enable site sudo ln -sf /etc/nginx/sites-available/aitbc-advanced.conf /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx echo 'Nginx configuration deployed to aitbc' " # Create nginx configuration for aitbc1 print_server "Deploying nginx configuration to aitbc1..." ssh $AITBC1_SERVER " sudo tee /etc/nginx/sites-available/aitbc1-advanced.conf > /dev/null << 'EOF' server { listen 80; server_name aitbc1.bubuit.net; root /var/www/aitbc.bubuit.net; index index.html; # Security headers add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection \"1; mode=block\"; add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains\" always; # Gzip compression gzip on; gzip_vary on; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json; # API routes location /api/ { proxy_pass http://localhost: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; } # Advanced features API location /api/v1/advanced/ { proxy_pass http://localhost:8001/; 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; } # Static files location / { try_files \$uri \$uri/ /index.html; expires 1y; add_header Cache-Control \"public, immutable\"; } # Health check location /health { access_log off; return 200 \"healthy\"; add_header Content-Type text/plain; } } EOF # Enable site sudo ln -sf /etc/nginx/sites-available/aitbc1-advanced.conf /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx echo 'Nginx configuration deployed to aitbc1' " print_success "Configuration files deployed to both servers" } # Verify deployment verify_deployment() { print_status "Verifying deployment..." # Verify aitbc server print_server "Verifying aitbc server deployment..." ssh $AITBC_SERVER " echo '=== aitbc Server Status ===' # Check services echo 'Services:' sudo systemctl is-active aitbc-cross-chain-reputation || echo 'cross-chain-reputation: INACTIVE' sudo systemctl is-active aitbc-agent-communication || echo 'agent-communication: INACTIVE' sudo systemctl is-active aitbc-advanced-learning || echo 'advanced-learning: INACTIVE' # Check nginx echo 'Nginx:' sudo systemctl is-active nginx || echo 'nginx: INACTIVE' sudo nginx -t || echo 'nginx config: ERROR' # Check web server echo 'Web server:' curl -s http://localhost/health || echo 'health check: FAILED' echo 'aitbc verification completed' " # Verify aitbc1 server print_server "Verifying aitbc1 server deployment..." ssh $AITBC1_SERVER " echo '=== aitbc1 Server Status ===' # Check services echo 'Services:' sudo systemctl is-active aitbc-cross-chain-reputation || echo 'cross-chain-reputation: INACTIVE' sudo systemctl is-active aitbc-agent-communication || echo 'agent-communication: INACTIVE' sudo systemctl is-active aitbc-advanced-learning || echo 'advanced-learning: INACTIVE' # Check nginx echo 'Nginx:' sudo systemctl is-active nginx || echo 'nginx: INACTIVE' sudo nginx -t || echo 'nginx config: ERROR' # Check web server echo 'Web server:' curl -s http://localhost/health || echo 'health check: FAILED' echo 'aitbc1 verification completed' " print_success "Deployment verification completed" } # Test external connectivity test_connectivity() { print_status "Testing external connectivity..." # Test aitbc server print_server "Testing aitbc external connectivity..." if curl -s "http://$AITBC_HOST/health" | grep -q "healthy"; then print_success "aitbc server is accessible externally" else print_warning "aitbc server external connectivity issue" fi # Test aitbc1 server print_server "Testing aitbc1 external connectivity..." if curl -s "http://$AITBC1_HOST/health" | grep -q "healthy"; then print_success "aitbc1 server is accessible externally" else print_warning "aitbc1 server external connectivity issue" fi } # Main execution main() { print_critical "🚀 STARTING AITBC PLATFORM SERVICES DEPLOYMENT TO aitbc AND aitbc1 SERVERS" # Run deployment steps check_prerequisites deploy_services deploy_frontend deploy_configuration verify_deployment test_connectivity print_success "🎉 AITBC PLATFORM SERVICES DEPLOYMENT COMPLETED!" echo "" echo "📊 Deployment Summary:" echo " Servers: aitbc, aitbc1" echo " Services: Deployed" echo " Frontend: Deployed" echo " Configuration: Deployed" echo " Verification: Completed" echo "" echo "🌐 Platform URLs:" echo " aitbc Frontend: http://$AITBC_HOST/" echo " aitbc API: http://$AITBC_HOST/api/" echo " aitbc Advanced: http://$AITBC_HOST/api/v1/advanced/" echo " aitbc1 Frontend: http://$AITBC1_HOST/" echo " aitbc1 API: http://$AITBC1_HOST/api/" echo " aitbc1 Advanced: http://$AITBC1_HOST/api/v1/advanced/" echo "" echo "🔧 Next Steps:" echo " 1. Monitor service performance on both servers" echo " 2. Test cross-server functionality" echo " 3. Verify load balancing if configured" echo " 4. Monitor system resources and scaling" echo " 5. Set up monitoring and alerting" echo " 6. Test failover scenarios" echo "" echo "⚠️ Important Notes:" echo " - Both servers are running identical configurations" echo " - Services are managed by systemd" echo " - Nginx is configured for reverse proxy" echo " - Health checks are available at /health" echo " - API endpoints are available at /api/ and /api/v1/advanced/" echo "" echo "🎯 Deployment Status: SUCCESS - SERVICES LIVE ON BOTH SERVERS!" } # Handle script interruption trap 'print_critical "Deployment interrupted - please check partial deployment"; exit 1' INT TERM # Run main function main "$@"