From dc2dd8b19242d7b8da1f881ae128b2746d8ff398 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Mon, 31 Aug 2020 15:41:39 +0200 Subject: [PATCH] mgr/cephadm: customize blink_device_light cmd via j2 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 '' 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 --- src/pybind/mgr/cephadm/module.py | 36 ++++++++++++++----- src/pybind/mgr/cephadm/template.py | 14 +++++++- .../templates/lsmcli_blink_lights_cmd.j2 | 1 + src/pybind/mgr/cephadm/tests/test_cephadm.py | 10 ++++++ 4 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 src/pybind/mgr/cephadm/templates/lsmcli_blink_lights_cmd.j2 diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index 2d2b6308f9075..2ff357e3c92ec 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -1,6 +1,7 @@ import json import errno import logging +import shlex from collections import defaultdict from contextlib import contextmanager from functools import wraps @@ -1763,16 +1764,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 '' + + 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) diff --git a/src/pybind/mgr/cephadm/template.py b/src/pybind/mgr/cephadm/template.py index f8e2affd4cd7b..8acff68cb6f10 100644 --- a/src/pybind/mgr/cephadm/template.py +++ b/src/pybind/mgr/cephadm/template.py @@ -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 index 0000000000000..dab115833dc32 --- /dev/null +++ b/src/pybind/mgr/cephadm/templates/lsmcli_blink_lights_cmd.j2 @@ -0,0 +1 @@ +lsmcli local-disk-{{ ident_fault }}-led-{{'on' if on else 'off'}} --path '{{ path or dev }}' diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index 9243294644ae2..348a3d4ecf190 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -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", [ -- 2.39.5