From: Sebastian Wagner Date: Fri, 11 Sep 2020 13:04:49 +0000 (+0200) Subject: mgr/cephadm: make j2 teamplates overwritable X-Git-Tag: v16.1.0~1009^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=cfc09e4d7903c404901344b4a7bdd6c5499603fb;p=ceph.git mgr/cephadm: make j2 teamplates overwritable This adds an escape hatch, if the configs generated by cephadm are not flexible enough. Downside is obviously that future upgrades might break the daemons. Use at your own risk. Signed-off-by: Sebastian Wagner --- diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index bf37872f55e3b..1722bf42132c7 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -392,7 +392,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule, 'iscsi': self.iscsi_service, } - self.template = TemplateMgr() + self.template = TemplateMgr(self) self.requires_post_actions = set() @@ -1837,11 +1837,7 @@ To check that the host is reachable: 'path': path } - custom_template = self.get_store('lsmcli_blink_lights_cmd', None) - if custom_template: - lsmcli_blink_lights_cmd = self.template.engine.render_plain(custom_template, j2_ctx) - else: - lsmcli_blink_lights_cmd = self.template.render('lsmcli_blink_lights_cmd.j2', j2_ctx) + lsmcli_blink_lights_cmd = self.template.render('lsmcli_blink_lights_cmd.j2', j2_ctx) cmd = shlex.split(lsmcli_blink_lights_cmd) diff --git a/src/pybind/mgr/cephadm/template.py b/src/pybind/mgr/cephadm/template.py index 8acff68cb6f10..b901ac02427dd 100644 --- a/src/pybind/mgr/cephadm/template.py +++ b/src/pybind/mgr/cephadm/template.py @@ -1,9 +1,12 @@ import copy -from typing import Optional +from typing import Optional, TYPE_CHECKING from jinja2 import Environment, PackageLoader, select_autoescape, StrictUndefined, Template from jinja2 import exceptions as j2_exceptions +if TYPE_CHECKING: + from cephadm.module import CephadmOrchestrator + class TemplateError(Exception): pass @@ -56,11 +59,12 @@ class Jinja2Engine(TemplateEngine): class TemplateMgr: - def __init__(self): + def __init__(self, mgr: "CephadmOrchestrator"): self.engine = Jinja2Engine() self.base_context = { 'cephadm_managed': 'This file is generated by cephadm.' } + self.mgr = mgr def render(self, name: str, context: Optional[dict] = None, managed_context=True) -> str: """Render a string from a template with context. @@ -81,5 +85,10 @@ class TemplateMgr: ctx = copy.deepcopy(self.base_context) if context is not None: ctx = {**ctx, **context} - return self.engine.render(name, ctx) + store_name = name.replace('/', '_').rstrip('.j2') + custom_template = self.mgr.get_store(store_name, None) + if custom_template: + return self.engine.render_plain(custom_template, ctx) + else: + return self.engine.render(name, ctx) diff --git a/src/pybind/mgr/cephadm/tests/test_template.py b/src/pybind/mgr/cephadm/tests/test_template.py index 962b30673e108..40b783bdbf9ac 100644 --- a/src/pybind/mgr/cephadm/tests/test_template.py +++ b/src/pybind/mgr/cephadm/tests/test_template.py @@ -2,15 +2,16 @@ import pathlib import pytest +from cephadm.tests.fixtures import cephadm_module from cephadm.template import TemplateMgr, UndefinedError, TemplateNotFoundError -def test_render(fs): +def test_render(cephadm_module, fs): template_base = (pathlib.Path(__file__).parent / '../templates').resolve() fake_template = template_base / 'foo/bar' fs.create_file(fake_template, contents='{{ cephadm_managed }}{{ var }}') - template_mgr = TemplateMgr() + template_mgr = TemplateMgr(cephadm_module) value = 'test' # with base context