Get container runtime version

Get the (major, minor) parts of the Docker/Podman version, to check if
some specific features can be used, or if we need a fallback. These
features are related with the upcoming gVisor integration, and will be
added in subsequent commits.
This commit is contained in:
Alex Pyrgiotis 2024-06-10 18:32:54 +03:00
parent cf9a545c1a
commit 7179d6f734
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA

View file

@ -7,7 +7,7 @@ import shlex
import shutil import shutil
import subprocess import subprocess
import sys import sys
from typing import Any, List, Optional from typing import Any, List, Optional, Tuple
from ..conversion import errors from ..conversion import errors
from ..document import Document from ..document import Document
@ -44,6 +44,39 @@ class Container(IsolationProvider):
runtime_name = "docker" runtime_name = "docker"
return runtime_name return runtime_name
@staticmethod
def get_runtime_version() -> Tuple[int, int]:
"""Get the major/minor parts of the Docker/Podman version.
Some of the operations we perform in this module rely on some Podman features
that are not available across all of our platforms. In order to have a proper
fallback, we need to know the Podman version. More specifically, we're fine with
just knowing the major and minor version, since writing/installing a full-blown
semver parser is an overkill.
"""
# Get the Docker/Podman version, using a Go template.
runtime = Container.get_runtime_name()
cmd = [runtime, "version", "-f", "{{.Client.Version}}"]
try:
version = subprocess.run(
cmd, capture_output=True, check=True
).stdout.decode()
except Exception as e:
msg = f"Could not get the version of the {runtime.capitalize()} tool: {e}"
raise RuntimeError(msg) from e
# Parse this version and return the major/minor parts, since we don't need the
# rest.
try:
major, minor, _ = version.split(".", 3)
return (int(major), int(minor))
except Exception as e:
msg = (
f"Could not parse the version of the {runtime.capitalize()} tool"
f" (found: '{version}') due to the following error: {e}"
)
raise RuntimeError(msg)
@staticmethod @staticmethod
def get_runtime() -> str: def get_runtime() -> str:
container_tech = Container.get_runtime_name() container_tech = Container.get_runtime_name()