2 # A Facter plugin that loads facts from /etc/facter/facts.d
3 # and /etc/puppetlabs/facter/facts.d.
5 # Facts can be in the form of JSON, YAML or Text files
6 # and any executable that returns key=value pairs.
8 # In the case of scripts you can also create a file that
9 # contains a cache TTL. For foo.sh store the ttl as just
10 # a number in foo.sh.ttl
12 # The cache is stored in $libdir/facts_dot_d.cache as a mode
13 # 600 file and will have the end result of not calling your
14 # fact scripts more often than is needed
15 class Facter::Util::DotD
17 # These will be nil if Puppet is not available.
18 def initialize(dir = '/etc/facts.d', cache_file = File.join(Puppet[:libdir], 'facts_dot_d.cache'))
20 @cache_file = cache_file
22 @types = { '.txt' => :txt, '.json' => :json, '.yaml' => :yaml }
27 Dir.entries(@dir).reject { |f| f =~ %r{^\.|\.ttl$} }.sort.map { |f| File.join(@dir, f) }
35 extension = File.extname(file)
37 type = @types[extension] || :unknown
39 type = :script if type == :unknown && File.executable?(file)
47 File.readlines(file).each do |line|
48 next unless line =~ %r{^([^=]+)=(.+)$}
49 var = Regexp.last_match(1)
50 val = Regexp.last_match(2)
56 rescue StandardError => e
57 Facter.warn("Failed to handle #{file} as text facts: #{e.class}: #{e}")
66 retry if require 'rubygems'
70 JSON.parse(File.read(file)).each_pair do |f, v|
75 rescue StandardError => e
76 Facter.warn("Failed to handle #{file} as json facts: #{e.class}: #{e}")
84 YAML.load_file(file).each_pair do |f, v|
89 rescue StandardError => e
90 Facter.warn("Failed to handle #{file} as yaml facts: #{e.class}: #{e}")
95 def script_parser(file)
96 result = cache_lookup(file)
97 ttl = cache_time(file)
100 Facter.debug("Using cached data for #{file}")
102 result = Facter::Util::Resolution.exec(file)
105 Facter.debug("Updating cache for #{file}")
106 cache_store(file, result)
111 result.split("\n").each do |line|
112 next unless line =~ %r{^(.+)=(.+)$}
113 var = Regexp.last_match(1)
114 val = Regexp.last_match(2)
120 rescue StandardError => e
121 Facter.warn("Failed to handle #{file} as script facts: #{e.class}: #{e}")
122 Facter.debug(e.backtrace.join("\n\t"))
128 File.open(@cache_file, 'w', 0o600) { |f| f.write(YAML.dump(cache)) }
129 rescue # rubocop:disable Lint/HandleExceptions
134 def cache_store(file, data)
137 @cache[file] = { :data => data, :stored => Time.now.to_i }
138 rescue # rubocop:disable Lint/HandleExceptions
143 def cache_lookup(file)
146 return nil if cache.empty?
148 ttl = cache_time(file)
150 return nil unless cache[file]
153 return cache[file][:data] if ttl == -1
154 return cache[file][:data] if (now - cache[file][:stored]) <= ttl
165 return File.read(meta).chomp.to_i
172 @cache ||= if File.exist?(@cache_file)
173 YAML.load_file(@cache_file)
186 entries.each do |fact|
187 type = fact_type(fact)
188 parser = "#{type}_parser"
190 next unless respond_to?("#{type}_parser")
191 Facter.debug("Parsing #{fact} using #{parser}")
198 mdata = Facter.version.match(%r{(\d+)\.(\d+)\.(\d+)})
200 (major, minor, _patch) = mdata.captures.map { |v| v.to_i }
202 # Facter 1.7 introduced external facts support directly
203 unless major == 1 && minor > 6
204 Facter::Util::DotD.new('/etc/facter/facts.d').create
205 Facter::Util::DotD.new('/etc/puppetlabs/facter/facts.d').create
207 # Windows has a different configuration directory that defaults to a vendor
208 # specific sub directory of the %COMMON_APPDATA% directory.
209 if Dir.const_defined? 'COMMON_APPDATA' # rubocop:disable Metrics/BlockNesting : Any attempt to alter this breaks it
210 windows_facts_dot_d = File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'facter', 'facts.d')
211 Facter::Util::DotD.new(windows_facts_dot_d).create