#!/bin/bash set -euo pipefail cd "$(dirname "$0")" if [[ ! -f .env ]]; then echo "缺少 .env,先执行 ./init-env.sh" exit 1 fi set -a # shellcheck disable=SC1091 source .env set +a python3 - <<'PY' import os from pathlib import Path root = Path(".") site = os.environ["SITE_DOMAIN"].strip() files = os.environ["FILES_DOMAIN"].strip() names = os.environ.get("SITE_NAMES", site).strip() flat = root / "certs" / "fullchain.pem" flat_key = root / "certs" / "privkey.pem" live = root / "certs" / "live" / site / "fullchain.pem" live_key = root / "certs" / "live" / site / "privkey.pem" https = (flat.is_file() and flat_key.is_file()) or (live.is_file() and live_key.is_file()) if flat.is_file() and flat_key.is_file(): cert_full = "/etc/nginx/certs/fullchain.pem" cert_key = "/etc/nginx/certs/privkey.pem" elif live.is_file() and live_key.is_file(): cert_full = f"/etc/nginx/certs/live/{site}/fullchain.pem" cert_key = f"/etc/nginx/certs/live/{site}/privkey.pem" else: cert_full = "" cert_key = "" src = root / "nginx" / ("https.conf.template" if https else "http.conf.template") text = src.read_text(encoding="utf-8") values = { "SITE_NAMES": names, "FILES_DOMAIN": files, "CERT_FULL": cert_full, "CERT_KEY": cert_key, } for key, value in values.items(): text = text.replace("${" + key + "}", value) if "${" in text: raise SystemExit("nginx 模板仍有未替换的占位符") out = root / "nginx" / "active.conf" out.write_text(text, encoding="utf-8") print(("https" if https else "http") + " 配置已写入 nginx/active.conf") PY