feat: package Beijing test deployment with HTTPS and preflight checks

This commit is contained in:
wlx committed 2026-09-10 18:10:31 +08:00
1 parent ae749a1fe9
commit c7278c00a6
33 files changed
+1153 -54

No files matched your search

+51 -23
View File
@@ -1,38 +1,66 @@
# Copy to .env.cloud on the target server. Never commit the real file.
# Run: python3 deploy.py init. Never commit .env.cloud.
# Single-quote passwords with '$', '#', spaces, etc. Do NOT source this file.
COMPOSE_PROJECT_NAME=moyi-test
MOYI_HTTP_PORT=8080
CONTROL_PLANE_CORS_ORIGINS=https://moyi.example.com
MOYI_DOMAIN=moyi.soulofmodel.com
CONTROL_PLANE_CORS_ORIGINS=http://localhost:8080
# Select an unused subnet outside all server/RDS/Tair VPC ranges.
MOYI_DOCKER_SUBNET=10.253.240.0/24
# Alibaba Cloud RDS PostgreSQL private endpoint.
MOYI_DB_URL=jdbc:postgresql://rds-private-endpoint:1921/moyi_control
# Source defaults. Release initializer selects runtime Dockerfiles and tag.
MOYI_IMAGE_TAG=dev
MOYI_API_DOCKERFILE=Dockerfile
MOYI_FRONTEND_DOCKERFILE=Dockerfile
JAVA_RUNTIME_IMAGE=eclipse-temurin:21-jre-alpine
NGINX_RUNTIME_IMAGE=nginx:1.27-alpine
# REQUIRED: dedicated EMPTY RDS PostgreSQL database, never the mini-app DB.
# Confirm port in RDS console (often 5432 or 1921). MySQL is NOT supported.
MOYI_DB_URL=jdbc:postgresql://CHANGE_ME_RDS_HOST:5432/moyi_control
MOYI_DB_USERNAME=moyi_app
MOYI_DB_PASSWORD=replace-with-a-long-random-password
MOYI_DB_PASSWORD='CHANGE_ME_RDS_PASSWORD'
DB_POOL_MAX_SIZE=10
DB_POOL_MIN_IDLE=2
# Alibaba Cloud Redis/Tair private endpoint. Use TLS parameters if enabled by the instance.
MOYI_REDIS_URL=rediss://:replace-with-redis-password@redis-private-endpoint:6379/0
# REQUIRED: Tair standard endpoint or cluster PROXY, NOT cluster direct.
# Separate fields avoid URI-encoding passwords. Match TLS to the instance.
MOYI_REDIS_HOST=CHANGE_ME_TAIR_HOST
MOYI_REDIS_PORT=6379
MOYI_REDIS_USERNAME=
MOYI_REDIS_PASSWORD='CHANGE_ME_TAIR_PASSWORD'
MOYI_REDIS_DATABASE=0
MOYI_REDIS_SSL=false
MOYI_REDIS_KEY_PREFIX=moyi:test:
# Generated by init. Keep stable and back up securely across upgrades.
AUTH_PASSWORD_PEPPER=GENERATE_AUTH_PEPPER
CREDENTIAL_ENCRYPTION_KEY=GENERATE_CREDENTIAL_KEY
ADMIN_ACCOUNT=adimin
ADMIN_PASSWORD=GENERATE_ADMIN_PASSWORD
ADMIN_DISPLAY_NAME=MOYI Admin
SESSION_TTL_HOURS=168
JAVA_OPTS='-Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError'
GATEWAY_REQUESTS_PER_MINUTE=60
GATEWAY_READS_PER_MINUTE=300
GATEWAY_DEFAULT_MODEL_CONCURRENCY=4
TASK_MAX_QUEUED_PER_MODEL=100
MOYI_TASK_WORKER_INTERVAL_MS=1000
SESSION_TTL_HOURS=168
AUTH_PASSWORD_PEPPER=replace-with-a-long-random-secret-at-least-32-chars
CREDENTIAL_ENCRYPTION_KEY=replace-with-a-long-random-secret-for-provider-credentials
ADMIN_ACCOUNT=adimin
ADMIN_PASSWORD=replace-with-a-strong-initial-password
ADMIN_DISPLAY_NAME=平台管理员
AUTH_SECURE_COOKIE=true
# 仅使用公网 IP + HTTP 临时验收时改为 false;正式域名 HTTPS 必须保持 true。
# OPTIONAL: use admin UI OR environment for one synchronous provider.
# Environment credentials take precedence over admin UI credentials.
DEEPSEEK_API_KEY=
DEEPSEEK_BASE_URL=https://api.deepseek.com
KIMI_API_KEY=
KIMI_BASE_URL=https://api.moonshot.ai/v1
ZHIPU_API_KEY=
ZHIPU_BASE_URL=https://open.bigmodel.cn/api/paas/v4
OSS_ENDPOINT=
OSS_REGION=
# RESERVED ONLY: Java Tripo/Seedance + OSS are NOT yet implemented.
# Leave these secrets empty; Compose intentionally does not inject them yet.
TRIPO_API_KEY=
VOLCENGINE_API_KEY=
OSS_REGION=cn-beijing
OSS_ENDPOINT=oss-cn-beijing-internal.aliyuncs.com
OSS_BUCKET=
OSS_ACCESS_KEY_ID=
OSS_ACCESS_KEY_SECRET=
TRIPO_API_KEY=
DEEPSEEK_API_KEY=
KIMI_API_KEY=
ZHIPU_API_KEY=
VOLCENGINE_API_KEY=
+9 -2
View File
@@ -1,10 +1,17 @@
# MOYI 容器化部署基线
## 北京服务器测试部署(当前入口)
当前已确认 Ubuntu 24.04 / 4C8G、RDS PostgreSQL 15、Tair 标准架构、`moyi.soulofmodel.com`。
完整阿里云配置、发布包、环境变量、证书、启动和回退说明见 [服务器测试部署手册](../../docs/11-server-test-deployment.md)。
推荐 `python3 deploy.py init` 生成配置,使用 `deploy.py check/up/smoke`,无证书时默认 SSH 隧道,有证书时添加 `--mode https`。
发布包由 `scripts/package-deployment.ps1` 构建,只包含编译产物及非敏感配置,不包含真实 `.env.cloud` 或证书。
这套 Compose 用于 MOYI 的 Java 后端迁移基线:Vue 前端以 Nginx 生产构建运行,Java Control Plane 以 Spring Boot 运行,PostgreSQL 和 Redis 使用独立持久化卷。OSS 不在 Compose 中自建,生产环境连接云厂商 OSS;本地如需模拟对象存储,可另行增加 MinIO profile。
## 当前迁移状态
`control-plane-java/` 当前提供 Java 21 / Spring Boot 的认证、模型目录、项目、Key、权限和最小 OpenAI 兼容接口。真实供应商适配、Redis 队列/限流、异步任务和计费完成并通过 UAT 前,不要直接把生产流量切到 Java 服务。
`control-plane-java/` 提供认证、模型目录、项目、Key、非流式同步供应商转发、Tair 网关保护及审计。真实异步执行、流式响应、OSS 和计费仍未完成,不要直接切入生产流量。
## 启动
@@ -26,7 +33,7 @@ Invoke-WebRequest http://localhost:8080/health
## 云上托管依赖
RDS、Redis 和 OSS 不需要部署在轻量应用服务器上。云上使用托管服务时,复制 `.env.cloud.example` 为 `.env.cloud`,填写 RDS/Redis 私网连接信息,然后执行:
RDS、Tair 和 OSS 不部署在轻量应用服务器上。填写 `.env.cloud` 后优先使用 `deploy.py` 预检查。以下原始命令仅用于理解底层 Compose 行为;默认只在服务器回环地址监听:
```powershell
docker compose --env-file .env.cloud -f docker-compose.cloud.yml build
+274
View File
@@ -0,0 +1,274 @@
#!/usr/bin/env python3
"""MOYI test deployment helper. Python 3 standard library only."""
import argparse
import hashlib
import http.cookiejar
import ipaddress
import json
import os
from pathlib import Path
import re
import secrets
import socket
import stat
import subprocess
import sys
import urllib.error
import urllib.parse
import urllib.request
HERE = Path(__file__).resolve().parent
ROOT = HERE.parent.parent
def fail(message):
raise ValueError(message)
def execute(command, capture=False, data=None):
result = subprocess.run(command, cwd=HERE, input=data, capture_output=capture,
text=True, encoding="utf-8", errors="replace")
if result.returncode:
# Compose error output can contain expanded credentials. Do not echo it.
fail("Command failed: " + " ".join(command[:3]) + ". Check local configuration/permissions; do not share secrets.")
return result.stdout if capture else ""
def initialize():
destination = HERE / ".env.cloud"
if destination.exists():
fail(".env.cloud already exists; refusing to overwrite credentials.")
content = (HERE / ".env.cloud.example").read_text(encoding="utf-8-sig")
for marker in ("GENERATE_AUTH_PEPPER", "GENERATE_CREDENTIAL_KEY", "GENERATE_ADMIN_PASSWORD"):
content = content.replace(marker, secrets.token_hex(32))
manifest = ROOT / "release-manifest.json"
if manifest.exists():
release = json.loads(manifest.read_text(encoding="utf-8"))
content = content.replace("MOYI_IMAGE_TAG=dev", "MOYI_IMAGE_TAG=" + release["release"])
content = content.replace("MOYI_API_DOCKERFILE=Dockerfile\n", "MOYI_API_DOCKERFILE=Dockerfile.runtime\n")
content = content.replace("MOYI_FRONTEND_DOCKERFILE=Dockerfile\n", "MOYI_FRONTEND_DOCKERFILE=Dockerfile.runtime\n")
descriptor = os.open(destination, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, "w", encoding="utf-8", newline="\n") as output:
output.write(content)
(HERE / "certs").mkdir(mode=0o700, exist_ok=True)
print("Created .env.cloud (0600). Secrets generated but NOT printed. Edit database/Tair settings locally.")
def compose_command(env_file, mode):
command = ["docker", "compose", "--env-file", str(env_file), "-f", "docker-compose.cloud.yml"]
if mode == "https":
command += ["-f", "docker-compose.https.yml"]
return command
def load_config(env_file, mode):
if not env_file.is_file():
fail("Environment file missing. Run: python3 deploy.py init")
if os.name != "nt" and stat.S_IMODE(env_file.stat().st_mode) & 0o077:
fail("Environment file must be private. Run chmod 600 on it.")
config = execute(compose_command(env_file, mode) + ["config", "--format", "json"], capture=True)
resolved = json.loads(config)
# Compose serializes literal '$' as '$$' for re-parseable config output.
for service in resolved.get("services", {}).values():
for key, value in service.get("environment", {}).items():
if isinstance(value, str):
service["environment"][key] = value.replace("$$", "$")
return resolved
def validate_config(config, mode):
services = config["services"]
api = services["moyi-api"]["environment"]
required = ("SPRING_DATASOURCE_URL", "SPRING_DATASOURCE_USERNAME", "SPRING_DATASOURCE_PASSWORD",
"MOYI_REDIS_HOST", "MOYI_REDIS_PASSWORD", "AUTH_PASSWORD_PEPPER",
"CREDENTIAL_ENCRYPTION_KEY", "ADMIN_PASSWORD")
for key in required:
value = str(api.get(key, ""))
if not value or any(marker in value.upper() for marker in ("CHANGE_ME", "GENERATE_", "REPLACE-WITH")):
fail("Missing/placeholder setting: " + key)
for key, minimum in (("AUTH_PASSWORD_PEPPER", 32), ("CREDENTIAL_ENCRYPTION_KEY", 32), ("ADMIN_PASSWORD", 16)):
if len(str(api[key])) < minimum:
fail(key + " is too short (minimum " + str(minimum) + " characters).")
if api["AUTH_PASSWORD_PEPPER"] == api["CREDENTIAL_ENCRYPTION_KEY"]:
fail("Auth pepper and credential encryption key must differ.")
url = str(api["SPRING_DATASOURCE_URL"])
if not url.startswith("jdbc:postgresql://"):
fail("This release requires RDS PostgreSQL (confirmed target: 15), not MySQL.")
database = urllib.parse.urlsplit(url[5:])
if not database.hostname or database.username or database.password or database.path in ("", "/", "/postgres", "/template0", "/template1"):
fail("Use a dedicated PostgreSQL database and separate username/password fields.")
if not re.fullmatch(r"[A-Za-z0-9:_-]{2,80}:", str(api.get("MOYI_REDIS_KEY_PREFIX", ""))):
fail("Set a dedicated Redis key prefix ending in ':'.")
if str(api.get("MOYI_REDIS_SSL", "")).lower() not in ("true", "false"):
fail("MOYI_REDIS_SSL must be true or false.")
if services["moyi-api"].get("ports"):
fail("Backend ports must not be published.")
ports = services["frontend"].get("ports", [])
if mode == "tunnel" and any(p.get("host_ip") != "127.0.0.1" for p in ports):
fail("Tunnel mode must bind only to 127.0.0.1.")
if mode == "https":
domain = services["frontend"]["environment"].get("MOYI_DOMAIN", "")
if not re.fullmatch(r"(?=.{1,253}$)[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?", domain) or "." not in domain:
fail("MOYI_DOMAIN must be a hostname, without protocol/port/path.")
if str(api.get("AUTH_SECURE_COOKIE")).lower() != "true":
fail("HTTPS requires secure cookies.")
elif str(api.get("AUTH_SECURE_COOKIE")).lower() != "false":
fail("Local SSH-tunnel HTTP requires secure cookies disabled.")
subnet = ipaddress.ip_network(config["networks"]["default"]["ipam"]["config"][0]["subnet"])
return api, database, subnet
def verify_release():
manifest_path = ROOT / "release-manifest.json"
if not manifest_path.exists():
print("Source checkout: no precompiled release manifest.")
return
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
for name, expected in manifest["sha256"].items():
path = (ROOT / name).resolve()
if not path.is_relative_to(ROOT.resolve()) or not path.is_file():
fail("Release file missing or outside package: " + name)
if hashlib.sha256(path.read_bytes()).hexdigest() != expected:
fail("Release checksum mismatch: " + name)
print("Release checksums OK: " + manifest["release"])
def check_certificate(domain):
certificate = HERE / "certs/fullchain.pem"
key = HERE / "certs/privkey.pem"
if not certificate.is_file() or not key.is_file():
fail("HTTPS requires certs/fullchain.pem and certs/privkey.pem.")
if os.name != "nt" and stat.S_IMODE(key.stat().st_mode) & 0o077:
fail("TLS private key must have mode 0600.")
# -checkhost output alone is not portable as an exit-status assertion.
details = execute(["openssl", "x509", "-in", str(certificate), "-noout", "-checkhost", domain], capture=True)
if "does match certificate" not in details:
fail("TLS certificate does not cover MOYI_DOMAIN.")
execute(["openssl", "x509", "-in", str(certificate), "-noout", "-checkend", "86400"], capture=True)
public_cert = execute(["openssl", "x509", "-in", str(certificate), "-noout", "-pubkey"], capture=True)
public_key = execute(["openssl", "pkey", "-in", str(key), "-pubout", "-passin", "pass:"], capture=True)
if public_cert.strip() != public_key.strip():
fail("TLS certificate and private key do not match.")
renewal = subprocess.run(["openssl", "x509", "-in", str(certificate), "-noout", "-checkend", "2592000"], capture_output=True)
print("TLS certificate/key OK." if renewal.returncode == 0 else "WARNING: TLS certificate expires within 30 days; renew it.")
def check_network(api, database, subnet):
targets = (("RDS", database.hostname, database.port or 5432),
("Tair", api["MOYI_REDIS_HOST"], int(api.get("MOYI_REDIS_PORT", 6379))))
for label, host, port in targets:
try:
addresses = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
if any(ipaddress.ip_address(item[4][0]) in subnet for item in addresses):
fail(label + " address overlaps MOYI_DOCKER_SUBNET. Select a non-overlapping Docker subnet.")
with socket.create_connection((host, port), timeout=5):
pass
except OSError:
fail(label + " TCP check failed. Check private-network interconnection and whitelist.")
print(label + " TCP reachable (authentication/TLS checked after application startup).")
def preflight(env_file, mode, network=False):
verify_release()
config = load_config(env_file, mode)
api, database, subnet = validate_config(config, mode)
if mode == "https":
check_certificate(config["services"]["frontend"]["environment"]["MOYI_DOMAIN"])
if network:
check_network(api, database, subnet)
print("Configuration OK. No credentials printed. Mode: " + mode)
return config
def smoke(config, mode, login=False):
ports = config["services"]["frontend"]["ports"]
port = next(str(p["published"]) for p in ports if p.get("host_ip") == "127.0.0.1")
base = "http://127.0.0.1:" + port if mode == "tunnel" else "https://" + config["services"]["frontend"]["environment"]["MOYI_DOMAIN"]
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
def request(path, expected=200, payload=None):
body = json.dumps(payload).encode() if payload is not None else None
req = urllib.request.Request(base + path, data=body, headers={"Content-Type": "application/json"})
try:
response = opener.open(req, timeout=15)
except urllib.error.HTTPError as error:
response = error
if response.code != expected:
fail("Smoke check failed: " + path + " HTTP " + str(response.code))
return response.read()
if b'<html' not in request("/").lower():
fail("Frontend did not return HTML.")
if json.loads(request("/health")).get("status") != "ok":
fail("Backend health response invalid.")
request("/actuator/health", 404)
request("/api/auth/me", 401)
request("/v1/models", 401)
if login:
api = config["services"]["moyi-api"]["environment"]
request("/api/auth/login", payload={"account": api["ADMIN_ACCOUNT"], "password": api["ADMIN_PASSWORD"]})
if json.loads(request("/api/auth/me"))["user"]["role"] != "admin":
fail("Admin login/cookie verification failed.")
request("/api/auth/logout", 204, {})
print("Initial admin login + session cookie OK. Audit records were created.")
print("Frontend, backend, authentication boundary and management endpoint checks passed.")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("action", choices=("init", "verify", "check", "up", "status", "logs", "stop", "smoke", "reload-tls"))
parser.add_argument("--env-file", type=Path, default=HERE / ".env.cloud")
parser.add_argument("--mode", choices=("tunnel", "https"))
parser.add_argument("--network", action="store_true", help="Test RDS/Tair TCP from this host")
parser.add_argument("--accept-migrations", action="store_true", help="Authorize Flyway on the configured dedicated database")
parser.add_argument("--login", action="store_true", help="Smoke-test initial admin credentials; writes a login audit entry")
args = parser.parse_args()
if args.action == "init":
initialize()
return
if args.action == "verify":
verify_release()
return
mode_file = HERE / ".deploy-mode"
mode = args.mode or (mode_file.read_text().strip() if mode_file.exists() else "tunnel")
if mode not in ("tunnel", "https"):
fail("Invalid saved deployment mode.")
env_file = args.env_file.resolve()
command = compose_command(env_file, mode)
if args.action in ("status", "logs", "stop"):
load_config(env_file, mode)
operation = {"status": ["ps"], "logs": ["logs", "--tail", "100"], "stop": ["stop"]}[args.action]
execute(command + operation)
return
config = preflight(env_file, mode, args.network or args.action == "up")
if args.action == "check":
return
if args.action == "up":
if not args.accept_migrations:
fail("Back up the dedicated RDS database, then add --accept-migrations. Never use the mini-app database.")
execute(["docker", "info", "--format", "{{.ServerVersion}}"], capture=True)
# Build first, keeping current containers running if an image download/build fails.
execute(command + ["build"])
execute(command + ["up", "-d", "--wait", "--wait-timeout", "180", "--no-build", "moyi-api"])
execute(command + ["run", "--rm", "--no-deps", "frontend", "nginx", "-t"])
execute(command + ["up", "-d", "--wait", "--wait-timeout", "180", "--no-build"])
mode_file.write_text(mode + "\n", encoding="ascii")
execute(command + ["ps"])
print("Started. Run: python3 deploy.py smoke --login")
elif args.action == "smoke":
smoke(config, mode, args.login)
elif args.action == "reload-tls":
if mode != "https":
fail("reload-tls requires HTTPS mode.")
execute(command + ["exec", "-T", "frontend", "nginx", "-t"])
execute(command + ["exec", "-T", "frontend", "nginx", "-s", "reload"])
if __name__ == "__main__":
try:
main()
except (ValueError, OSError, subprocess.SubprocessError, urllib.error.URLError) as error:
# Network/library exceptions may contain URLs; only deliberate errors are echoed.
message = str(error) if isinstance(error, ValueError) else type(error).__name__ + ": check local tools/network configuration."
print("ERROR: " + message, file=sys.stderr)
sys.exit(1)
+75 -11
View File
@@ -1,40 +1,104 @@
name: moyi
name: ${COMPOSE_PROJECT_NAME:-moyi-test}
services:
moyi-api:
build:
context: ../../control-plane-java
dockerfile: ${MOYI_API_DOCKERFILE:-Dockerfile}
args:
JAVA_RUNTIME_IMAGE: ${JAVA_RUNTIME_IMAGE:-eclipse-temurin:21-jre-alpine}
image: moyi-api:${MOYI_IMAGE_TAG:-dev}
restart: unless-stopped
env_file:
- path: .env.cloud
required: false
environment:
CONTROL_PLANE_HOST: 0.0.0.0
CONTROL_PLANE_PORT: 8787
CONTROL_PLANE_CORS_ORIGINS: ${CONTROL_PLANE_CORS_ORIGINS:-https://moyi.example.com}
CONTROL_PLANE_CORS_ORIGINS: ${CONTROL_PLANE_CORS_ORIGINS:-http://localhost:8080}
SPRING_DATASOURCE_URL: ${MOYI_DB_URL:?Set MOYI_DB_URL in infra/moyi/.env.cloud}
SPRING_DATASOURCE_USERNAME: ${MOYI_DB_USERNAME:?Set MOYI_DB_USERNAME in infra/moyi/.env.cloud}
SPRING_DATASOURCE_PASSWORD: ${MOYI_DB_PASSWORD:?Set MOYI_DB_PASSWORD in infra/moyi/.env.cloud}
SPRING_DATA_REDIS_URL: ${MOYI_REDIS_URL:?Set MOYI_REDIS_URL in infra/moyi/.env.cloud}
MOYI_REDIS_HOST: ${MOYI_REDIS_HOST:?Set MOYI_REDIS_HOST}
MOYI_REDIS_PORT: ${MOYI_REDIS_PORT:-6379}
MOYI_REDIS_USERNAME: ${MOYI_REDIS_USERNAME:-}
MOYI_REDIS_PASSWORD: ${MOYI_REDIS_PASSWORD:?Set MOYI_REDIS_PASSWORD}
MOYI_REDIS_DATABASE: ${MOYI_REDIS_DATABASE:-0}
MOYI_REDIS_SSL: ${MOYI_REDIS_SSL:-false}
MOYI_REDIS_KEY_PREFIX: ${MOYI_REDIS_KEY_PREFIX:-moyi:test:}
FLYWAY_ENABLED: true
# IP + HTTP 临时测试时可设为 false;绑定域名并启用 HTTPS 后必须设为 true。
AUTH_SECURE_COOKIE: ${AUTH_SECURE_COOKIE:-true}
FLYWAY_BASELINE_ON_MIGRATE: false
AUTH_SECURE_COOKIE: false
AUTH_PASSWORD_PEPPER: ${AUTH_PASSWORD_PEPPER:?Initialize auth secrets}
CREDENTIAL_ENCRYPTION_KEY: ${CREDENTIAL_ENCRYPTION_KEY:?Initialize credential encryption key}
ADMIN_ACCOUNT: ${ADMIN_ACCOUNT:-adimin}
ADMIN_PASSWORD: ${ADMIN_PASSWORD:?Set a strong initial admin password}
ADMIN_DISPLAY_NAME: ${ADMIN_DISPLAY_NAME:-MOYI Admin}
SESSION_TTL_HOURS: ${SESSION_TTL_HOURS:-168}
GATEWAY_REQUESTS_PER_MINUTE: ${GATEWAY_REQUESTS_PER_MINUTE:-60}
GATEWAY_READS_PER_MINUTE: ${GATEWAY_READS_PER_MINUTE:-300}
GATEWAY_DEFAULT_MODEL_CONCURRENCY: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:-4}
TASK_MAX_QUEUED_PER_MODEL: ${TASK_MAX_QUEUED_PER_MODEL:-100}
MOYI_TASK_WORKER_INTERVAL_MS: ${MOYI_TASK_WORKER_INTERVAL_MS:-1000}
DB_POOL_MAX_SIZE: ${DB_POOL_MAX_SIZE:-10}
DB_POOL_MIN_IDLE: ${DB_POOL_MIN_IDLE:-2}
JAVA_OPTS: ${JAVA_OPTS:--Xms256m -Xmx1536m -XX:+ExitOnOutOfMemoryError}
SERVER_TOMCAT_THREADS_MAX: 100
SERVER_FORWARD_HEADERS_STRATEGY: framework
MANAGEMENT_ENDPOINT_HEALTH_GROUP_READINESS_INCLUDE: readinessState,db,redis
MANAGEMENT_ENDPOINT_HEALTH_SHOW_DETAILS: never
DEEPSEEK_API_KEY: ${DEEPSEEK_API_KEY:-}
DEEPSEEK_BASE_URL: ${DEEPSEEK_BASE_URL:-https://api.deepseek.com}
KIMI_API_KEY: ${KIMI_API_KEY:-}
KIMI_BASE_URL: ${KIMI_BASE_URL:-https://api.moonshot.ai/v1}
ZHIPU_API_KEY: ${ZHIPU_API_KEY:-}
ZHIPU_BASE_URL: ${ZHIPU_BASE_URL:-https://open.bigmodel.cn/api/paas/v4}
mem_limit: 2560m
pids_limit: 512
read_only: true
tmpfs:
- /tmp:size=256m,mode=1777
security_opt:
- no-new-privileges:true
stop_grace_period: 40s
logging: &logging
driver: json-file
options:
max-size: "10m"
max-file: "3"
healthcheck:
test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8787/health | grep -q 'status'"]
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8787/actuator/health/readiness"]
interval: 10s
timeout: 5s
retries: 12
start_period: 20s
start_period: 60s
frontend:
build:
context: ../../prototype
dockerfile: ${MOYI_FRONTEND_DOCKERFILE:-Dockerfile}
args:
VITE_CONTROL_PLANE_URL: /
VITE_AUTH_MODE: remote
NGINX_RUNTIME_IMAGE: ${NGINX_RUNTIME_IMAGE:-nginx:1.27-alpine}
image: moyi-frontend:${MOYI_IMAGE_TAG:-dev}
restart: unless-stopped
ports:
- "${MOYI_HTTP_PORT:-8080}:80"
- "127.0.0.1:${MOYI_HTTP_PORT:-8080}:80"
volumes:
- ./nginx/http.conf:/etc/nginx/conf.d/default.conf:ro
- ./nginx/routes.conf:/etc/nginx/moyi-routes.conf:ro
mem_limit: 256m
pids_limit: 128
logging: *logging
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1/nginx-health"]
interval: 10s
timeout: 5s
retries: 6
depends_on:
moyi-api:
condition: service_healthy
networks:
default:
ipam:
config:
- subnet: ${MOYI_DOCKER_SUBNET:-10.253.240.0/24}
+17
View File
@@ -0,0 +1,17 @@
# Overlay: -f docker-compose.cloud.yml -f docker-compose.https.yml
services:
moyi-api:
environment:
CONTROL_PLANE_CORS_ORIGINS: https://${MOYI_DOMAIN:?Set MOYI_DOMAIN}
AUTH_SECURE_COOKIE: true
frontend:
ports:
- "80:80"
- "443:443"
environment:
MOYI_DOMAIN: ${MOYI_DOMAIN:?Set MOYI_DOMAIN}
NGINX_ENVSUBST_FILTER: ^MOYI_DOMAIN$
volumes:
- ./nginx/https-fallback.conf:/etc/nginx/conf.d/default.conf:ro
- ./nginx/https.conf.template:/etc/nginx/templates/moyi.conf.template:ro
- ./certs:/etc/nginx/certs:ro
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
if [ "$(id -u)" != 0 ]; then
printf '%s\n' 'Run with sudo. This script installs Docker Engine and the Compose plugin.' >&2
exit 1
fi
. /etc/os-release
if [ "${ID:-}" != ubuntu ] || [ "${VERSION_ID:-}" != 24.04 ]; then
printf '%s\n' 'This installer targets the confirmed Ubuntu 24.04 server only.' >&2
exit 1
fi
if command -v docker >/dev/null 2>&1; then
docker version
docker compose version
printf '%s\n' 'Docker already installed; existing packages and settings were left untouched.'
exit 0
fi
for package in docker.io docker-compose docker-compose-v2 podman-docker containerd runc; do
if dpkg-query -W -f='${Status}' "$package" 2>/dev/null | grep -q 'install ok installed'; then
printf 'Existing package %s found. Stop and review; nothing will be removed.\n' "$package" >&2
exit 1
fi
done
if [ -e /etc/apt/sources.list.d/docker.sources ] || [ -e /etc/apt/sources.list.d/docker.list ]; then
printf '%s\n' 'Docker APT configuration exists. Review it before continuing.' >&2
exit 1
fi
apt-get update
apt-get install -y ca-certificates curl python3 openssl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL --connect-timeout 10 --max-time 60 https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
printf 'Types: deb\nURIs: https://download.docker.com/linux/ubuntu\nSuites: noble\nComponents: stable\nArchitectures: %s\nSigned-By: /etc/apt/keyrings/docker.asc\n' "$(dpkg --print-architecture)" > /etc/apt/sources.list.d/docker.sources
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker
docker version
docker compose version
printf '%s\n' 'Installed. No firewall rules, Docker address pools, users or existing applications were changed.'
+7
View File
@@ -0,0 +1,7 @@
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.html;
include /etc/nginx/moyi-routes.conf;
}
+7
View File
@@ -0,0 +1,7 @@
# Unknown HTTP hosts cannot reach the application.
server {
listen 80 default_server;
server_name _;
location = /nginx-health { access_log off; return 200 "ok\n"; }
location / { return 444; }
}
+19
View File
@@ -0,0 +1,19 @@
server {
listen 80;
server_name ${MOYI_DOMAIN};
location = /nginx-health { access_log off; return 200 "ok\n"; }
location / { return 308 https://${MOYI_DOMAIN}$request_uri; }
}
server {
listen 443 ssl;
server_name ${MOYI_DOMAIN};
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:MOYI:10m;
ssl_session_timeout 1d;
root /usr/share/nginx/html;
index index.html;
include /etc/nginx/moyi-routes.conf;
}
+40
View File
@@ -0,0 +1,40 @@
server_tokens off;
client_max_body_size 20m;
add_header X-Content-Type-Options nosniff always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
add_header X-Frame-Options SAMEORIGIN always;
# Do not log prompts, credentials or query strings.
access_log off;
location = /nginx-health { access_log off; return 200 "ok\n"; }
location /actuator { return 404; }
location ~ /\. { return 404; }
location = /health {
proxy_pass http://moyi-api:8787/health;
}
location /api/ {
proxy_pass http://moyi-api:8787;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5s;
proxy_read_timeout 120s;
proxy_next_upstream off;
}
location /v1/ {
proxy_pass http://moyi-api:8787;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
proxy_buffering off;
proxy_cache off;
proxy_connect_timeout 5s;
proxy_read_timeout 120s;
proxy_send_timeout 120s;
proxy_next_upstream off;
}
location / { try_files $uri $uri/ /index.html; }
+139
View File
@@ -0,0 +1,139 @@
import importlib.util
import json
import shutil
import subprocess
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
DIRECTORY = Path(__file__).resolve().parents[1]
SPEC = importlib.util.spec_from_file_location("deploy", DIRECTORY / "deploy.py")
deploy = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(deploy)
def configuration():
return {
"services": {
"moyi-api": {"environment": {
"SPRING_DATASOURCE_URL": "jdbc:postgresql://rds.internal:5432/moyi_control",
"SPRING_DATASOURCE_USERNAME": "moyi_app",
"SPRING_DATASOURCE_PASSWORD": "test-only-rds-password",
"MOYI_REDIS_HOST": "redis.internal",
"MOYI_REDIS_PASSWORD": "test-only-tair-password",
"MOYI_REDIS_KEY_PREFIX": "moyi:test:",
"MOYI_REDIS_SSL": "false",
"AUTH_PASSWORD_PEPPER": "p" * 64,
"CREDENTIAL_ENCRYPTION_KEY": "k" * 64,
"ADMIN_PASSWORD": "test-only-admin-password",
"AUTH_SECURE_COOKIE": "false"
}},
"frontend": {"ports": [{"host_ip": "127.0.0.1", "published": "8080", "target": 80}], "environment": {"MOYI_DOMAIN": "moyi.soulofmodel.com"}}
},
"networks": {"default": {"ipam": {"config": [{"subnet": "10.253.240.0/24"}]}}}
}
class DeploymentTests(unittest.TestCase):
def test_tunnel_config(self):
deploy.validate_config(configuration(), "tunnel")
def test_reject_placeholder(self):
cfg = configuration()
cfg["services"]["moyi-api"]["environment"]["MOYI_REDIS_HOST"] = "CHANGE_ME_TAIR_HOST"
with self.assertRaisesRegex(ValueError, "placeholder"):
deploy.validate_config(cfg, "tunnel")
def test_reject_mysql(self):
cfg = configuration()
cfg["services"]["moyi-api"]["environment"]["SPRING_DATASOURCE_URL"] = "jdbc:mysql://db/app"
with self.assertRaisesRegex(ValueError, "PostgreSQL"):
deploy.validate_config(cfg, "tunnel")
def test_reject_public_http(self):
cfg = configuration()
cfg["services"]["frontend"]["ports"][0]["host_ip"] = "0.0.0.0"
with self.assertRaisesRegex(ValueError, "127.0.0.1"):
deploy.validate_config(cfg, "tunnel")
def test_reject_public_backend(self):
cfg = configuration()
cfg["services"]["moyi-api"]["ports"] = [{"published": "8787"}]
with self.assertRaisesRegex(ValueError, "Backend"):
deploy.validate_config(cfg, "tunnel")
def test_https_requires_secure_cookie(self):
with self.assertRaisesRegex(ValueError, "secure cookies"):
deploy.validate_config(configuration(), "https")
def test_https_domain_and_cookie(self):
cfg = configuration()
cfg["services"]["moyi-api"]["environment"]["AUTH_SECURE_COOKIE"] = "true"
deploy.validate_config(cfg, "https")
cfg["services"]["frontend"]["environment"]["MOYI_DOMAIN"] = "example.com; return 200;"
with self.assertRaisesRegex(ValueError, "hostname"):
deploy.validate_config(cfg, "https")
def test_initialization_never_overwrites_secrets(self):
with tempfile.TemporaryDirectory() as temporary:
directory = Path(temporary)
(directory / ".env.cloud.example").write_text((DIRECTORY / ".env.cloud.example").read_text())
with patch.object(deploy, "HERE", directory), patch.object(deploy, "ROOT", directory):
deploy.initialize()
before = (directory / ".env.cloud").read_bytes()
self.assertNotIn(b"GENERATE_AUTH_PEPPER", before)
with self.assertRaisesRegex(ValueError, "overwrite"):
deploy.initialize()
self.assertEqual(before, (directory / ".env.cloud").read_bytes())
def test_manifest_detects_tampering(self):
with tempfile.TemporaryDirectory() as temporary:
directory = Path(temporary)
(directory / "sample").write_bytes(b"original")
digest = deploy.hashlib.sha256(b"original").hexdigest()
(directory / "release-manifest.json").write_text(json.dumps({"release": "test", "sha256": {"sample": digest}}))
with patch.object(deploy, "ROOT", directory):
deploy.verify_release()
(directory / "sample").write_bytes(b"changed")
with self.assertRaisesRegex(ValueError, "checksum mismatch"):
deploy.verify_release()
def test_compose_paths_and_password_not_in_arguments(self):
command = deploy.compose_command(Path("settings"), "https")
self.assertIn("docker-compose.https.yml", command)
self.assertNotIn("password", " ".join(command))
@unittest.skipUnless(shutil.which("docker"), "Docker CLI required, daemon not needed")
def test_real_compose_interpolation_and_https_overlay(self):
content = (DIRECTORY / ".env.cloud.example").read_text(encoding="utf-8")
replacements = {
"CHANGE_ME_RDS_HOST": "rds.internal", "CHANGE_ME_RDS_PASSWORD": "test-db-$value#literal",
"CHANGE_ME_TAIR_HOST": "redis.internal", "CHANGE_ME_TAIR_PASSWORD": "test-cache-$value#literal",
"GENERATE_AUTH_PEPPER": "p" * 64, "GENERATE_CREDENTIAL_KEY": "k" * 64,
"GENERATE_ADMIN_PASSWORD": "test-only-admin-password",
}
for before, after in replacements.items():
content = content.replace(before, after)
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary) / ".env.cloud"
path.write_text(content, encoding="utf-8")
path.chmod(0o600)
for mode in ("tunnel", "https"):
cfg = deploy.load_config(path, mode)
deploy.validate_config(cfg, mode)
api = cfg["services"]["moyi-api"]["environment"]
self.assertEqual(api["MOYI_REDIS_PASSWORD"], "test-cache-$value#literal")
self.assertEqual(api["SPRING_DATASOURCE_PASSWORD"], "test-db-$value#literal")
self.assertNotIn("MOYI_REDIS_PASSWORD", cfg["services"]["frontend"].get("environment", {}))
self.assertNotIn("OSS_ACCESS_KEY_SECRET", api)
if mode == "https":
ports = cfg["services"]["frontend"]["ports"]
self.assertEqual({str(p["published"]) for p in ports}, {"80", "443", "8080"})
defaults = [v for v in cfg["services"]["frontend"]["volumes"] if v["target"] == "/etc/nginx/conf.d/default.conf"]
self.assertEqual(len(defaults), 1)
self.assertTrue(defaults[0]["source"].endswith("https-fallback.conf"))
if __name__ == "__main__":
unittest.main()