Files

91 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Рабочий секретарь: состояние рабочего контура (ВКС/Presale)."""
import json
import os
import subprocess
import time
import urllib.request
REPO_API = ("http://192.168.1.140:3000/api/v1/repos/infrastructure/buryan-work")
TOKEN = open("/root/.bwork-token", encoding="utf-8").read().strip() if os.path.exists(
"/root/.bwork-token") else ""
WORK = "/opt/buryan-work"
def sh(cmd, timeout=60):
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
return (r.stdout or "").strip()
def repo_state():
try:
req = urllib.request.Request(REPO_API, headers={"Authorization": "token " + TOKEN})
with urllib.request.urlopen(req, timeout=15) as r:
d = json.loads(r.read() or b"{}")
return {"ok": True, "updated": d.get("updated_at"), "size": d.get("size")}
except Exception as e:
return {"ok": False, "error": str(e)[:80]}
def local_state():
out = {"files": 0, "clients": 0, "meetings": 0, "dirty": False, "behind": None}
if not os.path.isdir(WORK):
return out
for root, _, files in os.walk(WORK):
if "/.git" in root:
continue
out["files"] += len(files)
if "/clients" in root:
out["clients"] += len(files)
if "/meetings" in root:
out["meetings"] += len(files)
out["dirty"] = bool(sh(f"cd {WORK} && git status --porcelain 2>/dev/null"))
return out
def tools_state():
return {
"transcribe": os.path.exists("/usr/local/bin/meeting-transcribe"),
"faster_whisper": sh("/opt/music-tools/venv/bin/python -c "
"'import faster_whisper;print(1)' 2>/dev/null") == "1",
}
def main():
rep = repo_state()
loc = local_state()
tools = tools_state()
problems = []
if not rep.get("ok"):
problems.append(f"репозиторий недоступен: {rep.get('error')}")
if loc["dirty"]:
problems.append("в рабочей копии есть незакоммиченные изменения")
if not tools["transcribe"]:
problems.append("нет утилиты транскрипции")
if loc["clients"] == 0:
problems.append("ни одного клиента в базе")
if loc["meetings"] == 0:
problems.append("ни одной заметки встречи")
report = {
"ts": time.strftime("%Y-%m-%dT%H:%M:%S"),
"repo": rep,
"local": loc,
"tools": tools,
"problems": problems,
}
os.makedirs("/var/lib/hub-agents/journal", exist_ok=True)
with open("/var/lib/hub-agents/journal/" + time.strftime("%Y-%m-%d")
+ "-work.json", "w", encoding="utf-8") as f:
json.dump(report, f, ensure_ascii=False, indent=1)
print("Рабочий секретарь")
print(f" репозиторий: {'ok' if rep.get('ok') else 'нет связи'} | файлов: {loc['files']}")
print(f" клиенты: {loc['clients']} | встречи: {loc['meetings']}")
print(f" транскрипция: {'есть' if tools['transcribe'] else 'нет'}")
for p in problems:
print(" ⚠️", p)
if __name__ == "__main__":
main()