From 77852505598e5e5db2c9a590a90834e7c8f1f77c Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Tue, 12 Nov 2019 08:24:31 -0600 Subject: [PATCH] mgr/orchestrator: move split_host into shared helpers Signed-off-by: Sage Weil --- src/pybind/mgr/orchestrator.py | 44 +++++++++++++++++------ src/pybind/mgr/orchestrator_cli/module.py | 27 ++------------ 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/pybind/mgr/orchestrator.py b/src/pybind/mgr/orchestrator.py index 5ef1338ecafa..1e4723d44c45 100644 --- a/src/pybind/mgr/orchestrator.py +++ b/src/pybind/mgr/orchestrator.py @@ -26,6 +26,40 @@ try: except ImportError: T, G = object, object +def split_host(host): + """ + Split host into host and (optional) daemon name parts + e.g., + "myhost=name" -> ('myhost', 'name') + "myhost" -> ('myhost', None) + """ + # TODO: stricter validation + a = host.split('=', 1) + if len(a) == 1: + return (a[0], None) + else: + assert len(a) == 2 + return tuple(a) + +def split_host_with_network(host): + """ + Split host into host, network, and (optional) daemon name parts. The network + part can be an IP, CIDR, or ceph addrvec like + '[v2:1.2.3.4:3300,v1:1.2.3.4:6789]'. + e.g., + "myhost:1.2.3.4=name" -> ('myhost', '1.2.3.4', 'name') + "myhost:1.2.3.0/24" -> ('myhost', '1.2.3.0/24', None) + """ + # TODO: stricter validation + (host, name) = host.split('=', 1) + parts = host.split(":", 1) + if len(parts) == 1: + return (parts[0], None, name) + elif len(parts) == 2: + return (parts[0], parts[1], name) + else: + raise RuntimeError("Invalid host specification: " + "'{}'".format(host)) class OrchestratorError(Exception): """ @@ -598,16 +632,6 @@ class PlacementSpec(object): def __init__(self, label=None, nodes=[]): self.label = label - def split_host(host): - """Split host into host and name parts""" - # TODO: stricter validation - a = host.split('=', 1) - if len(a) == 1: - return (a[0], None) - else: - assert len(a) == 2 - return tuple(a) - self.nodes = list(map(split_host, nodes)) def handle_type_error(method): diff --git a/src/pybind/mgr/orchestrator_cli/module.py b/src/pybind/mgr/orchestrator_cli/module.py index 9b402322ed46..9f3741b22842 100644 --- a/src/pybind/mgr/orchestrator_cli/module.py +++ b/src/pybind/mgr/orchestrator_cli/module.py @@ -551,19 +551,9 @@ Usage: return HandleCommandResult(-errno.EINVAL, stderr="Invalid number of mgrs: require {} > 0".format(num)) - def split_host(host): - """Split host into host and name parts""" - # TODO: stricter validation - a = host.split('=', 1) - if len(a) == 1: - return (a[0], None) - else: - assert len(a) == 2 - return tuple(a) - if hosts: try: - hosts = list(map(split_host, hosts)) + hosts = list(map(orchestrator.split_host, hosts)) except Exception as e: msg = "Failed to parse host list: '{}': {}".format(hosts, e) return HandleCommandResult(-errno.EINVAL, stderr=msg) @@ -584,22 +574,9 @@ Usage: return HandleCommandResult(-errno.EINVAL, stderr="Invalid number of mons: require {} > 0".format(num)) - def split_host(host): - """Split host into host and network parts""" - # TODO: stricter validation - (host, name) = host.split('=', 1) - parts = host.split(":", 1) - if len(parts) == 1: - return (parts[0], None, name) - elif len(parts) == 2: - return (parts[0], parts[1], name) - else: - raise RuntimeError("Invalid host specification: " - "'{}'".format(host)) - if hosts: try: - hosts = list(map(split_host, hosts)) + hosts = list(map(orchestrator.split_host_with_network, hosts)) except Exception as e: msg = "Failed to parse host list: '{}': {}".format(hosts, e) return HandleCommandResult(-errno.EINVAL, stderr=msg) -- 2.47.3