55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Проверка расхождений реестра автоматизаций.
|
|
|
|
Реестр — это карта того, что запускается. Если он молча устарел, карта врёт.
|
|
Скрипт пересобирает реестр и сообщает в шину, если он изменился.
|
|
"""
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import urllib.request
|
|
|
|
REG = "/root/buryan-infra/ops/automations/registry.yaml"
|
|
GEN = "/root/buryan-infra/ops/scripts/build_automations_registry.py"
|
|
|
|
|
|
def event(sev, title, body=""):
|
|
try:
|
|
p = {"source": "hermes:registry", "type": "drift", "severity": sev,
|
|
"title": title, "body": (body or "")[:500],
|
|
"dedup_key": "registry-drift"}
|
|
req = urllib.request.Request("http://127.0.0.1:9130/event",
|
|
data=json.dumps(p).encode(), method="POST",
|
|
headers={"Content-Type": "application/json"})
|
|
urllib.request.urlopen(req, timeout=8).read()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def main():
|
|
try:
|
|
old = open(REG, encoding="utf-8").read()
|
|
except OSError:
|
|
old = ""
|
|
subprocess.run([sys.executable, GEN], capture_output=True, timeout=600)
|
|
try:
|
|
new = open(REG, encoding="utf-8").read()
|
|
except OSError:
|
|
print("реестр не создан")
|
|
return 1
|
|
# сравниваем без строки с датой генерации
|
|
strip = lambda s: "\n".join(l for l in s.splitlines() if not l.startswith("# Сгенерировано"))
|
|
if strip(old) != strip(new):
|
|
oldn = len([l for l in strip(old).splitlines() if l.startswith("- ")])
|
|
newn = len([l for l in strip(new).splitlines() if l.startswith("- ")])
|
|
event("medium", "Реестр автоматизаций изменился",
|
|
"было записей: %d, стало: %d" % (oldn, newn))
|
|
print("изменился: %d → %d" % (oldn, newn))
|
|
else:
|
|
print("без изменений")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|