88f7f0cfca
Restructure manual-trading-hub settings with CSS tabs matching instance layout; add password API writing HUB_USERNAME/HUB_PASSWORD to hub .env. Co-authored-by: Cursor <cursoragent@cursor.com>
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""中控 .env 读写与 PM2 重启。"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from lib.env.env_file_lib import apply_env_updates, load_env_file_into_environ
|
|
|
|
HUB_DIR = Path(__file__).resolve().parent
|
|
|
|
|
|
def hub_env_path() -> str:
|
|
return str(HUB_DIR / ".env")
|
|
|
|
|
|
def update_hub_credentials(*, new_password: str, new_username: str | None = None) -> list[str]:
|
|
updates: dict[str, str] = {"HUB_PASSWORD": new_password}
|
|
if new_username:
|
|
updates["HUB_USERNAME"] = new_username
|
|
path = hub_env_path()
|
|
changed = apply_env_updates(path, updates)
|
|
if changed:
|
|
load_env_file_into_environ(path)
|
|
return changed
|
|
|
|
|
|
def restart_hub_pm2() -> dict[str, Any]:
|
|
app_name = (os.getenv("PM2_APP_NAME") or "").strip() or "manual-trading-hub"
|
|
if not sys.platform.startswith("linux"):
|
|
return {"ok": False, "msg": "仅 Linux 服务器支持 PM2 重启", "app": app_name}
|
|
try:
|
|
proc = subprocess.run(
|
|
["pm2", "restart", app_name, "--update-env"],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=120,
|
|
)
|
|
ok = proc.returncode == 0
|
|
return {
|
|
"ok": ok,
|
|
"app": app_name,
|
|
"msg": (proc.stdout or proc.stderr or "").strip()[:500],
|
|
"returncode": proc.returncode,
|
|
}
|
|
except FileNotFoundError:
|
|
return {"ok": False, "msg": "未找到 pm2 命令", "app": app_name}
|
|
except subprocess.TimeoutExpired:
|
|
return {"ok": False, "msg": "pm2 restart 超时", "app": app_name}
|
|
except Exception as e:
|
|
return {"ok": False, "msg": str(e), "app": app_name}
|