-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathcontainers.rb
81 lines (69 loc) · 2.34 KB
/
containers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
module Facter
module Resolvers
module Linux
class Containers < BaseResolver
# :virtual
# :hypervisor
init_resolver
INFO = { 'docker' => 'id', 'lxc' => 'name' }.freeze
class << self
private
def post_resolve(fact_name, _options)
@fact_list.fetch(fact_name) do
read_environ(fact_name) || read_cgroup(fact_name)
end
end
def read_cgroup(fact_name)
output_cgroup = Facter::Util::FileHelper.safe_read('/proc/1/cgroup', nil)
return unless output_cgroup
output_docker = %r{docker/(.+)}.match(output_cgroup)
output_lxc = %r{^/lxc/([^/]+)}.match(output_cgroup)
info, vm = extract_vm_and_info(output_docker, output_lxc)
@fact_list[:vm] = vm
@fact_list[:hypervisor] = { vm.to_sym => info } if vm
@fact_list[fact_name]
end
def read_environ(fact_name)
begin
container = Facter::Util::Linux::Proc.getenv_for_pid(1, 'container')
rescue StandardError => e
log.warn("Unable to getenv for pid 1, '#{e}'")
return nil
end
return if container.nil? || container.empty?
info = {}
case container
when 'lxc'
vm = 'lxc'
when 'podman'
vm = 'podman'
when 'crio'
vm = 'crio'
when 'systemd-nspawn'
vm = 'systemd_nspawn'
info = { 'id' => Facter::Util::FileHelper.safe_read('/etc/machine-id', nil).strip }
else
vm = 'container_other'
log.warn("Container runtime, '#{container}', is unsupported, setting to '#{vm}'")
end
@fact_list[:vm] = vm
@fact_list[:hypervisor] = { vm.to_sym => info } if vm
@fact_list[fact_name]
end
def extract_vm_and_info(output_docker, output_lxc)
vm = nil
if output_docker
vm = 'docker'
info = output_docker[1]
elsif output_lxc
vm = 'lxc'
info = output_lxc[1]
end
[info ? { INFO[vm] => info } : {}, vm]
end
end
end
end
end
end