From: Sebastian Wagner Date: Mon, 9 Mar 2020 11:58:52 +0000 (+0100) Subject: python-common, orch: Move ServiceSpec to python-common: Fix imports X-Git-Tag: v15.1.1~33^2~12 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=7247262386c7b2b1201724bf57b20d840a8fc2be;p=ceph-ci.git python-common, orch: Move ServiceSpec to python-common: Fix imports Signed-off-by: Sebastian Wagner --- diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index 8272bee58c3..20f046cad8f 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -30,11 +30,12 @@ import uuid from ceph.deployment import inventory, translate from ceph.deployment.drive_group import DriveGroupSpec from ceph.deployment.drive_selection import selector +from ceph.deployment.service_spec import HostPlacementSpec, ServiceSpec from mgr_module import MgrModule import orchestrator -from orchestrator import OrchestratorError, HostPlacementSpec, OrchestratorValidationError, HostSpec, \ - CLICommandMeta, ServiceSpec +from orchestrator import OrchestratorError, OrchestratorValidationError, HostSpec, \ + CLICommandMeta from . import remotes from .osd import RemoveUtil, OSDRemoval @@ -121,7 +122,7 @@ class SpecStore(): def __init__(self, mgr): # type: (CephadmOrchestrator) -> None self.mgr = mgr - self.specs = {} # type: Dict[str, orchestrator.ServiceSpec] + self.specs = {} # type: Dict[str, ServiceSpec] self.spec_created = {} # type: Dict[str, datetime.datetime] def load(self): @@ -142,7 +143,7 @@ class SpecStore(): pass def save(self, spec): - # type: (orchestrator.ServiceSpec) -> None + # type: (ServiceSpec) -> None self.specs[spec.service_name()] = spec self.spec_created[spec.service_name()] = datetime.datetime.utcnow() self.mgr.set_store( @@ -161,7 +162,7 @@ class SpecStore(): self.mgr.set_store(SPEC_STORE_PREFIX + service_name, None) def find(self, service_name): - # type: (str) -> List[orchestrator.ServiceSpec] + # type: (str) -> List[ServiceSpec] specs = [] for sn, spec in self.specs.items(): if sn == service_name or sn.startswith(service_name + '.'): @@ -2253,7 +2254,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): def _apply_all_services(self): r = False - specs = [] # type: List[orchestrator.ServiceSpec] + specs = [] # type: List[ServiceSpec] for sn, spec in self.spec_store.specs.items(): specs.append(spec) for spec in specs: @@ -2397,7 +2398,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): extra_config=extra_config) def add_mon(self, spec): - # type: (orchestrator.ServiceSpec) -> orchestrator.Completion + # type: (ServiceSpec) -> orchestrator.Completion return self._add_daemon('mon', spec, self._create_mon) def _create_mgr(self, mgr_id, host): @@ -2416,7 +2417,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): return self._create_daemon('mgr', mgr_id, host, keyring=keyring) def add_mgr(self, spec): - # type: (orchestrator.ServiceSpec) -> orchestrator.Completion + # type: (ServiceSpec) -> orchestrator.Completion return self._add_daemon('mgr', spec, self._create_mgr) def _apply(self, spec): @@ -2445,10 +2446,10 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): return self._apply(spec) def add_mds(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('mds', spec, self._create_mds, self._config_mds) - def apply_mds(self, spec: orchestrator.ServiceSpec) -> orchestrator.Completion: + def apply_mds(self, spec: ServiceSpec) -> orchestrator.Completion: return self._apply(spec) def _config_mds(self, spec): @@ -2742,7 +2743,7 @@ receivers: return self._apply(spec) def add_node_exporter(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('node-exporter', spec, self._create_node_exporter) @@ -2753,7 +2754,7 @@ receivers: return self._create_daemon('node-exporter', daemon_id, host) def add_crash(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('crash', spec, self._create_crash) @@ -2770,11 +2771,11 @@ receivers: return self._create_daemon('crash', daemon_id, host, keyring=keyring) def add_grafana(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('grafana', spec, self._create_grafana) def apply_grafana(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._apply(spec) def _create_grafana(self, daemon_id, host): @@ -2782,11 +2783,11 @@ receivers: return self._create_daemon('grafana', daemon_id, host) def add_alertmanager(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._add_daemon('alertmanager', spec, self._create_alertmanager) def apply_alertmanager(self, spec): - # type: (orchestrator.ServiceSpec) -> AsyncCompletion + # type: (ServiceSpec) -> AsyncCompletion return self._apply(spec) def _create_alertmanager(self, daemon_id, host): @@ -3046,14 +3047,14 @@ class HostAssignment(object): """ def __init__(self, - spec, # type: orchestrator.ServiceSpec + spec, # type: ServiceSpec get_hosts_func, # type: Callable[[Optional[str]],List[str]] get_daemons_func, # type: Callable[[str],List[orchestrator.DaemonDescription]] scheduler=None, # type: Optional[BaseScheduler] ): assert spec and get_hosts_func and get_daemons_func - self.spec = spec # type: orchestrator.ServiceSpec + self.spec = spec # type: ServiceSpec self.scheduler = scheduler if scheduler else SimpleScheduler(self.spec.placement) self.get_hosts_func = get_hosts_func self.get_daemons_func = get_daemons_func diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index 6080ee5503b..ea095a8b27f 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -12,8 +12,9 @@ try: except ImportError: pass +from ceph.deployment.service_spec import ServiceSpec, PlacementSpec, RGWSpec from orchestrator import ServiceDescription, DaemonDescription, InventoryHost, \ - ServiceSpec, PlacementSpec, RGWSpec, HostSpec, OrchestratorError + HostSpec, OrchestratorError from tests import mock from .fixtures import cephadm_module, wait, _run_cephadm, mon_command, match_glob from cephadm.module import CephadmOrchestrator diff --git a/src/pybind/mgr/cephadm/tests/test_scheduling.py b/src/pybind/mgr/cephadm/tests/test_scheduling.py index abd3b3e4e2d..e2a6a0191f7 100644 --- a/src/pybind/mgr/cephadm/tests/test_scheduling.py +++ b/src/pybind/mgr/cephadm/tests/test_scheduling.py @@ -1,8 +1,10 @@ from typing import NamedTuple, List import pytest +from ceph.deployment.service_spec import ServiceSpec, PlacementSpec, ServiceSpecValidationError + from cephadm.module import HostAssignment -from orchestrator import ServiceSpec, PlacementSpec, DaemonDescription, OrchestratorValidationError +from orchestrator import DaemonDescription, OrchestratorValidationError class NodeAssignmentTest(NamedTuple): @@ -241,5 +243,5 @@ def test_bad_placements(placement): try: s = PlacementSpec.from_string(placement.split(' ')) assert False - except OrchestratorValidationError as e: + except ServiceSpecValidationError as e: pass diff --git a/src/pybind/mgr/orchestrator/__init__.py b/src/pybind/mgr/orchestrator/__init__.py index 05ac4601166..5addd32cc28 100644 --- a/src/pybind/mgr/orchestrator/__init__.py +++ b/src/pybind/mgr/orchestrator/__init__.py @@ -8,9 +8,7 @@ from ._interface import \ CLICommand, _cli_write_command, _cli_read_command, CLICommandMeta, \ Orchestrator, OrchestratorClientMixin, \ OrchestratorValidationError, OrchestratorError, NoOrchestrator, \ - ServiceSpec, NFSServiceSpec, RGWSpec, HostPlacementSpec, \ - servicespec_validate_add, \ - ServiceDescription, InventoryFilter, PlacementSpec, HostSpec, \ + ServiceDescription, InventoryFilter, HostSpec, \ DaemonDescription, \ InventoryHost, DeviceLightLoc, \ OutdatableData, OutdatablePersistentDict, \ diff --git a/src/pybind/mgr/orchestrator/module.py b/src/pybind/mgr/orchestrator/module.py index ded3f5839cc..4ab1d93588b 100644 --- a/src/pybind/mgr/orchestrator/module.py +++ b/src/pybind/mgr/orchestrator/module.py @@ -18,12 +18,13 @@ except ImportError: from ceph.deployment.drive_group import DriveGroupSpec, DeviceSelection, \ DriveGroupSpecs +from ceph.deployment.service_spec import PlacementSpec, ServiceSpec from mgr_module import MgrModule, HandleCommandResult from ._interface import OrchestratorClientMixin, DeviceLightLoc, _cli_read_command, \ raise_if_exception, _cli_write_command, TrivialReadCompletion, OrchestratorError, \ - NoOrchestrator, ServiceSpec, PlacementSpec, OrchestratorValidationError, NFSServiceSpec, \ - RGWSpec, InventoryFilter, InventoryHost, HostPlacementSpec, HostSpec, CLICommandMeta + NoOrchestrator, OrchestratorValidationError, NFSServiceSpec, \ + RGWSpec, InventoryFilter, InventoryHost, HostSpec, CLICommandMeta def nice_delta(now, t, suffix=''): if t: diff --git a/src/python-common/ceph/deployment/drive_group.py b/src/python-common/ceph/deployment/drive_group.py index 8d4ab95b3d1..78502e14ec7 100644 --- a/src/python-common/ceph/deployment/drive_group.py +++ b/src/python-common/ceph/deployment/drive_group.py @@ -1,5 +1,7 @@ import fnmatch from ceph.deployment.inventory import Device +from ceph.deployment.service_spec import ServiceSpecValidationError + try: from typing import Optional, List, Dict, Any except ImportError: @@ -97,7 +99,7 @@ class DeviceSelection(object): return repr(self) == repr(other) -class DriveGroupValidationError(Exception): +class DriveGroupValidationError(ServiceSpecValidationError): """ Defining an exception here is a bit problematic, cause you cannot properly catch it, if it was raised in a different mgr module. diff --git a/src/python-common/ceph/deployment/service_spec.py b/src/python-common/ceph/deployment/service_spec.py index 5afe7c87ad0..852078c2df5 100644 --- a/src/python-common/ceph/deployment/service_spec.py +++ b/src/python-common/ceph/deployment/service_spec.py @@ -5,6 +5,16 @@ from typing import Optional, Dict, Any, List import six +class ServiceSpecValidationError(Exception): + """ + Defining an exception here is a bit problematic, cause you cannot properly catch it, + if it was raised in a different mgr module. + """ + + def __init__(self, msg): + super(ServiceSpecValidationError, self).__init__(msg) + + class HostPlacementSpec(namedtuple('HostPlacementSpec', ['hostname', 'network', 'name'])): def __str__(self): res = '' @@ -100,7 +110,7 @@ class PlacementSpec(object): def __init__(self, label=None, hosts=None, count=None, all_hosts=False): # type: (Optional[str], Optional[List], Optional[int], bool) -> None if all_hosts and (count or hosts or label): - raise ValueError('cannot combine all:true and count|hosts|label') + raise ServiceSpecValidationError('cannot combine all:true and count|hosts|label') self.label = label self.hosts = [] # type: List[HostPlacementSpec] if hosts: @@ -158,9 +168,9 @@ class PlacementSpec(object): def validate(self): if self.hosts and self.label: # TODO: a less generic Exception - raise ValueError('Host and label are mutually exclusive') + raise ServiceSpecValidationError('Host and label are mutually exclusive') if self.count is not None and self.count <= 0: - raise ValueError("num/count must be > 1") + raise ServiceSpecValidationError("num/count must be > 1") @classmethod def from_string(cls, arg): @@ -200,7 +210,7 @@ class PlacementSpec(object): else: strings = [arg] else: - raise ValueError('invalid placement %s' % arg) + raise ServiceSpecValidationError('invalid placement %s' % arg) count = None if strings: @@ -229,7 +239,7 @@ class PlacementSpec(object): hosts = [x for x in strings if x != '*' and 'label:' not in x] labels = [x[6:] for x in strings if 'label:' in x] if len(labels) > 1: - raise ValueError('more than one label provided: {}'.format(labels)) + raise ServiceSpecValidationError('more than one label provided: {}'.format(labels)) ps = PlacementSpec(count=count, hosts=hosts, @@ -307,9 +317,9 @@ def servicespec_validate_add(self: ServiceSpec): # This must not be a method of ServiceSpec, otherwise you'll hunt # sub-interpreter affinity bugs. if not self.service_type: - raise ValueError('Cannot add Service: type required') + raise ServiceSpecValidationError('Cannot add Service: type required') if self.service_type in ['mds', 'rgw', 'nfs'] and not self.service_id: - raise ValueError('Cannot add Service: id required') + raise ServiceSpecValidationError('Cannot add Service: id required') class NFSServiceSpec(ServiceSpec): @@ -328,7 +338,7 @@ class NFSServiceSpec(ServiceSpec): servicespec_validate_add(self) if not self.pool: - raise ValueError('Cannot add NFS: No Pool specified') + raise ServiceSpecValidationError('Cannot add NFS: No Pool specified') class RGWSpec(ServiceSpec):