Skip to content

Commit

Permalink
Merge pull request #2604 from mhashizume/FACT-3423/main/sys-filesyste…
Browse files Browse the repository at this point in the history
…m-fix

(FACT-3423) sys-filesystem updates
  • Loading branch information
tvpartytonight authored Sep 11, 2023
2 parents 5cf030e + f912088 commit 12b1e6f
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 42 deletions.
8 changes: 7 additions & 1 deletion facter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

# While we require both ffi and sys-filesystem in parts of Facter, we specify
# them as development, not runtime, dependencies. Both gems either directly
# or indirectly contain native extensions. The intent behind excluding these
# gems from runtime dependencies is to allow users to be able to install
# Facter without a compiler.
spec.add_development_dependency 'ffi'
spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 0.81.0'
spec.add_development_dependency 'rubocop-performance', '~> 1.5.2'
spec.add_development_dependency 'rubocop-rspec', '~> 1.38'
spec.add_development_dependency 'simplecov', '~> 0.17.1'
spec.add_development_dependency 'sys-filesystem', '~> 1.4'
spec.add_development_dependency 'webmock', '~> 3.12'
spec.add_development_dependency 'yard', '~> 0.9'

spec.add_runtime_dependency 'hocon', '~> 1.3'
spec.add_runtime_dependency 'sys-filesystem', '~> 1.4'
spec.add_runtime_dependency 'thor', ['>= 1.0.1', '< 2.0']
end
3 changes: 3 additions & 0 deletions lib/facter/custom_facts/util/windows_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Root
def self.root?
require_relative '../../../facter/resolvers/windows/ffi/identity_ffi'
IdentityFFI.privileged?
rescue LoadError => e
log = Facter::Log.new(self)
log.debug("The ffi gem has not been installed: #{e}")
end
end
end
Expand Down
27 changes: 16 additions & 11 deletions lib/facter/resolvers/macosx/mountpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Mountpoints < BaseResolver
include Facter::Util::Resolvers::FilesystemHelper
init_resolver

@log = Facter::Log.new(self)

class << self
private

Expand All @@ -16,18 +18,21 @@ def post_resolve(fact_name, _options)

def read_mounts
mounts = {}
begin
Facter::Util::Resolvers::FilesystemHelper.read_mountpoints.each do |fs|
device = fs.name
filesystem = fs.mount_type
path = fs.mount_point
options = read_options(fs.options)

Facter::Util::Resolvers::FilesystemHelper.read_mountpoints.each do |fs|
device = fs.name
filesystem = fs.mount_type
path = fs.mount_point
options = read_options(fs.options)

mounts[path] = read_stats(path).tap do |hash|
hash[:device] = device
hash[:filesystem] = filesystem
hash[:options] = options if options.any?
mounts[path] = read_stats(path).tap do |hash|
hash[:device] = device
hash[:filesystem] = filesystem
hash[:options] = options if options.any?
end
end
rescue LoadError => e
@log.debug("Could not read mounts: #{e}")
end

@fact_list[:mountpoints] = mounts
Expand All @@ -39,7 +44,7 @@ def read_stats(path)
size_bytes = stats.bytes_total
available_bytes = stats.bytes_available
used_bytes = size_bytes - available_bytes
rescue Sys::Filesystem::Error
rescue Sys::Filesystem::Error, LoadError
size_bytes = used_bytes = available_bytes = 0
end

Expand Down
24 changes: 15 additions & 9 deletions lib/facter/resolvers/mountpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ def compute_device(device)

def read_mounts(fact_name)
mounts = []
Facter::Util::Resolvers::FilesystemHelper.read_mountpoints.each do |file_system|
mount = {}
get_mount_data(file_system, mount)

next if mount[:path] =~ %r{^/(proc|sys)} && mount[:filesystem] != 'tmpfs' || mount[:filesystem] == 'autofs'

get_mount_sizes(mount)
mounts << mount
begin
Facter::Util::Resolvers::FilesystemHelper.read_mountpoints.each do |file_system|
mount = {}
get_mount_data(file_system, mount)

if mount[:path] =~ %r{^/(proc|sys)} && mount[:filesystem] != 'tmpfs' || mount[:filesystem] == 'autofs'
next
end

get_mount_sizes(mount)
mounts << mount
end
rescue LoadError => e
@log.debug("Could not read mounts: #{e}")
end

@fact_list[:mountpoints] = mounts
Expand All @@ -70,7 +76,7 @@ def get_mount_sizes(mount)
begin
stats = Facter::Util::Resolvers::FilesystemHelper.read_mountpoint_stats(mount[:path])
get_bytes_data(mount, stats)
rescue Sys::Filesystem::Error => e
rescue LoadError, Sys::Filesystem::Error => e
@log.debug("Could not get stats for mountpoint #{mount[:path]}, got #{e}")
mount[:size_bytes] = mount[:available_bytes] = mount[:used_bytes] = 0
end
Expand Down
47 changes: 26 additions & 21 deletions lib/facter/resolvers/solaris/mountpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Mountpoints < BaseResolver
include Facter::Util::Resolvers::FilesystemHelper
init_resolver

@log = Facter::Log.new(self)

class << self
private

Expand All @@ -24,31 +26,34 @@ def exclude_auto_home_mounts!
def read_mounts(fact_name) # rubocop:disable Metrics/MethodLength
@mounts = []
@auto_home_paths = []
begin
Facter::Util::Resolvers::FilesystemHelper.read_mountpoints&.each do |fs|
if fs.name == 'auto_home'
@auto_home_paths << fs.mount_point
next
end

Facter::Util::Resolvers::FilesystemHelper.read_mountpoints&.each do |fs|
if fs.name == 'auto_home'
@auto_home_paths << fs.mount_point
next
end
next if fs.mount_type == 'autofs'

next if fs.mount_type == 'autofs'
mounts = {}
device = fs.name
filesystem = fs.mount_type
path = fs.mount_point
options = fs.options.split(',').map(&:strip)

mounts = {}
device = fs.name
filesystem = fs.mount_type
path = fs.mount_point
options = fs.options.split(',').map(&:strip)
mounts = read_stats(path).tap do |hash|
hash[:device] = device
hash[:filesystem] = filesystem
hash[:path] = path
hash[:options] = options if options.any?
end

mounts = read_stats(path).tap do |hash|
hash[:device] = device
hash[:filesystem] = filesystem
hash[:path] = path
hash[:options] = options if options.any?
@mounts << Hash[Facter::Util::Resolvers::FilesystemHelper::MOUNT_KEYS
.zip(Facter::Util::Resolvers::FilesystemHelper::MOUNT_KEYS
.map { |v| mounts[v] })]
end

@mounts << Hash[Facter::Util::Resolvers::FilesystemHelper::MOUNT_KEYS
.zip(Facter::Util::Resolvers::FilesystemHelper::MOUNT_KEYS
.map { |v| mounts[v] })]
rescue LoadError => e
@log.debug("Could not read mounts: #{e}")
end

exclude_auto_home_mounts!
Expand All @@ -64,7 +69,7 @@ def read_stats(path)
available_bytes = stats.bytes_available.abs
used_bytes = stats.bytes_used.abs
total_bytes = used_bytes + available_bytes
rescue Sys::Filesystem::Error
rescue Sys::Filesystem::Error, LoadError
size_bytes = used_bytes = available_bytes = 0
end

Expand Down
3 changes: 3 additions & 0 deletions lib/facter/resolvers/windows/hardware_architecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def read_hardware_information(fact_name)
arch = determine_architecture(hard)
build_facts_list(hardware: hard, architecture: arch)
@fact_list[fact_name]
rescue LoadError => e
log = Facter::Log.new(self)
log.debug("The ffi gem has not been installed: #{e}")
end

def determine_hardware(sys_info)
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/windows/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def find_username
end

{ user: name_ptr.read_wide_string_with_length(size_ptr.read_uint32), privileged: IdentityFFI.privileged? }
rescue LoadError => e
@log.debug("Could not find username: #{e}")
end

def retrieve_facts(fact_name)
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/windows/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def read_os_version_information(fact_name)
build_facts_list(result)

@fact_list[fact_name]
rescue LoadError => e
@log.debug("Could not get OS version information: #{e}")
end

def build_facts_list(result)
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/windows/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def read_performance_information
@long_living_pointer = state_ptr

PerformanceInformation.new(state_ptr)
rescue LoadError => e
@log.debug("Could not resolve memory facts: #{e}")
end

def calculate_memory
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/windows/networking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def read_network_information(fact_name)
Facter::Util::Resolvers::Networking.expand_main_bindings(@fact_list)

@fact_list[fact_name]
rescue LoadError => e
@log.debug("The ffi gem has not been installed: #{e}")
end

def get_adapter_addresses(size_ptr, adapter_addresses, flags)
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/windows/system32.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def retrieve_windows_binaries_path
end

@fact_list[:system32] = construct_path(bool_ptr, windows_path)
rescue LoadError => e
@log.debug("Could not retrieve: #{e}")
end

def construct_path(bool_ptr, windows)
Expand Down
3 changes: 3 additions & 0 deletions lib/facter/resolvers/windows/timezone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def codepage_from_registry
def codepage_from_api
require_relative '../../../facter/resolvers/windows/ffi/winnls_ffi'
WinnlsFFI.GetACP.to_s
rescue LoadError => e
log = Facter::Log.new(self)
log.debug("Could not retrieve codepage: #{e}")
end
end
end
Expand Down

0 comments on commit 12b1e6f

Please sign in to comment.