This commit is contained in:
zyp
2026-11-18 16:01:50 +08:00
parent 0dc48ad092
commit 0c3d0c1ce4
30 changed files with 1525 additions and 93 deletions
+35 -6
View File
@@ -1,7 +1,9 @@
"""Prepare a local PMS bridge without printing credentials or touching business databases."""
import argparse
import json
import os
from pathlib import Path
import re
import secrets
import sys
@@ -11,6 +13,31 @@ sys.path.insert(0, str(PLATFORM_ROOT))
from app.core.config import get_settings # noqa: E402
def update_connection(content: str, base_url: str, key: str) -> str:
"""Update only the connection keys, preserving other assistant settings and comments."""
newline = "\r\n" if "\r\n" in content else "\n"
if content and not content.endswith("\n"):
content += newline
sections = list(re.finditer(
r"(?m)^assistant:[ \t]*(?:#[^\r\n]*)?\r?\n(?:(?:[ \t]+[^\r\n]*|[ \t]*)\r?\n)*",
content,
))
if len(sections) > 1:
raise ValueError("application.yml contains multiple assistant sections")
block = sections[0].group() if sections else "assistant:" + newline
for name, value in (("base-url", base_url), ("shared-secret", key)):
pattern = r"(?m)^ " + re.escape(name) + r":[^\r\n]*(\r?\n)"
replacement = " " + name + ": " + json.dumps(value) + newline
block, count = re.subn(pattern, lambda _: replacement, block)
if count > 1:
raise ValueError("application.yml contains duplicate assistant." + name)
if count == 0:
block = block.rstrip("\r\n") + newline + replacement
if sections:
return content[:sections[0].start()] + block + content[sections[0].end():]
return content.rstrip() + newline * 2 + block
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--pms-root", required=True, type=Path)
@@ -25,14 +52,16 @@ def main() -> None:
key = secrets.token_urlsafe(48)
env_path = PLATFORM_ROOT / ".env"
content = env_path.read_text(encoding="utf-8") if env_path.exists() else ""
import re
content = re.sub(r"(?m)^\s*WORK_ASSISTANT_SHARED_SECRET\s*=.*(?:\r?\n|$)", "", content)
env_path.write_text(content.rstrip() + "\nWORK_ASSISTANT_SHARED_SECRET=" + key + "\n", encoding="utf-8")
config = pms_root / ".work-assistant.properties"
content = "assistant.base-url=" + args.base_url + "\nassistant.shared-secret=" + key + "\n"
# Properties use ASCII; no real credential is ever committed or printed.
config.write_text(content, encoding="ascii")
print("Local assistant bridge configured. Credentials are stored only in ignored local files.")
yaml_config = pms_root / "ruoyi-admin/src/main/resources/application.yml"
with yaml_config.open(encoding="utf-8", newline="") as stream:
content = stream.read()
content = update_connection(content, args.base_url, key)
with yaml_config.open("w", encoding="utf-8", newline="") as stream:
stream.write(content)
print("Assistant endpoint and credential written to application.yml; other settings preserved.")
print("Credential not printed.")
print("No business database migration is required.")