Files
Enclume/enclume/packs.py
T
hcornet de7a691861
Construction et publication / verifier (push) Successful in 41s
Construction et publication / verifier (pull_request) Successful in 11s
Construction et publication / publier (pull_request) Skipped
Construction et publication / publier (push) Failing after 6m3s
Enclume : editeur CLAPI, catalogue de 26 packs, empaquetage Docker
2026-09-10 11:32:03 +02:00

61 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, 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.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