life-noc/patch_days_until_due_v2.sh

145 lines
4.9 KiB
Bash
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 bash
set -euo pipefail
ROOT="${1:-.}"
need_file() {
local f="$1"
if [[ ! -f "$ROOT/$f" ]]; then
echo "Fichier introuvable: $f" >&2
exit 1
fi
}
echo "==> Vérification"
need_file "scripts/generate_services.py"
need_file "domains.yaml"
echo "==> Sauvegarde"
mkdir -p "$ROOT/.patch-backup-days-until-due-v2"
cp -a "$ROOT/scripts/generate_services.py" "$ROOT/.patch-backup-days-until-due-v2/generate_services.py.bak"
cp -a "$ROOT/domains.yaml" "$ROOT/.patch-backup-days-until-due-v2/domains.yaml.bak"
echo "==> Patch de scripts/generate_services.py"
python3 - "$ROOT/scripts/generate_services.py" <<'PYEOF'
from pathlib import Path
import sys
path = Path(sys.argv[1])
text = path.read_text(encoding="utf-8")
# 1) Ajouter les seuils *_lt / unknown_gte si absents
needle = ' if "critical_gte" in thresholds:\n lines.append(f\' vars.life_noc_threshold_critical_gte = "{icinga_escape(str(thresholds["critical_gte"]))}"\')\n'
addition = (
' if "unknown_gte" in thresholds:\n'
' lines.append(f\' vars.life_noc_threshold_unknown_gte = "{icinga_escape(str(thresholds["unknown_gte"]))}"\')\n'
' if "ok_lt" in thresholds:\n'
' lines.append(f\' vars.life_noc_threshold_ok_lt = "{icinga_escape(str(thresholds["ok_lt"]))}"\')\n'
' if "warning_lt" in thresholds:\n'
' lines.append(f\' vars.life_noc_threshold_warning_lt = "{icinga_escape(str(thresholds["warning_lt"]))}"\')\n'
' if "critical_lt" in thresholds:\n'
' lines.append(f\' vars.life_noc_threshold_critical_lt = "{icinga_escape(str(thresholds["critical_lt"]))}"\')\n'
)
if 'vars.life_noc_threshold_unknown_gte' not in text:
if needle not in text:
raise SystemExit("Impossible de trouver le bloc des seuils dans generate_services.py")
text = text.replace(needle, needle + addition, 1)
# 2) Élargir validate_probe aux deux types
old_variants = [
' probe_type = str(probe.get("type", "")).strip()\n if probe_type not in {"elapsed_time"}:\n raise ValueError("seul probe.type=elapsed_time est supporté en v1")\n',
' probe_type = str(probe.get("type", "")).strip()\n if probe_type not in {"elapsed_time"}:\n raise ValueError("seul probe.type=elapsed_time est supporté en v1")\r\n',
]
new_block = ' probe_type = str(probe.get("type", "")).strip()\n if probe_type not in {"elapsed_time", "days_until_due"}:\n raise ValueError("seuls probe.type=elapsed_time et days_until_due sont supportés")\n'
replaced = False
for old in old_variants:
if old in text:
text = text.replace(old, new_block, 1)
replaced = True
break
if not replaced:
# si déjà patché, on accepte
if new_block.strip() not in text:
raise SystemExit("Bloc validate_probe introuvable ou déjà trop différent dans generate_services.py")
path.write_text(text, encoding="utf-8")
print("generate_services.py patché")
PYEOF
echo "==> Création du store data/inputs/obligations-legales-personnelles.yaml"
mkdir -p "$ROOT/data/inputs"
cat > "$ROOT/data/inputs/obligations-legales-personnelles.yaml" <<'YAMLEOF'
renouvellement-permis-conduire:
value: "2026-09-20"
captured_at: "2026-03-14T23:30:00Z"
origin: manual
YAMLEOF
echo "==> Mise à jour de domains.yaml"
python3 - "$ROOT/domains.yaml" <<'PYEOF'
from pathlib import Path
import sys
import yaml
path = Path(sys.argv[1])
data = yaml.safe_load(path.read_text(encoding="utf-8"))
domain = "obligations-legales-personnelles"
items = data.get("domains", {}).get(domain)
if not isinstance(items, list):
raise SystemExit(f"Domaine introuvable ou invalide: {domain}")
target = None
for item in items:
if isinstance(item, dict) and item.get("name") == "renouvellement-permis-conduire":
target = item
break
if target is None:
raise SystemExit("Item 'renouvellement-permis-conduire' introuvable")
target["date"] = "2026-09-20"
target["notes"] = "Vérifier la proximité de léchéance du permis"
target["probe"] = {
"type": "days_until_due",
"source": {
"type": "manual_date",
"inputs_file": "/opt/life-noc/data/inputs/obligations-legales-personnelles.yaml",
"item_key": "renouvellement-permis-conduire",
},
"metric": {
"unit": "days",
},
"thresholds": {
"critical_lt": 7,
"warning_lt": 30,
"ok_lt": 90,
"unknown_gte": 90,
},
"policy": {
"on_error": "critical",
},
}
path.write_text(yaml.safe_dump(data, sort_keys=False, allow_unicode=True), encoding="utf-8")
print("domains.yaml mis à jour")
PYEOF
echo "==> Régénération"
(
cd "$ROOT"
python3 scripts/generate_services.py
)
echo
echo "Patch terminé."
echo "Étapes suivantes :"
echo " python3 scripts/generate_services.py"
echo " make check"
echo " make deploy-with-bpm"
echo
echo "Test utile ensuite :"
echo " python3 scripts/life_noc_input.py set obligations-legales-personnelles renouvellement-permis-conduire 2026-03-20"