Add package update checks and upgrades
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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="⬅️ Назад")],
|
||||
],
|
||||
|
||||
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