This repository has been archived on 2026-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
Set-OPS/scripts/applications.py
Daniel Allaire abd6395a13 Faire de l'application une entite de premiere classe (BD multi-portee)
Une VM peut porter plusieurs applications ; c'est l'application, pas l'hote,
a laquelle une base se lie via son DSN.

- docs/applications.yml : nouveau registre application -> groupe, hote.
- docs/bases-donnees.yml : champ 'portee' (application|groupe|hote, defaut
  groupe). Une application A (groupe G, hote H) recoit les bases ou
  (portee=application ET consommateur=A) OU (portee=groupe ET consommateur=G)
  OU (portee=hote ET consommateur=H). Plusieurs DSN par application.
- inventory_rules.py : charger/valider_applications, applications_de_hote,
  bases_de_application, PORTEES_BD ; valider_bases croise portee=application
  avec le registre des applications.
- CLI : scripts/applications.py + bases_donnees.py --portee ; cibles make
  applications / applications-verifier ; integre a inventaire-verifier.
- GUI : vue Applications (CRUD), portee + consommateur dynamique dans Bases,
  vue Chaine par application (DSN resolus par hote).
- filter_plugins/registres.py : meme regle de resolution exposee a Ansible
  (source unique). Playbooks web_dorsaux/web_frontaux iterent sur les
  applications de l'hote (scaffold ; deploiement applicatif s'y insere).

Services en grappe (keycloak/icingadb/forgejo) restent en portee groupe.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 13:38:30 -04:00

91 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Lit et valide le registre des applications Set-OPS (docs/applications.yml)."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import yaml
from inventory_rules import (
bases_de_application,
chaine_connexion,
charger_applications,
charger_bases_donnees,
valider_applications,
)
RACINE = Path(__file__).resolve().parents[1]
FICHIER = RACINE / "docs/applications.yml"
FICHIER_BASES = RACINE / "docs/bases-donnees.yml"
def ecrire(registre: dict) -> None:
entete = (
"# Registre des applications Set-OPS (application = entite pivot).\n"
"# Edite par make inventaire-ui ou scripts/applications.py.\n"
"# Une VM peut porter plusieurs applications ; une base se lie a une application.\n"
"---\n"
)
with FICHIER.open("w", encoding="utf-8") as fichier:
fichier.write(entete)
yaml.safe_dump({"applications": registre.get("applications", {}) or {}},
fichier, default_flow_style=False, sort_keys=False, allow_unicode=True)
def lister(registre: dict, bases: dict) -> None:
apps = registre.get("applications", {})
if not apps:
print("Aucune application declaree.")
return
for nom, app in apps.items():
print(f"{nom} [groupe {app.get('groupe', '?')}, hote {app.get('hote', '?')}]")
for entree in bases_de_application(bases, application=nom,
groupe=app.get("groupe"), hote=app.get("hote")):
base = entree["base"]
portee = base.get("portee", "groupe")
print(f" DSN ({portee}/{base.get('usage', '-')}): {chaine_connexion(base, entree['serveur'])}")
def main() -> int:
parser = argparse.ArgumentParser(description="Registre des applications Set-OPS.")
sub = parser.add_subparsers(dest="commande", required=True)
sub.add_parser("lister", help="Affiche les applications et leurs DSN resolus.")
sub.add_parser("verifier", help="Valide la coherence du registre.")
pa = sub.add_parser("ajouter", help="Ajoute ou met a jour une application.")
pa.add_argument("--id", required=True)
pa.add_argument("--groupe", required=True)
pa.add_argument("--hote", required=True)
pr = sub.add_parser("retirer", help="Retire une application.")
pr.add_argument("--id", required=True)
args = parser.parse_args()
try:
registre = charger_applications(FICHIER)
if args.commande == "lister":
lister(registre, charger_bases_donnees(FICHIER_BASES))
elif args.commande == "verifier":
valider_applications(registre)
print("Registre des applications valide.")
elif args.commande == "ajouter":
registre["applications"][args.id] = {"groupe": args.groupe, "hote": args.hote}
valider_applications(registre)
ecrire(registre)
print(f"Application '{args.id}' enregistree.")
elif args.commande == "retirer":
registre["applications"].pop(args.id, None)
valider_applications(registre)
ecrire(registre)
print(f"Application '{args.id}' retiree.")
except Exception as exc:
print(f"erreur: {exc}", file=sys.stderr)
return 2
return 0
if __name__ == "__main__":
raise SystemExit(main())