-# A Facter plugin that loads facts from /etc/facter/facts.d
-# and /etc/puppetlabs/facter/facts.d.
+# @summary
+# A Facter plugin that loads facts from /etc/facter/facts.d
+# and /etc/puppetlabs/facter/facts.d.
#
# Facts can be in the form of JSON, YAML or Text files
# and any executable that returns key=value pairs.
# fact scripts more often than is needed
class Facter::Util::DotD
require 'yaml'
-
+ # These will be nil if Puppet is not available.
def initialize(dir = '/etc/facts.d', cache_file = File.join(Puppet[:libdir], 'facts_dot_d.cache'))
@dir = dir
@cache_file = cache_file
@types = { '.txt' => :txt, '.json' => :json, '.yaml' => :yaml }
end
+ # entries
def entries
Dir.entries(@dir).reject { |f| f =~ %r{^\.|\.ttl$} }.sort.map { |f| File.join(@dir, f) }
rescue
[]
end
+ # fact_type
+ # @param file
def fact_type(file)
extension = File.extname(file)
type
end
+ # txt_parser
+ # @param file
def txt_parser(file)
File.readlines(file).each do |line|
next unless line =~ %r{^([^=]+)=(.+)$}
Facter.warn("Failed to handle #{file} as text facts: #{e.class}: #{e}")
end
+ # json_parser
+ # @param file
def json_parser(file)
begin
require 'json'
Facter.warn("Failed to handle #{file} as json facts: #{e.class}: #{e}")
end
+ # yaml_parser
+ # @param file
def yaml_parser(file)
require 'yaml'
Facter.warn("Failed to handle #{file} as yaml facts: #{e.class}: #{e}")
end
+ # script_parser
+ # @param file
def script_parser(file)
result = cache_lookup(file)
ttl = cache_time(file)
Facter.debug(e.backtrace.join("\n\t"))
end
+ # cache_save
def cache_save!
cache = load_cache
File.open(@cache_file, 'w', 0o600) { |f| f.write(YAML.dump(cache)) }
rescue # rubocop:disable Lint/HandleExceptions
end
+ # cache_store
+ # @param file
def cache_store(file, data)
load_cache
rescue # rubocop:disable Lint/HandleExceptions
end
+ # cache_lookup
+ # @param file
def cache_lookup(file)
cache = load_cache
return nil
end
+ # cache_time
+ # @param file
def cache_time(file)
meta = file + '.ttl'
return 0
end
+ # load_cache
def load_cache
@cache ||= if File.exist?(@cache_file)
YAML.load_file(@cache_file)
return @cache
end
+ # create
def create
entries.each do |fact|
type = fact_type(fact)