Files
stackpilot/backend/routers/system.py
menzeljandClaude Opus 4.8 b553c1b861 Phase 2: Volume Wizard, GPU & device passthrough (0.2.0)
Backend:
- gpu_service: detect NVIDIA (nvidia-smi) + AMD/Intel (/dev/dri, sysfs);
  inject helpers (nvidia deploy.reservations, /dev/dri + groups + LIBVA)
- volume_service: list/orphaned/prune volumes; NFS/SMB/named/bind/tmpfs
  YAML generation (generate-yaml)
- device_service: USB/TTY/DRI detection + sandboxed host path browser
- compose_edit_service: server-side merge of volume/gpu/device fragments
- routers: volumes (+host paths), editor (services/add-volume/set-gpu/
  add-device/remove-device/set-privileged), system gpus+devices
- compose: bind-mount /dev:ro for detection

Frontend:
- split-pane StackEditor with helper panel (service picker + tabs)
- VolumeWizard (bind/named/nfs/smb/tmpfs) + HostPathBrowser
- GPUSelector, DevicePanel; api clients for volumes/editor/system

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-07 16:35:22 +00:00

106 lines
3.0 KiB
Python

"""Host / Docker system information."""
from __future__ import annotations
import os
import shutil
from fastapi import APIRouter, Depends
from auth import get_current_user
from config import settings
from docker_client import DockerError, get_client, safe_call
from models.user import User
from services import device_service, gpu_service
router = APIRouter(prefix="/api/system", tags=["system"])
def _read_proc(path: str) -> str:
full = os.path.join(settings.HOST_PROC_PATH, path)
if not os.path.isfile(full):
full = os.path.join("/proc", path)
try:
with open(full, "r", encoding="utf-8") as fh:
return fh.read()
except OSError:
return ""
def _mem_info() -> dict:
info = {}
for line in _read_proc("meminfo").splitlines():
parts = line.split(":")
if len(parts) == 2:
key = parts[0].strip()
val = parts[1].strip().split()[0]
try:
info[key] = int(val) * 1024 # kB -> bytes
except ValueError:
pass
total = info.get("MemTotal", 0)
available = info.get("MemAvailable", info.get("MemFree", 0))
return {"total": total, "available": available, "used": max(total - available, 0)}
def _uptime() -> float:
raw = _read_proc("uptime")
try:
return float(raw.split()[0])
except (IndexError, ValueError):
return 0.0
def _cpu_count() -> int:
return os.cpu_count() or 0
def _disk_usage() -> dict:
try:
usage = shutil.disk_usage(settings.DATA_DIR)
return {"total": usage.total, "used": usage.used, "free": usage.free}
except OSError:
return {"total": 0, "used": 0, "free": 0}
@router.get("/info")
def system_info(_user: User = Depends(get_current_user)) -> dict:
docker_version = ""
host_os = ""
containers_running = 0
containers_total = 0
try:
client = get_client()
version = safe_call(client.version)
docker_version = version.get("Version", "")
info = safe_call(client.info)
host_os = info.get("OperatingSystem", "")
containers_running = info.get("ContainersRunning", 0)
containers_total = info.get("Containers", 0)
except DockerError as exc:
docker_version = f"unavailable ({exc.error})"
return {
"docker_version": docker_version,
"host_os": host_os,
"hostname": os.uname().nodename,
"cpu_cores": _cpu_count(),
"ram": _mem_info(),
"disk": _disk_usage(),
"uptime_seconds": _uptime(),
"containers_running": containers_running,
"containers_total": containers_total,
"gpus": [g.to_dict() for g in gpu_service.detect_gpus()],
}
@router.get("/gpus")
def gpus(_user: User = Depends(get_current_user)) -> list[dict]:
"""Re-detect GPUs live."""
return [g.to_dict() for g in gpu_service.detect_gpus()]
@router.get("/devices")
def devices(_user: User = Depends(get_current_user)) -> dict:
"""List host USB / serial / DRI devices for passthrough."""
return device_service.detect_devices()