Use /proc/1/sched

This commit is contained in:
Jiri Tyr 2022-02-10 17:30:22 +00:00
parent 039db10d86
commit 5cfdaa3aee
2 changed files with 229 additions and 35 deletions

View file

@ -19,7 +19,7 @@ get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy
def _is_in_docker() -> bool:
def _is_in_docker_cgroup() -> bool:
try:
with open('/proc/1/cgroup', 'rb') as f:
for line in f.readlines():
@ -37,7 +37,27 @@ def _is_in_docker() -> bool:
return False
def _get_container_id() -> str:
def _is_in_docker_sched() -> bool:
try:
with open('/proc/1/sched', 'rb') as f:
line = f.readline()
if line.startswith(b'systemd ') or line.startswith(b'init '):
return False
return True
except FileNotFoundError:
return False
def _is_in_docker() -> bool:
if _is_in_docker_cgroup() or _is_in_docker_sched():
return True
return False
def _get_container_id_cgroup() -> 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.
@ -45,7 +65,90 @@ def _get_container_id() -> str:
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.')
return ''
def _get_container_id_sched() -> str:
# The idea here is to try to match the the workdir option found in the
# overlay mount with the GraphDriver.Data.WorkDir from the docker describe.
# Get details for the overlay mount type
try:
_, out, _ = cmd_output_b('mount', '-t', 'overlay')
except CalledProcessError:
# No mount command available or the -t option is not supported
return ''
lines = out.decode().strip().split('\n')
# There is always only one overlay mount inside the container
if len(lines) > 1 or lines[0] == '' or '(' not in lines[0]:
return ''
_, all_opts = lines[0].strip(')').split('(')
opts = all_opts.split(',')
# Search for workdir option
for opt in opts:
if '=' in opt:
k, v = opt.split('=')
if k == 'workdir':
# We have found workdir
workdir = v
break
else:
# No workdir was found
return ''
# Get list IDs for all running containers
try:
_, out, _ = cmd_output_b('docker', 'ps', '--format', '{{ .ID }}')
except CalledProcessError:
# There is probably no docker command
return ''
container_ids = out.decode().strip().split('\n')
# Check if there are any container IDs
if len(container_ids) == 1 and container_ids[0] == '':
return ''
# Search for a container that has the workdir we got from the mount command
for container_id in container_ids:
try:
_, out, _ = cmd_output_b('docker', 'inspect', container_id)
except CalledProcessError:
# Container probably doesn't exist anymore
return ''
container, = json.loads(out)
if (
'GraphDriver' in container and
'Data' in container['GraphDriver'] and
'WorkDir' in container['GraphDriver']['Data'] and
container['GraphDriver']['Data']['WorkDir'] == workdir
):
# We have found matching container!
return container_id
else:
# No matching container found
return ''
def _get_container_id() -> str:
container_id = _get_container_id_cgroup()
if container_id == '':
container_id = _get_container_id_sched()
if container_id == '':
raise RuntimeError('Failed to find the container ID.')
return container_id
def _get_docker_path(path: str) -> str: