Add a container.get_runtime_version static method

This commit is contained in:
Alexis Métaireau 2024-06-25 19:08:40 +02:00
parent 33ab2454b8
commit d8998ae464
No known key found for this signature in database
GPG key ID: C65C7A89A8FFC56E

View file

@ -78,6 +78,34 @@ class Container(IsolationProvider):
) )
raise RuntimeError(msg) raise RuntimeError(msg)
@staticmethod
def get_runtime_version() -> Tuple[int, int]:
"""Get the name and version number of the runtime (runc)
"""
container_engine = Container.get_container_engine_name()
query = '{{range .Server.Components}}{{if eq .Name "runc"}}{{.Version}}{{end}}{{end}}'
cmd = [container_engine, "version", "-f", query]
try:
version = subprocess.run(
cmd, capture_output=True, check=True
).stdout.decode()
except Exception as e:
msg = f"Could not get the version of {container_engine.capitalize()} runc: {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 {container_engine.capitalize()} runc"
f" (found: '{version}') due to the following error: {e}"
)
raise RuntimeError(msg)
@staticmethod @staticmethod
def get_container_engine() -> str: def get_container_engine() -> str:
container_name = Container.get_container_engine_name() container_name = Container.get_container_engine_name()