diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-08-12 12:03:27 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-08-12 12:03:27 +0000 |
commit | d46c288cfdeadb070fa71c744b9970b3f1c7a623 (patch) | |
tree | 729e2a3fd230cfaca2e921e06fbefa9f586a8f98 /pre_commit/languages | |
parent | Releasing debian version 2.13.0-1. (diff) | |
download | pre-commit-d46c288cfdeadb070fa71c744b9970b3f1c7a623.tar.xz pre-commit-d46c288cfdeadb070fa71c744b9970b3f1c7a623.zip |
Merging upstream version 2.14.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit/languages')
-rw-r--r-- | pre_commit/languages/docker.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/pre_commit/languages/docker.py b/pre_commit/languages/docker.py index 5b21ec9..644d8d2 100644 --- a/pre_commit/languages/docker.py +++ b/pre_commit/languages/docker.py @@ -1,7 +1,6 @@ import hashlib import json import os -import socket from typing import Sequence from typing import Tuple @@ -9,6 +8,7 @@ import pre_commit.constants as C from pre_commit.hook import Hook from pre_commit.languages import helpers from pre_commit.prefix import Prefix +from pre_commit.util import CalledProcessError from pre_commit.util import clean_path_on_failure from pre_commit.util import cmd_output_b @@ -26,12 +26,28 @@ def _is_in_docker() -> bool: return False +def _get_container_id() -> str: + # It's assumed that we already check /proc/1/cgroup in _is_in_docker. The + # cpuset cgroup controller existed since cgroups were introduced so this + # way of getting the container ID is pretty reliable. + with open('/proc/1/cgroup', 'rb') as f: + for line in f.readlines(): + if line.split(b':')[1] == b'cpuset': + return os.path.basename(line.split(b':')[2]).strip().decode() + raise RuntimeError('Failed to find the container ID in /proc/1/cgroup.') + + def _get_docker_path(path: str) -> str: if not _is_in_docker(): return path - hostname = socket.gethostname() - _, out, _ = cmd_output_b('docker', 'inspect', hostname) + container_id = _get_container_id() + + try: + _, out, _ = cmd_output_b('docker', 'inspect', container_id) + except CalledProcessError: + # self-container was not visible from here (perhaps docker-in-docker) + return path container, = json.loads(out) for mount in container['Mounts']: |