Files
moyi-model-intelligence-hub/infra/moyi/deploy.py

296 lines
15 KiB
Python

#!/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):
# Never let docker compose or a diagnostic command consume the caller's
# shell stdin. This is especially important when deploy.py is invoked from
# an SSH heredoc; only an explicit data payload may be sent to a process.
options = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL}
if command[:2] == ["docker", "compose"] and "--env-file" in command:
environment = os.environ.copy()
environment["MOYI_RUNTIME_ENV_FILE"] = str(Path(command[command.index("--env-file") + 1]).resolve())
options["env"] = environment
result = subprocess.run(command, cwd=HERE, capture_output=capture,
text=True, encoding="utf-8", errors="replace", **options)
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.")
# A later empty assignment silently overrides an earlier valid secret in
# Compose. Reject duplicates before resolving or starting any container.
seen = set()
for line in env_file.read_text(encoding="utf-8-sig").splitlines():
match = re.match(r"^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=", line)
if match:
key = match.group(1)
if key in seen:
fail("Duplicate environment setting: " + key + ". Keep exactly one assignment; values are not printed.")
seen.add(key)
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)
if not str(api["ADMIN_PASSWORD"]).strip():
fail("ADMIN_PASSWORD must not be blank.")
for key, minimum in (("AUTH_PASSWORD_PEPPER", 32), ("CREDENTIAL_ENCRYPTION_KEY", 32)):
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"] != "super_admin":
fail("Super administrator login/cookie verification failed.")
request("/api/admin/users")
request("/api/auth/logout", 204, {})
print("Initial super administrator 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)