Improve container detection. (#522)

This commit is contained in:
Felix Fontein
2022-12-06 08:11:44 +01:00
committed by GitHub
parent 3ed7c56704
commit e87b327764
2 changed files with 7 additions and 2 deletions
+5 -2
View File
@@ -116,15 +116,18 @@ def main():
with open(mountinfo_path, 'rb') as f:
contents = f.read().decode('utf-8')
# As to why this works, see the explanations by Matt Clay in
# https://github.com/ansible/ansible/blob/80d2f8da02052f64396da6b8caaf820eedbf18e2/test/lib/ansible_test/_internal/docker_util.py#L571-L610
for line in contents.splitlines():
parts = line.split()
if len(parts) >= 5 and parts[4] == '/etc/hostname':
m = re.match('.*/docker/containers/([a-f0-9]+)/hostname', parts[3])
m = re.match('.*/([a-f0-9]{64})/hostname$', parts[3])
if m:
container_id = m.group(1)
container_type = 'docker'
m = re.match('/containers/overlay-containers/([a-f0-9]+)/userdata/hostname', parts[3])
m = re.match('.*/([a-f0-9]{64})/userdata/hostname$', parts[3])
if m:
container_id = m.group(1)
container_type = 'podman'