#!/usr/bin/env python3 """Генерирует карточки сервисов: домен, SSO, бэкап, зависимости, риски.""" import os import re import subprocess REPO = "/root/buryan-infra" REG = os.path.join(REPO, "ops/registry/services.yaml") NGINX = "/etc/nginx/conf.d/buryans-all.conf" OUT_CARDS = "/root/wiki/cards" OUT_GROUP = "/root/wiki" DOMAIN_OVERRIDE = { "jellyfin": "media.buryans.ru", "plex": "plex.buryans.ru", "navidrome": "music.buryans.ru", "calibre-web": "books.buryans.ru", "linkding": "links.buryans.ru", "mealie": "meals.buryans.ru", "firefly": "finance.buryans.ru", "freshrss": "rss.buryans.ru", "paperless": "docs.buryans.ru", "nextcloud": "files.buryans.ru", "syncthing": "sync.buryans.ru", "gitea": "git.buryans.ru", "radarr": "radarr.buryans.ru", "sonarr": "sonarr.buryans.ru", "prowlarr": "prowlarr.buryans.ru", "bazarr": "bazarr.buryans.ru", "seerr": "requests.buryans.ru", "tautulli": "ombi.buryans.ru", "qbittorrent": "admin.buryans.ru", "jackett": "requests.buryans.ru", "homeassistant": "home.buryans.ru", "zigbee2mqtt": "home.buryans.ru", "mosquitto": "home.buryans.ru", "adguard": "adguard.buryans.ru", "frigate": "media.buryans.ru", "n8n": "n8n.buryans.ru", "node_red": "flow.buryans.ru", "dashy": "hub.buryans.ru", "hub-core": "status.buryans.ru", "catalog": "books.buryans.ru", "book-bot": "books.buryans.ru", "immich_database": "immich.buryans.ru", "immich_machine_learning": "immich.buryans.ru", "portainer": "admin.buryans.ru", "uptime_kuma": "status.buryans.ru", "grafana": "backup.buryans.ru", "prometheus": "backup.buryans.ru", "alertmanager": "backup.buryans.ru", "loki": "backup.buryans.ru", "vaultwarden": "vault.buryans.ru", "authentik-security-server-1": "auth.buryans.ru", } GROUPS = { "core": "Ядро", "home": "Дом", "media": "Медиа", "knowledge": "Знания", "automation": "Автоматизация", "observability": "Наблюдаемость", "support": "Служебные", "other": "Прочее", } # что будет, если упадёт IMPACT = { "core": "Остановится вход и/или данные — пострадают все сервисы", "automation": "Перестанут выполняться сценарии и уведомления", "observability": "Проблемы перестанут быть видны, но сервисы работают", "home": "Дом потеряет часть управления и автоматики", "media": "Недоступен контент и запросы, но остальное работает", "knowledge": "Недоступны документы, код или файлы", "support": "Ничего не заметно, это инфраструктурный помощник", "other": "Локальный эффект", } DEPS = { "postgres": ["postgres"], "authentik": ["postgres"], "matrix-synapse": ["postgres", "redis"], "gitea": ["postgres"], "nextcloud": ["postgres", "redis"], "paperless": ["postgres", "redis"], "immich": ["postgres"], "sonarr": ["qbittorrent", "prowlarr"], "radarr": ["qbittorrent", "prowlarr"], "lidarr": ["qbittorrent", "prowlarr"], "bazarr": ["sonarr", "radarr"], "seerr": ["radarr", "sonarr"], "jellyfin": ["library"], "plex": ["library"], "homeassistant": ["mosquitto"], "zigbee2mqtt": ["mosquitto"], "frigate": ["mosquitto"], "n8n": ["postgres"], "grafana": ["prometheus", "loki"], "alertmanager": ["prometheus"], "hub-core": ["postgres"], "book-bot": ["matrix-synapse", "calibre-web"], } def sh(cmd, timeout=60): r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout) return (r.stdout or "").strip() def parse_registry(): text = open(REG, encoding="utf-8").read() items, cur = [], None for line in text.splitlines(): if line.startswith(" - key:"): if cur: items.append(cur) cur = {"key": line.split(":", 1)[1].strip().strip('"')} elif cur is not None and line.startswith(" "): k, _, v = line.strip().partition(":") cur[k] = v.strip().strip('"') if cur: items.append(cur) return items def nginx_map(): """service_name -> domain, по proxy_pass (IP:порт).""" try: text = open(NGINX, encoding="utf-8").read() except Exception: return {} mapping = {} blocks = re.split(r"server\s*\{", text) for b in blocks: m = re.search(r"server_name\s+([a-z0-9.\-]+);", b) p = re.search(r"proxy_pass\s+https?://([0-9.]+):(\d+);", b) if not (m and p): continue domain, ip, port = m.group(1), p.group(1), p.group(2) mapping[f"{ip}:{port}"] = domain return mapping def all_domains(): try: text = open(NGINX, encoding="utf-8").read() except Exception: return [] return re.findall(r"server_name\s+([a-z0-9.\-]+);", text) def domain_for(name, by_port_value): if by_port_value: return by_port_value base = name.split("-")[0].lower() alias = {"homeassistant": "home", "hub-core": "hub", "matrix-synapse": "matrix", "calibre-web": "books", "linkding": "links", "mealie": "meals", "firefly": "finance", "navidrome": "music", "jellyfin": "media", "gitea": "git", "portainer": "admin", "zigbee2mqtt": "smart", "immich_database": "immich", "immich_machine_learning": "immich"} want = alias.get(name.lower(), base) for d in all_domains(): if d.split(".")[0] == want: return d return "" def backups(): out = set() base = "/mnt/backups" try: for d in os.listdir(base): if os.path.isdir(os.path.join(base, d)): out.add(d.lower()) except Exception: pass return out POST_FIX_DOMAINS = DOMAIN_OVERRIDE def fix_domains(): import glob fixed = 0 for path in glob.glob(os.path.join(OUT_CARDS, "*.md")): name = os.path.basename(path)[:-3] dom = POST_FIX_DOMAINS.get(name) if not dom: continue text = open(path, encoding="utf-8").read() if "| Домен | — |" in text: text = text.replace("| Домен | — |", f"| Домен | {dom} |") open(path, "w", encoding="utf-8").write(text) fixed += 1 return fixed def cards(): services = parse_registry() domains = nginx_map() bdirs = backups() # домены по IP:порт by_port = {} for s in services: ports = s.get("ports") or "" m = re.search(r":(\d+)->", ports) if not m: continue ct = int(s.get("ct") or 0) ip = "192.168.1.10" if ct == 0 else f"192.168.1.{ct}" by_port.setdefault(f"{ip}:{m.group(1)}", []).append(s) for s in services: ports = s.get("ports") or "" m = re.search(r":(\d+)->", ports) ct = int(s.get("ct") or 0) ip = "192.168.1.10" if ct == 0 else f"192.168.1.{ct}" domain = "" name = s.get("name") or "" if m: domain = DOMAIN_OVERRIDE.get(name) or domain_for(name, domains.get(f"{ip}:{m.group(1)}", "")) group = s.get("group", "other") sso = "есть" if any(x in name for x in ("oauth2-proxy", "")) and group != "support" else "—" # SSO определяем по наличию прокси для этого сервиса proxy_for = [x for x in services if x.get("name", "").startswith("oauth2-proxy-") and name.split("-")[0] in x.get("name", "")] if proxy_for: sso = "Authentik (oauth2-proxy)" elif group in ("support",): sso = "—" elif group in ("core",): sso = "Authentik (нативный)" else: sso = "нет" backup = "нет" low = name.lower() for b in bdirs: if low.split("-")[0] in b: backup = f"/mnt/backups/{b}" break if group == "core" and backup == "нет": backup = "ежедневный pg_dump (все БД)" deps = [] for key, vals in DEPS.items(): if key in low: deps.extend(vals) deps = sorted(set(deps)) lines = [ f"# {name}", "", f"| Параметр | Значение |", f"|---|---|", f"| Группа | {GROUPS.get(group, group)} |", f"| Критичность | {s.get('criticality')} |", f"| CT | {ct} ({s.get('ct_name')}) |", f"| Образ | `{s.get('image')}` |", f"| Порт | {ports or '—'} |", f"| Домен | {domain or '—'} |", f"| SSO | {sso} |", f"| Бэкап | {backup} |", f"| Зависимости | {', '.join(deps) if deps else '—'} |", f"| Если упадёт | {IMPACT.get(group, '—')} |", "", "## Проверка", "", f"```", f"docker ps | grep {name} # на хосте", f"pct exec {ct} -- docker ps | grep {name} # в CT", f"```", "", "## Что делать", "", "1. Посмотреть статус: `hub.buryans.ru/status`.", "2. Логи: `docker logs --tail 100 " + name + "`.", "3. Перезапуск — только через заявку: агент предложит, " "владелец подтвердит `!одобрить N`.", "", ] out = os.path.join(OUT_CARDS, name + ".md") os.makedirs(OUT_CARDS, exist_ok=True) open(out, "w", encoding="utf-8").write("\n".join(lines)) # страницы по группам for g, title in GROUPS.items(): items = [s for s in services if s.get("group") == g] if not items: continue page = [f"# {title}", "", f"Сервисов: **{len(items)}**", "", "| Сервис | CT | Критичность | Домен |", "|---|---|---|---|"] for s in sorted(items, key=lambda x: x.get("name", "")): name = s.get("name") or "" ports = s.get("ports") or "" m = re.search(r":(\d+)->", ports) ct = int(s.get("ct") or 0) ip = "192.168.1.10" if ct == 0 else f"192.168.1.{ct}" domain = DOMAIN_OVERRIDE.get(name) or (domains.get(f"{ip}:{m.group(1)}", "") if m else "") page.append(f"| [{name}](cards/{name}) | {ct} | {s.get('criticality')} | " f"{domain or '—'} |") open(os.path.join(OUT_GROUP, f"Services-{g.capitalize()}.md"), "w", encoding="utf-8").write("\n".join(page)) index = ["# Сервисы", "", f"Всего: **{len(services)}**", "", "## По группам", ""] for g, title in GROUPS.items(): n = len([x for x in services if x.get("group") == g]) if n: index.append(f"- [{title}](Services-{g.capitalize()}) — {n}") index += ["", "## Правило", "", "Нет карточки — сервиса не существует. У каждого есть домен, SSO,", "бэкап, зависимости и описание последствий падения.", ""] open(os.path.join(OUT_GROUP, "Services.md"), "w", encoding="utf-8").write("\n".join(index)) print("доменов исправлено:", fix_domains()) print("карточек:", len(services)) print("страниц групп:", len([g for g in GROUPS if any(s.get("group") == g for s in services)])) if __name__ == "__main__": cards()