Fix SSH key handling for monitoring
This commit is contained in:
@@ -44,7 +44,7 @@ def get_monitoring_overview(
|
|||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return one lightweight monitoring row per configured machine."""
|
"""Return one lightweight monitoring row per configured machine."""
|
||||||
machines = store.list_machines()
|
machines = store.list_machines()
|
||||||
rows = [collect_machine_overview(machine) for machine in machines]
|
rows = [collect_machine_overview(machine, store) for machine in machines]
|
||||||
poller = get_monitoring_poller().snapshot()
|
poller = get_monitoring_poller().snapshot()
|
||||||
enabled_count = sum(1 for machine in machines if machine.get("enabled"))
|
enabled_count = sum(1 for machine in machines if machine.get("enabled"))
|
||||||
logger.info("Dashboard monitoring machines=%s enabled=%s", len(machines), enabled_count)
|
logger.info("Dashboard monitoring machines=%s enabled=%s", len(machines), enabled_count)
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ from media_library_viewer_api.services.settings_store import SettingsStore
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def build_machine_client(machine: dict[str, Any]):
|
def build_machine_client(machine: dict[str, Any], store: SettingsStore):
|
||||||
"""Build the appropriate command client for a machine definition."""
|
"""Build the appropriate command client for a machine definition."""
|
||||||
mode = str(machine.get("mode") or "local").strip().lower()
|
mode = str(machine.get("mode") or "local").strip().lower()
|
||||||
if mode == "local":
|
if mode == "local":
|
||||||
@@ -41,12 +41,23 @@ def build_machine_client(machine: dict[str, Any]):
|
|||||||
key_directory = str(machine.get("key_directory") or "").strip()
|
key_directory = str(machine.get("key_directory") or "").strip()
|
||||||
key_name = str(machine.get("key_name") or "").strip()
|
key_name = str(machine.get("key_name") or "").strip()
|
||||||
key_path = f"{key_directory}/{key_name}" if key_directory and key_name else None
|
key_path = f"{key_directory}/{key_name}" if key_directory and key_name else None
|
||||||
|
private_key = str(machine.get("ssh_private_key") or "")
|
||||||
|
passphrase = str(machine.get("ssh_private_key_passphrase") or "")
|
||||||
|
ssh_key_id = str(machine.get("ssh_key_id") or "").strip()
|
||||||
|
if ssh_key_id:
|
||||||
|
ssh_key = store.get_ssh_key(ssh_key_id)
|
||||||
|
if ssh_key:
|
||||||
|
private_key = str(ssh_key.get("private_key") or private_key)
|
||||||
|
passphrase = str(ssh_key.get("passphrase") or passphrase)
|
||||||
|
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
return RemoteSSHClient(
|
return RemoteSSHClient(
|
||||||
host=str(machine.get("host") or ""),
|
host=str(machine.get("host") or ""),
|
||||||
username=str(machine.get("username") or ""),
|
username=str(machine.get("username") or ""),
|
||||||
port=int(machine.get("port") or 22),
|
port=int(machine.get("port") or 22),
|
||||||
key_filename=key_path,
|
key_filename=key_path,
|
||||||
|
private_key=private_key or None,
|
||||||
|
private_key_passphrase=passphrase or None,
|
||||||
password=str(machine.get("password") or "") or None,
|
password=str(machine.get("password") or "") or None,
|
||||||
known_hosts_path=str(settings.ssh_known_hosts_file),
|
known_hosts_path=str(settings.ssh_known_hosts_file),
|
||||||
)
|
)
|
||||||
@@ -96,7 +107,7 @@ def run_machine_operation(
|
|||||||
) -> Any:
|
) -> Any:
|
||||||
"""Run a machine operation, record history, and optionally raise on failure."""
|
"""Run a machine operation, record history, and optionally raise on failure."""
|
||||||
started = time.perf_counter()
|
started = time.perf_counter()
|
||||||
client = build_machine_client(machine)
|
client = build_machine_client(machine, store)
|
||||||
try:
|
try:
|
||||||
result = callback(client)
|
result = callback(client)
|
||||||
duration_ms = int((time.perf_counter() - started) * 1000)
|
duration_ms = int((time.perf_counter() - started) * 1000)
|
||||||
@@ -202,7 +213,13 @@ def _summarize_metric_samples(samples: list[dict[str, Any]], field: str) -> dict
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def collect_machine_overview(machine: dict[str, Any], *, metrics_window_seconds: int = 600, metrics_limit: int = 70_000) -> dict[str, Any]:
|
def collect_machine_overview(
|
||||||
|
machine: dict[str, Any],
|
||||||
|
store: SettingsStore,
|
||||||
|
*,
|
||||||
|
metrics_window_seconds: int = 600,
|
||||||
|
metrics_limit: int = 70_000,
|
||||||
|
) -> dict[str, Any]:
|
||||||
"""Collect a lightweight machine overview without recording history."""
|
"""Collect a lightweight machine overview without recording history."""
|
||||||
overview: dict[str, Any] = {
|
overview: dict[str, Any] = {
|
||||||
"machine": {k: machine.get(k) for k in ("id", "name", "mode", "enabled", "host", "port", "username", "media_root", "path_prefix", "notes")},
|
"machine": {k: machine.get(k) for k in ("id", "name", "mode", "enabled", "host", "port", "username", "media_root", "path_prefix", "notes")},
|
||||||
@@ -222,7 +239,7 @@ def collect_machine_overview(machine: dict[str, Any], *, metrics_window_seconds:
|
|||||||
"disk": None,
|
"disk": None,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
client: Any = build_machine_client(machine)
|
client: Any = build_machine_client(machine, store)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
overview["status_error"] = str(exc)
|
overview["status_error"] = str(exc)
|
||||||
overview["metrics_error"] = str(exc)
|
overview["metrics_error"] = str(exc)
|
||||||
|
|||||||
Reference in New Issue
Block a user