add type get_container_runtime & handle no runtime

There was no code to handle if at this stage the runtime existed.
This caused issues with type hints since `shutil.which()` can
return None, which had not previously been accounted for.

We did not use the opportunity to consolidate all the code for
detecting the runtime, to make this review easier.
This commit is contained in:
deeplow 2022-07-21 11:44:01 +01:00
parent 665e4d54f7
commit 6ddd411be8
No known key found for this signature in database
GPG key ID: 577982871529A52A
2 changed files with 13 additions and 3 deletions

View file

@ -52,11 +52,17 @@ def exec(args, stdout_callback=None):
def exec_container(command, extra_args=[], stdout_callback=None): def exec_container(command, extra_args=[], stdout_callback=None):
if container_tech == "podman": if container_tech == "podman":
container_runtime = shutil.which("podman") container_runtime = shutil.which("podman")
if container_runtime is None:
raise Exception(f"podman is not installed")
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") 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

@ -386,11 +386,15 @@ class GlobalCommon(object):
) )
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
def get_container_runtime(self): def get_container_runtime(self) -> str:
if platform.system() == "Linux": if platform.system() == "Linux":
return shutil.which("podman") runtime_name = "podman"
else: else:
return shutil.which("docker") runtime_name = "docker"
runtime = shutil.which(runtime_name)
if runtime is None:
raise Exception(f"{runtime_name} is not installed")
return runtime
def get_resource_path(self, filename: str) -> str: def get_resource_path(self, filename: str) -> str:
if getattr(sys, "dangerzone_dev", False): if getattr(sys, "dangerzone_dev", False):