98 lines
3.9 KiB
Python
98 lines
3.9 KiB
Python
"""Ligne de commande : meme moteur que le site, utilisable en CI.
|
|
|
|
python3 -m enclume.cli --list
|
|
python3 -m enclume.cli --pack os-linux-snmp --pack app-mysql --out ./sortie
|
|
python3 -m enclume.cli --projet projet.json --out ./sortie
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from .catalog import Catalog
|
|
from .generator import build_plan, render_bundle
|
|
from .model import Project
|
|
from .packs import pack_to_project
|
|
|
|
DEFAULT_CATALOG = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "catalog")
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description="Generateur CLAPI Centreon")
|
|
parser.add_argument("--catalog", default=os.environ.get("ENCLUME_CATALOG", DEFAULT_CATALOG))
|
|
parser.add_argument("--list", action="store_true", help="liste les packs disponibles")
|
|
parser.add_argument("--pack", action="append", default=[], help="pack a importer (repetable)")
|
|
parser.add_argument("--tout", action="store_true", help="importer toutes les sondes, pas seulement celles par defaut")
|
|
parser.add_argument("--projet", help="fichier de projet JSON exporte depuis le site")
|
|
parser.add_argument("--prefix", default="", help="prefixe applique aux objets crees")
|
|
parser.add_argument("--plugins-dir", default="/usr/lib/centreon/plugins")
|
|
parser.add_argument("--version-centreon", default="24.x")
|
|
parser.add_argument("--poller", default="Central")
|
|
parser.add_argument("--out", help="repertoire de sortie (defaut : stdout)")
|
|
parser.add_argument("--strict", action="store_true", help="code de retour non nul si le projet contient une erreur")
|
|
args = parser.parse_args(argv)
|
|
|
|
catalog = Catalog(args.catalog)
|
|
for erreur in catalog.errors:
|
|
print(f"catalogue : {erreur}", file=sys.stderr)
|
|
|
|
if args.list:
|
|
for pack in sorted(catalog.packs.values(), key=lambda p: (p.category, p.name)):
|
|
marque = " " if pack.status == "verified" else "!"
|
|
print(f"{marque} {pack.id:<28} {pack.category:<12} {len(pack.services):>2} sondes {pack.name}")
|
|
return 0
|
|
|
|
if args.projet:
|
|
with open(args.projet, encoding="utf-8") as handle:
|
|
project = Project.from_dict(json.load(handle))
|
|
elif args.pack:
|
|
project = Project(
|
|
name="Projet en ligne de commande",
|
|
centreon_version=args.version_centreon,
|
|
prefix=args.prefix,
|
|
plugins_dir=args.plugins_dir,
|
|
poller=args.poller,
|
|
)
|
|
for pack_id in args.pack:
|
|
pack = catalog.get(pack_id)
|
|
if pack is None:
|
|
print(f"pack inconnu : {pack_id}", file=sys.stderr)
|
|
return 2
|
|
for message in project.merge(pack_to_project(pack, only_defaults=not args.tout)):
|
|
print(f"fusion : {message}", file=sys.stderr)
|
|
else:
|
|
parser.error("indiquez --list, --pack ou --projet")
|
|
return 2
|
|
|
|
plan = build_plan(project)
|
|
fichiers = render_bundle(plan)
|
|
|
|
for probleme in plan.issues:
|
|
print(f"{probleme['level']} : {probleme['message']}", file=sys.stderr)
|
|
|
|
if not args.out:
|
|
sys.stdout.write(fichiers["deploy.clapi"])
|
|
else:
|
|
os.makedirs(args.out, exist_ok=True)
|
|
for nom, contenu in fichiers.items():
|
|
chemin = os.path.join(args.out, nom)
|
|
with open(chemin, "w", encoding="utf-8") as handle:
|
|
handle.write(contenu)
|
|
if nom.endswith(".sh"):
|
|
os.chmod(chemin, 0o755)
|
|
with open(os.path.join(args.out, "projet.json"), "w", encoding="utf-8") as handle:
|
|
json.dump(project.to_dict(), handle, indent=2, ensure_ascii=False)
|
|
print(f"{args.out} : " + ", ".join(f"{v} {k}" for k, v in plan.summary.items() if v))
|
|
|
|
return 1 if (args.strict and plan.has_errors) else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
raise SystemExit(main())
|
|
except BrokenPipeError:
|
|
raise SystemExit(0)
|