Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / facter / facter_dot_d.rb
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 $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
15   require 'yaml'
16
17   def initialize(dir = '/etc/facts.d', cache_file = File.join(Puppet[:libdir], 'facts_dot_d.cache'))
18     @dir = dir
19     @cache_file = cache_file
20     @cache = nil
21     @types = { '.txt' => :txt, '.json' => :json, '.yaml' => :yaml }
22   end
23
24   def entries
25     Dir.entries(@dir).reject { |f| f =~ %r{^\.|\.ttl$} }.sort.map { |f| File.join(@dir, f) }
26   rescue
27     []
28   end
29
30   def fact_type(file)
31     extension = File.extname(file)
32
33     type = @types[extension] || :unknown
34
35     type = :script if type == :unknown && File.executable?(file)
36
37     type
38   end
39
40   def txt_parser(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)
45
46       Facter.add(var) do
47         setcode { val }
48       end
49     end
50   rescue StandardError => e
51     Facter.warn("Failed to handle #{file} as text facts: #{e.class}: #{e}")
52   end
53
54   def json_parser(file)
55     begin
56       require 'json'
57     rescue LoadError
58       retry if require 'rubygems'
59       raise
60     end
61
62     JSON.parse(File.read(file)).each_pair do |f, v|
63       Facter.add(f) do
64         setcode { v }
65       end
66     end
67   rescue StandardError => e
68     Facter.warn("Failed to handle #{file} as json facts: #{e.class}: #{e}")
69   end
70
71   def yaml_parser(file)
72     require 'yaml'
73
74     YAML.load_file(file).each_pair do |f, v|
75       Facter.add(f) do
76         setcode { v }
77       end
78     end
79   rescue StandardError => e
80     Facter.warn("Failed to handle #{file} as yaml facts: #{e.class}: #{e}")
81   end
82
83   def script_parser(file)
84     result = cache_lookup(file)
85     ttl = cache_time(file)
86
87     if result
88       Facter.debug("Using cached data for #{file}")
89     else
90       result = Facter::Util::Resolution.exec(file)
91
92       if ttl > 0
93         Facter.debug("Updating cache for #{file}")
94         cache_store(file, result)
95         cache_save!
96       end
97     end
98
99     result.split("\n").each do |line|
100       next unless line =~ %r{^(.+)=(.+)$}
101       var = Regexp.last_match(1)
102       val = Regexp.last_match(2)
103
104       Facter.add(var) do
105         setcode { val }
106       end
107     end
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"))
111   end
112
113   def cache_save!
114     cache = load_cache
115     File.open(@cache_file, 'w', 0o600) { |f| f.write(YAML.dump(cache)) }
116   rescue # rubocop:disable Lint/HandleExceptions
117   end
118
119   def cache_store(file, data)
120     load_cache
121
122     @cache[file] = { :data => data, :stored => Time.now.to_i }
123   rescue # rubocop:disable Lint/HandleExceptions
124   end
125
126   def cache_lookup(file)
127     cache = load_cache
128
129     return nil if cache.empty?
130
131     ttl = cache_time(file)
132
133     return nil unless cache[file]
134     now = Time.now.to_i
135
136     return cache[file][:data] if ttl == -1
137     return cache[file][:data] if (now - cache[file][:stored]) <= ttl
138     return nil
139   rescue
140     return nil
141   end
142
143   def cache_time(file)
144     meta = file + '.ttl'
145
146     return File.read(meta).chomp.to_i
147   rescue
148     return 0
149   end
150
151   def load_cache
152     @cache ||= if File.exist?(@cache_file)
153                  YAML.load_file(@cache_file)
154                else
155                  {}
156                end
157
158     return @cache
159   rescue
160     @cache = {}
161     return @cache
162   end
163
164   def create
165     entries.each do |fact|
166       type = fact_type(fact)
167       parser = "#{type}_parser"
168
169       next unless respond_to?("#{type}_parser")
170       Facter.debug("Parsing #{fact} using #{parser}")
171
172       send(parser, fact)
173     end
174   end
175 end
176
177 mdata = Facter.version.match(%r{(\d+)\.(\d+)\.(\d+)})
178 if mdata
179   (major, minor, _patch) = mdata.captures.map { |v| v.to_i }
180   if major < 2
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
185
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
191       end
192     end
193   end
194 end