Files
gsfdc/deploy/init-env.sh
T
wuzilongandCursor a8e2e741d1 feat(deploy): 在同一台机器上接入继续教育和 Dify
4 核 8G 同时跑协会站点、PlayEdu 和估价智能体,并让门户专家页底色透出页面背景。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-23 14:12:18 +08:00

130 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# 没有 .env 时生成。已有文件只补缺失项,不改已经写过的密钥。
set -euo pipefail
cd "$(dirname "$0")"
umask 077
python3 - <<'PY'
import base64
import os
import secrets
from pathlib import Path
root = Path(".")
path = root / ".env"
def q(value: str) -> str:
return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"'
def unquote(value: str) -> str:
value = value.strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'":
return value[1:-1]
return value
def parse(text: str) -> dict[str, str]:
found = {}
for line in text.splitlines():
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
found[key] = unquote(value)
return found
current = path.read_text(encoding="utf-8") if path.exists() else ""
have = parse(current)
tag = os.environ.get("IMAGE_TAG", "").strip() or have.get("IMAGE_TAG", "")
tag_file = root / "IMAGE_TAG"
if not tag and tag_file.exists():
tag = tag_file.read_text(encoding="utf-8").strip()
if not tag:
tag = "20261123"
site = os.environ.get("SITE_DOMAIN", "").strip() or have.get("SITE_DOMAIN", "www.gsrea.org.cn")
apex = site[4:] if site.startswith("www.") else site
names = os.environ.get("SITE_NAMES", "").strip() or have.get("SITE_NAMES", f"{site} {apex}")
files = os.environ.get("FILES_DOMAIN", "").strip() or have.get("FILES_DOMAIN", f"files.{apex}")
ai = os.environ.get("AI_DOMAIN", "").strip() or have.get("AI_DOMAIN", f"ai.{apex}")
edu = os.environ.get("EDU_DOMAIN", "").strip() or have.get("EDU_DOMAIN", f"edu.{apex}")
edu_h5 = os.environ.get("EDU_H5_DOMAIN", "").strip() or have.get("EDU_H5_DOMAIN", f"h5.{apex}")
edu_admin = os.environ.get("EDU_ADMIN_DOMAIN", "").strip() or have.get(
"EDU_ADMIN_DOMAIN", f"edu-admin.{apex}"
)
data_root = os.environ.get("DATA_ROOT", "").strip() or have.get("DATA_ROOT", "./data") or "./data"
cert_ready = (root / "certs" / "fullchain.pem").is_file() or (
root / "certs" / "live" / site / "fullchain.pem"
).is_file()
scheme = "https" if cert_ready else "http"
origin = f"{scheme}://{site}"
cors = f"{scheme}://{site}"
if apex != site:
cors = f"{cors},{scheme}://{apex}"
wanted = {
"IMAGE_TAG": tag,
"DATA_ROOT": data_root,
"SITE_DOMAIN": site,
"SITE_NAMES": names,
"FILES_DOMAIN": files,
"AI_DOMAIN": ai,
"EDU_DOMAIN": edu,
"EDU_H5_DOMAIN": edu_h5,
"EDU_ADMIN_DOMAIN": edu_admin,
"SITE_ORIGIN": origin,
"CORS_ORIGINS": cors,
"MINIO_PUBLIC_SECURE": "true" if cert_ready else "false",
"MINIO_BASE_URL": f"{scheme}://{files}/gsfdc-public",
"DIFY_PUBLIC_URL": f"{scheme}://{ai}",
"DIFY_VALUATION_API_KEY": "",
"MYSQL_DATABASE": "gsfdc",
"MYSQL_USER": "gsfdc",
"MYSQL_PASSWORD": secrets.token_hex(24),
"MYSQL_ROOT_PASSWORD": secrets.token_hex(24),
"PLAYEDU_DB_PASSWORD": secrets.token_hex(24),
"PLAYEDU_JWT_KEY": secrets.token_hex(24),
"REDIS_PASSWORD": secrets.token_hex(24),
"MINIO_ROOT_USER": "gsfdc",
"MINIO_ROOT_PASSWORD": secrets.token_hex(24),
"JWT_SECRET": secrets.token_hex(32),
"FIELD_AES_KEY": base64.b64encode(secrets.token_bytes(32)).decode("ascii"),
"DIFY_SSO_SECRET": secrets.token_urlsafe(48),
"GAODE_KEY": "",
"GAODE_SECURITY_JS_CODE": "",
"SMTP_HOST": "",
"SMTP_PORT": "465",
"SMTP_USER": "",
"SMTP_PASSWORD": "",
"SMTP_SENDER": "",
"SMTP_USE_SSL": "true",
"CERTBOT_EMAIL": "",
}
missing = [key for key in wanted if key not in have]
if path.exists() and not missing:
print(".env 已存在,保持不变")
raise SystemExit(0)
lines = []
if path.exists():
lines.append(current if current.endswith("\n") else current + "\n")
for key in missing:
lines.append(f"{key}={q(wanted[key])}\n")
path.write_text("".join(lines), encoding="utf-8")
os.chmod(path, 0o600)
print("已补上 .env 里缺少的项:" + ", ".join(missing))
raise SystemExit(0)
for key in wanted:
lines.append(f"{key}={q(wanted[key])}")
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
os.chmod(path, 0o600)
print(f"已生成 .env ,权限 600。站点 {origin} ,附件 {scheme}://{files}/")
print(f"Dify {scheme}://{ai}/ ,继续教育 {scheme}://{edu}/")
print("字段加密密钥只在这个文件里。不要重新生成,否则已加密的手机号和身份证无法解密。")
PY