refactor(presence-service): 优化 deploy.sh 与主基础设施集成
- 更新容器名和镜像名与项目规范一致 - 添加 load_env 函数支持共享环境配置 - 添加 up/logs-all/clean-all 命令 - 使用动态 HEALTH_ENDPOINT 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
f132b86899
commit
d6227a574b
|
|
@ -2,13 +2,16 @@
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Presence Service - Individual Deployment Script
|
# Presence Service - Individual Deployment Script
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
# Uses shared infrastructure from main docker-compose.yml
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
SERVICE_NAME="presence-service"
|
SERVICE_NAME="presence-service"
|
||||||
CONTAINER_NAME="presence-service"
|
CONTAINER_NAME="rwa-presence-service"
|
||||||
IMAGE_NAME="presence-service"
|
IMAGE_NAME="services-presence-service"
|
||||||
PORT=3001
|
PORT=3011
|
||||||
|
HEALTH_ENDPOINT="http://localhost:${PORT}/api/v1/health"
|
||||||
|
|
||||||
# Colors
|
# Colors
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
|
|
@ -24,11 +27,18 @@ log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
|
||||||
|
|
||||||
# Get script directory
|
# Get script directory
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
SERVICES_DIR="$(dirname "$SCRIPT_DIR")"
|
||||||
|
|
||||||
# Load environment
|
# Load environment from parent services directory (shared .env)
|
||||||
if [ -f "$SCRIPT_DIR/.env.production" ]; then
|
load_env() {
|
||||||
export $(cat "$SCRIPT_DIR/.env.production" | grep -v '^#' | xargs)
|
if [ -f "$SERVICES_DIR/.env" ]; then
|
||||||
fi
|
export $(cat "$SERVICES_DIR/.env" | grep -v '^#' | xargs)
|
||||||
|
log_info "Loaded .env from $SERVICES_DIR"
|
||||||
|
elif [ -f "$SCRIPT_DIR/.env.production" ]; then
|
||||||
|
export $(cat "$SCRIPT_DIR/.env.production" | grep -v '^#' | xargs)
|
||||||
|
log_info "Loaded .env.production from $SCRIPT_DIR"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
build)
|
build)
|
||||||
|
|
@ -44,14 +54,18 @@ case "$1" in
|
||||||
;;
|
;;
|
||||||
|
|
||||||
start)
|
start)
|
||||||
log_info "Starting $SERVICE_NAME..."
|
load_env
|
||||||
cd "$SCRIPT_DIR"
|
log_info "Starting $SERVICE_NAME using shared infrastructure..."
|
||||||
docker compose up -d
|
cd "$SERVICES_DIR"
|
||||||
|
docker compose up -d "$SERVICE_NAME"
|
||||||
log_success "$SERVICE_NAME started"
|
log_success "$SERVICE_NAME started"
|
||||||
|
log_info "Waiting for service to be healthy..."
|
||||||
|
sleep 5
|
||||||
|
$0 health
|
||||||
;;
|
;;
|
||||||
|
|
||||||
start-deps)
|
start-deps)
|
||||||
log_info "Starting dependencies only..."
|
log_info "Starting dependencies only (for local development)..."
|
||||||
cd "$SCRIPT_DIR"
|
cd "$SCRIPT_DIR"
|
||||||
docker compose -f docker-compose.dev.yml up -d
|
docker compose -f docker-compose.dev.yml up -d
|
||||||
log_success "Dependencies started"
|
log_success "Dependencies started"
|
||||||
|
|
@ -59,8 +73,8 @@ case "$1" in
|
||||||
|
|
||||||
stop)
|
stop)
|
||||||
log_info "Stopping $SERVICE_NAME..."
|
log_info "Stopping $SERVICE_NAME..."
|
||||||
cd "$SCRIPT_DIR"
|
docker stop "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
docker compose down
|
docker rm "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
log_success "$SERVICE_NAME stopped"
|
log_success "$SERVICE_NAME stopped"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|
@ -76,6 +90,13 @@ case "$1" in
|
||||||
$0 start
|
$0 start
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
up)
|
||||||
|
load_env
|
||||||
|
log_info "Starting $SERVICE_NAME in foreground..."
|
||||||
|
cd "$SERVICES_DIR"
|
||||||
|
docker compose up "$SERVICE_NAME"
|
||||||
|
;;
|
||||||
|
|
||||||
logs)
|
logs)
|
||||||
docker logs -f "$CONTAINER_NAME"
|
docker logs -f "$CONTAINER_NAME"
|
||||||
;;
|
;;
|
||||||
|
|
@ -84,6 +105,11 @@ case "$1" in
|
||||||
docker logs --tail 100 "$CONTAINER_NAME"
|
docker logs --tail 100 "$CONTAINER_NAME"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
logs-all)
|
||||||
|
cd "$SERVICES_DIR"
|
||||||
|
docker compose logs -f "$SERVICE_NAME"
|
||||||
|
;;
|
||||||
|
|
||||||
status)
|
status)
|
||||||
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
||||||
log_success "$SERVICE_NAME is running"
|
log_success "$SERVICE_NAME is running"
|
||||||
|
|
@ -95,10 +121,10 @@ case "$1" in
|
||||||
|
|
||||||
health)
|
health)
|
||||||
log_info "Checking health of $SERVICE_NAME..."
|
log_info "Checking health of $SERVICE_NAME..."
|
||||||
if curl -sf "http://localhost:$PORT/api/v1/health" > /dev/null 2>&1; then
|
if curl -sf "$HEALTH_ENDPOINT" > /dev/null 2>&1; then
|
||||||
HEALTH=$(curl -s "http://localhost:$PORT/api/v1/health")
|
HEALTH=$(curl -s "$HEALTH_ENDPOINT")
|
||||||
log_success "$SERVICE_NAME is healthy"
|
log_success "$SERVICE_NAME is healthy"
|
||||||
echo "$HEALTH"
|
echo "$HEALTH" | jq . 2>/dev/null || echo "$HEALTH"
|
||||||
else
|
else
|
||||||
log_error "$SERVICE_NAME health check failed"
|
log_error "$SERVICE_NAME health check failed"
|
||||||
exit 1
|
exit 1
|
||||||
|
|
@ -158,12 +184,19 @@ case "$1" in
|
||||||
|
|
||||||
clean)
|
clean)
|
||||||
log_info "Cleaning up..."
|
log_info "Cleaning up..."
|
||||||
cd "$SCRIPT_DIR"
|
docker stop "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
docker compose down -v
|
docker rm "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
docker rmi "$IMAGE_NAME" 2>/dev/null || true
|
|
||||||
log_success "Cleanup completed"
|
log_success "Cleanup completed"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
clean-all)
|
||||||
|
log_info "Cleaning $SERVICE_NAME (removing container and image)..."
|
||||||
|
docker stop "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
|
docker rm "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
|
docker rmi "$IMAGE_NAME" 2>/dev/null || true
|
||||||
|
log_success "$SERVICE_NAME fully cleaned"
|
||||||
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
echo "Usage: $0 {command}"
|
echo "Usage: $0 {command}"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
@ -172,15 +205,17 @@ case "$1" in
|
||||||
echo " build-no-cache - Build Docker image without cache"
|
echo " build-no-cache - Build Docker image without cache"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Service Commands:"
|
echo "Service Commands:"
|
||||||
echo " start - Start the full stack (service + dependencies)"
|
echo " start - Start using shared infrastructure"
|
||||||
echo " start-deps - Start dependencies only (for local development)"
|
echo " start-deps - Start dependencies only (for local development)"
|
||||||
echo " stop - Stop all containers"
|
echo " stop - Stop the service container"
|
||||||
echo " stop-deps - Stop dependencies only"
|
echo " stop-deps - Stop dependencies only"
|
||||||
echo " restart - Restart the service"
|
echo " restart - Restart the service"
|
||||||
|
echo " up - Start in foreground mode"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Monitoring Commands:"
|
echo "Monitoring Commands:"
|
||||||
echo " logs - Follow logs"
|
echo " logs - Follow logs"
|
||||||
echo " logs-tail - Show last 100 log lines"
|
echo " logs-tail - Show last 100 log lines"
|
||||||
|
echo " logs-all - Follow logs via docker compose"
|
||||||
echo " status - Show service status"
|
echo " status - Show service status"
|
||||||
echo " health - Check service health"
|
echo " health - Check service health"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
@ -198,7 +233,8 @@ case "$1" in
|
||||||
echo " test-e2e - Run E2E tests only"
|
echo " test-e2e - Run E2E tests only"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Maintenance Commands:"
|
echo "Maintenance Commands:"
|
||||||
echo " clean - Remove containers and images"
|
echo " clean - Remove container"
|
||||||
|
echo " clean-all - Remove container and image"
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue