Add package update checks and upgrades
This commit is contained in:
68
services/updates.py
Normal file
68
services/updates.py
Normal file
@@ -0,0 +1,68 @@
|
||||
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)
|
||||
return f"📦 Updates (apt)\n```{out}```" if rc == 0 else f"❌ apt list failed\n```{out}```"
|
||||
|
||||
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"
|
||||
Reference in New Issue
Block a user