From 62d5c29914315e68d16328909bc45477b25053da Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Tue, 17 Mar 2020 15:08:50 +0100 Subject: [PATCH] python-common,mgr/cephadm: move assert_valid_host to service_spec Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/module.py | 15 ++------------- .../mgr/cephadm/tests/test_scheduling.py | 1 + .../ceph/deployment/service_spec.py | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index b6ac1ca4561..487f520ecf3 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -31,7 +31,8 @@ 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, PlacementSpec +from ceph.deployment.service_spec import HostPlacementSpec, ServiceSpec, PlacementSpec, \ + assert_valid_host from mgr_module import MgrModule import orchestrator @@ -107,18 +108,6 @@ def name_to_config_section(name): else: return 'mon' -def assert_valid_host(name): - p = re.compile('^[a-zA-Z0-9-]+$') - try: - assert len(name) <= 250, 'name is too long (max 250 chars)' - parts = name.split('.') - for part in name.split('.'): - assert len(part) > 0, '.-delimited name component must not be empty' - assert len(part) <= 63, '.-delimited name component must not be more than 63 chars' - assert p.match(part), 'name component must include only a-z, 0-9, and -' - except AssertionError as e: - raise OrchestratorError(e) - class SpecStore(): def __init__(self, mgr): diff --git a/src/pybind/mgr/cephadm/tests/test_scheduling.py b/src/pybind/mgr/cephadm/tests/test_scheduling.py index 5c01d6d75ae..54402bad469 100644 --- a/src/pybind/mgr/cephadm/tests/test_scheduling.py +++ b/src/pybind/mgr/cephadm/tests/test_scheduling.py @@ -246,6 +246,7 @@ def test_node_assignment3(service_type, placement, hosts, ('1 *'), ('* label:foo'), ('* host1 host2'), + ('hostname12hostname12hostname12hostname12hostname12hostname12hostname12'), # > 63 chars ]) def test_bad_placements(placement): try: diff --git a/src/python-common/ceph/deployment/service_spec.py b/src/python-common/ceph/deployment/service_spec.py index 07dd2a14f8f..08edc09170d 100644 --- a/src/python-common/ceph/deployment/service_spec.py +++ b/src/python-common/ceph/deployment/service_spec.py @@ -16,6 +16,18 @@ class ServiceSpecValidationError(Exception): super(ServiceSpecValidationError, self).__init__(msg) +def assert_valid_host(name): + p = re.compile('^[a-zA-Z0-9-]+$') + try: + assert len(name) <= 250, 'name is too long (max 250 chars)' + for part in name.split('.'): + assert len(part) > 0, '.-delimited name component must not be empty' + assert len(part) <= 63, '.-delimited name component must not be more than 63 chars' + assert p.match(part), 'name component must include only a-z, 0-9, and -' + except AssertionError as e: + raise ServiceSpecValidationError(e) + + class HostPlacementSpec(namedtuple('HostPlacementSpec', ['hostname', 'network', 'name'])): def __str__(self): res = '' @@ -99,9 +111,12 @@ class HostPlacementSpec(namedtuple('HostPlacementSpec', ['hostname', 'network', except ValueError as e: # logging? raise e - + host_spec.validate() return host_spec + def validate(self): + assert_valid_host(self.hostname) + class PlacementSpec(object): """ @@ -198,6 +213,8 @@ class PlacementSpec(object): raise ServiceSpecValidationError("num/count must be > 1") if self.host_pattern and self.hosts: raise ServiceSpecValidationError('cannot combine host patterns and hosts') + for h in self.hosts: + h.validate() @classmethod def from_string(cls, arg): -- 2.39.5