From cc087cf979a5be4b1ca8b8a0857c5e73bb52585f Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Thu, 30 Jan 2025 18:48:26 -0500 Subject: [PATCH] cephadm: add parsed_container_stats to container_engines Add a new function that combines the call and parse operations that exist in cephadm.py (currently get_container_stats 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 | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/cephadm/cephadmlib/container_engines.py b/src/cephadm/cephadmlib/container_engines.py index 9eb2f622b8f17..6bc896bcd8a4d 100644 --- a/src/cephadm/cephadmlib/container_engines.py +++ b/src/cephadm/cephadmlib/container_engines.py @@ -329,3 +329,44 @@ class ContainerInfo: and self.start == other.start and self.version == other.version ) + + +def _container_stats( + ctx: CephadmContext, + container_name: str, + *, + container_path: str, +) -> Tuple[str, str, int]: + """returns container id, image name, image id, created time, and ceph version if available""" + container_path = container_path or ctx.container_engine.path + out, err, code = '', '', -1 + cmd = [ + container_path, + 'inspect', + '--format', + '{{.Id}},{{.Config.Image}},{{.Image}},{{.Created}},{{index .Config.Labels "io.ceph.version"}}', + container_name, + ] + out, err, code = call(ctx, cmd, verbosity=CallVerbosity.QUIET) + return out, err, code + + +def _parse_container_stats( + out: str, err: str, code: int +) -> Optional[ContainerInfo]: + if code != 0: + return None + # container_id, image_name, image_id, start, version + return ContainerInfo(*list(out.strip().split(','))) + + +def parsed_container_stats( + ctx: CephadmContext, + container_name: str, + *, + container_path: str, +) -> Optional[ContainerInfo]: + out, err, code = _container_stats( + ctx, container_name, container_path=container_path + ) + return _parse_container_stats(out, err, code) -- 2.39.5