Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure platform-specific code is correctly namespaced #2743

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/facter/facts/aix/filesystems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Filesystems
FACT_NAME = 'filesystems'

def call_the_resolver
fact_value = Facter::Resolvers::Aix::Filesystem.resolve(:file_systems)
fact_value = Facter::Resolvers::Aix::Filesystems.resolve(:file_systems)
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/hypervisors/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call_the_resolver
end

def check_docker
info = Facter::Resolvers::Containers.resolve(:hypervisor)
info = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)
info[:docker] if info
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/hypervisors/lxc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call_the_resolver
end

def check_lxc
info = Facter::Resolvers::Containers.resolve(:hypervisor)
info = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)
info[:lxc] if info
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/hypervisors/systemd_nspawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call_the_resolver
end

def check_nspawn
info = Facter::Resolvers::Containers.resolve(:hypervisor)
info = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)
info[:systemd_nspawn] if info
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/system_uptime/days.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Days
ALIASES = 'uptime_days'

def call_the_resolver
hypervisors = Facter::Resolvers::Containers.resolve(:hypervisor)
hypervisors = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)

fact_value = if hypervisors && hypervisors[:docker]
Facter::Resolvers::Linux::DockerUptime.resolve(:days)
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/system_uptime/hours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Hours
ALIASES = 'uptime_hours'

def call_the_resolver
hypervisors = Facter::Resolvers::Containers.resolve(:hypervisor)
hypervisors = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)

fact_value = if hypervisors && hypervisors[:docker]
Facter::Resolvers::Linux::DockerUptime.resolve(:hours)
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/system_uptime/seconds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Seconds
ALIASES = 'uptime_seconds'

def call_the_resolver
hypervisors = Facter::Resolvers::Containers.resolve(:hypervisor)
hypervisors = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)

fact_value = if hypervisors && hypervisors[:docker]
Facter::Resolvers::Linux::DockerUptime.resolve(:seconds)
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/facts/linux/system_uptime/uptime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Uptime
ALIASES = 'uptime'

def call_the_resolver
hypervisors = Facter::Resolvers::Containers.resolve(:hypervisor)
hypervisors = Facter::Resolvers::Linux::Containers.resolve(:hypervisor)

fact_value = if hypervisors && hypervisors[:docker]
Facter::Resolvers::Linux::DockerUptime.resolve(:uptime)
Expand Down
18 changes: 9 additions & 9 deletions lib/facter/framework/core/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,24 @@
require_relative '../../resolvers/augeas'
require_relative '../../resolvers/az'
require_relative '../../resolvers/base_resolver'
require_relative '../../resolvers/containers'
require_relative '../../resolvers/debian_version'
require_relative '../../resolvers/disks'
require_relative '../../resolvers/dmi'
require_relative '../../resolvers/dmi_decode'
require_relative '../../resolvers/ec2'
require_relative '../../resolvers/eos_release'
require_relative '../../resolvers/facterversion'
require_relative '../../resolvers/filesystems'
require_relative '../../resolvers/fips_enabled'
require_relative '../../resolvers/gce'
require_relative '../../resolvers/hostname'
require_relative '../../resolvers/identity'
require_relative '../../resolvers/load_averages'
require_relative '../../resolvers/lpar'
require_relative '../../resolvers/lsb_release'
require_relative '../../resolvers/lspci'
require_relative '../../resolvers/memory'
require_relative '../../resolvers/mountpoints'
require_relative '../../resolvers/networking'
require_relative '../../resolvers/open_vz'
require_relative '../../resolvers/os_release'
require_relative '../../resolvers/partitions'
require_relative '../../resolvers/path'
require_relative '../../resolvers/processors'
require_relative '../../resolvers/processors_lscpu'
require_relative '../../resolvers/redhat_release'
require_relative '../../resolvers/release_from_first_line'
require_relative '../../resolvers/ruby'
Expand Down Expand Up @@ -219,7 +211,7 @@

require_relative '../../resolvers/aix/architecture'
require_relative '../../resolvers/aix/disks'
require_relative '../../resolvers/aix/filesystem'
require_relative '../../resolvers/aix/filesystems'
require_relative '../../resolvers/aix/hardware'
require_relative '../../resolvers/aix/load_averages'
require_relative '../../resolvers/aix/memory'
Expand Down Expand Up @@ -510,10 +502,18 @@
require_relative '../../facts/linux/virtual'
require_relative '../../facts/linux/xen'

require_relative '../../resolvers/linux/containers'
require_relative '../../resolvers/linux/disks'
require_relative '../../resolvers/linux/dmi'
require_relative '../../resolvers/linux/docker_uptime'
require_relative '../../resolvers/linux/filesystems'
require_relative '../../resolvers/linux/fips_enabled'
require_relative '../../resolvers/linux/hostname'
require_relative '../../resolvers/linux/load_averages'
require_relative '../../resolvers/linux/memory'
require_relative '../../resolvers/linux/networking'
require_relative '../../resolvers/linux/processors'
require_relative '../../resolvers/linux/processors_lscpu'

when 'linuxmint'
require_relative '../../facts/linuxmint/os/name'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Facter
module Resolvers
module Aix
class Filesystem < BaseResolver
class Filesystems < BaseResolver
init_resolver

class << self
Expand Down
79 changes: 0 additions & 79 deletions lib/facter/resolvers/containers.rb

This file was deleted.

81 changes: 81 additions & 0 deletions lib/facter/resolvers/linux/containers.rb
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/facter/util/facts/posix/virtual_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def platform
private

def check_docker_lxc
Facter::Resolvers::Containers.resolve(:vm)
Facter::Resolvers::Linux::Containers.resolve(:vm)
end

def check_gce
Expand Down
2 changes: 1 addition & 1 deletion spec/facter/facts/aix/filesystems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:files) { 'apfs,autofs,devfs' }

before do
allow(Facter::Resolvers::Aix::Filesystem).to \
allow(Facter::Resolvers::Aix::Filesystems).to \
receive(:resolve).with(:file_systems).and_return(files)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/facter/facts/linux/hypervisors/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject(:fact) { Facts::Linux::Hypervisors::Docker.new }

before do
allow(Facter::Resolvers::Containers).to \
allow(Facter::Resolvers::Linux::Containers).to \
receive(:resolve).with(:hypervisor).and_return(hv)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/facter/facts/linux/hypervisors/lxc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject(:fact) { Facts::Linux::Hypervisors::Lxc.new }

before do
allow(Facter::Resolvers::Containers).to \
allow(Facter::Resolvers::Linux::Containers).to \
receive(:resolve).with(:hypervisor).and_return(hv)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject(:fact) { Facts::Linux::Hypervisors::SystemdNspawn.new }

before do
allow(Facter::Resolvers::Containers).to \
allow(Facter::Resolvers::Linux::Containers).to \
receive(:resolve).with(:hypervisor).and_return(hv)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/facter/facts/linux/system_uptime/days_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

context 'when on linux' do
before do
allow(Facter::Resolvers::Containers).to receive(:resolve).with(:hypervisor).and_return(nil)
allow(Facter::Resolvers::Linux::Containers).to receive(:resolve).with(:hypervisor).and_return(nil)
allow(Facter::Resolvers::Uptime).to receive(:resolve).with(:days).and_return(value)
end

Expand All @@ -21,7 +21,7 @@

context 'when in docker container' do
before do
allow(Facter::Resolvers::Containers).to receive(:resolve).with(:hypervisor).and_return({ docker: '123' })
allow(Facter::Resolvers::Linux::Containers).to receive(:resolve).with(:hypervisor).and_return({ docker: '123' })
allow(Facter::Resolvers::Linux::DockerUptime).to receive(:resolve).with(:days).and_return(value)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/facter/facts/linux/system_uptime/hours_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

context 'when on linux' do
before do
allow(Facter::Resolvers::Containers).to receive(:resolve).with(:hypervisor).and_return(nil)
allow(Facter::Resolvers::Linux::Containers).to receive(:resolve).with(:hypervisor).and_return(nil)
allow(Facter::Resolvers::Uptime).to receive(:resolve).with(:hours).and_return(value)
end

Expand All @@ -21,7 +21,7 @@

context 'when in docker container' do
before do
allow(Facter::Resolvers::Containers).to receive(:resolve).with(:hypervisor).and_return({ docker: '123' })
allow(Facter::Resolvers::Linux::Containers).to receive(:resolve).with(:hypervisor).and_return({ docker: '123' })
allow(Facter::Resolvers::Linux::DockerUptime).to receive(:resolve).with(:hours).and_return(value)
end

Expand Down
Loading