91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
import os
|
|
from services.runner import run_cmd
|
|
|
|
|
|
def detect_pkg_manager() -> str | None:
|
|
if os.path.exists("/usr/bin/apt"):
|
|
return "apt"
|
|
if os.path.exists("/usr/bin/dnf"):
|
|
return "dnf"
|
|
if os.path.exists("/usr/bin/yum"):
|
|
return "yum"
|
|
if os.path.exists("/usr/bin/pacman"):
|
|
return "pacman"
|
|
return None
|
|
|
|
|
|
async def list_updates() -> str:
|
|
pm = detect_pkg_manager()
|
|
if not pm:
|
|
return "⚠️ No supported package manager found"
|
|
|
|
if pm == "apt":
|
|
await run_cmd(["sudo", "apt", "update"], timeout=300)
|
|
rc, out = await run_cmd(["apt", "list", "--upgradable"], timeout=120)
|
|
if rc != 0:
|
|
return f"❌ apt list failed\n```{out}```"
|
|
|
|
lines = []
|
|
for line in out.splitlines():
|
|
if not line or line.startswith("Listing..."):
|
|
continue
|
|
# Format: name/version ... [upgradable from: old]
|
|
name_ver = line.split(" ", 1)[0]
|
|
if "/" not in name_ver:
|
|
continue
|
|
name, new_ver = name_ver.split("/", 1)
|
|
old_ver = None
|
|
marker = "upgradable from: "
|
|
if marker in line:
|
|
old_ver = line.split(marker, 1)[1].rstrip("]").strip()
|
|
if old_ver:
|
|
lines.append(f"{name}: {old_ver} -> {new_ver}")
|
|
else:
|
|
lines.append(f"{name}: -> {new_ver}")
|
|
|
|
body = "\n".join(lines) if lines else "No updates"
|
|
return f"📦 Updates (apt)\n```{body}```"
|
|
|
|
if pm == "dnf":
|
|
rc, out = await run_cmd(["sudo", "dnf", "check-update"], timeout=300)
|
|
if rc in (0, 100):
|
|
return f"📦 Updates (dnf)\n```{out}```"
|
|
return f"❌ dnf check-update failed\n```{out}```"
|
|
|
|
if pm == "yum":
|
|
rc, out = await run_cmd(["sudo", "yum", "check-update"], timeout=300)
|
|
if rc in (0, 100):
|
|
return f"📦 Updates (yum)\n```{out}```"
|
|
return f"❌ yum check-update failed\n```{out}```"
|
|
|
|
if pm == "pacman":
|
|
rc, out = await run_cmd(["pacman", "-Qu"], timeout=120)
|
|
return f"📦 Updates (pacman)\n```{out}```" if rc == 0 else f"❌ pacman -Qu failed\n```{out}```"
|
|
|
|
return "⚠️ Unsupported package manager"
|
|
|
|
|
|
async def apply_updates() -> str:
|
|
pm = detect_pkg_manager()
|
|
if not pm:
|
|
return "⚠️ No supported package manager found"
|
|
|
|
if pm == "apt":
|
|
await run_cmd(["sudo", "apt", "update"], timeout=300)
|
|
rc, out = await run_cmd(["sudo", "apt-get", "-y", "upgrade"], timeout=1800)
|
|
return f"⬆️ Upgrade (apt)\n```{out}```" if rc == 0 else f"❌ apt upgrade failed\n```{out}```"
|
|
|
|
if pm == "dnf":
|
|
rc, out = await run_cmd(["sudo", "dnf", "-y", "upgrade"], timeout=1800)
|
|
return f"⬆️ Upgrade (dnf)\n```{out}```" if rc == 0 else f"❌ dnf upgrade failed\n```{out}```"
|
|
|
|
if pm == "yum":
|
|
rc, out = await run_cmd(["sudo", "yum", "-y", "update"], timeout=1800)
|
|
return f"⬆️ Upgrade (yum)\n```{out}```" if rc == 0 else f"❌ yum update failed\n```{out}```"
|
|
|
|
if pm == "pacman":
|
|
rc, out = await run_cmd(["sudo", "pacman", "-Syu", "--noconfirm"], timeout=1800)
|
|
return f"⬆️ Upgrade (pacman)\n```{out}```" if rc == 0 else f"❌ pacman -Syu failed\n```{out}```"
|
|
|
|
return "⚠️ Unsupported package manager"
|