Files
Enclume/enclume/packs.py
T
hcornet b063d59525
build / Garde-fou (pull_request) Failing after 7s
build / Images Harbor (catalog-sync, Dockerfile.catalog-sync) (pull_request) Skipped
build / Images Harbor (web, Dockerfile) (pull_request) Skipped
build / Garde-fou (push) Failing after 8s
build / Images Harbor (catalog-sync, Dockerfile.catalog-sync) (push) Skipped
build / Images Harbor (web, Dockerfile) (push) Skipped
Partage par lien, comptes Authentik, soumission et moderation de packs, prerequis des sondes ; corrige le lien casse vers le script de l'editeur
2026-09-10 16:28:26 +02:00

62 lines
2.1 KiB
Python

"""Passerelle entre le catalogue de packs et le modele de projet.
Le catalogue reste un point de depart : importer un pack copie ses objets dans
le projet de l'utilisateur, qui les modifie ensuite librement.
"""
from __future__ import annotations
from .catalog import Pack
from .model import Command, HostTemplate, Macro, Prerequis, Project, ServiceTemplate
def _macro(macro) -> Macro:
return Macro(
name=macro.name,
value=macro.value,
description=macro.description,
is_password=macro.is_password,
)
def pack_to_project(pack: Pack, only_defaults: bool = True, service_parent: str = "generic-active-service-custom") -> Project:
"""Construit un projet ne contenant que les objets du pack."""
project = Project(name=pack.name)
project.prerequis = Prerequis.from_dict(pack.prerequis)
project.commands = []
project.service_templates = []
htpl = HostTemplate(
name=pack.host_template.name,
alias=pack.host_template.alias,
parents=list(pack.host_template.parents),
check_command=pack.host_template.check_command,
check_command_args=pack.host_template.check_command_args,
macros=[_macro(m) for m in pack.host_template.macros],
)
if any("--snmp-" in svc.line for svc in pack.services):
htpl.snmp_version = "2c"
htpl.snmp_community = "public"
project.host_templates = [htpl]
for svc in pack.services:
if only_defaults and not svc.enabled_by_default:
continue
name = f"{pack.prefix}-{svc.name}"
project.commands.append(Command(name=name, type="check", line=svc.line, comment=svc.description))
project.service_templates.append(
ServiceTemplate(
name=name,
alias=svc.alias,
parent=service_parent,
command=name,
check_interval=svc.check_interval or 5,
retry_interval=svc.retry_interval or 1,
max_attempts=svc.max_attempts or 3,
host_templates=[htpl.name],
macros=[_macro(m) for m in svc.macros],
)
)
return project