]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: customize blink_device_light cmd via j2
authorSebastian Wagner <sebastian.wagner@suse.com>
Mon, 31 Aug 2020 13:41:39 +0000 (15:41 +0200)
committerSebastian Wagner <sebastian.wagner@suse.com>
Thu, 10 Sep 2020 09:00:52 +0000 (11:00 +0200)
Customized blink a device light. By default running something like:

    lsmcli local-disk-ident-led-on --path $path

If you must, you can customize this via:

    ceph config-key set mgr/cephadm/lsmcli_blink_lights_cmd '<my jinja2 template>'

where my jinja2 template is something like:

    lsmcli local-disk-{{ ident_fault }}-led-{{'on' if on else 'off'}} --path '{{ path or dev }}'

Signed-off-by: Sebastian Wagner <sebastian.wagner@suse.com>
(cherry picked from commit dc2dd8b19242d7b8da1f881ae128b2746d8ff398)

src/pybind/mgr/cephadm/module.py
src/pybind/mgr/cephadm/template.py
src/pybind/mgr/cephadm/templates/lsmcli_blink_lights_cmd.j2 [new file with mode: 0644]
src/pybind/mgr/cephadm/tests/test_cephadm.py

index 92eaec1b3964b0a5a433ed6725025a07dd028bbb..187a417ae66ce61e0bb051ff74c748b14a047a79 100644 (file)
@@ -1,6 +1,7 @@
 import json
 import errno
 import logging
+import shlex
 from collections import defaultdict
 from contextlib import contextmanager
 from functools import wraps
@@ -1764,16 +1765,35 @@ To check that the host is reachable:
         return '\n'.join(out + err)
 
     @trivial_completion
-    def blink_device_light(self, ident_fault, on, locs) -> List[str]:
+    def blink_device_light(self, ident_fault: str, on: bool, locs: List[orchestrator.DeviceLightLoc]) -> List[str]:
+        """
+        Blink a device light. Calling something like::
+
+          lsmcli local-disk-ident-led-on --path $path
+
+        If you must, you can customize this via::
+
+          ceph config-key set mgr/cephadm/lsmcli_blink_lights_cmd '<my jinja2 template>'
+
+        See templates/lsmcli_blink_lights_cmd.j2
+        """
         @forall_hosts
         def blink(host, dev, path):
-            cmd = [
-                'lsmcli',
-                'local-disk-%s-led-%s' % (
-                    ident_fault,
-                    'on' if on else 'off'),
-                '--path', path or dev,
-            ]
+            j2_ctx = {
+                'on': on,
+                'ident_fault': ident_fault,
+                'dev': dev,
+                '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)
+
+            cmd = shlex.split(lsmcli_blink_lights_cmd)
+
             out, err, code = self._run_cephadm(
                 host, 'osd', 'shell', ['--'] + cmd,
                 error_ok=True)
index f8e2affd4cd7b78efd0172d7e05b76dbef8b75be..8acff68cb6f10b07893e101cc235f07f37c102e5 100644 (file)
@@ -1,7 +1,7 @@
 import copy
 from typing import Optional
 
-from jinja2 import Environment, PackageLoader, select_autoescape, StrictUndefined
+from jinja2 import Environment, PackageLoader, select_autoescape, StrictUndefined, Template
 from jinja2 import exceptions as j2_exceptions
 
 
@@ -43,6 +43,17 @@ class Jinja2Engine(TemplateEngine):
         except j2_exceptions.TemplateNotFound as e:
             raise TemplateNotFoundError(e.message)
 
+    def render_plain(self, source, context):
+        try:
+            template = self.env.from_string(source)
+            if context is None:
+                return template.render()
+            return template.render(context)
+        except j2_exceptions.UndefinedError as e:
+            raise UndefinedError(e.message)
+        except j2_exceptions.TemplateNotFound as e:
+            raise TemplateNotFoundError(e.message)
+
 
 class TemplateMgr:
     def __init__(self):
@@ -71,3 +82,4 @@ class TemplateMgr:
         if context is not None:
             ctx = {**ctx, **context}
         return self.engine.render(name, ctx)
+
diff --git a/src/pybind/mgr/cephadm/templates/lsmcli_blink_lights_cmd.j2 b/src/pybind/mgr/cephadm/templates/lsmcli_blink_lights_cmd.j2
new file mode 100644 (file)
index 0000000..dab1158
--- /dev/null
@@ -0,0 +1 @@
+lsmcli local-disk-{{ ident_fault }}-led-{{'on' if on else 'off'}} --path '{{ path or dev }}'
index 9243294644ae273d48d131d894cc01da2813951c..348a3d4ecf190088e7f16b3e0c2cc54c2465cc38 100644 (file)
@@ -605,6 +605,16 @@ class TestCephadm(object):
             _run_cephadm.assert_called_with('test', 'osd', 'shell', [
                                             '--', 'lsmcli', f'local-disk-{fault_ident}-led-{on_off}', '--path', 'dev'], error_ok=True)
 
+    @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm")
+    def test_blink_device_light_custom(self, _run_cephadm, cephadm_module):
+        _run_cephadm.return_value = '{}', '', 0
+        with with_host(cephadm_module, 'test'):
+            cephadm_module.set_store('lsmcli_blink_lights_cmd', 'echo hello')
+            c = cephadm_module.blink_device_light('ident', True, [('test', '', 'dev')])
+            assert wait(cephadm_module, c) == ['Set ident light for test: on']
+            _run_cephadm.assert_called_with('test', 'osd', 'shell', [
+                                            '--', 'echo', 'hello'], error_ok=True)
+
     @pytest.mark.parametrize(
         "spec, meth",
         [