check /.dockerenv and /.dockerinit to guess a dockercontainer (#74349)

* use os.path.exists for docker-check

Co-authored-by: Jacob Floyd <cognifloyd@gmail.com>
pull/76219/head
Jan Fader 3 years ago committed by GitHub
parent 636b317a4f
commit 17ec2d4952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- setup - detect docker container with check for ./dockerenv or ./dockinit (https://github.com/ansible/ansible/pull/74349).

@ -110,6 +110,16 @@ class LinuxVirtual(Virtual):
virtual_facts['virtualization_role'] = 'guest' virtual_facts['virtualization_role'] = 'guest'
found_virt = True found_virt = True
# If docker/containerd has a custom cgroup parent, checking /proc/1/cgroup (above) might fail.
# https://docs.docker.com/engine/reference/commandline/dockerd/#default-cgroup-parent
# Fallback to more rudimentary checks.
if os.path.exists('/.dockerenv') or os.path.exists('/.dockerinit'):
guest_tech.add('docker')
if not found_virt:
virtual_facts['virtualization_type'] = 'docker'
virtual_facts['virtualization_role'] = 'guest'
found_virt = True
# ensure 'container' guest_tech is appropriately set # ensure 'container' guest_tech is appropriately set
if guest_tech.intersection(set(['docker', 'lxc', 'podman', 'openvz', 'containerd'])) or systemd_container: if guest_tech.intersection(set(['docker', 'lxc', 'podman', 'openvz', 'containerd'])) or systemd_container:
guest_tech.add('container') guest_tech.add('container')

@ -8,6 +8,30 @@ __metaclass__ = type
from ansible.module_utils.facts.virtual import linux from ansible.module_utils.facts.virtual import linux
def mock_os_path_is_file_docker(filename):
if filename in ('/.dockerenv', '/.dockerinit'):
return True
return False
def test_get_virtual_facts_docker(mocker):
mocker.patch('os.path.exists', mock_os_path_is_file_docker)
module = mocker.Mock()
module.run_command.return_value = (0, '', '')
inst = linux.LinuxVirtual(module)
facts = inst.get_virtual_facts()
expected = {
'virtualization_role': 'guest',
'virtualization_tech_host': set(),
'virtualization_type': 'docker',
'virtualization_tech_guest': set(['docker', 'container']),
}
assert facts == expected
def test_get_virtual_facts_bhyve(mocker): def test_get_virtual_facts_bhyve(mocker):
mocker.patch('os.path.exists', return_value=False) mocker.patch('os.path.exists', return_value=False)
mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_content', return_value='') mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_content', return_value='')

Loading…
Cancel
Save