forked from puppetlabs/facter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Namespace the container resolver into Linux
FreeBSD has containers (jails) but already handled through the 'virutal' resolver, so for now we just consider Linux being able to handle containers as the code aleady depend on Linux-specific resolvers.
- Loading branch information
Showing
20 changed files
with
119 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
spec/facter/resolvers/containers_spec.rb → ...facter/resolvers/linux/containers_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.