feat: add reproducible backend deployment

This commit is contained in:
wlx committed 2026-08-07 12:39:15 +08:00
1 parent 6190e58511
commit ac0d3856f0
11 files changed
+431 -16

No files matched your search

+45
View File
@@ -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}}'
+108
View File
@@ -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'
+28
View File
@@ -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"
+55
View File
@@ -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'