From f103702ac256de270e39eb0852c3ca57387a4556 Mon Sep 17 00:00:00 2001 From: Avan Thakkar Date: Fri, 11 Jun 2021 16:37:10 +0530 Subject: [PATCH] cephadm: expose gather-facts api method Fixes: https://tracker.ceph.com/issues/51209 This PR intends to expose host metadata(gather-facts) api method Signed-off-by: Avan Thakkar Signed-off-by: Aashish Sharma --- src/pybind/mgr/cephadm/module.py | 13 +++++++++++++ src/pybind/mgr/cephadm/tests/test_facts.py | 12 ++++++++++++ src/pybind/mgr/dashboard/services/orchestrator.py | 4 ++++ src/pybind/mgr/orchestrator/_interface.py | 6 ++++++ 4 files changed, 35 insertions(+) create mode 100644 src/pybind/mgr/cephadm/tests/test_facts.py diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index cde603154deb3..45f9f82934d03 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -1490,6 +1490,19 @@ def get_hosts(self): """ return list(self.inventory.all_specs()) + @handle_orch_error + def get_facts(self, hostname: Optional[str] = None) -> List[Dict[str, Any]]: + """ + Return a list of hosts metadata(gather_facts) managed by the orchestrator. + + Notes: + - skip async: manager reads from cache. + """ + if hostname: + return [self.cache.get_facts(hostname)] + + return [self.cache.get_facts(hostname) for hostname in self.cache.get_hosts()] + @handle_orch_error def add_host_label(self, host: str, label: str) -> str: self.inventory.add_label(host, label) diff --git a/src/pybind/mgr/cephadm/tests/test_facts.py b/src/pybind/mgr/cephadm/tests/test_facts.py new file mode 100644 index 0000000000000..79e6db00ce83c --- /dev/null +++ b/src/pybind/mgr/cephadm/tests/test_facts.py @@ -0,0 +1,12 @@ +import pytest + +from ..inventory import HostCache +from ..import CephadmOrchestrator + + +@pytest.fixture() +def test_facts(): + facts = {'node-1.ceph.com', {'bios_version': 'F2', 'cpu_cores': 16}} + HostCache.facts = facts + ret_facts = CephadmOrchestrator.get_facts('node-1.ceph.com') + assert ret_facts == [{'bios_version': 'F2', 'cpu_cores': 16}] diff --git a/src/pybind/mgr/dashboard/services/orchestrator.py b/src/pybind/mgr/dashboard/services/orchestrator.py index d564c46994080..742c82acf0953 100644 --- a/src/pybind/mgr/dashboard/services/orchestrator.py +++ b/src/pybind/mgr/dashboard/services/orchestrator.py @@ -62,6 +62,10 @@ def get(self, hostname: str) -> Optional[HostSpec]: hosts = [host for host in self.list() if host.hostname == hostname] return hosts[0] if hosts else None + @wait_api_result + def get_facts(self, hostname: Optional[str] = None) -> List[Dict[str, Any]]: + return self.api.get_facts(hostname) + @wait_api_result def add(self, hostname: str, addr: str, labels: List[str], status: str): return self.api.add_host(HostSpec(hostname, addr=addr, labels=labels, status=status)) diff --git a/src/pybind/mgr/orchestrator/_interface.py b/src/pybind/mgr/orchestrator/_interface.py index 773db30be8f80..e315da3feab21 100644 --- a/src/pybind/mgr/orchestrator/_interface.py +++ b/src/pybind/mgr/orchestrator/_interface.py @@ -368,6 +368,12 @@ def get_hosts(self) -> OrchResult[List[HostSpec]]: """ raise NotImplementedError() + def get_facts(self, hostname: Optional[str] = None) -> OrchResult[List[Dict[str, Any]]]: + """ + Return hosts metadata(gather_facts). + """ + raise NotImplementedError() + def add_host_label(self, host: str, label: str) -> OrchResult[str]: """ Add a host label