Skip to content

Commit 59de5f7

Browse files
(FACT-2872_2) Reimplement linux networking resolver
2 parents bda346c + dbf3b23 commit 59de5f7

30 files changed

+269
-44
lines changed

agent/facter-ng.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55

66
Gem::Specification.new do |spec|
77
spec.name = 'facter-ng'
8-
spec.version = '4.0.46'
8+
spec.version = '4.0.47'
99
spec.authors = ['Puppet']
1010
spec.email = ['[email protected]']
1111

facter.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55

66
Gem::Specification.new do |spec|
77
spec.name = 'facter'
8-
spec.version = '4.0.46'
8+
spec.version = '4.0.47'
99
spec.authors = ['Puppet']
1010
spec.email = ['[email protected]']
1111

lib/facter/custom_facts/util/collection.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def custom_facts
115115
internal_loader.load_all unless @loaded
116116
@loaded = true
117117

118-
custom_facts = @facts.select { |_k, v| v.options[:fact_type] == :custom }
119-
@custom_facts = Facter::Utils.deep_copy(custom_facts.keys)
118+
@custom_facts = @facts.select { |_k, v| v.options[:fact_type] == :custom }
120119
end
121120

122121
def load(name)

lib/facter/custom_facts/util/loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def load_env(fact = nil)
143143
# match it.
144144
next if fact && (env_name != fact)
145145

146-
LegacyFacter.add(Regexp.last_match(1), fact_type: :external) do
146+
LegacyFacter.add(Regexp.last_match(1), fact_type: :external, is_env: true) do
147147
has_weight 1_000_000
148148
setcode { value }
149149
end

lib/facter/custom_facts/util/resolution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def evaluate(&block)
110110
#
111111
# @api private
112112
def options(options)
113-
accepted_options = %i[name value timeout weight fact_type file]
113+
accepted_options = %i[name value timeout weight fact_type file is_env]
114114

115115
accepted_options.each do |option_name|
116116
instance_variable_set("@#{option_name}", options.delete(option_name)) if options.key?(option_name)

lib/facter/framework/core/fact_loaders/external_fact_loader.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ def load_custom_facts
2121

2222
custom_facts_to_load = LegacyFacter.collection.custom_facts
2323

24-
custom_facts_to_load&.each do |custom_fact_name|
25-
loaded_fact = LoadedFact.new(custom_fact_name.to_s, nil, :custom)
24+
custom_facts_to_load&.each do |k, v|
25+
loaded_fact = LoadedFact.new(k.to_s, nil, :custom)
26+
loaded_fact.is_env = v.options[:is_env] if v.options[:is_env]
2627
custom_facts << loaded_fact
2728
end
2829

@@ -37,6 +38,7 @@ def load_external_facts
3738
external_facts_to_load&.each do |k, v|
3839
loaded_fact = LoadedFact.new(k.to_s, nil, :external)
3940
loaded_fact.file = v.options[:file]
41+
loaded_fact.is_env = v.options[:is_env]
4042
external_facts << loaded_fact
4143
end
4244

lib/facter/framework/core/fact_loaders/fact_loader.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,27 @@ def load(options)
2424
@internal_facts = load_internal_facts(options)
2525
@external_facts = load_external_facts(options)
2626

27+
filter_env_facts
28+
2729
@facts = @internal_facts + @external_facts
2830
end
2931

3032
private
3133

34+
def filter_env_facts
35+
env_fact_names = @external_facts.select { |fact| fact.is_env == true }.map(&:name)
36+
return unless env_fact_names.any?
37+
38+
@internal_facts.delete_if do |fact|
39+
if env_fact_names.include?(fact.name)
40+
@log.debug("Reading #{fact.name} fact from environment variable")
41+
true
42+
else
43+
false
44+
end
45+
end
46+
end
47+
3248
def load_internal_facts(options)
3349
@log.debug('Loading internal facts')
3450
internal_facts = []

lib/facter/framework/logging/logger.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ def error(msg, colorize = false)
100100
end
101101

102102
def log_exception(exception)
103-
msg = colorize(exception.message, RED) + "\n"
104-
msg += exception.backtrace.join("\n") if Options[:trace]
103+
msg = exception.message
104+
msg += "\n" + exception.backtrace.join("\n") if Options[:trace]
105105

106-
@@logger.error(@class_name + ' - ' + msg)
106+
error(msg, true)
107107
end
108108

109109
private

lib/facter/models/loaded_fact.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
module Facter
44
class LoadedFact
55
attr_reader :name, :klass, :type
6-
attr_accessor :file
6+
attr_accessor :file, :is_env
77

88
def initialize(name, klass, type = nil, file = nil)
99
@name = name
1010
@klass = klass
1111
@type = type.nil? ? :core : type
1212
@file = file
13+
@is_env = false
1314
end
1415
end
1516
end

lib/facter/resolvers/base_resolver.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def self.resolve(fact_name)
2727

2828
cache_nil_for_unresolved_facts(fact_name)
2929
end
30+
rescue NoMethodError => e
31+
log.debug("Could not resolve #{fact_name}, got #{e}")
32+
@fact_list[fact_name] = nil
3033
rescue LoadError, NameError => e
3134
log.debug("resolving fact #{fact_name}, but #{e}")
3235
@fact_list[fact_name] = nil

0 commit comments

Comments
 (0)