1 # A Facter plugin that loads facts from /etc/facter/facts.d
2 # and /etc/puppetlabs/facter/facts.d.
4 # Facts can be in the form of JSON, YAML or Text files
5 # and any executable that returns key=value pairs.
7 # In the case of scripts you can also create a file that
8 # contains a cache TTL. For foo.sh store the ttl as just
9 # a number in foo.sh.ttl
11 # The cache is stored in $libdir/facts_dot_d.cache as a mode
12 # 600 file and will have the end result of not calling your
13 # fact scripts more often than is needed
14 class Facter::Util::DotD
17 def initialize(dir = '/etc/facts.d', cache_file = File.join(Puppet[:libdir], 'facts_dot_d.cache'))
19 @cache_file = cache_file
21 @types = { '.txt' => :txt, '.json' => :json, '.yaml' => :yaml }
25 Dir.entries(@dir).reject { |f| f =~ %r{^\.|\.ttl$} }.sort.map { |f| File.join(@dir, f) }
31 extension = File.extname(file)
33 type = @types[extension] || :unknown
35 type = :script if type == :unknown && File.executable?(file)
41 File.readlines(file).each do |line|
42 next unless line =~ %r{^([^=]+)=(.+)$}
43 var = Regexp.last_match(1)
44 val = Regexp.last_match(2)
50 rescue StandardError => e
51 Facter.warn("Failed to handle #{file} as text facts: #{e.class}: #{e}")
58 retry if require 'rubygems'
62 JSON.parse(File.read(file)).each_pair do |f, v|
67 rescue StandardError => e
68 Facter.warn("Failed to handle #{file} as json facts: #{e.class}: #{e}")
74 YAML.load_file(file).each_pair do |f, v|
79 rescue StandardError => e
80 Facter.warn("Failed to handle #{file} as yaml facts: #{e.class}: #{e}")
83 def script_parser(file)
84 result = cache_lookup(file)
85 ttl = cache_time(file)
88 Facter.debug("Using cached data for #{file}")
90 result = Facter::Util::Resolution.exec(file)
93 Facter.debug("Updating cache for #{file}")
94 cache_store(file, result)
99 result.split("\n").each do |line|
100 next unless line =~ %r{^(.+)=(.+)$}
101 var = Regexp.last_match(1)
102 val = Regexp.last_match(2)
108 rescue StandardError => e
109 Facter.warn("Failed to handle #{file} as script facts: #{e.class}: #{e}")
110 Facter.debug(e.backtrace.join("\n\t"))
115 File.open(@cache_file, 'w', 0o600) { |f| f.write(YAML.dump(cache)) }
116 rescue # rubocop:disable Lint/HandleExceptions
119 def cache_store(file, data)
122 @cache[file] = { :data => data, :stored => Time.now.to_i }
123 rescue # rubocop:disable Lint/HandleExceptions
126 def cache_lookup(file)
129 return nil if cache.empty?
131 ttl = cache_time(file)
133 return nil unless cache[file]
136 return cache[file][:data] if ttl == -1
137 return cache[file][:data] if (now - cache[file][:stored]) <= ttl
146 return File.read(meta).chomp.to_i
152 @cache ||= if File.exist?(@cache_file)
153 YAML.load_file(@cache_file)
165 entries.each do |fact|
166 type = fact_type(fact)
167 parser = "#{type}_parser"
169 next unless respond_to?("#{type}_parser")
170 Facter.debug("Parsing #{fact} using #{parser}")
177 mdata = Facter.version.match(%r{(\d+)\.(\d+)\.(\d+)})
179 (major, minor, _patch) = mdata.captures.map { |v| v.to_i }
181 # Facter 1.7 introduced external facts support directly
182 unless major == 1 && minor > 6
183 Facter::Util::DotD.new('/etc/facter/facts.d').create
184 Facter::Util::DotD.new('/etc/puppetlabs/facter/facts.d').create
186 # Windows has a different configuration directory that defaults to a vendor
187 # specific sub directory of the %COMMON_APPDATA% directory.
188 if Dir.const_defined? 'COMMON_APPDATA' # rubocop:disable Metrics/BlockNesting : Any attempt to alter this breaks it
189 windows_facts_dot_d = File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'facter', 'facts.d')
190 Facter::Util::DotD.new(windows_facts_dot_d).create