feat: simplify Discover view into socket cards & backend upgrades

This commit is contained in:
Hitonabi
2026-06-26 07:53:41 +02:00
parent b90cef3968
commit bc3abb127a
21 changed files with 2019 additions and 715 deletions
+62 -15
View File
@@ -83,45 +83,92 @@ def updates() -> dict:
"models": len(ups), "model_list": ups}
def _run(cmd: list[str]) -> dict:
def _run(cmd: list[str], sudo_password: str | None = None) -> dict:
actual_cmd = list(cmd)
has_sudo = False
if cmd and cmd[0] == "sudo":
has_sudo = True
# If we have a password, use -S instead of -n
if sudo_password is not None:
if "-n" in actual_cmd:
actual_cmd = [x for x in actual_cmd if x != "-n"]
if "-S" not in actual_cmd:
actual_cmd.insert(1, "-S")
else:
# Force -n to fail cleanly if password is required
if "-S" in actual_cmd:
actual_cmd = [x for x in actual_cmd if x != "-S"]
if "-n" not in actual_cmd:
actual_cmd.insert(1, "-n")
try:
p = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
input_data = (sudo_password + "\n") if (has_sudo and sudo_password is not None) else None
p = subprocess.run(actual_cmd, input=input_data, capture_output=True, text=True, timeout=120)
err_msg = p.stderr or ""
if p.returncode != 0 and ("a password is required" in err_msg or "password" in err_msg.lower() or "sudo:" in err_msg):
if sudo_password is not None:
return {"ok": False, "status": "incorrect_password", "out": p.stdout or "", "err": "Falsches Sudo-Passwort."}
return {"ok": False, "status": "password_required", "out": p.stdout or "", "err": "Sudo-Passwort erforderlich."}
return {"ok": p.returncode == 0, "out": (p.stdout or "")[-4000:], "err": (p.stderr or "")[-2000:]}
except Exception as exc: # noqa: BLE001
return {"ok": False, "out": "", "err": str(exc)}
def restart_service(name: str) -> dict:
def check_sudo_needs_password(sudo_password: str | None = None) -> dict | None:
"""Checks if sudo needs a password. Returns error dict if password required/incorrect, else None."""
res = _run(["sudo", "true"], sudo_password=sudo_password)
if not res["ok"]:
return res
return None
def restart_service(name: str, sudo_password: str | None = None) -> dict:
if name in SYSTEM_SERVICES:
return _run(["sudo", "-n", "systemctl", "restart", name])
if err := check_sudo_needs_password(sudo_password):
return err
return _run(["sudo", "systemctl", "restart", name], sudo_password=sudo_password)
if name in USER_SERVICES:
return _run(["systemctl", "--user", "restart", name])
return {"ok": False, "err": f"Dienst '{name}' nicht erlaubt."}
def logs(service: str, lines: int = 200) -> dict:
def logs(service: str, lines: int = 200, sudo_password: str | None = None) -> dict:
lines = max(1, min(lines, 1000))
if service in SYSTEM_SERVICES:
cmd = ["sudo", "-n", "journalctl", "-u", service, "-n", str(lines), "--no-pager"]
if err := check_sudo_needs_password(sudo_password):
return err
cmd = ["sudo", "journalctl", "-u", service, "-n", str(lines), "--no-pager"]
r = _run(cmd, sudo_password=sudo_password)
elif service in USER_SERVICES:
cmd = ["journalctl", "--user", "-u", service, "-n", str(lines), "--no-pager"]
r = _run(cmd)
else:
return {"ok": False, "text": "", "err": "Dienst nicht erlaubt."}
r = _run(cmd)
return {"ok": r["ok"], "text": r["out"] or r["err"]}
def os_update_job() -> str:
cmd = "sudo -n apt-get update && sudo -n DEBIAN_FRONTEND=noninteractive apt-get upgrade -y"
return jobengine.start_job(["bash", "-c", cmd], "OS-Update (apt)")
def os_update_job(sudo_password: str | None = None) -> dict:
if err := check_sudo_needs_password(sudo_password):
return err
cmd = "sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y"
job_id = jobengine.start_job(["bash", "-c", cmd], "OS-Update (apt)", sudo_password=sudo_password)
return {"ok": True, "job_id": job_id}
def engine_update_job() -> str | None:
def engine_update_job(sudo_password: str | None = None) -> dict | None:
if not ENGINE_UPDATE_CMD:
return None
cmd = f"{ENGINE_UPDATE_CMD} && sudo -n systemctl restart llama-swap"
return jobengine.start_job(["bash", "-c", cmd], "Engine-Update (llama.cpp)")
if err := check_sudo_needs_password(sudo_password):
return err
cmd = f"{ENGINE_UPDATE_CMD} && sudo systemctl restart llama-swap"
job_id = jobengine.start_job(["bash", "-c", cmd], "Engine-Update (llama.cpp)", sudo_password=sudo_password)
return {"ok": True, "job_id": job_id}
def reboot() -> dict:
return _run(["sudo", "-n", "reboot"])
def reboot(sudo_password: str | None = None) -> dict:
if err := check_sudo_needs_password(sudo_password):
return err
return _run(["sudo", "reboot"], sudo_password=sudo_password)