Merge pull request #1 from Plaud-AI/feat/start_script

[Feat] Launch Script Ready
This commit is contained in:
Leo 2025-11-06 10:37:33 +08:00 committed by GitHub
commit 3ad597c1a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1832 additions and 17 deletions

1
.gitignore vendored
View file

@ -171,6 +171,7 @@ main/manager-api/.vscode
# Ignore webpack cache directory # Ignore webpack cache directory
main/manager-web/.webpack_cache/ main/manager-web/.webpack_cache/
main/xiaozhi-server/mysql main/xiaozhi-server/mysql
main/xiaozhi-server/redis
uploadfile uploadfile
*.json *.json
.vscode .vscode

View file

@ -0,0 +1,49 @@
# Dockerfile for local development and testing
# Build xiaozhi-server image from local source code
FROM python:3.10-slim
# Install system dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends \
libopus0 \
ffmpeg \
build-essential \
git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Configure pip to use Aliyun mirror for faster downloads in China
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
pip config set global.trusted-host mirrors.aliyun.com && \
pip config set global.timeout 120 && \
pip config set install.retries 5
# Set working directory
WORKDIR /opt/xiaozhi-esp32-server
# Copy requirements file
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
pip install --no-cache-dir -r requirements.txt --default-timeout=120 --retries 5
# Copy application code
COPY . .
# Set timezone
ENV TZ=Asia/Shanghai
# Expose ports
# 8000: WebSocket server
# 8003: HTTP server for OTA and vision analysis
EXPOSE 8000 8003
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD python -c "import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.settimeout(5); s.connect(('localhost', 8000)); s.close()" || exit 1
# Start application
CMD ["python", "app.py"]

View file

@ -24,18 +24,18 @@ def load_config():
if cached_config is not None: if cached_config is not None:
return cached_config return cached_config
default_config_path = get_project_dir() + "config.yaml" # default_config_path = get_project_dir() + "config.yaml"
custom_config_path = get_project_dir() + "data/.config.yaml" custom_config_path = get_project_dir() + "custom_config.yaml"
# 加载默认配置 # 加载默认配置
default_config = read_config(default_config_path) # default_config = read_config(default_config_path)
custom_config = read_config(custom_config_path) custom_config = read_config(custom_config_path)
if custom_config.get("manager-api", {}).get("url"): if custom_config.get("manager-api", {}).get("url"):
config = get_config_from_api(custom_config) config = get_config_from_api(custom_config)
else: # else:
# 合并配置 # # 合并配置
config = merge_configs(default_config, custom_config) # config = merge_configs(default_config, custom_config)
# 初始化目录 # 初始化目录
ensure_directories(config) ensure_directories(config)

View file

@ -44,7 +44,17 @@ class ManageApiClient:
if "你" in cls.config.get("secret"): if "你" in cls.config.get("secret"):
raise Exception("请先配置manager-api的secret") raise Exception("请先配置manager-api的secret")
cls._secret = cls.config.get("secret") # 根据aws服务器所在机房路由不同secret
region = os.getenv("REGION", "LOCAL") # 默认 LOCAL
if region == "SINGAPORE":
cls._secret = "a1c50356-4307-4bb7-8c16-c661d06ec17b"
elif region == "US-WEST-2":
cls._secret = "ff22d758-79dd-47ff-a897-c6e39983e0e1"
else:
# LOCAL 或其他未知区域,使用配置文件中的 secret
cls._secret = cls.config.get("secret")
print(f"[ManageApiClient] Region: {region}, Secret: {cls._secret[:8]}...")
cls.max_retries = cls.config.get("max_retries", 6) # 最大重试次数 cls.max_retries = cls.config.get("max_retries", 6) # 最大重试次数
cls.retry_delay = cls.config.get("retry_delay", 10) # 初始重试延迟(秒) cls.retry_delay = cls.config.get("retry_delay", 10) # 初始重试延迟(秒)
# NOTE(goody): 2025/4/16 http相关资源统一管理,后续可以增加线程池或者超时 # NOTE(goody): 2025/4/16 http相关资源统一管理,后续可以增加线程池或者超时

View file

@ -2,7 +2,7 @@ import os
from config.config_loader import read_config, get_project_dir, load_config from config.config_loader import read_config, get_project_dir, load_config
default_config_file = "config.yaml" default_config_file = "custom_config.yaml"
config_file_valid = False config_file_valid = False
@ -13,21 +13,16 @@ def check_config_file():
""" """
简化的配置检查,仅提示用户配置文件的使用情况 简化的配置检查,仅提示用户配置文件的使用情况
""" """
custom_config_file = get_project_dir() + "data/." + default_config_file custom_config_file = get_project_dir() + default_config_file
if not os.path.exists(custom_config_file): if not os.path.exists(custom_config_file):
raise FileNotFoundError( raise FileNotFoundError(
"找不到data/.config.yaml文件,请按教程确认该配置文件是否存在" "找不到custom_config.yaml文件,请按教程确认该配置文件是否存在"
) )
# 检查是否从API读取配置 # 检查是否从API读取配置
config = load_config() config = load_config()
if config.get("read_config_from_api", False): if config.get("read_config_from_api", False):
print("从API读取配置") print("从API读取配置")
old_config_origin = read_config(custom_config_file) # 支持混合模式:API 优先,本地配置作为兜底
if old_config_origin.get("selected_module") is not None: # 允许同时存在 API 配置和本地 selected_module/role_id
error_msg = "您的配置文件好像既包含智控台的配置又包含本地配置:\n"
error_msg += "\n建议您:\n"
error_msg += "1、将根目录的config_from_api.yaml文件复制到data下,重命名为.config.yaml\n"
error_msg += "2、按教程配置好接口地址和密钥\n"
raise ValueError(error_msg)
config_file_valid = True config_file_valid = True

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,181 @@
# Docker Compose configuration for local development
# This file uses locally built images instead of pulling from remote registry
services:
# ============================================================================
# xiaozhi-server (Python WebSocket Server)
# ============================================================================
xiaozhi-esp32-server:
build:
context: .
dockerfile: Dockerfile
image: xiaozhi-esp32-server:local
container_name: xiaozhi-esp32-server
depends_on:
xiaozhi-esp32-server-db:
condition: service_healthy
xiaozhi-esp32-server-redis:
condition: service_healthy
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# WebSocket server
- "8000:8000"
# HTTP server for vision analysis and OTA
- "8003:8003"
security_opt:
- seccomp:unconfined
environment:
- TZ=Asia/Shanghai
- PYTHONUNBUFFERED=1
- LOG_LEVEL=INFO
- REGION=LOCAL
healthcheck:
disable: true
volumes:
# Configuration directory
- ./data:/opt/xiaozhi-esp32-server/data
# Custom config (optional override, uncomment if you need to customize)
- ./custom_config.yaml:/opt/xiaozhi-esp32-server/custom_config.yaml
# Model files (important!)
- ./models/SenseVoiceSmall:/opt/xiaozhi-esp32-server/models/SenseVoiceSmall
- ./models/snakers4_silero-vad:/opt/xiaozhi-esp32-server/models/snakers4_silero-vad
# Temporary files
- ./tmp:/opt/xiaozhi-esp32-server/tmp
# Music files (optional)
- ./music:/opt/xiaozhi-esp32-server/music
# Logs (optional, for debugging)
- ./logs:/opt/xiaozhi-esp32-server/logs
# ============================================================================
# manager-web (Management Console + API)
# ============================================================================
xiaozhi-esp32-server-web:
# Build from local source (supports ARM64/Apple Silicon)
build:
context: ../../
dockerfile: Dockerfile-web
image: xiaozhi-esp32-server-web:local
# Option: Use pre-built image from registry (x86 only)
# image: ghcr.nju.edu.cn/xinnan-tech/xiaozhi-esp32-server:web_latest
# platform: linux/amd64 # Uncomment to force x86 emulation on ARM64
container_name: xiaozhi-esp32-server-web
restart: unless-stopped
networks:
- xiaozhi-network
depends_on:
xiaozhi-esp32-server-db:
condition: service_healthy
xiaozhi-esp32-server-redis:
condition: service_healthy
ports:
# Management console and API
- "8002:8002"
environment:
- TZ=Asia/Shanghai
- SPRING_PROFILES_ACTIVE=dev
# Database connection
- SPRING_DATASOURCE_DRUID_URL=jdbc:mysql://xiaozhi-esp32-server-db:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&connectTimeout=30000&socketTimeout=30000&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
- SPRING_DATASOURCE_DRUID_USERNAME=root
- SPRING_DATASOURCE_DRUID_PASSWORD=123456
# Redis connection
- SPRING_DATA_REDIS_HOST=xiaozhi-esp32-server-redis
- SPRING_DATA_REDIS_PORT=6379
- SPRING_DATA_REDIS_PASSWORD=
volumes:
# Upload files directory
- ./uploadfile:/uploadfile
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
# ============================================================================
# MySQL Database
# ============================================================================
xiaozhi-esp32-server-db:
image: mysql:8.0
container_name: xiaozhi-esp32-server-db
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# Expose MySQL port for local debugging (optional)
- "3307:3306"
volumes:
# Persistent database storage
- ./mysql/data:/var/lib/mysql
# MySQL configuration (optional)
# - ./mysql/conf:/etc/mysql/conf.d
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=xiaozhi_esp32_server
- MYSQL_INITDB_ARGS=--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --default-authentication-plugin=mysql_native_password
- --max_connections=1000
- --max_allowed_packet=256M
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p123456"]
timeout: 45s
interval: 10s
retries: 10
start_period: 60s
# ============================================================================
# Redis Cache
# ============================================================================
xiaozhi-esp32-server-redis:
image: redis:7-alpine
container_name: xiaozhi-esp32-server-redis
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# Expose Redis port for local debugging (optional)
- "6380:6379"
volumes:
# Persistent Redis data (optional)
- ./redis/data:/data
environment:
- TZ=Asia/Shanghai
command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
logging:
driver: "json-file"
options:
max-size: "5m"
max-file: "3"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
# ============================================================================
# Networks
# ============================================================================
networks:
xiaozhi-network:
driver: bridge
name: xiaozhi-local-network
# ============================================================================
# Volumes (optional - for named volumes instead of bind mounts)
# ============================================================================
# volumes:
# mysql-data:
# redis-data:

View file

@ -0,0 +1,181 @@
# Docker Compose configuration for local development
# This file uses locally built images instead of pulling from remote registry
services:
# ============================================================================
# xiaozhi-server (Python WebSocket Server)
# ============================================================================
xiaozhi-esp32-server:
build:
context: .
dockerfile: Dockerfile
image: xiaozhi-esp32-server:local
container_name: xiaozhi-esp32-server
depends_on:
xiaozhi-esp32-server-db:
condition: service_healthy
xiaozhi-esp32-server-redis:
condition: service_healthy
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# WebSocket server
- "8000:8000"
# HTTP server for vision analysis and OTA
- "8003:8003"
security_opt:
- seccomp:unconfined
environment:
- TZ=Asia/Shanghai
- PYTHONUNBUFFERED=1
- LOG_LEVEL=INFO
- REGION=SINGAPORE
healthcheck:
disable: true
volumes:
# Configuration directory
- ./data:/opt/xiaozhi-esp32-server/data
# Custom config (optional override, uncomment if you need to customize)
- ./custom_config.yaml:/opt/xiaozhi-esp32-server/custom_config.yaml
# Model files (important!)
- ./models/SenseVoiceSmall:/opt/xiaozhi-esp32-server/models/SenseVoiceSmall
- ./models/snakers4_silero-vad:/opt/xiaozhi-esp32-server/models/snakers4_silero-vad
# Temporary files
- ./tmp:/opt/xiaozhi-esp32-server/tmp
# Music files (optional)
- ./music:/opt/xiaozhi-esp32-server/music
# Logs (optional, for debugging)
- ./logs:/opt/xiaozhi-esp32-server/logs
# ============================================================================
# manager-web (Management Console + API)
# ============================================================================
xiaozhi-esp32-server-web:
# Build from local source (supports ARM64/Apple Silicon)
build:
context: ../../
dockerfile: Dockerfile-web
image: xiaozhi-esp32-server-web:local
# Option: Use pre-built image from registry (x86 only)
# image: ghcr.nju.edu.cn/xinnan-tech/xiaozhi-esp32-server:web_latest
# platform: linux/amd64 # Uncomment to force x86 emulation on ARM64
container_name: xiaozhi-esp32-server-web
restart: unless-stopped
networks:
- xiaozhi-network
depends_on:
xiaozhi-esp32-server-db:
condition: service_healthy
xiaozhi-esp32-server-redis:
condition: service_healthy
ports:
# Management console and API
- "8002:8002"
environment:
- TZ=Asia/Shanghai
- SPRING_PROFILES_ACTIVE=dev
# Database connection
- SPRING_DATASOURCE_DRUID_URL=jdbc:mysql://xiaozhi-esp32-server-db:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&connectTimeout=30000&socketTimeout=30000&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
- SPRING_DATASOURCE_DRUID_USERNAME=root
- SPRING_DATASOURCE_DRUID_PASSWORD=123456
# Redis connection
- SPRING_DATA_REDIS_HOST=xiaozhi-esp32-server-redis
- SPRING_DATA_REDIS_PORT=6379
- SPRING_DATA_REDIS_PASSWORD=
volumes:
# Upload files directory
- ./uploadfile:/uploadfile
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
# ============================================================================
# MySQL Database
# ============================================================================
xiaozhi-esp32-server-db:
image: mysql:8.0
container_name: xiaozhi-esp32-server-db
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# Expose MySQL port for local debugging (optional)
- "3307:3306"
volumes:
# Persistent database storage
- ./mysql/data:/var/lib/mysql
# MySQL configuration (optional)
# - ./mysql/conf:/etc/mysql/conf.d
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=xiaozhi_esp32_server
- MYSQL_INITDB_ARGS=--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --default-authentication-plugin=mysql_native_password
- --max_connections=1000
- --max_allowed_packet=256M
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p123456"]
timeout: 45s
interval: 10s
retries: 10
start_period: 60s
# ============================================================================
# Redis Cache
# ============================================================================
xiaozhi-esp32-server-redis:
image: redis:7-alpine
container_name: xiaozhi-esp32-server-redis
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# Expose Redis port for local debugging (optional)
- "6380:6379"
volumes:
# Persistent Redis data (optional)
- ./redis/data:/data
environment:
- TZ=Asia/Shanghai
command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
logging:
driver: "json-file"
options:
max-size: "5m"
max-file: "3"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
# ============================================================================
# Networks
# ============================================================================
networks:
xiaozhi-network:
driver: bridge
name: xiaozhi-local-network
# ============================================================================
# Volumes (optional - for named volumes instead of bind mounts)
# ============================================================================
# volumes:
# mysql-data:
# redis-data:

View file

@ -0,0 +1,181 @@
# Docker Compose configuration for local development
# This file uses locally built images instead of pulling from remote registry
services:
# ============================================================================
# xiaozhi-server (Python WebSocket Server)
# ============================================================================
xiaozhi-esp32-server:
build:
context: .
dockerfile: Dockerfile
image: xiaozhi-esp32-server:local
container_name: xiaozhi-esp32-server
depends_on:
xiaozhi-esp32-server-db:
condition: service_healthy
xiaozhi-esp32-server-redis:
condition: service_healthy
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# WebSocket server
- "8000:8000"
# HTTP server for vision analysis and OTA
- "8003:8003"
security_opt:
- seccomp:unconfined
healthcheck:
disable: true
environment:
- TZ=Asia/Shanghai
- PYTHONUNBUFFERED=1
- LOG_LEVEL=INFO
- REGION=US-WEST-2
volumes:
# Configuration directory
- ./data:/opt/xiaozhi-esp32-server/data
# Custom config (optional override, uncomment if you need to customize)
- ./custom_config.yaml:/opt/xiaozhi-esp32-server/custom_config.yaml
# Model files (important!)
- ./models/SenseVoiceSmall:/opt/xiaozhi-esp32-server/models/SenseVoiceSmall
- ./models/snakers4_silero-vad:/opt/xiaozhi-esp32-server/models/snakers4_silero-vad
# Temporary files
- ./tmp:/opt/xiaozhi-esp32-server/tmp
# Music files (optional)
- ./music:/opt/xiaozhi-esp32-server/music
# Logs (optional, for debugging)
- ./logs:/opt/xiaozhi-esp32-server/logs
# ============================================================================
# manager-web (Management Console + API)
# ============================================================================
xiaozhi-esp32-server-web:
# Build from local source (supports ARM64/Apple Silicon)
build:
context: ../../
dockerfile: Dockerfile-web
image: xiaozhi-esp32-server-web:local
# Option: Use pre-built image from registry (x86 only)
# image: ghcr.nju.edu.cn/xinnan-tech/xiaozhi-esp32-server:web_latest
# platform: linux/amd64 # Uncomment to force x86 emulation on ARM64
container_name: xiaozhi-esp32-server-web
restart: unless-stopped
networks:
- xiaozhi-network
depends_on:
xiaozhi-esp32-server-db:
condition: service_healthy
xiaozhi-esp32-server-redis:
condition: service_healthy
ports:
# Management console and API
- "8002:8002"
environment:
- TZ=Asia/Shanghai
- SPRING_PROFILES_ACTIVE=dev
# Database connection
- SPRING_DATASOURCE_DRUID_URL=jdbc:mysql://xiaozhi-esp32-server-db:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&connectTimeout=30000&socketTimeout=30000&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
- SPRING_DATASOURCE_DRUID_USERNAME=root
- SPRING_DATASOURCE_DRUID_PASSWORD=123456
# Redis connection
- SPRING_DATA_REDIS_HOST=xiaozhi-esp32-server-redis
- SPRING_DATA_REDIS_PORT=6379
- SPRING_DATA_REDIS_PASSWORD=
volumes:
# Upload files directory
- ./uploadfile:/uploadfile
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
# ============================================================================
# MySQL Database
# ============================================================================
xiaozhi-esp32-server-db:
image: mysql:8.0
container_name: xiaozhi-esp32-server-db
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# Expose MySQL port for local debugging (optional)
- "3307:3306"
volumes:
# Persistent database storage
- ./mysql/data:/var/lib/mysql
# MySQL configuration (optional)
# - ./mysql/conf:/etc/mysql/conf.d
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=xiaozhi_esp32_server
- MYSQL_INITDB_ARGS=--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --default-authentication-plugin=mysql_native_password
- --max_connections=1000
- --max_allowed_packet=256M
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p123456"]
timeout: 45s
interval: 10s
retries: 10
start_period: 60s
# ============================================================================
# Redis Cache
# ============================================================================
xiaozhi-esp32-server-redis:
image: redis:7-alpine
container_name: xiaozhi-esp32-server-redis
restart: unless-stopped
networks:
- xiaozhi-network
ports:
# Expose Redis port for local debugging (optional)
- "6380:6379"
volumes:
# Persistent Redis data (optional)
- ./redis/data:/data
environment:
- TZ=Asia/Shanghai
command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
logging:
driver: "json-file"
options:
max-size: "5m"
max-file: "3"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
# ============================================================================
# Networks
# ============================================================================
networks:
xiaozhi-network:
driver: bridge
name: xiaozhi-local-network
# ============================================================================
# Volumes (optional - for named volumes instead of bind mounts)
# ============================================================================
# volumes:
# mysql-data:
# redis-data:

View file

@ -36,3 +36,9 @@ psutil==7.0.0
portalocker==3.2.0 portalocker==3.2.0
Jinja2==3.1.6 Jinja2==3.1.6
vosk==0.3.44 vosk==0.3.44
redis>=4.0.0,<5.0.0
# TTS SDKs
elevenlabs>=1.11.0
cartesia>=1.0.0

174
main/xiaozhi-server/start.sh Executable file
View file

@ -0,0 +1,174 @@
#!/bin/bash
# Xiaozhi Server Startup Script
# Usage: ./start.sh --region <region> --action <action>
# Region: sg, us, local
# Action: server, all
set -e
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Display banner
echo -e "${CYAN}"
echo "=================================="
echo " Xiaozhi Server Startup Script"
echo "=================================="
echo -e "${NC}"
# Function to display usage
show_usage() {
echo -e "${RED}Error: Missing or invalid arguments${NC}"
echo ""
echo "Usage: $0 --region <region> --action <action>"
echo ""
echo "Options:"
echo " -r, --region <region> Deployment region"
echo " Values: sg, us, local"
echo ""
echo " -a, --action <action> Action to perform"
echo " Values: server, web, all"
echo ""
echo " -h, --help Show this help message"
echo ""
echo "Region details:"
echo " sg - Singapore (REGION=SINGAPORE)"
echo " us - US West 2 (REGION=US-WEST-2)"
echo " local - Local development (REGION=LOCAL)"
echo ""
echo "Action details:"
echo " server - Rebuild and restart xiaozhi-esp32-server"
echo " web - Rebuild and restart xiaozhi-esp32-server-web (manager-api + manager-web)"
echo " all - Rebuild and restart server + web (does not rebuild db/redis)"
echo ""
echo "Examples:"
echo " $0 --region sg --action server"
echo " $0 -r local -a web"
echo " $0 --region local --action all"
exit 1
}
# Parse command line arguments
REGION=""
ACTION=""
while [[ $# -gt 0 ]]; do
case $1 in
-r|--region)
REGION="$2"
shift 2
;;
-a|--action)
ACTION="$2"
shift 2
;;
-h|--help)
show_usage
;;
*)
echo -e "${RED}Error: Unknown option '$1'${NC}"
echo ""
show_usage
;;
esac
done
# Check if required arguments are provided
if [ -z "$REGION" ] || [ -z "$ACTION" ]; then
show_usage
fi
# Validate region
case $REGION in
sg|SG)
COMPOSE_FILE="docker-compose_sg_all.yml"
REGION_NAME="Singapore"
REGION_ENV="SINGAPORE"
;;
us|US)
COMPOSE_FILE="docker-compose_us_all.yml"
REGION_NAME="US West 2"
REGION_ENV="US-WEST-2"
;;
local|LOCAL)
COMPOSE_FILE="docker-compose_local_all.yml"
REGION_NAME="Local"
REGION_ENV="LOCAL"
;;
*)
echo -e "${RED}Error: Invalid region '${REGION}'${NC}"
echo "Valid regions: sg, us, local"
exit 1
;;
esac
# Validate action
case $ACTION in
server|SERVER)
ACTION_NAME="Server Only"
SERVICES="xiaozhi-esp32-server"
;;
web|WEB)
ACTION_NAME="Web Only (Manager API + Web)"
SERVICES="xiaozhi-esp32-server-web"
;;
all|ALL)
ACTION_NAME="Server + Web"
SERVICES="xiaozhi-esp32-server xiaozhi-esp32-server-web"
;;
*)
echo -e "${RED}Error: Invalid action '${ACTION}'${NC}"
echo "Valid actions: server, web, all"
exit 1
;;
esac
# Check if compose file exists
if [ ! -f "$COMPOSE_FILE" ]; then
echo -e "${RED}Error: Compose file '${COMPOSE_FILE}' not found${NC}"
exit 1
fi
echo -e "${GREEN}Configuration:${NC}"
echo " Region: ${REGION_NAME} (${REGION_ENV})"
echo " Action: ${ACTION_NAME}"
echo " Compose file: ${COMPOSE_FILE}"
echo ""
# Display current status
echo -e "${BLUE}Current container status:${NC}"
docker ps --filter name=xiaozhi --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo "No containers running"
echo ""
# Execute action with rebuild
echo -e "${YELLOW}→ Rebuilding and starting: ${ACTION_NAME}${NC}"
echo -e "${CYAN} Services: ${SERVICES}${NC}"
echo ""
# Use up --build -d to rebuild images and start containers
docker compose -f "$COMPOSE_FILE" up --build -d $SERVICES
echo ""
echo -e "${GREEN}✓ Operation completed${NC}"
echo ""
# Display final status
echo -e "${BLUE}Updated container status:${NC}"
docker ps --filter name=xiaozhi --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
echo ""
# Show logs option
echo -e "${CYAN}To view logs, run:${NC}"
if [ -n "$SERVICES" ]; then
echo " docker compose -f $COMPOSE_FILE logs -f $SERVICES"
else
echo " docker compose -f $COMPOSE_FILE logs -f"
fi
echo ""