From 4bdbbeb6fd7db670fce4f39e6afac15bfa42ad54 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Fri, 20 Mar 2020 17:17:39 +0100 Subject: [PATCH] mgr/cephadm: replace async_map_completion with a simple wrapper There is no need to wrap everything into completions. Signed-off-by: Sebastian Wagner (cherry picked from commit a78f42e225bbc041324dc3e565deb207feddad26) --- src/pybind/mgr/cephadm/module.py | 50 +++++++++++++++++-- .../mgr/cephadm/tests/test_completion.py | 14 +++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index 70922599cb2..5a0f2b83471 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -222,6 +222,36 @@ def ssh_completion(cls=AsyncCompletion, **c_kwargs): return wrapper return decorator +def forall_hosts(f): + @wraps(f) + def forall_hosts_wrapper(*args) -> list: + + # Some weired logic to make calling functions with multiple arguments work. + if len(args) == 1: + vals = args[0] + self = None + elif len(args) == 2: + self, vals = args + else: + assert 'either f([...]) or self.f([...])' + + def do_work(arg): + if not isinstance(arg, tuple): + arg = (arg, ) + try: + if self: + return f(self, *arg) + return f(*arg) + except Exception as e: + logger.exception(f'executing {f.__name__}({args}) failed.') + raise + + assert CephadmOrchestrator.instance is not None + return CephadmOrchestrator.instance._worker_pool.map(do_work, vals) + + + return forall_hosts_wrapper + def async_completion(f): # type: (Callable) -> Callable[..., AsyncCompletion] @@ -1614,6 +1644,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): result.append(dd) return result + @trivial_completion def service_action(self, action, service_name): args = [] for host, dm in self.cache.daemons.items(): @@ -1624,7 +1655,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): self.log.info('%s service %s' % (action.capitalize(), service_name)) return self._daemon_actions(args) - @async_map_completion + @forall_hosts def _daemon_actions(self, daemon_type, daemon_id, host, action): return self._daemon_action(daemon_type, daemon_id, host, action) @@ -1666,6 +1697,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): ','.join(['%s.%s' % (a[0], a[1]) for a in args]))) return self._daemon_actions(args) + @trivial_completion def remove_daemons(self, names): # type: (List[str]) -> orchestrator.Completion args = [] @@ -1728,8 +1760,9 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): raise OrchestratorError('Zap failed: %s' % '\n'.join(out + err)) return '\n'.join(out + err) + @trivial_completion def blink_device_light(self, ident_fault, on, locs): - @async_map_completion + @forall_hosts def blink(host, dev, path): cmd = [ 'lsmcli', @@ -1896,7 +1929,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): return "{} {} on host '{}'".format( 'Reconfigured' if reconfig else 'Deployed', name, host) - @async_map_completion + @forall_hosts def _remove_daemons(self, name, host): return self._remove_daemon(name, host) @@ -2177,7 +2210,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): ) daemons.append(sd) - @async_map_completion + @forall_hosts def create_func_map(*args): return create_func(*args) @@ -2187,10 +2220,12 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_mon(self, spec): return self._apply(spec) + @trivial_completion def add_mon(self, spec): # type: (ServiceSpec) -> orchestrator.Completion return self._add_daemon('mon', spec, self.mon_service.create) + @trivial_completion def add_mgr(self, spec): # type: (ServiceSpec) -> orchestrator.Completion return self._add_daemon('mgr', spec, self.mgr_service.create) @@ -2239,6 +2274,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_mgr(self, spec): return self._apply(spec) + @trivial_completion def add_mds(self, spec: ServiceSpec): return self._add_daemon('mds', spec, self.mds_service.create, self.mds_service.config) @@ -2246,6 +2282,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_mds(self, spec: ServiceSpec): return self._apply(spec) + @trivial_completion def add_rgw(self, spec): return self._add_daemon('rgw', spec, self.rgw_service.create, self.rgw_service.config) @@ -2279,6 +2316,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): # type: () -> str return self.get('mgr_map').get('services', {}).get('dashboard', '') + @trivial_completion def add_prometheus(self, spec): return self._add_daemon('prometheus', spec, self.prometheus_service.create) @@ -2286,6 +2324,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_prometheus(self, spec): return self._apply(spec) + @trivial_completion def add_node_exporter(self, spec): # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('node-exporter', spec, @@ -2295,6 +2334,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_node_exporter(self, spec): return self._apply(spec) + @trivial_completion def add_crash(self, spec): # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('crash', spec, @@ -2304,6 +2344,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_crash(self, spec): return self._apply(spec) + @trivial_completion def add_grafana(self, spec): # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('grafana', spec, self.grafana_service.create) @@ -2312,6 +2353,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def apply_grafana(self, spec: ServiceSpec): return self._apply(spec) + @trivial_completion def add_alertmanager(self, spec): # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('alertmanager', spec, self.alertmanager_service.create) diff --git a/src/pybind/mgr/cephadm/tests/test_completion.py b/src/pybind/mgr/cephadm/tests/test_completion.py index 085ea6c0a80..f39aa6c1845 100644 --- a/src/pybind/mgr/cephadm/tests/test_completion.py +++ b/src/pybind/mgr/cephadm/tests/test_completion.py @@ -12,7 +12,7 @@ import pytest from tests import mock from .fixtures import cephadm_module, wait -from ..module import trivial_completion, async_completion, async_map_completion +from ..module import trivial_completion, async_completion, async_map_completion, forall_hosts class TestCompletion(object): @@ -53,6 +53,11 @@ class TestCompletion(object): wait(cephadm_module, c) assert c.result == expected + @forall_hosts + def run_forall(*args): + return str(args) + assert run_forall(input) == expected + def test_async_self(self, cephadm_module): class Run(object): def __init__(self): @@ -83,10 +88,17 @@ class TestCompletion(object): assert self.attr == 1 return str(args) + @forall_hosts + def run_forall(self, *args): + assert self.attr == 1 + return str(args) + c = Run().run(input) wait(cephadm_module, c) assert c.result == expected + assert Run().run_forall(input) == expected + def test_then1(self, cephadm_module): @async_map_completion def run(x): -- 2.39.5