]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: make j2 teamplates overwritable
authorSebastian Wagner <sebastian.wagner@suse.com>
Fri, 11 Sep 2020 13:04:49 +0000 (15:04 +0200)
committerNathan Cutler <ncutler@suse.com>
Tue, 6 Oct 2020 09:40:53 +0000 (11:40 +0200)
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 <sebastian.wagner@suse.com>
(cherry picked from commit cfc09e4d7903c404901344b4a7bdd6c5499603fb)

src/pybind/mgr/cephadm/module.py
src/pybind/mgr/cephadm/template.py
src/pybind/mgr/cephadm/tests/test_template.py

index eb3fe8370088476f690a7655a663ade986701a6e..68d837da6cc63db8176401059110bb23483bad04 100644 (file)
@@ -393,7 +393,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule):
             'iscsi': self.iscsi_service,
         }
 
-        self.template = TemplateMgr()
+        self.template = TemplateMgr(self)
 
         self.requires_post_actions = set()
 
@@ -1838,11 +1838,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)
 
index 8acff68cb6f10b07893e101cc235f07f37c102e5..b901ac02427ddbbf775b566fffce267c6a187660 100644 (file)
@@ -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)
index 962b30673e10876069d9ca558b16831372d5dfe5..40b783bdbf9ac68f266dd1eab1ebfbc6d9548008 100644 (file)
@@ -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