From 6202c0dba97ed3dad2ecbd2ad9418ae6f11d7a39 Mon Sep 17 00:00:00 2001 From: deeplow Date: Thu, 15 Sep 2022 11:12:29 +0100 Subject: [PATCH] deduplicate container-tech-checking logic The logic for detecting if we were are running on docker or podman and identifying its respective binary were scattered across the codebase. This centralizes it all in container.py --- dangerzone/container.py | 32 ++++++++++++++------------------ dangerzone/gui/main_window.py | 10 +++------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/dangerzone/container.py b/dangerzone/container.py index 4caa4dc..0eb67ea 100644 --- a/dangerzone/container.py +++ b/dangerzone/container.py @@ -14,13 +14,6 @@ from .util import get_resource_path, get_subprocess_startupinfo container_name = "dangerzone.rocks/dangerzone" -# What container tech is used for this platform? -if platform.system() == "Linux": - container_tech = "podman" -else: - # Windows, Darwin, and unknown use docker for now, dangerzone-vm eventually - container_tech = "docker" - # Define startupinfo for subprocesses if platform.system() == "Windows": startupinfo = subprocess.STARTUPINFO() # type: ignore [attr-defined] @@ -34,14 +27,23 @@ log = logging.getLogger(__name__) container_name = "dangerzone.rocks/dangerzone" -def get_container_runtime() -> str: +class NoContainerTechException(Exception): + pass + + +def get_container_tech() -> str: if platform.system() == "Linux": runtime_name = "podman" else: + # Windows, Darwin, and unknown use docker for now, dangerzone-vm eventually runtime_name = "docker" - runtime = shutil.which(runtime_name) + return runtime_name + + +def get_container_runtime() -> str: + runtime = shutil.which(get_container_tech()) if runtime is None: - raise Exception(f"{runtime_name} is not installed") + raise NoContainerTechException(f"{runtime_name} is not installed") return runtime @@ -149,19 +151,13 @@ def exec_container( extra_args: List[str] = [], stdout_callback: Callable[[str], None] = None, ) -> int: - if container_tech == "podman": - container_runtime = shutil.which("podman") - if container_runtime is None: - raise Exception(f"podman is not installed") + container_runtime = container.get_container_runtime() + if get_container_tech() == "podman": platform_args = [] security_args = ["--security-opt", "no-new-privileges"] security_args += ["--userns", "keep-id"] else: - container_runtime = shutil.which("docker") - if container_runtime is None: - raise Exception(f"docker is not installed") - platform_args = ["--platform", "linux/amd64"] security_args = ["--security-opt=no-new-privileges:true"] diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 00dfd48..12cf066 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -156,13 +156,9 @@ class WaitingWidget(QtWidgets.QWidget): def check_state(self) -> None: state: Optional[str] = None - # Can we find the container runtime binary binary - if platform.system() == "Linux": - container_runtime = shutil.which("podman") - else: - container_runtime = shutil.which("docker") - - if container_runtime is None: + try: + container_runtime = container.get_container_runtime() + except container.NoContainerTechException: log.error("Docker is not installed") state = "not_installed"