|
4 | 4 | import sys |
5 | 5 |
|
6 | 6 | import docker |
| 7 | +from more_itertools import ( |
| 8 | + one, |
| 9 | +) |
7 | 10 |
|
8 | 11 | from azul.logging import ( |
9 | 12 | configure_script_logging, |
@@ -31,19 +34,22 @@ def resolve_container_path(container_path): |
31 | 34 | try: |
32 | 35 | with open(mountinfo) as f: |
33 | 36 | log.info('Found %s', mountinfo) |
34 | | - # Entries in /proc/self/mountinfo look like this: |
35 | | - # 752 744 259:2 /docker/containers/dc61d93…ID…/hosts … |
| 37 | + # Entries of interest in /proc/self/mountinfo look as follows: |
| 38 | + # 752 744 259:2 /docker/containers/dc61d9…CONTAINER_ID…/<variable> … |
36 | 39 | prefix = '/docker/containers' |
37 | | - contents = csv.reader(f, delimiter=' ') |
38 | | - for line in contents: |
| 40 | + log.info('cgroup v2 prefix is %s', prefix) |
| 41 | + containers = set() |
| 42 | + for line in csv.reader(f, delimiter=' '): |
39 | 43 | path = line[3] |
40 | 44 | if path.startswith(prefix): |
41 | | - log.info('Extracting the container ID from %s', path) |
42 | | - parts = path.rsplit('/', maxsplit=2)[:-1] |
43 | | - assert len(parts) == 2 and parts[0] == prefix, parts |
44 | | - container_id = parts[1] |
| 45 | + log.info('Extracting container ID from %s', path) |
| 46 | + prfx, id, var = path.rsplit('/', maxsplit=2) |
| 47 | + assert var in ('hosts', 'hostname', 'resolv.conf'), var |
| 48 | + assert prfx == prefix, prfx |
| 49 | + containers.add(id) |
| 50 | + container_id = one(containers) |
45 | 51 | except FileNotFoundError: |
46 | | - log.info('Did not find %s', proc_cgroup) |
| 52 | + log.info('Did not find %s', mountinfo) |
47 | 53 | else: |
48 | 54 | api = docker.client.from_env().api |
49 | 55 | for mount in api.inspect_container(container_id)['Mounts']: |
|
0 commit comments