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 /tmp/facts_cache.yaml as a mode
12 # 600 file and will have the end result of not calling your
13 # fact scripts more often than is needed
15 class Facter::Util::DotD
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}
26 Dir.entries(@dir).reject { |f| f =~ /^\.|\.ttl$/ }.sort.map { |f| File.join(@dir, f) }
32 extension = File.extname(file)
34 type = @types[extension] || :unknown
36 type = :script if type == :unknown && File.executable?(file)
42 File.readlines(file).each do |line|
43 if line =~ /^([^=]+)=(.+)$/
52 Facter.warn("Failed to handle #{file} as text facts: #{e.class}: #{e}")
59 retry if require 'rubygems'
63 JSON.load(File.read(file)).each_pair do |f, v|
69 Facter.warn("Failed to handle #{file} as json facts: #{e.class}: #{e}")
75 YAML.load_file(file).each_pair do |f, v|
81 Facter.warn("Failed to handle #{file} as yaml facts: #{e.class}: #{e}")
84 def script_parser(file)
85 result = cache_lookup(file)
86 ttl = cache_time(file)
89 result = Facter::Util::Resolution.exec(file)
92 Facter.debug("Updating cache for #{file}")
93 cache_store(file, result)
97 Facter.debug("Using cached data for #{file}")
100 result.split("\n").each do |line|
101 if line =~ /^(.+)=(.+)$/
109 rescue Exception => e
110 Facter.warn("Failed to handle #{file} as script facts: #{e.class}: #{e}")
111 Facter.debug(e.backtrace.join("\n\t"))
116 File.open(@cache_file, "w", 0600) { |f| f.write(YAML.dump(cache)) }
120 def cache_store(file, data)
123 @cache[file] = {:data => data, :stored => Time.now.to_i}
127 def cache_lookup(file)
130 return nil if cache.empty?
132 ttl = cache_time(file)
137 return cache[file][:data] if ttl == -1
138 return cache[file][:data] if (now - cache[file][:stored]) <= ttl
150 return File.read(meta).chomp.to_i
157 if File.exist?(@cache_file)
158 @cache = YAML.load_file(@cache_file)
171 entries.each do |fact|
172 type = fact_type(fact)
173 parser = "#{type}_parser"
175 if respond_to?("#{type}_parser")
176 Facter.debug("Parsing #{fact} using #{parser}")
185 mdata = Facter.version.match(/(\d+)\.(\d+)\.(\d+)/)
187 (major, minor, patch) = mdata.captures.map { |v| v.to_i }
189 # Facter 1.7 introduced external facts support directly
190 unless major == 1 and minor > 6
191 Facter::Util::DotD.new("/etc/facter/facts.d").create
192 Facter::Util::DotD.new("/etc/puppetlabs/facter/facts.d").create
194 # Windows has a different configuration directory that defaults to a vendor
195 # specific sub directory of the %COMMON_APPDATA% directory.
196 if Dir.const_defined? 'COMMON_APPDATA' then
197 windows_facts_dot_d = File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'facter', 'facts.d')
198 Facter::Util::DotD.new(windows_facts_dot_d).create