70 lines
3.0 KiB
Python
70 lines
3.0 KiB
Python
"""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
|
|
|
|
PLATFORM_ROOT = Path(__file__).resolve().parents[1]
|
|
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)
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:8000/api/v1/work-assistant")
|
|
args = parser.parse_args()
|
|
pms_root = args.pms_root.resolve()
|
|
if not (pms_root / "ruoyi-admin" / "pom.xml").is_file():
|
|
parser.error("--pms-root must be the management system repository")
|
|
os.chdir(PLATFORM_ROOT)
|
|
key = get_settings().work_assistant_shared_secret or ""
|
|
if len(key) < 32:
|
|
key = secrets.token_urlsafe(48)
|
|
env_path = PLATFORM_ROOT / ".env"
|
|
content = env_path.read_text(encoding="utf-8") if env_path.exists() else ""
|
|
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")
|
|
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.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|