rwadurian/backend/mpc-system/deploy.sh

232 lines
6.9 KiB
Bash

#!/bin/bash
# =============================================================================
# MPC System - Deployment Script
# =============================================================================
# 部署位置: 192.168.1.100 (Nginx + MPC 服务器)
# 对外端口: 4000 (Account Service HTTP) - 供 mpc-service 调用
# =============================================================================
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Load environment
if [ -f ".env" ]; then
export $(cat .env | grep -v '^#' | xargs)
elif [ -f ".env.production" ]; then
export $(cat .env.production | grep -v '^#' | xargs)
fi
# Core services list
CORE_SERVICES="postgres redis rabbitmq"
MPC_SERVICES="session-coordinator message-router server-party-1 server-party-2 server-party-3 account-service"
ALL_SERVICES="$CORE_SERVICES $MPC_SERVICES"
case "$1" in
build)
log_info "Building MPC System services..."
docker compose build
log_success "MPC System built successfully"
;;
build-no-cache)
log_info "Building MPC System (no cache)..."
docker compose build --no-cache
log_success "MPC System built successfully"
;;
up|start)
log_info "Starting MPC System..."
docker compose up -d
log_success "MPC System started"
echo ""
log_info "Services status:"
docker compose ps
;;
down|stop)
log_info "Stopping MPC System..."
docker compose down
log_success "MPC System stopped"
;;
restart)
log_info "Restarting MPC System..."
docker compose down
docker compose up -d
log_success "MPC System restarted"
;;
logs)
if [ -n "$2" ]; then
docker compose logs -f "$2"
else
docker compose logs -f
fi
;;
logs-tail)
if [ -n "$2" ]; then
docker compose logs --tail 100 "$2"
else
docker compose logs --tail 100
fi
;;
status|ps)
log_info "MPC System status:"
docker compose ps
;;
health)
log_info "Checking MPC System health..."
# Check infrastructure
echo ""
echo "=== Infrastructure ==="
for svc in $CORE_SERVICES; do
if docker compose ps "$svc" --format json 2>/dev/null | grep -q '"Health":"healthy"'; then
log_success "$svc is healthy"
else
log_warn "$svc is not healthy"
fi
done
# Check MPC services
echo ""
echo "=== MPC Services ==="
for svc in $MPC_SERVICES; do
if docker compose ps "$svc" --format json 2>/dev/null | grep -q '"Health":"healthy"'; then
log_success "$svc is healthy"
else
log_warn "$svc is not healthy"
fi
done
# Check external API
echo ""
echo "=== External API ==="
if curl -sf "http://localhost:4000/health" > /dev/null 2>&1; then
log_success "Account Service API (port 4000) is accessible"
else
log_error "Account Service API (port 4000) is not accessible"
fi
;;
infra)
case "$2" in
up)
log_info "Starting infrastructure services..."
docker compose up -d $CORE_SERVICES
log_success "Infrastructure started"
;;
down)
log_info "Stopping infrastructure services..."
docker compose stop $CORE_SERVICES
log_success "Infrastructure stopped"
;;
*)
echo "Usage: $0 infra {up|down}"
exit 1
;;
esac
;;
mpc)
case "$2" in
up)
log_info "Starting MPC services..."
docker compose up -d $MPC_SERVICES
log_success "MPC services started"
;;
down)
log_info "Stopping MPC services..."
docker compose stop $MPC_SERVICES
log_success "MPC services stopped"
;;
restart)
log_info "Restarting MPC services..."
docker compose stop $MPC_SERVICES
docker compose up -d $MPC_SERVICES
log_success "MPC services restarted"
;;
*)
echo "Usage: $0 mpc {up|down|restart}"
exit 1
;;
esac
;;
clean)
log_warn "This will remove all containers and volumes!"
read -p "Are you sure? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
docker compose down -v
log_success "MPC System cleaned"
else
log_info "Cancelled"
fi
;;
shell)
if [ -n "$2" ]; then
log_info "Opening shell in $2..."
docker compose exec "$2" sh
else
log_info "Opening shell in account-service..."
docker compose exec account-service sh
fi
;;
test-api)
log_info "Testing Account Service API..."
echo ""
echo "Health check:"
curl -s "http://localhost:4000/health" | jq . 2>/dev/null || curl -s "http://localhost:4000/health"
echo ""
;;
*)
echo "MPC System Deployment Script"
echo ""
echo "Usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " build - Build all Docker images"
echo " build-no-cache - Build images without cache"
echo " up|start - Start all services"
echo " down|stop - Stop all services"
echo " restart - Restart all services"
echo " logs [service] - Follow logs (all or specific service)"
echo " logs-tail [svc] - Show last 100 log lines"
echo " status|ps - Show services status"
echo " health - Check all services health"
echo ""
echo " infra up|down - Start/stop infrastructure only"
echo " mpc up|down|restart - Start/stop/restart MPC services only"
echo ""
echo " shell [service] - Open shell in container"
echo " test-api - Test Account Service API"
echo " clean - Remove all containers and volumes"
echo ""
echo "Services:"
echo " Infrastructure: $CORE_SERVICES"
echo " MPC Services: $MPC_SERVICES"
exit 1
;;
esac