diff --git a/handlers/system.py b/handlers/system.py index b15eb21..254e32d 100644 --- a/handlers/system.py +++ b/handlers/system.py @@ -7,6 +7,8 @@ from system_checks import security, disks from app import cfg from services.http_checks import get_url_checks, check_url import asyncio +from services.queue import enqueue +from services.updates import list_updates, apply_updates @dp.message(F.text == "๐Ÿ’ฝ Disks") @@ -50,3 +52,29 @@ async def urls(msg: Message): await msg.answer("\n".join(lines), reply_markup=system_kb) asyncio.create_task(worker()) + + +@dp.message(F.text == "๐Ÿ“ฆ Updates") +async def updates_list(msg: Message): + if not is_admin_msg(msg): + return + + async def job(): + text = await list_updates() + await msg.answer(text, reply_markup=system_kb, parse_mode="Markdown") + + pos = await enqueue("pkg-updates", job) + await msg.answer(f"๐Ÿ•“ Updates queued (#{pos})", reply_markup=system_kb) + + +@dp.message(F.text == "โฌ†๏ธ Upgrade") +async def updates_apply(msg: Message): + if not is_admin_msg(msg): + return + + async def job(): + text = await apply_updates() + await msg.answer(text, reply_markup=system_kb, parse_mode="Markdown") + + pos = await enqueue("pkg-upgrade", job) + await msg.answer(f"๐Ÿ•“ Upgrade queued (#{pos})", reply_markup=system_kb) diff --git a/keyboards.py b/keyboards.py index b45e86f..2afd969 100644 --- a/keyboards.py +++ b/keyboards.py @@ -47,7 +47,8 @@ artifacts_kb = ReplyKeyboardMarkup( system_kb = ReplyKeyboardMarkup( keyboard=[ [KeyboardButton(text="๐Ÿ’ฝ Disks"), KeyboardButton(text="๐Ÿ” Security")], - [KeyboardButton(text="๐ŸŒ URLs")], + [KeyboardButton(text="๐ŸŒ URLs"), KeyboardButton(text="๐Ÿ“ฆ Updates")], + [KeyboardButton(text="โฌ†๏ธ Upgrade")], [KeyboardButton(text="๐Ÿ”„ Reboot")], [KeyboardButton(text="โฌ…๏ธ ะะฐะทะฐะด")], ], diff --git a/services/updates.py b/services/updates.py new file mode 100644 index 0000000..988e36b --- /dev/null +++ b/services/updates.py @@ -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"