From 9c32759dcc6e6b2f48f535e2ad9728c1608c5529 Mon Sep 17 00:00:00 2001 From: ndt Date: Thu, 16 Dec 2021 10:21:01 -0500 Subject: [PATCH] Add basic Podman support Previously, with Podman installed in rootless mode, it-depends crashes telling me I need to `apt-get install` docker. Add some basic conditional logic to check for a Podman rootless socket and use it if present. If not found, _try_podman_socket() returns `None`, the previous value supplied by it-depends for base_url. This should make it backwards-compatible with existing it-depends users. --- it_depends/docker.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/it_depends/docker.py b/it_depends/docker.py index 3b8c4c3..f0ffab1 100644 --- a/it_depends/docker.py +++ b/it_depends/docker.py @@ -3,6 +3,7 @@ import shutil import subprocess import sys +import os from pathlib import Path from tempfile import mkdtemp from tqdm import tqdm @@ -246,7 +247,8 @@ def rebuild(self, nocache: bool = False): raise ValueError("Could not find the Dockerfile.") # use the low-level APIClient so we can get streaming build status try: - cli = docker.APIClient() + sock = self._try_podman_socket() + cli = docker.APIClient(base_url=sock) except DockerException as e: raise ValueError("Docker not installed. Try `sudo apt install docker`.") from e with tqdm(desc="Archiving the build directory", unit=" steps", leave=False) as t: @@ -281,3 +283,24 @@ def rebuild(self, nocache: bool = False): t.update(new_line - last_line) last_line = new_line t.write(line["stream"].replace("\n", "").strip()) + + def _try_podman_socket(self): + """Try to find a podman socket. If it fails, use Docker default. + + First try to construct the Docker socket via the podman rootless + socket path. Failing that, fallback to Docker library defaults with + base_url=None. + + This isn't as robust as it could be, but it should work on systems + using either rootless podman or Docker. + """ + runtime_dir = os.environ.get("XDG_RUNTIME_DIR", None) + if runtime_dir is None: + # We're not gonna find it without XDG's help; fallback to Docker. + return None + sock_path = Path(f'{runtime_dir}/podman/podman.sock') + if sock_path.exists(): + sock = f'unix://{sock_path}' + if not sock_path.exists(): + sock = None + return sock