From d8998ae464db8d52d74570b2b79dc72529ae9146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Tue, 25 Jun 2024 19:08:40 +0200 Subject: [PATCH] Add a `container.get_runtime_version` static method --- dangerzone/isolation_provider/container.py | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index 49b32d4..f31ffff 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -78,6 +78,34 @@ class Container(IsolationProvider): ) 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 def get_container_engine() -> str: container_name = Container.get_container_engine_name()