From e965813da15e02ac640e9cb3635b556e49dcbd30 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Thu, 30 Jan 2025 19:04:32 -0500 Subject: [PATCH] cephadm: add parsed_container_image_stats to container_engines Add a new function that combines the call and parse operations that exist in cephadm.py (currently get_container_stats_by_image_name and related parsing code). This will be used in a future commit to replace that code and reduce the size of cephadm.py. Signed-off-by: John Mulligan --- src/cephadm/cephadmlib/container_engines.py | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/cephadm/cephadmlib/container_engines.py b/src/cephadm/cephadmlib/container_engines.py index 6bc896bcd8a4d..9b308fdd4174e 100644 --- a/src/cephadm/cephadmlib/container_engines.py +++ b/src/cephadm/cephadmlib/container_engines.py @@ -370,3 +370,48 @@ def parsed_container_stats( ctx, container_name, container_path=container_path ) return _parse_container_stats(out, err, code) + + +def _container_image_stats( + ctx: CephadmContext, image_name: str, *, container_path: str = '' +) -> Tuple[str, str, int]: + """returns image id, created time, and ceph version if available""" + container_path = container_path or ctx.container_engine.path + cmd = [ + container_path, + 'image', + 'inspect', + '--format', + '{{.Id}},{{.Created}},{{index .Config.Labels "io.ceph.version"}}', + image_name, + ] + out, err, code = call(ctx, cmd, verbosity=CallVerbosity.QUIET) + return out, err, code + + +def _parse_container_image_stats( + image_name: str, + out: str, + err: str, + code: int, +) -> Optional[ContainerInfo]: + if code != 0: + return None + (image_id, start, version) = out.strip().split(',') + # keep in mind, the daemon container is not running, so no container id here + return ContainerInfo( + container_id='', + image_name=image_name, + image_id=image_id, + start=start, + version=version, + ) + + +def parsed_container_image_stats( + ctx: CephadmContext, image_name: str, *, container_path: str = '' +) -> Optional[ContainerInfo]: + out, err, code = _container_image_stats( + ctx, image_name, container_path=container_path + ) + return _parse_container_image_stats(image_name, out, err, code) -- 2.39.5