"""
comms-agent — единый административный бот buryan-hub.
Собран слиянием двух прежних ботов:
* бот на CT 108 — /issues и heartbeat в agent_registry;
* бот на VPS — двусторонний Matrix и /restart через Remediation API.
Работает в режиме опроса Telegram, поэтому у бота НЕ должен быть
установлен webhook: Telegram не разрешает одновременно getUpdates и webhook.
Webhook закреплён за отдельным ботом Auth (@Adm_buryans_bot) для потока
самообслуживания F6-2.
"""
import os
import asyncio
import base64
import logging
import aiohttp
import asyncpg
from aiogram import Bot, Dispatcher
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.filters import Command
from aiogram.types import (
CallbackQuery,
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
)
from nio import AsyncClient, RoomMessageText
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
log = logging.getLogger("comms-agent")
dp = Dispatcher()
bot_instance: Bot | None = None
# --- Конфигурация -----------------------------------------------------------
ADMIN_ID = str(os.getenv("ADMIN_ID") or "0")
GITEA_URL = (os.getenv("GITEA_URL") or "http://192.168.1.140:3000").rstrip("/")
GITEA_REPO = os.getenv("GITEA_REPO") or "agent-qwen/buryan-infra"
GITEA_TOKEN = os.getenv("GITEA_TOKEN") or ""
PG_DSN = dict(
host=os.getenv("PG_HOST"),
database=os.getenv("PG_DB"),
user=os.getenv("PG_USER"),
password=os.getenv("PG_PASSWORD"),
port=int(os.getenv("PG_PORT") or 5432),
)
REMEDIATION_API_URL = (os.getenv("REMEDIATION_API_URL") or "").rstrip("/")
REMEDIATION_API_KEY = os.getenv("REMEDIATION_API_KEY") or ""
MATRIX_ROOM_ID = os.getenv("MATRIX_ROOM_ID") or ""
matrix_client = AsyncClient(os.getenv("MATRIX_HOMESERVER") or "https://matrix.buryans.ru")
HELP_TEXT = (
"Доступные команды:\n"
"/status — состояние хостов\n"
"/issues — открытые задачи Gitea\n"
"/runbook <сервис> — ранбук сервиса\n"
"/restart <сервис> — перезапуск через Remediation API"
)
gitea_headers = {"Authorization": f"token {GITEA_TOKEN}"}
def is_admin(obj) -> bool:
user = getattr(obj, "from_user", None)
if user is None and getattr(obj, "message", None) is not None:
user = obj.message.from_user
return user is not None and str(user.id) == ADMIN_ID
# --- Matrix -----------------------------------------------------------------
async def send_matrix_message(text: str) -> None:
if not MATRIX_ROOM_ID or not matrix_client.access_token:
return
html_text = text.replace("\n", "
").replace("**", "").replace("`", "")
try:
await matrix_client.room_send(
room_id=MATRIX_ROOM_ID,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": text,
"format": "org.matrix.custom.html",
"formatted_body": html_text,
},
)
except Exception as exc: # noqa: BLE001 - сообщение не должно ронять бота
log.error("Matrix send error: %s", exc)
async def reply(text: str, source: str, message: Message | None = None) -> None:
"""Отправляет ответ туда, откуда пришла команда, и дублирует в Matrix."""
if source == "telegram" and message is not None:
await message.answer(text)
else:
await send_matrix_message(text)
# --- Логика команд ----------------------------------------------------------
async def handle_status_logic(source: str, message: Message | None = None) -> None:
try:
conn = await asyncpg.connect(**PG_DSN)
rows = await conn.fetch(
"SELECT host, ip, mem, disk FROM host_states ORDER BY host ASC LIMIT 10;"
)
await conn.close()
if not rows:
text = "Данных о хостах пока нет."
else:
lines = [
f"{r['host']} ({r['ip']}): RAM {r['mem'] or '?'}%, диск {r['disk'] or '?'}%"
for r in rows
]
text = "Хосты:\n" + "\n".join(lines)
except Exception as exc: # noqa: BLE001
text = f"Ошибка базы: {exc}"
await reply(text, source, message)
async def handle_issues_logic(source: str, message: Message | None = None) -> None:
url = f"{GITEA_URL}/api/v1/repos/{GITEA_REPO}/issues?state=open&limit=10"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=gitea_headers) as resp:
if resp.status != 200:
text = f"Gitea вернул {resp.status}"
else:
issues = await resp.json()
if not issues:
text = "Открытых задач нет."
else:
lines = [f"#{i['number']} {i['title']}" for i in issues]
text = f"Открытые задачи ({len(issues)}):\n" + "\n".join(lines)
except Exception as exc: # noqa: BLE001
text = f"Ошибка Gitea: {exc}"
await reply(text, source, message)
async def fetch_runbook(service: str) -> str | None:
"""Ищет ранбук сначала в services/, затем в knowledge/runbooks/."""
for path in (
f"services/{service}/runbook.md",
f"knowledge/runbooks/{service}.md",
):
url = f"{GITEA_URL}/api/v1/repos/{GITEA_REPO}/contents/{path}?ref=main"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=gitea_headers) as resp:
if resp.status == 200:
data = await resp.json()
return base64.b64decode(data["content"]).decode("utf-8")
except Exception as exc: # noqa: BLE001
log.error("Gitea fetch error for %s: %s", path, exc)
return None
async def handle_runbook_logic(
service: str, source: str, message: Message | None = None
) -> None:
content = await fetch_runbook(service)
if content is None:
await reply(
f"Ранбук для «{service}» не найден ни в services/, ни в knowledge/runbooks/.",
source,
message,
)
return
# Отправляем без разметки: содержимое ранбука ломает парсер Markdown.
text = f"Ранбук: {service}\n\n{content[:3800]}"
if source == "telegram" and message is not None:
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(
text="Перезапустить", callback_data=f"restart_{service}"
)
]
]
)
await message.answer(text, reply_markup=keyboard)
else:
await send_matrix_message(text)
async def handle_restart_logic(
service: str, source: str, message: Message | None = None
) -> None:
if not REMEDIATION_API_URL:
await reply("Remediation API не настроен (REMEDIATION_API_URL пуст).", source, message)
return
await reply(f"Запрашиваю перезапуск: {service}...", source, message)
headers = {
"X-API-Key": REMEDIATION_API_KEY,
"Content-Type": "application/json",
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(
f"{REMEDIATION_API_URL}/api/v1/remediate",
json={"action": "restart_service", "target": service},
headers=headers,
timeout=aiohttp.ClientTimeout(total=120),
) as resp:
data = await resp.json()
if resp.status == 200:
text = f"Готово: {data.get('message', 'ok')}"
else:
text = f"Отказ: {data.get('detail', resp.status)}"
except Exception as exc: # noqa: BLE001
text = f"Ошибка связи с Remediation API: {exc}"
await reply(text, source, message)
async def handle_runbook_and_restart(service: str, source: str, message: Message | None = None) -> None:
await handle_runbook_logic(service, source, message)
# --- Telegram ---------------------------------------------------------------
@dp.message(Command("start", "help"))
async def cmd_start(message: Message) -> None:
if is_admin(message):
await message.answer("comms-agent на связи.\n\n" + HELP_TEXT)
else:
await message.answer(f"Доступ запрещён. Ваш ID: {message.from_user.id}")
@dp.message(Command("status"))
async def cmd_status(message: Message) -> None:
if is_admin(message):
await handle_status_logic("telegram", message)
@dp.message(Command("issues"))
async def cmd_issues(message: Message) -> None:
if is_admin(message):
await handle_issues_logic("telegram", message)
@dp.message(Command("runbook"))
async def cmd_runbook(message: Message) -> None:
if not is_admin(message):
return
parts = message.text.split(maxsplit=1)
if len(parts) < 2:
await message.answer("Использование: /runbook <сервис>")
return
await handle_runbook_logic(parts[1].strip().lower(), "telegram", message)
@dp.message(Command("restart"))
async def cmd_restart(message: Message) -> None:
if not is_admin(message):
return
parts = message.text.split(maxsplit=1)
if len(parts) < 2:
await message.answer("Использование: /restart <сервис>")
return
await handle_restart_logic(parts[1].strip().lower(), "telegram", message)
@dp.callback_query(lambda c: (c.data or "").startswith("restart_"))
async def on_restart_callback(callback: CallbackQuery) -> None:
if not is_admin(callback):
await callback.answer("Доступ запрещён", show_alert=True)
return
service = callback.data.split("_", 1)[1]
await callback.message.edit_text(f"Запрашиваю перезапуск: {service}...")
await handle_restart_logic(service, "telegram")
await callback.answer()
# --- Matrix приём команд ----------------------------------------------------
async def matrix_message_callback(room, event) -> None:
if room.room_id != MATRIX_ROOM_ID:
return
if not isinstance(event, RoomMessageText):
return
if event.sender == matrix_client.user_id:
return
text = (event.body or "").strip()
log.info("Matrix команда от %s: %s", event.sender, text)
if text.startswith(("/status", "!status")):
await handle_status_logic("matrix")
elif text.startswith(("/issues", "!issues")):
await handle_issues_logic("matrix")
elif text.startswith(("/runbook ", "!runbook ")):
await handle_runbook_logic(text.split(" ", 1)[1].lower(), "matrix")
elif text.startswith(("/restart ", "!restart ")):
await handle_restart_logic(text.split(" ", 1)[1].lower(), "matrix")
else:
await send_matrix_message(f"Неизвестная команда: {text}\n\n{HELP_TEXT}")
async def matrix_sync_loop() -> None:
matrix_client.access_token = os.getenv("MATRIX_ACCESS_TOKEN")
matrix_client.user_id = os.getenv(
"MATRIX_USER_ID", "@comms_agent:matrix.buryans.ru"
)
if not matrix_client.access_token:
log.warning("MATRIX_ACCESS_TOKEN не задан — Matrix-канал выключен")
return
try:
await matrix_client.sync(timeout=30000)
except Exception as exc: # noqa: BLE001
log.error("Первичная синхронизация Matrix не удалась: %s", exc)
return
matrix_client.add_event_callback(matrix_message_callback, RoomMessageText)
log.info("Matrix-слушатель запущен")
while True:
try:
await matrix_client.sync(timeout=30000)
except Exception as exc: # noqa: BLE001
log.error("Matrix sync error: %s", exc)
await asyncio.sleep(5)
# --- Реестр агентов ---------------------------------------------------------
async def heartbeat() -> None:
while True:
try:
conn = await asyncpg.connect(**PG_DSN)
await conn.execute(
"INSERT INTO agent_registry (name, domain, status, last_heartbeat) "
"VALUES ('comms-agent', 'comms', 'active', CURRENT_TIMESTAMP) "
"ON CONFLICT (name) DO UPDATE SET status='active', "
"last_heartbeat=CURRENT_TIMESTAMP"
)
await conn.close()
except Exception as exc: # noqa: BLE001
log.error("Heartbeat: %s", exc)
await asyncio.sleep(30)
# --- Запуск -----------------------------------------------------------------
async def main() -> None:
global bot_instance
log.info("Запуск comms-agent (Telegram + Matrix)...")
session = AiohttpSession(proxy=os.getenv("HTTP_PROXY") or None)
bot_instance = Bot(token=os.getenv("BOT_TOKEN"), session=session)
me = await bot_instance.get_me()
log.info("Бот @%s (id=%s), админ ID=%s", me.username, me.id, ADMIN_ID)
webhook = await bot_instance.get_webhook_info()
if webhook.url:
log.error(
"У бота установлен webhook (%s) — опрос работать не будет. "
"Нужен отдельный бот без webhook.",
webhook.url,
)
asyncio.create_task(heartbeat())
asyncio.create_task(matrix_sync_loop())
try:
await dp.start_polling(bot_instance)
finally:
await session.close()
if __name__ == "__main__":
asyncio.run(main())