From be1a8c2c43e97f91bd26554ba9dadc71c499865d Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 24 May 2024 07:16:14 +0800 Subject: [PATCH] cephadm: extract python() helper to execute python statement to prepare for a change to use importlib, then fallback to pkg_resources. as the former is only available in Python 3.8, while the latter is deprecated. Refs https://tracker.ceph.com/issues/66201 Signed-off-by: Kefu Chai --- src/cephadm/cephadmlib/daemons/iscsi.py | 32 ++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/cephadm/cephadmlib/daemons/iscsi.py b/src/cephadm/cephadmlib/daemons/iscsi.py index ade88a90af0ff..da03fe36c03f3 100644 --- a/src/cephadm/cephadmlib/daemons/iscsi.py +++ b/src/cephadm/cephadmlib/daemons/iscsi.py @@ -119,22 +119,26 @@ class CephIscsi(ContainerDaemonForm): @staticmethod def get_version(ctx, container_id): # type: (CephadmContext, str) -> Optional[str] - version = None - out, err, code = call( - ctx, - [ - ctx.container_engine.path, - 'exec', - container_id, - '/usr/bin/python3', - '-c', - "import pkg_resources; print(pkg_resources.require('ceph_iscsi')[0].version)", - ], - verbosity=CallVerbosity.QUIET, + def python(s: str) -> Tuple[str, str, int]: + return call( + ctx, + [ + ctx.container_engine.path, + 'exec', + container_id, + '/usr/bin/python3', + '-c', + s, + ], + verbosity=CallVerbosity.QUIET, + ) + + out, _, code = python( + "from importlib.metadata import version; print(version('ceph_iscsi'))" ) if code == 0: - version = out.strip() - return version + return out.strip() + return None def validate(self): # type: () -> None -- 2.39.5