facter_dot_d.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # A Facter plugin that loads facts from /etc/facter/facts.d
  2. # and /etc/puppetlabs/facter/facts.d.
  3. #
  4. # Facts can be in the form of JSON, YAML or Text files
  5. # and any executable that returns key=value pairs.
  6. #
  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
  10. #
  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
  14. class Facter::Util::DotD
  15. require 'yaml'
  16. def initialize(dir="/etc/facts.d", cache_file="/tmp/facts_cache.yml")
  17. @dir = dir
  18. @cache_file = cache_file
  19. @cache = nil
  20. @types = {".txt" => :txt, ".json" => :json, ".yaml" => :yaml}
  21. end
  22. def entries
  23. Dir.entries(@dir).reject{|f| f =~ /^\.|\.ttl$/}.sort.map {|f| File.join(@dir, f) }
  24. rescue
  25. []
  26. end
  27. def fact_type(file)
  28. extension = File.extname(file)
  29. type = @types[extension] || :unknown
  30. type = :script if type == :unknown && File.executable?(file)
  31. return type
  32. end
  33. def txt_parser(file)
  34. File.readlines(file).each do |line|
  35. if line =~ /^(.+)=(.+)$/
  36. var = $1; val = $2
  37. Facter.add(var) do
  38. setcode { val }
  39. end
  40. end
  41. end
  42. rescue Exception => e
  43. Facter.warn("Failed to handle #{file} as text facts: #{e.class}: #{e}")
  44. end
  45. def json_parser(file)
  46. begin
  47. require 'json'
  48. rescue LoadError
  49. retry if require 'rubygems'
  50. raise
  51. end
  52. JSON.load(File.read(file)).each_pair do |f, v|
  53. Facter.add(f) do
  54. setcode { v }
  55. end
  56. end
  57. rescue Exception => e
  58. Facter.warn("Failed to handle #{file} as json facts: #{e.class}: #{e}")
  59. end
  60. def yaml_parser(file)
  61. require 'yaml'
  62. YAML.load_file(file).each_pair do |f, v|
  63. Facter.add(f) do
  64. setcode { v }
  65. end
  66. end
  67. rescue Exception => e
  68. Facter.warn("Failed to handle #{file} as yaml facts: #{e.class}: #{e}")
  69. end
  70. def script_parser(file)
  71. result = cache_lookup(file)
  72. ttl = cache_time(file)
  73. unless result
  74. result = Facter::Util::Resolution.exec(file)
  75. if ttl > 0
  76. Facter.debug("Updating cache for #{file}")
  77. cache_store(file, result)
  78. cache_save!
  79. end
  80. else
  81. Facter.debug("Using cached data for #{file}")
  82. end
  83. result.split("\n").each do |line|
  84. if line =~ /^(.+)=(.+)$/
  85. var = $1; val = $2
  86. Facter.add(var) do
  87. setcode { val }
  88. end
  89. end
  90. end
  91. rescue Exception => e
  92. Facter.warn("Failed to handle #{file} as script facts: #{e.class}: #{e}")
  93. Facter.debug(e.backtrace.join("\n\t"))
  94. end
  95. def cache_save!
  96. cache = load_cache
  97. File.open(@cache_file, "w", 0600) {|f| f.write(YAML.dump(cache)) }
  98. rescue
  99. end
  100. def cache_store(file, data)
  101. load_cache
  102. @cache[file] = {:data => data, :stored => Time.now.to_i}
  103. rescue
  104. end
  105. def cache_lookup(file)
  106. cache = load_cache
  107. return nil if cache.empty?
  108. ttl = cache_time(file)
  109. if cache[file]
  110. now = Time.now.to_i
  111. return cache[file][:data] if ttl == -1
  112. return cache[file][:data] if (now - cache[file][:stored]) <= ttl
  113. return nil
  114. else
  115. return nil
  116. end
  117. rescue
  118. return nil
  119. end
  120. def cache_time(file)
  121. meta = file + ".ttl"
  122. return File.read(meta).chomp.to_i
  123. rescue
  124. return 0
  125. end
  126. def load_cache
  127. unless @cache
  128. if File.exist?(@cache_file)
  129. @cache = YAML.load_file(@cache_file)
  130. else
  131. @cache = {}
  132. end
  133. end
  134. return @cache
  135. rescue
  136. @cache = {}
  137. return @cache
  138. end
  139. def create
  140. entries.each do |fact|
  141. type = fact_type(fact)
  142. parser = "#{type}_parser"
  143. if respond_to?("#{type}_parser")
  144. Facter.debug("Parsing #{fact} using #{parser}")
  145. send(parser, fact)
  146. end
  147. end
  148. end
  149. end
  150. mdata = Facter.version.match(/(\d+)\.(\d+)\.(\d+)/)
  151. if mdata
  152. (major, minor, patch) = mdata.captures.map { |v| v.to_i }
  153. if major < 2
  154. # Facter 1.7 introduced external facts support directly
  155. unless major == 1 and minor > 6
  156. Facter::Util::DotD.new("/etc/facter/facts.d").create
  157. Facter::Util::DotD.new("/etc/puppetlabs/facter/facts.d").create
  158. # Windows has a different configuration directory that defaults to a vendor
  159. # specific sub directory of the %COMMON_APPDATA% directory.
  160. if Dir.const_defined? 'COMMON_APPDATA' then
  161. windows_facts_dot_d = File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'facter', 'facts.d')
  162. Facter::Util::DotD.new(windows_facts_dot_d).create
  163. end
  164. end
  165. end
  166. end