diff --git a/README.md b/README.md index 9f17ff3..7e4eb57 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## 从这里开始 -先阅读 [Ubuntu 22.04 本地部署运行手册](docs/DEPLOYMENT-RUNBOOK.md)、[后端本地部署](docs/BACKEND-DEPLOYMENT.md)、[平台前端本地部署](docs/FRONTEND-DEPLOYMENT.md)、[样机硬件评估报告](docs/POC-HARDWARE-BASELINE.md) 和 [源码版本核对](docs/SOURCE-RECONCILIATION.md)。当前可以启动 PostgreSQL、Redis、MinIO、平台后端和平台前端;管理端 AI-only 模式和统一网关仍在后续阶段。 +先阅读 [Ubuntu 22.04 本地部署运行手册](docs/DEPLOYMENT-RUNBOOK.md)、[后端本地部署](docs/BACKEND-DEPLOYMENT.md)、[平台前端本地部署](docs/FRONTEND-DEPLOYMENT.md)、[管理端本地部署](docs/ADMIN-DEPLOYMENT.md)、[样机硬件评估报告](docs/POC-HARDWARE-BASELINE.md) 和 [源码版本核对](docs/SOURCE-RECONCILIATION.md)。当前可以启动 PostgreSQL、Redis、MinIO、平台后端、平台前端和 AI SoulMate 管理端;统一网关仍在后续阶段。 本仓库是 AI SoulMate 私有化部署的编排与留档仓库,用于固定源码版本、记录硬件基线、建设部署配置并沉淀压测结果。业务源码仍保留在各自仓库中,不复制源码、不改写原有 Git 历史。 diff --git a/compose/.env.example b/compose/.env.example index ab7bab7..d70b37f 100644 --- a/compose/.env.example +++ b/compose/.env.example @@ -41,6 +41,25 @@ FRONTEND_HOST_PORT=8080 FRONTEND_MEMORY_LIMIT=1g FRONTEND_CPU_LIMIT=1.0 +ADMIN_COMPOSE_PROJECT_NAME=ai-soulmate-admin +ADMIN_BACKEND_IMAGE=ai-soulmate-admin-backend:replace-with-source-commit +ADMIN_FRONTEND_IMAGE=ai-soulmate-admin-frontend:replace-with-source-commit +ADMIN_POSTGRES_DB=mlkj_admin +ADMIN_BACKEND_BIND_ADDRESS=127.0.0.1 +ADMIN_BACKEND_HOST_PORT=8088 +ADMIN_FRONTEND_BIND_ADDRESS=127.0.0.1 +ADMIN_FRONTEND_HOST_PORT=8081 +ADMIN_BACKEND_MEMORY_LIMIT=2g +ADMIN_BACKEND_CPU_LIMIT=1.5 +ADMIN_FRONTEND_MEMORY_LIMIT=512m +ADMIN_FRONTEND_CPU_LIMIT=0.5 +ADMIN_INITIAL_USERNAME=localadmin +ADMIN_INITIAL_PASSWORD= +ADMIN_JWT_SECRET=replace-with-a-random-value +ADMIN_MAVEN_IMAGE=maven:3.9-eclipse-temurin-21 +ADMIN_JRE_IMAGE=eclipse-temurin:21-jre +ADMIN_NODE_IMAGE=node:22-bookworm-slim + APP_ENV=production SECRET_KEY=replace-with-a-random-value ENCRYPTION_KEY=replace-with-a-random-value diff --git a/compose/README.md b/compose/README.md index 7b52092..8d57e2b 100644 --- a/compose/README.md +++ b/compose/README.md @@ -1,6 +1,6 @@ # Compose 状态 -当前提交基础设施 POC 与平台应用:PostgreSQL、Redis、MinIO、FastAPI 后端和 Vue 平台前端。管理端和网关仍在后续阶段。 +当前提交基础设施 POC 与平台应用:PostgreSQL、Redis、MinIO、FastAPI 后端、Vue 平台前端和 AI SoulMate 本地管理端。统一网关仍在后续阶段。 ## 运行基础设施 @@ -21,11 +21,15 @@ compose/.env 只放在服务器,已经被 .gitignore 忽略。生成后执行 按照 `docs/FRONTEND-DEPLOYMENT.md` 构建提交绑定的前端镜像,并验证静态页面与同源后端代理。 +## 运行管理端 + +按照 `docs/ADMIN-DEPLOYMENT.md` 创建独立的本地管理库,构建 `mlkj-admin` 的 `local-deployment` 分支并验证 AI SoulMate-only 菜单与本地数据连接。 + ## 当前门槛 - 平台后端仍需完成 MinIO 预签名上传、下载和浏览器 CORS 联调。 - 平台前端仍需完成浏览器登录、预签名对象上传和主要页面联调。 -- mlkj-admin 仍需实现 AI SoulMate-only 启动模式,避免初始化无关业务数据库和默认账号。 +- 管理端仍需完成浏览器登录以及用户、订单、资产、CDK 和模型归档的业务联调。 - nginx-server 的公网路由不能原样复用;完成业务镜像后再使用环境无关的网关模板或客户现有网关。 上述门槛完成并记录镜像 digest、迁移版本和恢复演练后,才能形成完整的可交付应用编排。 diff --git a/compose/docker-compose.admin.yml b/compose/docker-compose.admin.yml new file mode 100644 index 0000000..16f5c97 --- /dev/null +++ b/compose/docker-compose.admin.yml @@ -0,0 +1,82 @@ +name: ${ADMIN_COMPOSE_PROJECT_NAME:-ai-soulmate-admin} + +services: + admin-backend: + image: ${ADMIN_BACKEND_IMAGE:?ADMIN_BACKEND_IMAGE is required} + restart: unless-stopped + environment: + POLY_ADMIN_BACKEND_PORT: "8088" + JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false + POLY_ADMIN_JDBC_URL: jdbc:postgresql://postgres:5432/${ADMIN_POSTGRES_DB:?ADMIN_POSTGRES_DB is required}?currentSchema=public + POLY_ADMIN_DB_USER: ${POSTGRES_USER:?POSTGRES_USER is required} + POLY_ADMIN_DB_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required} + AI_SOULMATE_JDBC_URL: jdbc:postgresql://postgres:5432/${POSTGRES_DB:?POSTGRES_DB is required}?currentSchema=public + AI_SOULMATE_DB_USER: ${POSTGRES_USER:?POSTGRES_USER is required} + AI_SOULMATE_DB_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required} + MLKJ_JDBC_URL: jdbc:postgresql://postgres:5432/${ADMIN_POSTGRES_DB:?ADMIN_POSTGRES_DB is required}?currentSchema=public + MLKJ_DB_USER: ${POSTGRES_USER:?POSTGRES_USER is required} + MLKJ_DB_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required} + BCJH_JDBC_URL: jdbc:postgresql://postgres:5432/${ADMIN_POSTGRES_DB:?ADMIN_POSTGRES_DB is required}?currentSchema=public + BCJH_DB_USER: ${POSTGRES_USER:?POSTGRES_USER is required} + BCJH_DB_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required} + POLY_ADMIN_DB_POOL_MAX_SIZE: "3" + AI_SOULMATE_DB_POOL_MAX_SIZE: "3" + MLKJ_DB_POOL_MAX_SIZE: "1" + BCJH_DB_POOL_MAX_SIZE: "1" + POLY_ADMIN_DB_POOL_MIN_IDLE: "0" + POLY_ADMIN_DB_POOL_CONNECTION_TIMEOUT_MS: "5000" + POLY_ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET:?ADMIN_JWT_SECRET is required} + POLY_ADMIN_JWT_EXPIRES_SECONDS: "86400" + POLY_ADMIN_INITIAL_USERNAME: ${ADMIN_INITIAL_USERNAME:?ADMIN_INITIAL_USERNAME is required} + POLY_ADMIN_INITIAL_PASSWORD: ${ADMIN_INITIAL_PASSWORD:-} + AI_SOULMATE_CDK_CODE_PEPPER: ${CDK_CODE_PEPPER:?CDK_CODE_PEPPER is required} + AI_SOULMATE_ADMIN_API_BASE: http://backend:3000 + AI_SOULMATE_STORAGE_PROVIDER: minio + AI_SOULMATE_STORAGE_ENDPOINT: http://minio:9000 + AI_SOULMATE_STORAGE_BUCKET: ${MINIO_BUCKET:?MINIO_BUCKET is required} + AI_SOULMATE_STORAGE_ACCESS_KEY: ${MINIO_APP_ACCESS_KEY:?MINIO_APP_ACCESS_KEY is required} + AI_SOULMATE_STORAGE_SECRET_KEY: ${MINIO_APP_SECRET_KEY:?MINIO_APP_SECRET_KEY is required} + AI_SOULMATE_MODEL_DOWNLOAD_ALLOWED_HOSTS: "" + POLY_TRAFFIC_INGEST_ENABLED: "false" + POLY_RUNTIME_LOGS_ENABLED: "false" + TZ: ${TZ:-Asia/Shanghai} + ports: + - "${ADMIN_BACKEND_BIND_ADDRESS:-127.0.0.1}:${ADMIN_BACKEND_HOST_PORT:-8088}:8088" + mem_limit: ${ADMIN_BACKEND_MEMORY_LIMIT:-2g} + cpus: ${ADMIN_BACKEND_CPU_LIMIT:-1.5} + security_opt: + - no-new-privileges:true + logging: + driver: json-file + options: + max-size: 10m + max-file: "3" + networks: + - internal + + admin-frontend: + image: ${ADMIN_FRONTEND_IMAGE:?ADMIN_FRONTEND_IMAGE is required} + restart: unless-stopped + environment: + FRONTEND_PORT: "9527" + API_TARGET: http://admin-backend:8088 + ports: + - "${ADMIN_FRONTEND_BIND_ADDRESS:?ADMIN_FRONTEND_BIND_ADDRESS is required}:${ADMIN_FRONTEND_HOST_PORT:-8081}:9527" + depends_on: + - admin-backend + mem_limit: ${ADMIN_FRONTEND_MEMORY_LIMIT:-512m} + cpus: ${ADMIN_FRONTEND_CPU_LIMIT:-0.5} + security_opt: + - no-new-privileges:true + logging: + driver: json-file + options: + max-size: 10m + max-file: "3" + networks: + - internal + +networks: + internal: + name: ${DOCKER_NETWORK_NAME:-ai-soulmate-internal} + external: true diff --git a/docs/ADMIN-DEPLOYMENT.md b/docs/ADMIN-DEPLOYMENT.md new file mode 100644 index 0000000..7ad591c --- /dev/null +++ b/docs/ADMIN-DEPLOYMENT.md @@ -0,0 +1,20 @@ +# AI SoulMate 管理端本地部署 + +本地管理端只展示 `AI_SOULMATE` 与基础 `CONSOLE`,不连接官网或壁材京华云数据库。管理端自身数据使用同一 PostgreSQL 实例中的独立 `mlkj_admin` 数据库;AI SoulMate 业务查询连接本地 `ai_soulmate` 数据库;模型归档读取本地 MinIO。 + +## 源码 + +源码必须使用 `mlkj-admin` 仓库的 `local-deployment` 分支。该分支增加 MinIO 客户端和本地菜单配置,不修改远程 `main`。 + +## 顺序 + +1. 运行 `configure-admin-env.sh` 写入提交绑定镜像名与本地监听地址。 +2. 运行 `set-admin-credentials.sh`,在终端中无回显设置初始管理员密码。 +3. 运行 `initialize-admin-database.sh` 创建并初始化本地 `mlkj_admin` 数据库。 +4. 运行 `build-admin-images.sh` 构建 Java 后端与 Node 前端镜像。 +5. 运行 `start-admin.sh` 和 `verify-admin.sh`。 +6. 验收成功后运行 `clear-admin-bootstrap-password.sh`,从环境文件和容器中移除一次性初始密码。 + +管理端前端默认监听局域网 `8081`,Java 后端只绑定 `127.0.0.1:8088`。不要把 PostgreSQL、管理端 Java 端口或 MinIO 控制台暴露到路由器公网。 + +初始管理员只在数据库不存在 `SUPER_ADMIN` 时创建。部署完成后不要通过修改环境变量来改密码,应登录管理端使用管理员功能完成账号治理。 diff --git a/scripts/backup-infra.sh b/scripts/backup-infra.sh index c1faaee..5385a6a 100755 --- a/scripts/backup-infra.sh +++ b/scripts/backup-infra.sh @@ -28,6 +28,10 @@ env_value() { printf '%s' "$value" } +env_optional_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + compose_command=(docker compose --env-file "$env_file" -f "$compose_file") "${compose_command[@]}" config --quiet @@ -56,6 +60,26 @@ mkdir -p "$staging_directory/minio" 'exec pg_dump --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --format=custom --compress=6 --no-owner --no-acl' \ > "$staging_directory/postgres.dump" +postgres_user="$(env_value POSTGRES_USER)" +admin_database="$(env_optional_value ADMIN_POSTGRES_DB)" +admin_artifact="null" +if [[ -n "$admin_database" ]]; then + if [[ ! "$admin_database" =~ ^[A-Za-z][A-Za-z0-9_]{0,62}$ ]]; then + printf 'ADMIN_POSTGRES_DB is not a safe PostgreSQL identifier.\n' >&2 + exit 1 + fi + admin_database_exists="$("${compose_command[@]}" exec -T postgres psql \ + --username "$postgres_user" --dbname postgres --tuples-only --no-align \ + --command "SELECT 1 FROM pg_database WHERE datname = '$admin_database'")" + if [[ "$admin_database_exists" == "1" ]]; then + "${compose_command[@]}" exec -T postgres pg_dump \ + --username "$postgres_user" --dbname "$admin_database" \ + --format=custom --compress=6 --no-owner --no-acl \ + > "$staging_directory/admin-postgres.dump" + admin_artifact='"admin-postgres.dump"' + fi +fi + "${compose_command[@]}" exec -T redis sh -ec \ 'redis-cli --no-auth-warning -a "$REDIS_PASSWORD" SAVE >/dev/null' redis_container_id="$("${compose_command[@]}" ps -q redis)" @@ -84,12 +108,13 @@ fi minio_object_count="$(find "$staging_directory/minio" -type f | wc -l | tr -d '[:space:]')" cat > "$staging_directory/manifest.yml" < "$staging_directory/checksums.sha256" diff --git a/scripts/build-admin-images.sh b/scripts/build-admin-images.sh new file mode 100755 index 0000000..e9259c8 --- /dev/null +++ b/scripts/build-admin-images.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}" + +for required_command in awk docker git; 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" || ! -r "$source_directory/backend-java/Dockerfile" || ! -r "$source_directory/frontend/Dockerfile" ]]; then + printf 'Admin environment or source Dockerfiles are unavailable.\n' >&2 + exit 1 +fi + +env_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + +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}" +backend_image="$(env_value ADMIN_BACKEND_IMAGE)" +frontend_image="$(env_value ADMIN_FRONTEND_IMAGE)" +if [[ "$source_branch" != "local-deployment" ]]; then + printf 'Expected mlkj-admin 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 mlkj-admin source changes are present; refusing an uncommitted build.\n' >&2 + exit 1 +fi +if [[ "$backend_image" != "ai-soulmate-admin-backend:${source_short_commit}" || "$frontend_image" != "ai-soulmate-admin-frontend:${source_short_commit}" ]]; then + printf 'Admin image tags do not match source commit; rerun configure-admin-env.sh.\n' >&2 + exit 1 +fi + +docker build \ + --build-context "maven-local-repository=$source_directory/.empty-maven-repository" \ + --build-arg "MAVEN_IMAGE=$(env_value ADMIN_MAVEN_IMAGE)" \ + --build-arg "JRE_IMAGE=$(env_value ADMIN_JRE_IMAGE)" \ + --target runtime \ + --label "org.opencontainers.image.revision=${source_commit}" \ + --file "$source_directory/backend-java/Dockerfile" \ + --tag "$backend_image" \ + "$source_directory" + +docker build \ + --build-arg "NODE_IMAGE=$(env_value ADMIN_NODE_IMAGE)" \ + --build-arg ADMIN_MODULE_CONFIG=module-config.local.json \ + --target runtime \ + --label "org.opencontainers.image.revision=${source_commit}" \ + --tag "$frontend_image" \ + "$source_directory/frontend" + +docker image inspect "$backend_image" "$frontend_image" \ + --format 'Admin image built: {{index .RepoTags 0}} id={{.Id}} size={{.Size}} revision={{index .Config.Labels "org.opencontainers.image.revision"}}' diff --git a/scripts/clear-admin-bootstrap-password.sh b/scripts/clear-admin-bootstrap-password.sh new file mode 100755 index 0000000..ba8fc94 --- /dev/null +++ b/scripts/clear-admin-bootstrap-password.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +admin_compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}" +infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" + +if [[ ! -r "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi +env_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + +infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file") +super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \ + --username "$(env_value POSTGRES_USER)" --dbname "$(env_value ADMIN_POSTGRES_DB)" \ + --tuples-only --no-align \ + --command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'")" +if [[ "$super_admin_count" != "1" ]]; then + printf 'Refusing to clear bootstrap password; expected one SUPER_ADMIN, found: %s\n' "$super_admin_count" >&2 + exit 1 +fi + +temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")" +awk ' + BEGIN { found = 0 } + index($0, "ADMIN_INITIAL_PASSWORD=") == 1 { + if (!found) print "ADMIN_INITIAL_PASSWORD=" + found = 1 + next + } + { print } + END { if (!found) print "ADMIN_INITIAL_PASSWORD=" } +' "$env_file" > "$temporary_file" +chmod 600 "$temporary_file" +mv -f "$temporary_file" "$env_file" + +project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)" +admin_compose=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$admin_compose_file") +"${admin_compose[@]}" config --quiet +"${admin_compose[@]}" up -d --force-recreate admin-backend +printf 'Admin bootstrap password cleared from compose/.env and backend container environment.\n' diff --git a/scripts/configure-admin-env.sh b/scripts/configure-admin-env.sh new file mode 100755 index 0000000..6893eec --- /dev/null +++ b/scripts/configure-admin-env.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}" +public_host="${3:-}" + +for required_command in awk git 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 [[ ! -r "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi +if [[ ! -d "$source_directory/.git" ]]; then + printf 'mlkj-admin 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 mlkj-admin 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 mlkj-admin source changes are present.\n' >&2 + exit 1 +fi + +env_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + +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() { + if [[ -z "$(env_value "$1")" ]]; then + set_value "$1" "$(openssl rand -hex 32)" + fi +} + +set_value ADMIN_COMPOSE_PROJECT_NAME ai-soulmate-admin +set_value ADMIN_BACKEND_IMAGE "ai-soulmate-admin-backend:${source_short_commit}" +set_value ADMIN_FRONTEND_IMAGE "ai-soulmate-admin-frontend:${source_short_commit}" +set_value ADMIN_POSTGRES_DB mlkj_admin +set_value ADMIN_BACKEND_BIND_ADDRESS 127.0.0.1 +set_value ADMIN_BACKEND_HOST_PORT 8088 +set_value ADMIN_FRONTEND_BIND_ADDRESS "$public_host" +set_value ADMIN_FRONTEND_HOST_PORT 8081 +set_value ADMIN_BACKEND_MEMORY_LIMIT 2g +set_value ADMIN_BACKEND_CPU_LIMIT 1.5 +set_value ADMIN_FRONTEND_MEMORY_LIMIT 512m +set_value ADMIN_FRONTEND_CPU_LIMIT 0.5 +if [[ -z "$(env_value ADMIN_INITIAL_USERNAME)" ]]; then + set_value ADMIN_INITIAL_USERNAME localadmin +fi +if ! grep -q '^ADMIN_INITIAL_PASSWORD=' "$env_file"; then + set_value ADMIN_INITIAL_PASSWORD "" +fi +ensure_secret ADMIN_JWT_SECRET +set_value ADMIN_MAVEN_IMAGE maven:3.9-eclipse-temurin-21 +set_value ADMIN_JRE_IMAGE eclipse-temurin:21-jre +set_value ADMIN_NODE_IMAGE node:22-bookworm-slim + +chmod 600 "$env_file" +printf 'Admin environment configured without displaying secrets.\n' +printf 'Source commit: %s\n' "$source_commit" +printf 'Admin backend image: ai-soulmate-admin-backend:%s\n' "$source_short_commit" +printf 'Admin frontend image: ai-soulmate-admin-frontend:%s\n' "$source_short_commit" +printf 'Admin frontend bind: %s:8081\n' "$public_host" +printf 'Admin backend remains bound to 127.0.0.1:8088.\n' diff --git a/scripts/create-local-env.sh b/scripts/create-local-env.sh index c3635b0..bd5b60d 100755 --- a/scripts/create-local-env.sh +++ b/scripts/create-local-env.sh @@ -66,6 +66,25 @@ FRONTEND_HOST_PORT=8080 FRONTEND_MEMORY_LIMIT=1g FRONTEND_CPU_LIMIT=1.0 +ADMIN_COMPOSE_PROJECT_NAME=ai-soulmate-admin +ADMIN_BACKEND_IMAGE=ai-soulmate-admin-backend:local-deployment +ADMIN_FRONTEND_IMAGE=ai-soulmate-admin-frontend:local-deployment +ADMIN_POSTGRES_DB=mlkj_admin +ADMIN_BACKEND_BIND_ADDRESS=127.0.0.1 +ADMIN_BACKEND_HOST_PORT=8088 +ADMIN_FRONTEND_BIND_ADDRESS=127.0.0.1 +ADMIN_FRONTEND_HOST_PORT=8081 +ADMIN_BACKEND_MEMORY_LIMIT=2g +ADMIN_BACKEND_CPU_LIMIT=1.5 +ADMIN_FRONTEND_MEMORY_LIMIT=512m +ADMIN_FRONTEND_CPU_LIMIT=0.5 +ADMIN_INITIAL_USERNAME=localadmin +ADMIN_INITIAL_PASSWORD= +ADMIN_JWT_SECRET=$(random_secret) +ADMIN_MAVEN_IMAGE=maven:3.9-eclipse-temurin-21 +ADMIN_JRE_IMAGE=eclipse-temurin:21-jre +ADMIN_NODE_IMAGE=node:22-bookworm-slim + APP_ENV=production SECRET_KEY=$(random_secret) ENCRYPTION_KEY=$(random_secret) diff --git a/scripts/initialize-admin-database.sh b/scripts/initialize-admin-database.sh new file mode 100755 index 0000000..36b6db8 --- /dev/null +++ b/scripts/initialize-admin-database.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}" +compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" + +for required_command in awk docker git; 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 [[ ! -d "$source_directory/.git" ]]; then + printf 'mlkj-admin source checkout not found: %s\n' "$source_directory" >&2 + exit 1 +fi + +env_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + +postgres_user="$(env_value POSTGRES_USER)" +admin_database="$(env_value ADMIN_POSTGRES_DB)" +source_commit="$(git -C "$source_directory" rev-parse HEAD)" +if [[ ! "$admin_database" =~ ^[A-Za-z][A-Za-z0-9_]{0,62}$ ]]; then + printf 'ADMIN_POSTGRES_DB is not a safe PostgreSQL identifier.\n' >&2 + exit 1 +fi +if [[ "$(git -C "$source_directory" branch --show-current)" != "local-deployment" ]]; then + printf 'Expected mlkj-admin branch local-deployment.\n' >&2 + exit 1 +fi + +schema_files=( + "$source_directory/db/cloud/02_create_poly_admin_schema.sql" + "$source_directory/db/cloud/04_add_single_login_session.sql" + "$source_directory/db/cloud/05_create_poly_traffic_analytics.sql" + "$source_directory/db/cloud/06_backfill_poly_traffic_request_uri.sql" +) +for schema_file in "${schema_files[@]}"; do + if [[ ! -r "$schema_file" ]]; then + printf 'Admin schema file is missing: %s\n' "$schema_file" >&2 + exit 1 + fi +done + +compose_command=(docker compose --env-file "$env_file" -f "$compose_file") +"${compose_command[@]}" config --quiet +if [[ -z "$("${compose_command[@]}" ps -q postgres)" ]]; then + printf 'PostgreSQL is not running.\n' >&2 + exit 1 +fi + +database_exists="$("${compose_command[@]}" exec -T postgres psql \ + --username "$postgres_user" --dbname postgres --tuples-only --no-align \ + --command "SELECT 1 FROM pg_database WHERE datname = '$admin_database'")" +if [[ "$database_exists" != "1" ]]; then + "${compose_command[@]}" exec -T postgres createdb \ + --username "$postgres_user" --owner "$postgres_user" --encoding UTF8 "$admin_database" +fi + +for schema_file in "${schema_files[@]}"; do + printf 'Applying admin schema: %s\n' "$(basename "$schema_file")" + "${compose_command[@]}" exec -T postgres psql \ + --username "$postgres_user" --dbname "$admin_database" \ + --set ON_ERROR_STOP=1 < "$schema_file" +done + +"${compose_command[@]}" exec -T postgres psql \ + --username "$postgres_user" --dbname "$admin_database" \ + --set ON_ERROR_STOP=1 --set source_commit="$source_commit" <<'SQL' +CREATE TABLE IF NOT EXISTS local_deployment_schema_history ( + source_commit text PRIMARY KEY, + applied_at timestamptz NOT NULL DEFAULT now() +); +INSERT INTO local_deployment_schema_history(source_commit) +VALUES (:'source_commit') +ON CONFLICT (source_commit) DO NOTHING; +SQL + +printf 'Local admin database initialized: %s at source %s\n' "$admin_database" "$source_commit" diff --git a/scripts/set-admin-credentials.sh b/scripts/set-admin-credentials.sh new file mode 100755 index 0000000..9f18967 --- /dev/null +++ b/scripts/set-admin-credentials.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +if [[ ! -r "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi +if [[ ! -t 0 ]]; then + printf 'Run this script from an interactive terminal.\n' >&2 + exit 1 +fi + +current_username="$(awk -F= '$1 == "ADMIN_INITIAL_USERNAME" {sub(/^[^=]*=/, ""); print; exit}' "$env_file")" +read -r -p "Initial admin username [${current_username:-localadmin}]: " username +username="${username:-${current_username:-localadmin}}" +if [[ ! "$username" =~ ^[A-Za-z0-9_.-]{3,80}$ ]]; then + printf 'Username must be 3-80 characters using letters, numbers, dot, underscore, or hyphen.\n' >&2 + exit 1 +fi + +read -r -s -p 'Initial admin password (at least 16 letters/numbers/_/-): ' password +printf '\n' +read -r -s -p 'Confirm initial admin password: ' password_confirmation +printf '\n' +if [[ "$password" != "$password_confirmation" ]]; then + printf 'Passwords do not match.\n' >&2 + exit 1 +fi +if [[ ${#password} -lt 16 || ! "$password" =~ ^[A-Za-z0-9_-]+$ ]]; then + printf 'Password must be at least 16 characters using only letters, numbers, underscore, or hyphen.\n' >&2 + exit 1 +fi + +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" +} + +set_value ADMIN_INITIAL_USERNAME "$username" +set_value ADMIN_INITIAL_PASSWORD "$password" +unset password password_confirmation +chmod 600 "$env_file" +printf 'Initial admin credentials stored without displaying the password.\n' diff --git a/scripts/start-admin.sh b/scripts/start-admin.sh new file mode 100755 index 0000000..d2bcaad --- /dev/null +++ b/scripts/start-admin.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}" +infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" + +if [[ ! -r "$env_file" ]]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi +env_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + +project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)" +backend_image="$(env_value ADMIN_BACKEND_IMAGE)" +frontend_image="$(env_value ADMIN_FRONTEND_IMAGE)" +if [[ -z "$(env_value ADMIN_INITIAL_PASSWORD)" ]]; then + infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file") + super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \ + --username "$(env_value POSTGRES_USER)" --dbname "$(env_value ADMIN_POSTGRES_DB)" \ + --tuples-only --no-align \ + --command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'" 2>/dev/null || true)" + if [[ "$super_admin_count" != "1" ]]; then + printf 'Initial admin password is unset and no SUPER_ADMIN exists; run scripts/set-admin-credentials.sh first.\n' >&2 + exit 1 + fi +fi +for image in "$backend_image" "$frontend_image"; do + if [[ -z "$image" ]] || ! docker image inspect "$image" >/dev/null 2>&1; then + printf 'Configured admin image is unavailable: %s\n' "$image" >&2 + exit 1 + fi +done + +compose_command=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$compose_file") +"${compose_command[@]}" config --quiet +"${compose_command[@]}" up -d +"${compose_command[@]}" ps --all diff --git a/scripts/verify-admin.sh b/scripts/verify-admin.sh new file mode 100755 index 0000000..d60c0ab --- /dev/null +++ b/scripts/verify-admin.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +env_file="${1:-compose/.env}" +admin_compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}" +infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" + +for required_command in awk curl docker jq; 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 +env_value() { + awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" +} + +project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)" +backend_url="http://$(env_value ADMIN_BACKEND_BIND_ADDRESS):$(env_value ADMIN_BACKEND_HOST_PORT)" +frontend_url="http://$(env_value ADMIN_FRONTEND_BIND_ADDRESS):$(env_value ADMIN_FRONTEND_HOST_PORT)" +admin_database="$(env_value ADMIN_POSTGRES_DB)" +postgres_user="$(env_value POSTGRES_USER)" +admin_compose=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$admin_compose_file") +infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file") +"${admin_compose[@]}" config --quiet + +backend_health="" +frontend_health="" +for _attempt in $(seq 1 24); do + backend_health="$(curl --fail --silent --show-error --max-time 5 "$backend_url/actuator/health" 2>/dev/null || true)" + frontend_health="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/actuator/health" 2>/dev/null || true)" + if printf '%s' "$backend_health" | jq -e '.status == "UP"' >/dev/null 2>&1 \ + && printf '%s' "$frontend_health" | jq -e '.status == "UP"' >/dev/null 2>&1; then + break + fi + backend_health="" + frontend_health="" + sleep 5 +done +if [[ -z "$backend_health" || -z "$frontend_health" ]]; then + printf 'Admin health checks did not become ready.\n' >&2 + "${admin_compose[@]}" ps >&2 + exit 1 +fi + +index_html="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/")" +module_config="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/assets/module-config.json")" +if ! printf '%s' "$index_html" | grep -q 'id="app"'; then + printf 'Admin frontend index is invalid.\n' >&2 + exit 1 +fi +if [[ "$(printf '%s' "$module_config" | jq -r '[.modules[].code] | join(",")')" != "AI_SOULMATE,CONSOLE" ]]; then + printf 'Admin frontend is not using the AI SoulMate local module configuration.\n' >&2 + exit 1 +fi + +super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \ + --username "$postgres_user" --dbname "$admin_database" --tuples-only --no-align \ + --command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'")" +if [[ "$super_admin_count" != "1" ]]; then + printf 'Expected exactly one local SUPER_ADMIN, found: %s\n' "$super_admin_count" >&2 + exit 1 +fi +"${infra_compose[@]}" exec -T postgres psql \ + --username "$postgres_user" --dbname "$(env_value POSTGRES_DB)" --tuples-only --no-align \ + --command "SELECT count(*) FROM pg_tables WHERE schemaname = 'public'" | grep -Eq '^[1-9][0-9]*$' + +printf '%s\n' "$frontend_health" | jq . +"${admin_compose[@]}" ps +printf 'AI SoulMate local admin, local PostgreSQL, and MinIO configuration checks passed.\n' diff --git a/scripts/verify-infra-backup.sh b/scripts/verify-infra-backup.sh index 6d2554d..34f2f88 100755 --- a/scripts/verify-infra-backup.sh +++ b/scripts/verify-infra-backup.sh @@ -48,29 +48,43 @@ postgres_user="$(env_value POSTGRES_USER)" sha256sum --check checksums.sha256 ) -restore_database="restore_check_$(date +%s)_$$" +restore_databases=() cleanup_postgres() { - "${compose_command[@]}" exec -T postgres dropdb \ - --if-exists --username "$postgres_user" "$restore_database" >/dev/null 2>&1 || true + local restore_database + for restore_database in "${restore_databases[@]}"; do + "${compose_command[@]}" exec -T postgres dropdb \ + --if-exists --username "$postgres_user" "$restore_database" >/dev/null 2>&1 || true + done } trap cleanup_postgres EXIT -"${compose_command[@]}" exec -T postgres createdb \ - --username "$postgres_user" "$restore_database" -"${compose_command[@]}" exec -T postgres pg_restore \ - --username "$postgres_user" \ - --dbname "$restore_database" \ - --no-owner \ - --no-acl \ - --exit-on-error \ - < "$backup_directory/postgres.dump" -"${compose_command[@]}" exec -T postgres psql \ - --username "$postgres_user" \ - --dbname "$restore_database" \ - --tuples-only \ - --no-align \ - --command 'SELECT 1' \ - | grep -qx '1' +verify_postgres_dump() { + local dump_path="$1" + local suffix="$2" + local restore_database="restore_check_${suffix}_$(date +%s)_$$" + restore_databases+=("$restore_database") + "${compose_command[@]}" exec -T postgres createdb \ + --username "$postgres_user" "$restore_database" + "${compose_command[@]}" exec -T postgres pg_restore \ + --username "$postgres_user" \ + --dbname "$restore_database" \ + --no-owner \ + --no-acl \ + --exit-on-error \ + < "$dump_path" + "${compose_command[@]}" exec -T postgres psql \ + --username "$postgres_user" \ + --dbname "$restore_database" \ + --tuples-only \ + --no-align \ + --command 'SELECT 1' \ + | grep -qx '1' +} + +verify_postgres_dump "$backup_directory/postgres.dump" primary +if [[ -f "$backup_directory/admin-postgres.dump" ]]; then + verify_postgres_dump "$backup_directory/admin-postgres.dump" admin +fi cleanup_postgres trap - EXIT