]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/orchestrator: add filtering and count option for orch host ls 44531/head
authorAdam King <adking@redhat.com>
Fri, 19 Nov 2021 00:43:35 +0000 (19:43 -0500)
committerSebastian Wagner <sewagner@redhat.com>
Tue, 11 Jan 2022 12:44:59 +0000 (13:44 +0100)
Filter orch host ls output for only hosts whose name
contains a certain substring or who have a certain label

Add a count flag that causes the command to return the number
of hosts found (either overall or matching the substring and/or
label) instead of a list of all the matching hosts

Fixes: https://tracker.ceph.com/issues/47774
Fixes: https://tracker.ceph.com/issues/53452
Signed-off-by: Adam King <adking@redhat.com>
(cherry picked from commit edd9bf38c3f07f5fdb6714e7f66515820c736d2e)

doc/cephadm/host-management.rst
src/pybind/mgr/orchestrator/module.py

index c109e2dc73b322d49ca110c727ed9789a6ca2544..d6aa89faa24acad4574341f9d5e8f676c9968c7f 100644 (file)
@@ -8,7 +8,13 @@ To list hosts associated with the cluster:
 
 .. prompt:: bash #
 
-    ceph orch host ls [--format yaml]
+    ceph orch host ls [--format yaml] [--host-pattern <name>] [--label <label>] [--host-status <status>]
+
+where the optional arguments "host-pattern", "label" and "host-status" are used for filtering.
+"host-pattern" is a regex that will match against hostnames and will only return matching hosts
+"label" will only return hosts with the given label
+"host-status" will only return hosts with the given status (currently "offline" or "maintenance")
+Any combination of these filtering flags is valid. You may filter against name, label and/or status simultaneously
 
 .. _cephadm-adding-hosts:    
     
index ff3765e515cf68a0256eea93f0c4d682ccd4a167..94c560984e775a8b950b436b576a9e36dffc3719 100644 (file)
@@ -370,11 +370,21 @@ class OrchestratorCli(OrchestratorClientMixin, MgrModule,
         return HandleCommandResult(stdout=completion.result_str())
 
     @_cli_read_command('orch host ls')
-    def _get_hosts(self, format: Format = Format.plain) -> HandleCommandResult:
+    def _get_hosts(self, format: Format = Format.plain, host_pattern: str = '', label: str = '', host_status: str = '') -> HandleCommandResult:
         """List hosts"""
         completion = self.get_hosts()
         hosts = raise_if_exception(completion)
 
+        filter_spec = PlacementSpec(
+            host_pattern=host_pattern,
+            label=label
+        )
+        filtered_hosts: List[str] = filter_spec.filter_matching_hostspecs(hosts)
+        hosts = [h for h in hosts if h.hostname in filtered_hosts]
+
+        if host_status:
+            hosts = [h for h in hosts if h.status.lower() == host_status]
+
         if format != Format.plain:
             output = to_format(hosts, format, many=True, cls=HostSpec)
         else:
@@ -388,6 +398,14 @@ class OrchestratorCli(OrchestratorClientMixin, MgrModule,
                 table.add_row((host.hostname, host.addr, ' '.join(
                     host.labels), host.status.capitalize()))
             output = table.get_string()
+        if format == Format.plain:
+            output += f'\n{len(hosts)} hosts in cluster'
+            if label:
+                output += f' who had label {label}'
+            if host_pattern:
+                output += f' whose hostname matched {host_pattern}'
+            if host_status:
+                output += f' with status {host_status}'
         return HandleCommandResult(stdout=output)
 
     @_cli_write_command('orch host label add')