diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2022-02-15 16:24:47 -0500 |
---|---|---|
committer | Jake Hunsaker <jhunsake@redhat.com> | 2022-02-16 10:48:31 -0500 |
commit | d0f9d507b0ec63c9e8f3e5d7b6507d9d0f97c038 (patch) | |
tree | e87c39e1df0146aaebce400376d89795ea585db8 | |
parent | ac4eb48fa35c13b99ada41540831412480babf8d (diff) | |
download | sos-d0f9d507b0ec63c9e8f3e5d7b6507d9d0f97c038.tar.gz |
[runtimes] Allow container IDs to be used with `container_exists()`
As container runtimes can interchange container names and container IDs,
sos should also allow the use of container IDs when checking for the
presence of a given container.
In particular, this change unblocks the use of `Plugin.exec_cmd()` when
used in conjunction with `Plugin.get_container_by_name()` to pick a
container based on a provided regex that the container name may match.
Related: #2856
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
-rw-r--r-- | sos/policies/runtimes/__init__.py | 17 | ||||
-rw-r--r-- | sos/report/plugins/__init__.py | 6 |
2 files changed, 20 insertions, 3 deletions
diff --git a/sos/policies/runtimes/__init__.py b/sos/policies/runtimes/__init__.py index 5ac67354..d2837349 100644 --- a/sos/policies/runtimes/__init__.py +++ b/sos/policies/runtimes/__init__.py @@ -147,6 +147,23 @@ class ContainerRuntime(): vols.append(ent[-1]) return vols + def container_exists(self, container): + """Check if a given container ID or name exists on the system from the + perspective of the container runtime. + + Note that this will only check _running_ containers + + :param container: The name or ID of the container + :type container: ``str`` + + :returns: True if the container exists, else False + :rtype: ``bool`` + """ + for _contup in self.containers: + if container in _contup: + return True + return False + def fmt_container_cmd(self, container, cmd, quotecmd): """Format a command to run inside a container using the runtime diff --git a/sos/report/plugins/__init__.py b/sos/report/plugins/__init__.py index 2988be08..cc5cb65b 100644 --- a/sos/report/plugins/__init__.py +++ b/sos/report/plugins/__init__.py @@ -2593,7 +2593,7 @@ class Plugin(): """If a container runtime is present, check to see if a container with a given name is currently running - :param name: The name of the container to check presence of + :param name: The name or ID of the container to check presence of :type name: ``str`` :returns: ``True`` if `name` exists, else ``False`` @@ -2601,8 +2601,8 @@ class Plugin(): """ _runtime = self._get_container_runtime() if _runtime is not None: - con = _runtime.get_container_by_name(name) - return con is not None + return (_runtime.container_exists(name) or + _runtime.get_container_by_name(name) is not None) return False def get_all_containers_by_regex(self, regex, get_all=False): |