57d33d495330feade840571a1d9a5e9df87110d6
[packages/precise/mcollective.git] / plugins / mcollective / facts / yaml_facts.rb
1 module MCollective
2   module Facts
3     require 'yaml'
4
5     # A factsource that reads a hash of facts from a YAML file
6     #
7     # Multiple files can be specified seperated with a : in the
8     # config file, they will be merged with later files overriding
9     # earlier ones in the list.
10     class Yaml_facts<Base
11       def initialize
12         @yaml_file_mtimes = {}
13
14         super
15       end
16
17       def load_facts_from_source
18         config = Config.instance
19
20         fact_files = config.pluginconf["yaml"].split(File::PATH_SEPARATOR)
21         facts = {}
22
23         fact_files.each do |file|
24           begin
25             if File.exist?(file)
26               facts.merge!(YAML.load_file(file))
27             else
28               raise("Can't find YAML file to load: #{file}")
29             end
30           rescue Exception => e
31             Log.error("Failed to load yaml facts from #{file}: #{e.class}: #{e}")
32           end
33         end
34
35         facts
36       end
37
38       # force fact reloads when the mtime on the yaml file change
39       def force_reload?
40         config = Config.instance
41
42         fact_files = config.pluginconf["yaml"].split(File::PATH_SEPARATOR)
43
44         fact_files.each do |file|
45           @yaml_file_mtimes[file] ||= File.stat(file).mtime
46           mtime = File.stat(file).mtime
47
48           if mtime > @yaml_file_mtimes[file]
49             @yaml_file_mtimes[file] = mtime
50
51             Log.debug("Forcing fact reload due to age of #{file}")
52
53             return true
54           end
55         end
56
57         false
58       end
59     end
60   end
61 end