- Hermes CLI wrapper using subprocess.run for gateway status - Gateway API: status list (with filter), events (placeholder), start/stop/restart with audit logging - Frontend GatewayPage: timeline, filter bar, status table with 10s polling, operation modals - usePolling hook for periodic data refresh Co-Authored-By: Claude <noreply@anthropic.com>
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""Hermes CLI 封装 — 通过 subprocess 调用 hermes 命令。"""
|
|
import json
|
|
import logging
|
|
import subprocess
|
|
|
|
from app.config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _run_hermes(*args: str, timeout: int = 15) -> subprocess.CompletedProcess:
|
|
"""执行 hermes CLI 命令,返回 CompletedProcess。"""
|
|
return subprocess.run(
|
|
[settings.hermes_cli_path, *args],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=timeout,
|
|
)
|
|
|
|
|
|
def gateway_status_all() -> list[dict]:
|
|
"""执行 `hermes gateway status`,返回解析后的 Gateway 状态列表。
|
|
|
|
出错时返回空列表并记录告警日志。
|
|
"""
|
|
try:
|
|
proc = _run_hermes("gateway", "status")
|
|
if proc.returncode != 0:
|
|
logger.warning("hermes gateway status 返回非零: %s", proc.stderr.strip())
|
|
return []
|
|
data = json.loads(proc.stdout)
|
|
if isinstance(data, list):
|
|
return data
|
|
return data.get("gateways", [])
|
|
except FileNotFoundError:
|
|
logger.warning("Hermes CLI 未找到: %s", settings.hermes_cli_path)
|
|
except json.JSONDecodeError as e:
|
|
logger.warning("解析 hermes gateway status 输出失败: %s", e)
|
|
except subprocess.TimeoutExpired:
|
|
logger.warning("hermes gateway status 执行超时")
|
|
except Exception as e:
|
|
logger.warning("调用 hermes gateway status 异常: %s", e)
|
|
return []
|