life-noc/scripts/generate_bpm.py
2026-03-06 14:50:02 -05:00

136 lines
3.7 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
from pathlib import Path
import json
import re
import sys
import yaml
INPUT_FILE = Path("domains.yaml")
OUTPUT_DIR = Path("bpm")
OUTPUT_FILE = OUTPUT_DIR / "life-noc.json"
HOST_NAME = "life-noc"
def slugify(value: str) -> str:
value = value.strip().lower()
replacements = {
"à": "a", "â": "a", "ä": "a",
"ç": "c",
"é": "e", "è": "e", "ê": "e", "ë": "e",
"î": "i", "ï": "i",
"ô": "o", "ö": "o",
"ù": "u", "û": "u", "ü": "u",
"ÿ": "y",
"œ": "oe",
"æ": "ae",
"'": "",
"": "",
}
for old, new in replacements.items():
value = value.replace(old, new)
value = re.sub(r"[^a-z0-9\-]+", "-", value)
value = re.sub(r"-{2,}", "-", value)
return value.strip("-")
def build_service_name(domain_slug: str, item_name: str) -> str:
return f"{domain_slug}-{item_name.strip()}"
def make_leaf_node(service_name: str) -> dict:
return {
"type": "service",
"host": HOST_NAME,
"service": service_name
}
def make_domain_process(domain_label: str, domain_slug: str, services: list[dict]) -> dict:
leaves = []
for item in services:
service_name = build_service_name(domain_slug, item["name"])
leaves.append(make_leaf_node(service_name))
return {
"name": domain_label.upper(),
"operator": "worst",
"nodes": leaves
}
def main() -> int:
if not INPUT_FILE.exists():
print(f"Erreur: fichier introuvable: {INPUT_FILE}", file=sys.stderr)
return 1
with INPUT_FILE.open("r", encoding="utf-8") as f:
data = yaml.safe_load(f)
if not isinstance(data, dict) or "domains" not in data:
print("Erreur: le YAML doit contenir une clé racine 'domains'.", file=sys.stderr)
return 1
domains = data["domains"]
if not isinstance(domains, dict):
print("Erreur: 'domains' doit être un objet YAML.", file=sys.stderr)
return 1
processes = []
root_nodes = []
for raw_domain, services in domains.items():
if not isinstance(services, list):
print(f"Erreur: le domaine '{raw_domain}' doit contenir une liste.", file=sys.stderr)
return 1
for idx, item in enumerate(services, start=1):
if not isinstance(item, dict):
print(f"Erreur: entrée invalide dans '{raw_domain}' à la position {idx}.", file=sys.stderr)
return 1
missing = [key for key in ("name", "date", "notes") if key not in item]
if missing:
print(
f"Erreur: dans le domaine '{raw_domain}', entrée {idx}, champs manquants: {', '.join(missing)}",
file=sys.stderr,
)
return 1
domain_slug = slugify(str(raw_domain))
domain_label = str(raw_domain)
domain_process = make_domain_process(domain_label, domain_slug, services)
processes.append(domain_process)
root_nodes.append({
"type": "process",
"name": domain_label.upper()
})
root_process = {
"name": "LIFE-NOC",
"operator": "worst",
"nodes": root_nodes
}
bpm_document = {
"version": 1,
"processes": [root_process] + processes
}
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
with OUTPUT_FILE.open("w", encoding="utf-8") as f:
json.dump(bpm_document, f, ensure_ascii=False, indent=2)
print(f"BPM généré: {OUTPUT_FILE}")
print("Processus générés :")
print(" - LIFE-NOC")
for raw_domain in domains.keys():
print(f" - {str(raw_domain).upper()}")
return 0
if __name__ == "__main__":
raise SystemExit(main())