From: Sebastian Wagner Date: Wed, 22 Jul 2020 12:39:26 +0000 (+0200) Subject: mgr/cephadm: move `forall_hosts()` to utils module X-Git-Tag: wip-pdonnell-testing-20200918.022351~574^2~1 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=031365d357f02cdc5a0743c31672cbc392df8816;p=ceph-ci.git mgr/cephadm: move `forall_hosts()` to utils module Signed-off-by: Sebastian Wagner --- diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index f395096a5ad..b4d461e9eb4 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -45,6 +45,7 @@ from .schedule import HostAssignment, HostPlacementSpec from .inventory import Inventory, SpecStore, HostCache, EventStore from .upgrade import CEPH_UPGRADE_ORDER, CephadmUpgrade from .template import TemplateMgr +from .utils import forall_hosts try: import remoto @@ -86,37 +87,6 @@ CEPH_DATEFMT = '%Y-%m-%dT%H:%M:%S.%fZ' CEPH_TYPES = set(CEPH_UPGRADE_ORDER) -def forall_hosts(f: Callable[..., T]) -> Callable[..., List[T]]: - @wraps(f) - def forall_hosts_wrapper(*args) -> List[T]: - - # 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 - - class CephadmCompletion(orchestrator.Completion): def evaluate(self): self.finalize(None) diff --git a/src/pybind/mgr/cephadm/utils.py b/src/pybind/mgr/cephadm/utils.py index 4000a3329fc..a933fbc6dd7 100644 --- a/src/pybind/mgr/cephadm/utils.py +++ b/src/pybind/mgr/cephadm/utils.py @@ -1,8 +1,13 @@ -import re +import logging +from functools import wraps +from typing import Optional, Callable, TypeVar, List from orchestrator import OrchestratorError -from typing import Optional + +T = TypeVar('T') +logger = logging.getLogger(__name__) + def name_to_config_section(name: str) -> str: """ @@ -37,3 +42,35 @@ def name_to_auth_entity(daemon_type, # type: str return daemon_type + "." + daemon_id else: raise OrchestratorError("unknown auth entity name") + + +def forall_hosts(f: Callable[..., T]) -> Callable[..., List[T]]: + @wraps(f) + def forall_hosts_wrapper(*args) -> List[T]: + from cephadm.module import CephadmOrchestrator + + # 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 \ No newline at end of file