From ac0d3856f0b8bfd27f26ecc98059064c8e24d4d8 Mon Sep 17 00:00:00 2001 From: WangLX <519407855@qq.com> Date: Fri, 7 Aug 2026 12:39:15 +0800 Subject: [PATCH] feat: add reproducible backend deployment --- README.md | 8 +-- compose/.env.example | 28 ++++++++ compose/README.md | 14 ++-- compose/docker-compose.backend.yml | 74 ++++++++++++++++++++ compose/docker-compose.infra.yml | 9 ++- docs/BACKEND-DEPLOYMENT.md | 64 +++++++++++++++++ docs/DEPLOYMENT-RUNBOOK.md | 14 ++-- scripts/build-backend-image.sh | 45 ++++++++++++ scripts/configure-backend-env.sh | 108 +++++++++++++++++++++++++++++ scripts/create-local-env.sh | 28 ++++++++ scripts/verify-backend.sh | 55 +++++++++++++++ 11 files changed, 431 insertions(+), 16 deletions(-) create mode 100644 compose/docker-compose.backend.yml create mode 100644 docs/BACKEND-DEPLOYMENT.md create mode 100755 scripts/build-backend-image.sh create mode 100755 scripts/configure-backend-env.sh create mode 100755 scripts/verify-backend.sh diff --git a/README.md b/README.md index 941cae1..dac38a6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## 从这里开始 -先阅读 [Ubuntu 22.04 本地部署运行手册](docs/DEPLOYMENT-RUNBOOK.md)、[样机硬件评估报告](docs/POC-HARDWARE-BASELINE.md) 和 [源码版本核对](docs/SOURCE-RECONCILIATION.md)。当前可以启动 PostgreSQL、Redis 和 MinIO 基础设施 POC;业务系统仍需完成对象存储、管理端 AI-only 模式和生产镜像改造后再编排。 +先阅读 [Ubuntu 22.04 本地部署运行手册](docs/DEPLOYMENT-RUNBOOK.md)、[后端本地部署](docs/BACKEND-DEPLOYMENT.md)、[样机硬件评估报告](docs/POC-HARDWARE-BASELINE.md) 和 [源码版本核对](docs/SOURCE-RECONCILIATION.md)。当前可以启动 PostgreSQL、Redis、MinIO 和平台后端;平台前端、管理端 AI-only 模式和统一网关仍在后续阶段。 本仓库是 AI SoulMate 私有化部署的编排与留档仓库,用于固定源码版本、记录硬件基线、建设部署配置并沉淀压测结果。业务源码仍保留在各自仓库中,不复制源码、不改写原有 Git 历史。 @@ -32,7 +32,7 @@ - `docs/CROSS-COMPUTER-HANDOFF.md`:换电脑继续工作的状态、重建 Git 和接管清单。 - `decisions/`:影响交付方式的架构决策。 - `inventory/`:参与部署的源码版本清单。 -- `compose/`:后续形成可交付的 Compose 编排;当前仅保留准入说明。 +- `compose/`:基础设施和平台后端 Compose 编排。 - `scripts/`:硬件与运行状态采集脚本。 - `benchmark/`:压测场景和单次运行元数据模板。 - `results/`:压测报告与不入库的原始数据。 @@ -40,8 +40,8 @@ ## 推荐执行顺序 1. 固化样机硬件、网络、系统和源码版本基线。 -2. 完成对象存储抽象、管理端 AI-only 模式和生产镜像改造。 -3. 建设单机 Compose、初始化/迁移、备份恢复和健康检查。 +2. 完成后端对象存储适配、生产镜像和单机 Compose。 +3. 完成平台前端、管理端 AI-only 模式和统一网关。 4. 在样机上完成联网 POC,验证 Tripo 出站访问和全业务链路。 5. 先测本地链路容量,再单独测包含 Tripo 的端到端体验。 6. 增加农场工作站,验证任务下发、断线恢复和打印机适配。 diff --git a/compose/.env.example b/compose/.env.example index 20c5cfa..1b70de2 100644 --- a/compose/.env.example +++ b/compose/.env.example @@ -18,7 +18,35 @@ MINIO_IMAGE=minio/minio:latest MINIO_MC_IMAGE=minio/mc:latest MINIO_ROOT_USER=replace-with-a-unique-admin-name MINIO_ROOT_PASSWORD=replace-with-a-random-value +MINIO_APP_ACCESS_KEY=ai_soulmate_app +MINIO_APP_SECRET_KEY=replace-with-a-random-value MINIO_BUCKET=ai-soulmate +MINIO_API_BIND_ADDRESS=127.0.0.1 MINIO_API_HOST_PORT=9000 MINIO_CONSOLE_HOST_PORT=9001 MINIO_VOLUME_NAME=ai-soulmate-minio-data +MINIO_API_CORS_ALLOW_ORIGIN=http://localhost:8080,http://127.0.0.1:8080 + +BACKEND_COMPOSE_PROJECT_NAME=ai-soulmate-backend +BACKEND_IMAGE=ai-soulmate-backend:replace-with-source-commit +BACKEND_BIND_ADDRESS=127.0.0.1 +BACKEND_HOST_PORT=3001 +BACKEND_MEMORY_LIMIT=4g +BACKEND_CPU_LIMIT=2.0 + +APP_ENV=production +SECRET_KEY=replace-with-a-random-value +ENCRYPTION_KEY=replace-with-a-random-value +ORDER_NO_SECRET=replace-with-a-random-value +CDK_CODE_PEPPER=replace-with-a-random-value +MLKJ_INTEGRATION_KEY=replace-with-a-random-value +MLKJ_INTEGRATION_SECRET=replace-with-a-random-value +PRINT_CLIENT_SHARED_TOKEN=replace-with-a-random-value + +OSS_PUBLIC_ENDPOINT=http://127.0.0.1:9000 +CORS_ALLOWED_ORIGINS=http://localhost:8080,http://127.0.0.1:8080 +AI_SOULMATE_PUBLIC_URL=http://127.0.0.1:8080 +FRONTEND_URL=http://127.0.0.1:8080 +TRIPO3D_API_KEY= +TRIPO3D_BASE_URL=https://api.tripo3d.com/v2/openapi +TRIPO3D_DEFAULT_MODEL_VERSION=v3.1-20260211 diff --git a/compose/README.md b/compose/README.md index 52d613f..77b87d0 100644 --- a/compose/README.md +++ b/compose/README.md @@ -1,6 +1,6 @@ # Compose 状态 -当前只提交基础设施 POC:PostgreSQL、Redis 和 MinIO。它不启动业务前端、后端、管理端或网关,因为这些服务还没有完成本地对象存储、管理端 AI-only 模式和生产镜像改造。 +当前提交基础设施 POC 与平台后端:PostgreSQL、Redis、MinIO 和 FastAPI 后端。平台前端、管理端和网关仍在后续阶段。 ## 运行基础设施 @@ -11,13 +11,17 @@ docker compose --env-file compose/.env -f compose/docker-compose.infra.yml ps ./scripts/verify-infra.sh compose/.env -compose/.env 只放在服务器,已经被 .gitignore 忽略。生成后执行 chmod 600 compose/.env。数据库、Redis 和 MinIO 的宿主机端口只绑定到 127.0.0.1,业务容器接入同一个 Docker 网络后使用服务名访问;不要把这些端口改成 0.0.0.0。 +compose/.env 只放在服务器,已经被 .gitignore 忽略。生成后执行 chmod 600 compose/.env。数据库和 Redis 只绑定到 127.0.0.1;MinIO API 可绑定批准的局域网地址,MinIO 控制台保持回环绑定。业务容器在 Docker 内部网络中通过服务名访问基础设施,不要把端口改成 0.0.0.0。 + +## 运行后端 + +按照 `docs/BACKEND-DEPLOYMENT.md` 生成后端密钥、创建 MinIO 应用账号、构建提交绑定的镜像并执行连接验收。 ## 当前门槛 -- ai-soulmate-server 当前 Dockerfile 只有开发目标,开发 Compose 还使用源码 bind mount,不能作为生产镜像。 -- 平台和管理端仍需把 Alibaba OSS 收敛到可切换的存储接口,并通过本地 S3 兼容服务完成预签名、分片、CORS、权限、备份和恢复测试。 +- 平台后端仍需完成 MinIO 预签名上传、下载和浏览器 CORS 联调。 +- 平台前端仍需生成生产镜像并接入本地后端。 - mlkj-admin 仍需实现 AI SoulMate-only 启动模式,避免初始化无关业务数据库和默认账号。 - nginx-server 的公网路由不能原样复用;完成业务镜像后再使用环境无关的网关模板或客户现有网关。 -上述门槛完成并记录镜像 digest、迁移版本和恢复演练后,才能新增可交付的应用 Compose。 +上述门槛完成并记录镜像 digest、迁移版本和恢复演练后,才能形成完整的可交付应用编排。 diff --git a/compose/docker-compose.backend.yml b/compose/docker-compose.backend.yml new file mode 100644 index 0000000..70094dc --- /dev/null +++ b/compose/docker-compose.backend.yml @@ -0,0 +1,74 @@ +name: ${BACKEND_COMPOSE_PROJECT_NAME:-ai-soulmate-backend} + +services: + backend: + image: ${BACKEND_IMAGE:?BACKEND_IMAGE is required} + restart: unless-stopped + environment: + APP_ENV: ${APP_ENV:-production} + DATABASE_URL: postgresql://${POSTGRES_USER:?POSTGRES_USER is required}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}@postgres:5432/${POSTGRES_DB:?POSTGRES_DB is required} + REDIS_URL: redis://:${REDIS_PASSWORD:?REDIS_PASSWORD is required}@redis:6379/0 + SECRET_KEY: ${SECRET_KEY:?SECRET_KEY is required} + ENCRYPTION_KEY: ${ENCRYPTION_KEY:?ENCRYPTION_KEY is required} + ORDER_NO_SECRET: ${ORDER_NO_SECRET:?ORDER_NO_SECRET is required} + CDK_CODE_PEPPER: ${CDK_CODE_PEPPER:?CDK_CODE_PEPPER is required} + MLKJ_INTEGRATION_KEY: ${MLKJ_INTEGRATION_KEY:?MLKJ_INTEGRATION_KEY is required} + MLKJ_INTEGRATION_SECRET: ${MLKJ_INTEGRATION_SECRET:?MLKJ_INTEGRATION_SECRET is required} + PRINT_CLIENT_SHARED_TOKEN: ${PRINT_CLIENT_SHARED_TOKEN:?PRINT_CLIENT_SHARED_TOKEN is required} + OSS_PROVIDER: minio + OSS_ENDPOINT: http://minio:9000 + OSS_BUCKET_NAME: ${MINIO_BUCKET:?MINIO_BUCKET is required} + OSS_ACCESS_KEY_ID: ${MINIO_APP_ACCESS_KEY:?MINIO_APP_ACCESS_KEY is required} + OSS_ACCESS_KEY_SECRET: ${MINIO_APP_SECRET_KEY:?MINIO_APP_SECRET_KEY is required} + OSS_PUBLIC_ENDPOINT: ${OSS_PUBLIC_ENDPOINT:?OSS_PUBLIC_ENDPOINT is required} + OSS_AUTO_CONFIGURE_CORS: "false" + CORS_ALLOWED_ORIGINS: ${CORS_ALLOWED_ORIGINS:?CORS_ALLOWED_ORIGINS is required} + AI_SOULMATE_PUBLIC_URL: ${AI_SOULMATE_PUBLIC_URL:?AI_SOULMATE_PUBLIC_URL is required} + FRONTEND_URL: ${FRONTEND_URL:?FRONTEND_URL is required} + TRIPO3D_API_KEY: ${TRIPO3D_API_KEY:-} + TRIPO3D_BASE_URL: ${TRIPO3D_BASE_URL:-https://api.tripo3d.com/v2/openapi} + TRIPO3D_DEFAULT_MODEL_VERSION: ${TRIPO3D_DEFAULT_MODEL_VERSION:-v3.1-20260211} + PAYMENT_V2_ENABLED: "false" + ALLOW_DEV_VERIFICATION_CODE_RESPONSE: "false" + UPLOAD_DIR: /app/uploads + TZ: ${TZ:-Asia/Shanghai} + ports: + - "${BACKEND_BIND_ADDRESS:-127.0.0.1}:${BACKEND_HOST_PORT:-3001}:3000" + volumes: + - backend_uploads:/app/uploads + - backend_logs:/app/logs + healthcheck: + test: + - CMD + - python + - -c + - | + import json, urllib.request + result = json.load(urllib.request.urlopen("http://127.0.0.1:3000/api/health", timeout=5)) + assert result.get("status") == "healthy" and result.get("database") == "connected" + interval: 10s + timeout: 6s + retries: 9 + start_period: 30s + mem_limit: ${BACKEND_MEMORY_LIMIT:-4g} + cpus: ${BACKEND_CPU_LIMIT:-2.0} + security_opt: + - no-new-privileges:true + logging: + driver: json-file + options: + max-size: 10m + max-file: "3" + networks: + - internal + +volumes: + backend_uploads: + name: ai-soulmate-backend-uploads + backend_logs: + name: ai-soulmate-backend-logs + +networks: + internal: + name: ${DOCKER_NETWORK_NAME:-ai-soulmate-internal} + external: true diff --git a/compose/docker-compose.infra.yml b/compose/docker-compose.infra.yml index aa76f72..af76739 100644 --- a/compose/docker-compose.infra.yml +++ b/compose/docker-compose.infra.yml @@ -63,9 +63,10 @@ services: environment: MINIO_ROOT_USER: ${MINIO_ROOT_USER:?MINIO_ROOT_USER is required} MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD is required} + MINIO_API_CORS_ALLOW_ORIGIN: ${MINIO_API_CORS_ALLOW_ORIGIN:-http://localhost:8080,http://127.0.0.1:8080} TZ: ${TZ:-Asia/Shanghai} ports: - - "127.0.0.1:${MINIO_API_HOST_PORT:-9000}:9000" + - "${MINIO_API_BIND_ADDRESS:-127.0.0.1}:${MINIO_API_HOST_PORT:-9000}:9000" - "127.0.0.1:${MINIO_CONSOLE_HOST_PORT:-9001}:9001" volumes: - minio_data:/data @@ -95,11 +96,17 @@ services: MINIO_ROOT_USER: ${MINIO_ROOT_USER:?MINIO_ROOT_USER is required} MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD is required} MINIO_BUCKET: ${MINIO_BUCKET:?MINIO_BUCKET is required} + MINIO_APP_ACCESS_KEY: ${MINIO_APP_ACCESS_KEY:?MINIO_APP_ACCESS_KEY is required} + MINIO_APP_SECRET_KEY: ${MINIO_APP_SECRET_KEY:?MINIO_APP_SECRET_KEY is required} entrypoint: ["/bin/sh", "-ec"] command: - | mc alias set local http://minio:9000 "$$MINIO_ROOT_USER" "$$MINIO_ROOT_PASSWORD" mc mb --ignore-existing "local/$$MINIO_BUCKET" + if ! mc admin user info local "$$MINIO_APP_ACCESS_KEY" >/dev/null 2>&1; then + mc admin user add local "$$MINIO_APP_ACCESS_KEY" "$$MINIO_APP_SECRET_KEY" + fi + mc admin policy attach local readwrite --user "$$MINIO_APP_ACCESS_KEY" mem_limit: 256m cpus: 0.5 logging: diff --git a/docs/BACKEND-DEPLOYMENT.md b/docs/BACKEND-DEPLOYMENT.md new file mode 100644 index 0000000..3ba3ba7 --- /dev/null +++ b/docs/BACKEND-DEPLOYMENT.md @@ -0,0 +1,64 @@ +# AI SoulMate 后端本地部署 + +本阶段只启动平台后端。PostgreSQL 和 Redis 继续只监听服务器回环地址;后端也先监听 `127.0.0.1:3001`,等待统一网关接入。MinIO API 监听经批准的局域网地址,以便后续浏览器和农场工作站使用预签名 URL;MinIO 控制台继续只监听 `127.0.0.1:9001`。 + +## 前置条件 + +- 基础设施健康检查已通过。 +- 应用数据库初始化和初始化后备份校验已通过。 +- 源码位于 `/srv/ai-soulmate/sources/ai-soulmate-server`,分支为 `local-deployment`,且没有未提交的受跟踪文件。 +- 局域网地址已经固定或配置 DHCP reservation。地址变化后必须重新生成后端配置并重建相关容器。 + +## 生成配置 + +以下示例中的地址是当前 POC 样机地址: + +```bash +cd /srv/ai-soulmate-local-deployment +./scripts/configure-backend-env.sh \ + compose/.env \ + /srv/ai-soulmate/sources/ai-soulmate-server \ + 192.168.3.102 +``` + +脚本保留已有基础设施密码,补齐后端密钥,文件权限保持为 `600`,并且不会把密钥打印到终端。Tripo Key 默认留空;健康检查不依赖 Tripo,联调 Tripo 前再通过受控方式注入已轮换的新 Key。 + +## 重建 MinIO 配置 + +```bash +docker compose --env-file compose/.env \ + -f compose/docker-compose.infra.yml \ + up -d minio minio-init + +./scripts/verify-infra.sh compose/.env +``` + +该操作不会删除 `ai-soulmate-minio-data` 数据卷。初始化任务会保留 Bucket,并创建无管理权限的应用账号;后端不使用 MinIO 管理员账号。 + +## 构建并启动后端 + +```bash +./scripts/build-backend-image.sh \ + compose/.env \ + /srv/ai-soulmate/sources/ai-soulmate-server + +docker compose --env-file compose/.env \ + -f compose/docker-compose.backend.yml \ + up -d + +./scripts/verify-backend.sh compose/.env +``` + +构建脚本拒绝错误分支、受跟踪源码改动以及与源码提交不一致的镜像标签。后端验证会检查 HTTP 健康状态,以及容器到 PostgreSQL、Redis 和 MinIO Bucket 的实际连接。 + +## 当前网络边界 + +| 服务 | 监听地址 | 当前用途 | +| --- | --- | --- | +| PostgreSQL | `127.0.0.1:5432` | 宿主机维护和 Docker 内部访问 | +| Redis | `127.0.0.1:6379` | 宿主机维护和 Docker 内部访问 | +| 后端 | `127.0.0.1:3001` | 本机验收,后续由网关代理 | +| MinIO API | `192.168.3.102:9000` | 局域网预签名上传和下载 | +| MinIO 控制台 | `127.0.0.1:9001` | 仅 SSH 隧道管理 | + +不要在路由器上为 `3001`、`5432`、`6379`、`9000` 或 `9001` 配置公网端口转发。正式交付应通过统一网关和 TLS 暴露业务入口,并按客户网络划分限制 MinIO API 来源。 diff --git a/docs/DEPLOYMENT-RUNBOOK.md b/docs/DEPLOYMENT-RUNBOOK.md index 37e102e..cb07010 100644 --- a/docs/DEPLOYMENT-RUNBOOK.md +++ b/docs/DEPLOYMENT-RUNBOOK.md @@ -107,13 +107,15 @@ backup_directory="$(printf '%s\n' "$backup_output" | sed -n 's/^Infrastructure b ## 5. 业务部署门槛 -当前 ai-soulmate-server 的 Dockerfile 只有开发目标,开发 Compose 还挂载源码;压缩包中的 ai-soulmate-server-master 没有可验证的 Git 历史。先完成以下改造,再写应用 Compose: +后端本地容器的配置、构建、启动和连接验收见 `docs/BACKEND-DEPLOYMENT.md`。后端先绑定回环地址,数据库和 Redis 不向局域网开放;MinIO API 只绑定批准的局域网地址,控制台保持回环绑定。 -1. 平台和管理端引入统一对象存储接口,同时保留 Alibaba OSS provider,并让 MinIO 等本地 S3 provider 通过预签名上传、下载、分片、CORS、权限、元数据和恢复测试。 -2. mlkj-admin 增加 AI SoulMate-only profile,不能初始化无关业务库、默认账号或固定密码。 -3. 为后端、平台前端和管理端生成生产镜像;禁止源码 bind mount,固定基础镜像版本并记录 digest。 -4. 添加迁移、健康检查、资源上限、日志轮换、备份恢复和回滚流程。 -5. 再决定是否使用 nginx-server。它只承担环境无关的 TLS、路由、上传限制、访问日志和限流;若现场已有企业网关,优先复用企业网关。 +平台后端已有独立镜像、无源码挂载的 Compose、资源上限、日志轮换、健康检查和 MinIO provider。继续完成以下业务门槛: + +1. 验证 MinIO 预签名上传、下载、浏览器 CORS、权限和备份恢复。 +2. 为平台前端生成生产镜像,并与本地后端完成登录和核心业务联调。 +3. 为 mlkj-admin 增加 AI SoulMate-only profile,不能初始化无关业务库、默认账号或固定密码。 +4. 固定后端、平台前端和管理端基础镜像版本,记录最终镜像 digest 和回滚版本。 +5. 部署环境无关的统一网关,或接入客户已有企业网关,由其承担 TLS、路由、上传限制、访问日志和限流。 ## 6. 验收顺序 diff --git a/scripts/build-backend-image.sh b/scripts/build-backend-image.sh new file mode 100755 index 0000000..3ad8c72 --- /dev/null +++ b/scripts/build-backend-image.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +source_directory="${2:-/srv/ai-soulmate/sources/ai-soulmate-server}" + +for required_command in docker git sed; do + if ! command -v "$required_command" >/dev/null 2>&1; then + printf 'Required command is unavailable: %s\n' "$required_command" >&2 + exit 1 + fi +done +if [[ ! -r "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi +if [[ ! -r "$source_directory/backend/Dockerfile" ]]; then + printf 'Backend Dockerfile not found under: %s\n' "$source_directory" >&2 + exit 1 +fi + +source_branch="$(git -C "$source_directory" branch --show-current)" +source_commit="$(git -C "$source_directory" rev-parse HEAD)" +source_short_commit="${source_commit:0:12}" +image="$(sed -n 's/^BACKEND_IMAGE=//p' "$env_file" | head -n 1)" +if [[ "$source_branch" != "local-deployment" ]]; then + printf 'Expected source branch local-deployment, found: %s\n' "$source_branch" >&2 + exit 1 +fi +if [[ -n "$(git -C "$source_directory" status --short --untracked-files=no)" ]]; then + printf 'Tracked source changes are present; refusing to build an uncommitted image.\n' >&2 + exit 1 +fi +if [[ "$image" != "ai-soulmate-backend:${source_short_commit}" ]]; then + printf 'BACKEND_IMAGE does not match source commit; rerun configure-backend-env.sh.\n' >&2 + exit 1 +fi + +docker build \ + --label "org.opencontainers.image.revision=${source_commit}" \ + --tag "$image" \ + "$source_directory/backend" + +docker image inspect "$image" \ + --format 'Backend image built: {{index .RepoTags 0}} id={{.Id}} size={{.Size}}' diff --git a/scripts/configure-backend-env.sh b/scripts/configure-backend-env.sh new file mode 100755 index 0000000..2e04b88 --- /dev/null +++ b/scripts/configure-backend-env.sh @@ -0,0 +1,108 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +source_directory="${2:-/srv/ai-soulmate/sources/ai-soulmate-server}" +public_host="${3:-}" + +for required_command in git grep mktemp openssl; do + if ! command -v "$required_command" >/dev/null 2>&1; then + printf 'Required command is unavailable: %s\n' "$required_command" >&2 + exit 1 + fi +done + +if [[ ! -f "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi +if [[ ! -d "$source_directory/.git" ]]; then + printf 'AI SoulMate source checkout not found: %s\n' "$source_directory" >&2 + exit 1 +fi +if [[ -z "$public_host" || ! "$public_host" =~ ^[A-Za-z0-9.-]+$ ]]; then + printf 'Provide the approved LAN IPv4 address or DNS name as argument 3.\n' >&2 + exit 1 +fi + +source_branch="$(git -C "$source_directory" branch --show-current)" +source_commit="$(git -C "$source_directory" rev-parse HEAD)" +source_short_commit="${source_commit:0:12}" +if [[ "$source_branch" != "local-deployment" ]]; then + printf 'Expected source branch local-deployment, found: %s\n' "$source_branch" >&2 + exit 1 +fi +if [[ -n "$(git -C "$source_directory" status --short --untracked-files=no)" ]]; then + printf 'Tracked source changes are present; commit or discard them before building.\n' >&2 + exit 1 +fi + +env_value() { + local key="$1" + sed -n "s/^${key}=//p" "$env_file" | head -n 1 +} + +set_value() { + local key="$1" + local value="$2" + local temporary_file + temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")" + awk -v key="$key" -v value="$value" ' + BEGIN { prefix = key "="; found = 0 } + index($0, prefix) == 1 { + if (!found) print prefix value + found = 1 + next + } + { print } + END { if (!found) print prefix value } + ' "$env_file" > "$temporary_file" + chmod 600 "$temporary_file" + mv -f "$temporary_file" "$env_file" +} + +ensure_secret() { + local key="$1" + if [[ -z "$(env_value "$key")" ]]; then + set_value "$key" "$(openssl rand -hex 32)" + fi +} + +set_value MINIO_API_BIND_ADDRESS "$public_host" +set_value MINIO_API_CORS_ALLOW_ORIGIN "http://${public_host},http://${public_host}:8080,http://${public_host}:8081,http://localhost:8080,http://127.0.0.1:8080" +set_value MINIO_APP_ACCESS_KEY ai_soulmate_app +ensure_secret MINIO_APP_SECRET_KEY + +set_value BACKEND_COMPOSE_PROJECT_NAME ai-soulmate-backend +set_value BACKEND_IMAGE "ai-soulmate-backend:${source_short_commit}" +set_value BACKEND_BIND_ADDRESS 127.0.0.1 +set_value BACKEND_HOST_PORT 3001 +set_value BACKEND_MEMORY_LIMIT 4g +set_value BACKEND_CPU_LIMIT 2.0 + +set_value APP_ENV production +ensure_secret SECRET_KEY +ensure_secret ENCRYPTION_KEY +ensure_secret ORDER_NO_SECRET +ensure_secret CDK_CODE_PEPPER +ensure_secret MLKJ_INTEGRATION_KEY +ensure_secret MLKJ_INTEGRATION_SECRET +ensure_secret PRINT_CLIENT_SHARED_TOKEN + +set_value OSS_PUBLIC_ENDPOINT "http://${public_host}:9000" +set_value CORS_ALLOWED_ORIGINS "http://${public_host},http://${public_host}:8080,http://${public_host}:8081,http://localhost:8080,http://127.0.0.1:8080" +set_value AI_SOULMATE_PUBLIC_URL "http://${public_host}:8080" +set_value FRONTEND_URL "http://${public_host}:8080" +if ! grep -q '^TRIPO3D_API_KEY=' "$env_file"; then + set_value TRIPO3D_API_KEY "" +fi +set_value TRIPO3D_BASE_URL https://api.tripo3d.com/v2/openapi +set_value TRIPO3D_DEFAULT_MODEL_VERSION v3.1-20260211 + +chmod 600 "$env_file" +printf 'Backend environment configured without displaying secrets.\n' +printf 'Source commit: %s\n' "$source_commit" +printf 'Backend image: ai-soulmate-backend:%s\n' "$source_short_commit" +printf 'Backend bind: 127.0.0.1:3001\n' +printf 'MinIO API bind: %s:9000\n' "$public_host" +printf 'MinIO console remains bound to 127.0.0.1.\n' diff --git a/scripts/create-local-env.sh b/scripts/create-local-env.sh index 296e2f8..52b517d 100755 --- a/scripts/create-local-env.sh +++ b/scripts/create-local-env.sh @@ -43,10 +43,38 @@ MINIO_IMAGE=minio/minio:latest MINIO_MC_IMAGE=minio/mc:latest MINIO_ROOT_USER=ai_soulmate_admin MINIO_ROOT_PASSWORD=$(random_secret) +MINIO_APP_ACCESS_KEY=ai_soulmate_app +MINIO_APP_SECRET_KEY=$(random_secret) MINIO_BUCKET=ai-soulmate +MINIO_API_BIND_ADDRESS=127.0.0.1 MINIO_API_HOST_PORT=9000 MINIO_CONSOLE_HOST_PORT=9001 MINIO_VOLUME_NAME=ai-soulmate-minio-data +MINIO_API_CORS_ALLOW_ORIGIN=http://localhost:8080,http://127.0.0.1:8080 + +BACKEND_COMPOSE_PROJECT_NAME=ai-soulmate-backend +BACKEND_IMAGE=ai-soulmate-backend:local-deployment +BACKEND_BIND_ADDRESS=127.0.0.1 +BACKEND_HOST_PORT=3001 +BACKEND_MEMORY_LIMIT=4g +BACKEND_CPU_LIMIT=2.0 + +APP_ENV=production +SECRET_KEY=$(random_secret) +ENCRYPTION_KEY=$(random_secret) +ORDER_NO_SECRET=$(random_secret) +CDK_CODE_PEPPER=$(random_secret) +MLKJ_INTEGRATION_KEY=$(random_secret) +MLKJ_INTEGRATION_SECRET=$(random_secret) +PRINT_CLIENT_SHARED_TOKEN=$(random_secret) + +OSS_PUBLIC_ENDPOINT=http://127.0.0.1:9000 +CORS_ALLOWED_ORIGINS=http://localhost:8080,http://127.0.0.1:8080 +AI_SOULMATE_PUBLIC_URL=http://127.0.0.1:8080 +FRONTEND_URL=http://127.0.0.1:8080 +TRIPO3D_API_KEY= +TRIPO3D_BASE_URL=https://api.tripo3d.com/v2/openapi +TRIPO3D_DEFAULT_MODEL_VERSION=v3.1-20260211 EOF chmod 600 "$target_path" diff --git a/scripts/verify-backend.sh b/scripts/verify-backend.sh new file mode 100755 index 0000000..90872d3 --- /dev/null +++ b/scripts/verify-backend.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +compose_file="${BACKEND_COMPOSE_FILE:-compose/docker-compose.backend.yml}" + +for required_command in curl docker jq sed; do + if ! command -v "$required_command" >/dev/null 2>&1; then + printf 'Required command is unavailable: %s\n' "$required_command" >&2 + exit 1 + fi +done +if [[ ! -r "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi + +env_value() { + sed -n "s/^${1}=//p" "$env_file" | head -n 1 +} + +backend_bind_address="$(env_value BACKEND_BIND_ADDRESS)" +backend_host_port="$(env_value BACKEND_HOST_PORT)" +oss_public_endpoint="$(env_value OSS_PUBLIC_ENDPOINT)" +compose_command=(docker compose --env-file "$env_file" -f "$compose_file") + +"${compose_command[@]}" config --quiet + +health_url="http://${backend_bind_address}:${backend_host_port}/api/health" +health_json="" +for _attempt in $(seq 1 18); do + if health_json="$(curl --fail --silent --show-error --max-time 5 "$health_url" 2>/dev/null)"; then + if printf '%s' "$health_json" | jq -e '.status == "healthy" and .database == "connected"' >/dev/null; then + break + fi + fi + health_json="" + sleep 5 +done +if [[ -z "$health_json" ]]; then + printf 'Backend health check did not become healthy: %s\n' "$health_url" >&2 + "${compose_command[@]}" ps >&2 + exit 1 +fi + +"${compose_command[@]}" exec -T backend python -c \ + 'import os, redis; assert redis.from_url(os.environ["REDIS_URL"]).ping()' +"${compose_command[@]}" exec -T backend python -c \ + 'import os; from urllib.parse import urlsplit; from minio import Minio; endpoint=urlsplit(os.environ["OSS_ENDPOINT"]); client=Minio(endpoint.netloc, access_key=os.environ["OSS_ACCESS_KEY_ID"], secret_key=os.environ["OSS_ACCESS_KEY_SECRET"], secure=endpoint.scheme == "https"); assert client.bucket_exists(os.environ["OSS_BUCKET_NAME"])' +curl --fail --silent --show-error --max-time 5 \ + "${oss_public_endpoint}/minio/health/live" >/dev/null + +printf '%s\n' "$health_json" | jq . +"${compose_command[@]}" ps +printf 'Backend, PostgreSQL, Redis, and MinIO connectivity checks passed.\n'