Skip to content

Commit

Permalink
Merge pull request ceph#44020 from adk3798/host-ls-scale
Browse files Browse the repository at this point in the history
mgr/orchestrator: add filtering and count option for orch host ls

Reviewed-by: Patrick Donnelly <[email protected]>
Reviewed-by: Sebastian Wagner <[email protected]>
  • Loading branch information
sebastian-philipp authored Jan 7, 2022
2 parents fa2173d + edd9bf3 commit af5f475
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion doc/cephadm/host-management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
20 changes: 19 additions & 1 deletion src/pybind/mgr/orchestrator/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,21 @@ def _update_set_addr(self, hostname: str, addr: str) -> HandleCommandResult:
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:
Expand All @@ -389,6 +399,14 @@ def _get_hosts(self, format: Format = Format.plain) -> HandleCommandResult:
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')
Expand Down

0 comments on commit af5f475

Please sign in to comment.