127 lines
6.7 KiB
Python
127 lines
6.7 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Une sonde `par_application` : un script, un service, un nom permis, par application cliente.
|
||
|
|
|
||
|
|
POURQUOI (2026-11-06). Le dorsal rend une sonde par application (`app-<application>`) au lieu
|
||
|
|
d'une pour toutes. Le nom du service n'est plus ecrit dans `meta/supervision.yml` : il se
|
||
|
|
derive du plan, en quatre endroits qui doivent dire la meme chose. Ces cas les rendent sur un
|
||
|
|
plan fictif (deux applications sur `web-dorsal-01`, aucune sur `web-dorsal-02`) :
|
||
|
|
1. la sonde, branche python et branche statique, se lit en bash et frappe ce qu'il faut ;
|
||
|
|
2. Icinga cree `app-monregistraire` et `app-vitrine` sur le premier dorsal, rien sur le
|
||
|
|
second, et jamais un service `app` nu ;
|
||
|
|
3. le compte d'API permet ces deux noms, et eux seuls (pas de joker) ;
|
||
|
|
4. la vue metier les nomme a l'identique ;
|
||
|
|
5. P-sondes : la fiche affiche `app-<application>`.
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
import re
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import jinja2
|
||
|
|
|
||
|
|
RACINE = Path(__file__).resolve().parents[2]
|
||
|
|
ROLES = RACINE / "roles"
|
||
|
|
sys.path.insert(0, str(RACINE / "scripts"))
|
||
|
|
|
||
|
|
echecs = 0
|
||
|
|
|
||
|
|
APPS = [{"key": "monregistraire", "value": {"hote": "web-dorsal-01", "source": "exemple/monregistraire"}},
|
||
|
|
{"key": "vitrine", "value": {"hote": "web-dorsal-01", "source": "exemple/vitrine"}}]
|
||
|
|
SONDES = {"serveur_web_dorsal": [{"nom": "app", "par_application": True, "metier": "web", "ttl": 5400,
|
||
|
|
"raison": "r"}],
|
||
|
|
"client_sante": [{"nom": "disque", "metier": "socle", "ttl": 5400, "raison": "r"}]}
|
||
|
|
GROUPES = {"serveur_web_dorsal": ["web-dorsal-01", "web-dorsal-02"],
|
||
|
|
"client_sante": ["web-dorsal-01", "web-dorsal-02"], "serveur_debian": []}
|
||
|
|
|
||
|
|
|
||
|
|
def verifier(cond: bool, quoi: str) -> None:
|
||
|
|
global echecs
|
||
|
|
print(("OK " if cond else "ECHEC ") + quoi)
|
||
|
|
echecs += 0 if cond else 1
|
||
|
|
|
||
|
|
|
||
|
|
def environnement(texte: str) -> tuple[jinja2.Environment, str]:
|
||
|
|
options = {}
|
||
|
|
premiere, _, reste = texte.partition("\n")
|
||
|
|
if premiere.startswith("#jinja2:"):
|
||
|
|
options = dict(re.findall(r"(\w+):'([^']*)'", premiere))
|
||
|
|
texte = reste
|
||
|
|
env = jinja2.Environment(undefined=jinja2.StrictUndefined, extensions=["jinja2.ext.do"], **options)
|
||
|
|
env.filters["intersect"] = lambda a, b: [x for x in a if x in b]
|
||
|
|
env.filters["regex_replace"] = lambda v, m, r="": re.sub(m, r.replace("\\1", "\\g<1>"), str(v))
|
||
|
|
env.filters["unique"] = lambda v: list(dict.fromkeys(v))
|
||
|
|
env.filters["to_json"] = lambda v, **k: json.dumps(v)
|
||
|
|
return env, texte
|
||
|
|
|
||
|
|
|
||
|
|
def rendre(chemin: Path, **variables) -> str:
|
||
|
|
env, texte = environnement(chemin.read_text(encoding="utf-8"))
|
||
|
|
return env.from_string(texte).render(**variables)
|
||
|
|
|
||
|
|
|
||
|
|
def bash_lit(script: str) -> bool:
|
||
|
|
with tempfile.NamedTemporaryFile("w", suffix=".sh", delete=False) as f:
|
||
|
|
f.write(script)
|
||
|
|
return subprocess.run(["bash", "-n", f.name], capture_output=True).returncode == 0
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> int:
|
||
|
|
gabarit = ROLES / "serveur_web_dorsal" / "templates" / "sonde-app.sh.j2"
|
||
|
|
commun = {"serveur_web_dorsal_sonde_codes": "HTTP/1.1 200", "serveur_web_dorsal_port": 80}
|
||
|
|
py = rendre(gabarit, serveur_web_dorsal_sonde={"nom": "monregistraire", "execution": "python",
|
||
|
|
"port": 23456, "sante": "/sante", "version": "v1.0.0",
|
||
|
|
"nom_servi": "registre.exemple.org"}, **commun)
|
||
|
|
st = rendre(gabarit, serveur_web_dorsal_sonde={"nom": "vitrine", "execution": "statique", "port": 1,
|
||
|
|
"sante": "/", "version": "v2.0.0",
|
||
|
|
"nom_servi": "vitrine.exemple.org"}, **commun)
|
||
|
|
verifier(bash_lit(py) and bash_lit(st), "1. les deux branches se lisent en bash")
|
||
|
|
verifier("setops-app-${NOM}.service" in py and "-p '23456'" in py and "NOM='monregistraire'" in py,
|
||
|
|
"1. python : l'unite et le port local de l'application")
|
||
|
|
verifier("-H 'vitrine.exemple.org' -I 127.0.0.1" in st and "systemctl" not in st,
|
||
|
|
"1. statique : le nom servi, aucune unite")
|
||
|
|
|
||
|
|
variables = {"serveur_icinga_sondes": SONDES, "groups": GROUPES, "serveur_icinga_fraicheur_sondes": 5400,
|
||
|
|
"serveur_icinga_applications_clientes": APPS, "hostvars": {}, "role_path": str(ROLES / "serveur_icinga")}
|
||
|
|
conf = rendre(ROLES / "serveur_icinga" / "templates" / "setops-sondes.conf.j2", **variables)
|
||
|
|
services = re.findall(r'object Service "([^"]+)" \{\n import "setops-rapport-attendu"\n host_name = "([^"]+)"', conf)
|
||
|
|
verifier(("app-monregistraire", "web-dorsal-01") in services and ("app-vitrine", "web-dorsal-01") in services,
|
||
|
|
"2. Icinga : un service par application du premier dorsal")
|
||
|
|
verifier(not any(h == "web-dorsal-02" and n.startswith("app") for n, h in services),
|
||
|
|
"2. Icinga : aucun service d'application sur le dorsal qui n'en porte pas")
|
||
|
|
verifier(not any(n == "app" for n, _ in services), "2. Icinga : jamais un service `app` nu")
|
||
|
|
verifier(("disque", "web-dorsal-02") in services, "2. Icinga : les autres sondes inchangees")
|
||
|
|
|
||
|
|
ligne = next(l for l in (ROLES / "serveur_icinga" / "templates" / "setops-api-users.conf.j2")
|
||
|
|
.read_text(encoding="utf-8").splitlines() if l.strip().startswith("filter ="))
|
||
|
|
env, _ = environnement("")
|
||
|
|
filtre = env.from_string(ligne).render(**variables)
|
||
|
|
verifier('service.name == "app-monregistraire"' in filtre and 'service.name == "app-vitrine"' in filtre,
|
||
|
|
"3. compte d'API : les deux noms permis")
|
||
|
|
verifier('match("app' not in filtre and 'service.name == "app"' not in filtre,
|
||
|
|
"3. compte d'API : ni joker ni nom nu")
|
||
|
|
|
||
|
|
bpm = rendre(ROLES / "serveur_icingaweb2" / "templates" / "bpm-membres.json.j2",
|
||
|
|
serveur_icingaweb2_sondes=SONDES, groups=GROUPES, hostvars={},
|
||
|
|
serveur_icingaweb2_applications_clientes=APPS, serveur_icingaweb2_bpm_sauvegarde_attendue=[],
|
||
|
|
serveur_icingaweb2_bpm_hote_sauvegarde="", setops_materiel_hotes=[],
|
||
|
|
role_path=str(ROLES / "serveur_icingaweb2"))
|
||
|
|
membres = json.loads(bpm[bpm.index("{"):]) if "{" in bpm else {}
|
||
|
|
web = [tuple(x) for x in membres.get("web", [])]
|
||
|
|
verifier(web == [("web-dorsal-01", "app-monregistraire"), ("web-dorsal-01", "app-vitrine")],
|
||
|
|
f"4. vue metier : les memes noms ({web})")
|
||
|
|
|
||
|
|
import fiche_role
|
||
|
|
verifier(fiche_role.nom_sonde({"nom": "app", "par_application": True}) == "app-<application>"
|
||
|
|
and fiche_role.nom_sonde({"nom": "disque"}) == "disque", "5. la fiche nomme `app-<application>`")
|
||
|
|
|
||
|
|
print(f"\n{'TOUT VERT' if not echecs else f'{echecs} ECHEC(S)'}")
|
||
|
|
return 1 if echecs else 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|