#!/usr/bin/env bash # ============================================================================= # Chat-Agent-System 多环境与服务管理脚本 (env-service.sh) # # 支持环境: # shared : 共享知识库基础设施 (pgvector / regulations-db) # dev : 开发环境服务 (端口 8080, DB 5433, 数据隔离 ./data/dev) # test : 测试环境服务 (端口 8081, DB 5434, 数据隔离 ./data/test) # main : 生产环境服务 (端口 8000, DB 5435, 数据隔离 ./data/main) # all : 所有环境 # # 支持命令: start | stop | restart | status | logs | down # ============================================================================= set -eo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" cd "${ROOT_DIR}" ENV_TARGET="$1" ACTION="$2" show_usage() { cat < 示例: $0 shared start # 启动共享知识库基础服务 $0 dev start # 启动开发分支环境 $0 test start # 启动测试分支环境 $0 main start # 启动主分支生产环境 $0 all status # 查看所有环境服务状态 $0 dev logs # 查看开发环境日志 $0 dev stop # 停止开发环境 EOF exit 1 } if [[ -z "${ENV_TARGET}" || -z "${ACTION}" ]]; then show_usage fi # 确保环境变量配置文件与存储目录存在 prepare_env_file() { local target="$1" local env_file="configs/.env.${target}" local env_example="configs/.env.${target}.example" if [[ ! -f "${env_file}" ]]; then if [[ -f "${env_example}" ]]; then echo "[提示] 配置文件 ${env_file} 不存在,正在从 ${env_example} 自动初始化..." cp "${env_example}" "${env_file}" fi fi # 确保隔离数据目录存在 # 确保隔离数据目录与法规目录存在 if [[ "${target}" == "shared" ]]; then mkdir -p "data/shared/kb" mkdir -p ".regulations/intake" ".regulations/library" ".regulations/quarantine" ".regulations/work" else mkdir -p "data/${target}/workspaces" fi } # 确保 Docker 共享网络已创建 ensure_shared_network() { if ! docker network ls --format '{{.Name}}' | grep -q '^chat-agent-shared-net$'; then echo "[网络] 创建共享基础设施 Docker 网络: chat-agent-shared-net..." docker network create chat-agent-shared-net >/dev/null 2>&1 || true fi } run_compose() { local target="$1" local act="$2" local compose_file="docker-compose.${target}.yml" local env_file="configs/.env.${target}" if [[ ! -f "${compose_file}" ]]; then echo "[错误] 找不到编排文件: ${compose_file}" exit 1 fi prepare_env_file "${target}" local env_arg="" if [[ -f "${env_file}" ]]; then env_arg="--env-file ${env_file}" fi case "${act}" in start) if [[ "${target}" != "shared" ]]; then ensure_shared_network fi echo "==> [${target}] 启动服务 (${compose_file})..." docker compose -f "${compose_file}" ${env_arg} up -d echo "==> [${target}] 服务已启动" ;; stop) echo "==> [${target}] 停止服务..." docker compose -f "${compose_file}" ${env_arg} stop ;; restart) if [[ "${target}" != "shared" ]]; then ensure_shared_network fi echo "==> [${target}] 重启服务..." docker compose -f "${compose_file}" ${env_arg} restart ;; status|ps) echo "------------------------------------------------------------" echo "环境: [${target}] (编排: ${compose_file})" echo "------------------------------------------------------------" docker compose -f "${compose_file}" ${env_arg} ps echo "" ;; logs) docker compose -f "${compose_file}" ${env_arg} logs -f --tail=100 ;; down) echo "==> [${target}] 清理并下线容器..." docker compose -f "${compose_file}" ${env_arg} down ;; *) echo "[错误] 未知操作: ${act}" show_usage ;; esac } case "${ENV_TARGET}" in shared|dev|test|main) run_compose "${ENV_TARGET}" "${ACTION}" ;; all) case "${ACTION}" in start) run_compose "shared" "start" run_compose "dev" "start" run_compose "test" "start" run_compose "main" "start" ;; stop|down) run_compose "dev" "${ACTION}" run_compose "test" "${ACTION}" run_compose "main" "${ACTION}" run_compose "shared" "${ACTION}" ;; status|ps) run_compose "shared" "status" run_compose "dev" "status" run_compose "test" "status" run_compose "main" "status" ;; *) echo "[错误] 'all' 仅支持 start | stop | down | status 操作" exit 1 ;; esac ;; *) echo "[错误] 未知环境目标: ${ENV_TARGET}" show_usage ;; esac