]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/orchestrator: add filtering and count option for orch host ls 44020/head
authorAdam King <adking@redhat.com>
Fri, 19 Nov 2021 00:43:35 +0000 (19:43 -0500)
committerAdam King <adking@redhat.com>
Fri, 17 Dec 2021 14:51:56 +0000 (09:51 -0500)
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>
doc/cephadm/host-management.rst
src/pybind/mgr/orchestrator/module.py

index c10e372f7be44327e41eb4f0b6560131d49f1ae9..f342558fafcb74fcf841a51f36fb9c4cd4faa069 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 febe13b8b63ca1d39bce49a8412259517d061205..f4a6a9f7d96850554a0fe3c58b19fa4bc61adca2 100644 (file)
@@ -371,11 +371,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:
@@ -389,6 +399,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')