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
This commit is contained in:
deeplow 2022-09-15 11:12:29 +01:00
parent a822870853
commit 6202c0dba9
No known key found for this signature in database
GPG key ID: 577982871529A52A
2 changed files with 17 additions and 25 deletions

View file

@ -14,13 +14,6 @@ from .util import get_resource_path, get_subprocess_startupinfo
container_name = "dangerzone.rocks/dangerzone" 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 # Define startupinfo for subprocesses
if platform.system() == "Windows": if platform.system() == "Windows":
startupinfo = subprocess.STARTUPINFO() # type: ignore [attr-defined] startupinfo = subprocess.STARTUPINFO() # type: ignore [attr-defined]
@ -34,14 +27,23 @@ log = logging.getLogger(__name__)
container_name = "dangerzone.rocks/dangerzone" container_name = "dangerzone.rocks/dangerzone"
def get_container_runtime() -> str: class NoContainerTechException(Exception):
pass
def get_container_tech() -> str:
if platform.system() == "Linux": if platform.system() == "Linux":
runtime_name = "podman" runtime_name = "podman"
else: else:
# Windows, Darwin, and unknown use docker for now, dangerzone-vm eventually
runtime_name = "docker" 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: if runtime is None:
raise Exception(f"{runtime_name} is not installed") raise NoContainerTechException(f"{runtime_name} is not installed")
return runtime return runtime
@ -149,19 +151,13 @@ def exec_container(
extra_args: List[str] = [], extra_args: List[str] = [],
stdout_callback: Callable[[str], None] = None, stdout_callback: Callable[[str], None] = None,
) -> int: ) -> int:
if container_tech == "podman": container_runtime = container.get_container_runtime()
container_runtime = shutil.which("podman")
if container_runtime is None:
raise Exception(f"podman is not installed")
if get_container_tech() == "podman":
platform_args = [] platform_args = []
security_args = ["--security-opt", "no-new-privileges"] security_args = ["--security-opt", "no-new-privileges"]
security_args += ["--userns", "keep-id"] security_args += ["--userns", "keep-id"]
else: else:
container_runtime = shutil.which("docker")
if container_runtime is None:
raise Exception(f"docker is not installed")
platform_args = ["--platform", "linux/amd64"] platform_args = ["--platform", "linux/amd64"]
security_args = ["--security-opt=no-new-privileges:true"] security_args = ["--security-opt=no-new-privileges:true"]

View file

@ -156,13 +156,9 @@ class WaitingWidget(QtWidgets.QWidget):
def check_state(self) -> None: def check_state(self) -> None:
state: Optional[str] = None state: Optional[str] = None
# Can we find the container runtime binary binary try:
if platform.system() == "Linux": container_runtime = container.get_container_runtime()
container_runtime = shutil.which("podman") except container.NoContainerTechException:
else:
container_runtime = shutil.which("docker")
if container_runtime is None:
log.error("Docker is not installed") log.error("Docker is not installed")
state = "not_installed" state = "not_installed"