]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: replace async_map_completion with a simple wrapper
authorSebastian Wagner <sebastian.wagner@suse.com>
Fri, 20 Mar 2020 16:17:39 +0000 (17:17 +0100)
committerSebastian Wagner <sebastian.wagner@suse.com>
Thu, 21 May 2020 21:33:18 +0000 (23:33 +0200)
There is no need to wrap everything into completions.

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

src/pybind/mgr/cephadm/module.py
src/pybind/mgr/cephadm/tests/test_completion.py

index d430e5fdbb163488a52901ed439532b10599ef12..b351900578b955485d871b91d8ace82efa634e69 100644 (file)
@@ -220,6 +220,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]
@@ -1349,6 +1379,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():
@@ -1359,7 +1390,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)
 
@@ -1401,6 +1432,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 = []
@@ -1463,8 +1495,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',
@@ -1631,7 +1664,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)
 
@@ -1912,7 +1945,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule):
             )
             daemons.append(sd)
 
-        @async_map_completion
+        @forall_hosts
         def create_func_map(*args):
             return create_func(*args)
 
@@ -1922,10 +1955,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)
@@ -1974,6 +2009,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)
 
@@ -1981,6 +2017,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)
 
@@ -2014,6 +2051,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)
 
@@ -2021,6 +2059,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,
@@ -2030,6 +2069,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,
@@ -2039,6 +2079,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)
@@ -2047,6 +2088,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)
index 085ea6c0a803b374570e26f2aa22bff0dcf13b62..f39aa6c18459821a1430c63b6e7254943b168e54 100644 (file)
@@ -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):