Skip to content

Commit 37269ae

Browse files
cephadm: combine get_container_info with helper function
Combine the get_container_info function with the helper function _get_matching_daemons_by_name (it's only caller). Usually I like to break things up but this change consolidates the code of get_container_info in preparation for refactoring it to be based on the new listing functions. Signed-off-by: John Mulligan <[email protected]>
1 parent 2769e36 commit 37269ae

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/cephadm/cephadm.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,22 @@ def daemon_name_or_type(daemon: Dict[str, str]) -> str:
481481
logger.warning(f'Trying to get container info using invalid daemon name {daemon_filter}')
482482
return None
483483
if by_name:
484-
matching_daemons = _get_matching_daemons_by_name(ctx, daemon_filter)
484+
# NOTE: we are not passing detail=False to this list_daemons call
485+
# as we want the container_image name in the case where we are
486+
# doing this by name and this is skipped when detail=False
487+
matching_daemons = list_daemons(ctx, daemon_name=daemon_filter)
488+
if len(matching_daemons) > 1:
489+
logger.warning(f'Found multiple daemons sharing same name: {daemon_filter}')
490+
# Take the first daemon we find that is actually running, or just the
491+
# first in the list if none are running
492+
matched_daemon = None
493+
for d in matching_daemons:
494+
if 'state' in d and d['state'] == 'running':
495+
matched_daemon = d
496+
break
497+
if not matched_daemon:
498+
matched_daemon = matching_daemons[0]
499+
matching_daemons = [matched_daemon]
485500
else:
486501
# NOTE: we are passing detail=False here as in this case where we are not
487502
# doing it by_name, we really only need the names of the daemons. Additionally,
@@ -514,26 +529,6 @@ def daemon_name_or_type(daemon: Dict[str, str]) -> str:
514529
return None
515530

516531

517-
def _get_matching_daemons_by_name(ctx: CephadmContext, daemon_filter: str) -> List[Dict[str, str]]:
518-
# NOTE: we are not passing detail=False to this list_daemons call
519-
# as we want the container_image name in the case where we are
520-
# doing this by name and this is skipped when detail=False
521-
matching_daemons = list_daemons(ctx, daemon_name=daemon_filter)
522-
if len(matching_daemons) > 1:
523-
logger.warning(f'Found multiple daemons sharing same name: {daemon_filter}')
524-
# Take the first daemon we find that is actually running, or just the
525-
# first in the list if none are running
526-
matched_daemon = None
527-
for d in matching_daemons:
528-
if 'state' in d and d['state'] == 'running':
529-
matched_daemon = d
530-
break
531-
if not matched_daemon:
532-
matched_daemon = matching_daemons[0]
533-
matching_daemons = [matched_daemon]
534-
return matching_daemons
535-
536-
537532
def infer_local_ceph_image(ctx: CephadmContext, container_path: str) -> Optional[str]:
538533
"""
539534
Infer the local ceph image based on the following priority criteria:

0 commit comments

Comments
 (0)