92 lines
3.3 KiB
Python
Executable File
92 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Сверяет compose-файл с живым контейнером ДО его удаления.
|
|
|
|
Зачем: 28.10.2026 генератор терял аргументы запуска (в docker inspect поле
|
|
называется Cmd, а не Command), из-за чего loki, alertmanager и exportarr
|
|
поднялись без нужных флагов и легли. Проверка ловит такое расхождение заранее.
|
|
|
|
python3 check-compose-parity.py 134
|
|
|
|
Сравнивает для каждого сервиса: image, entrypoint, command, набор ключей env,
|
|
монтирования, порты. Печатает расхождения и возвращает 1, если они есть.
|
|
"""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
REPO = "/root/buryan-infra"
|
|
|
|
|
|
def pct(ct, *a):
|
|
return subprocess.run(["pct", "exec", ct, "--", *a], capture_output=True, text=True)
|
|
|
|
|
|
def live(ct):
|
|
ids = pct(ct, "docker", "ps", "-q").stdout.split()
|
|
if not ids:
|
|
return {}
|
|
data = json.loads(pct(ct, "docker", "inspect", *ids).stdout)
|
|
out = {}
|
|
for c in data:
|
|
name = c["Name"].lstrip("/")
|
|
cfg = c["Config"]
|
|
out[name] = {
|
|
"image": cfg["Image"],
|
|
"entrypoint": cfg.get("Entrypoint"),
|
|
"cmd": cfg.get("Cmd"),
|
|
"env": sorted(e.split("=")[0] for e in (cfg.get("Env") or []) if "=" in e and not e.startswith("PATH=")),
|
|
"mounts": sorted((m["Source"], m["Destination"]) for m in (c.get("Mounts") or [])),
|
|
"ports": sorted(c["HostConfig"].get("PortBindings") or {}),
|
|
}
|
|
return out
|
|
|
|
|
|
def wanted(ct):
|
|
p = f"{REPO}/deploy/ct{ct}/docker-compose.yml"
|
|
if not os.path.exists(p):
|
|
return {}
|
|
y = yaml.safe_load(open(p, encoding="utf-8")) or {}
|
|
out = {}
|
|
for name, svc in (y.get("services") or {}).items():
|
|
out[svc.get("container_name", name)] = {
|
|
"image": svc.get("image"),
|
|
"entrypoint": svc.get("entrypoint"),
|
|
"cmd": svc.get("command"),
|
|
"env": sorted((svc.get("environment") or {}).keys()),
|
|
"mounts": sorted(
|
|
(v.split(":")[0], v.split(":")[1]) for v in (svc.get("volumes") or []) if ":" in v
|
|
),
|
|
"ports": sorted(str(x).split(":")[-1] for x in (svc.get("ports") or [])),
|
|
}
|
|
return out
|
|
|
|
|
|
def main():
|
|
ct = sys.argv[1]
|
|
L, W = live(ct), wanted(ct)
|
|
problems = 0
|
|
for name, w in W.items():
|
|
l = L.get(name)
|
|
if not l:
|
|
print(f"{name}: контейнера нет (будет создан)")
|
|
continue
|
|
for field in ("image", "entrypoint", "cmd"):
|
|
if w.get(field) != l.get(field):
|
|
print(f"{name}.{field}: compose={w.get(field)} живой={l.get(field)}")
|
|
problems += 1
|
|
if w.get("env") and l.get("env"):
|
|
missing = set(w["env"]) - set(l["env"])
|
|
if missing:
|
|
print(f"{name}.env: в compose лишние ключи {sorted(missing)}")
|
|
problems += 1
|
|
print(f"CT{ct}: сервисов {len(W)}, расхождений {problems}")
|
|
return 1 if problems else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|