class MCollective::Facts::Base
A base class for fact providers, to make a new fully functional fact provider inherit from this and simply provide a self.get_facts method that returns a hash like:
{"foo" => "bar", "bar" => "baz"}
Public Class Methods
inherited(klass)
click to toggle source
Registers new fact sources into the plugin manager
# File lib/mcollective/facts/base.rb 18 def self.inherited(klass) 19 PluginManager << {:type => "facts_plugin", :class => klass.to_s} 20 end
new()
click to toggle source
# File lib/mcollective/facts/base.rb 10 def initialize 11 @mutex = Mutex.new 12 @facts = {} 13 @last_good_facts = {} 14 @last_facts_load = 0 15 end
Public Instance Methods
force_reload?()
click to toggle source
Plugins can override this to provide forced fact invalidation
# File lib/mcollective/facts/base.rb 78 def force_reload? 79 false 80 end
get_fact(fact=nil)
click to toggle source
Returns the value of a single fact
# File lib/mcollective/facts/base.rb 23 def get_fact(fact=nil) 24 config = Config.instance 25 26 cache_time = config.fact_cache_time || 300 27 28 @mutex.synchronize do 29 begin 30 if (Time.now.to_i - @last_facts_load > cache_time.to_i ) || force_reload? 31 Log.debug("Resetting facter cache, now: #{Time.now.to_i} last-known-good: #{@last_facts_load}") 32 33 tfacts = load_facts_from_source 34 35 # Force reset to last known good state on empty facts 36 raise "Got empty facts" if tfacts.empty? 37 38 @facts = normalize_facts(tfacts) 39 40 @last_good_facts = @facts.clone 41 @last_facts_load = Time.now.to_i 42 else 43 Log.debug("Using cached facts now: #{Time.now.to_i} last-known-good: #{@last_facts_load}") 44 end 45 rescue Exception => e 46 Log.error("Failed to load facts: #{e.class}: #{e}") 47 48 # Avoid loops where failing fact loads cause huge CPU 49 # loops, this way it only retries once every cache_time 50 # seconds 51 @last_facts_load = Time.now.to_i 52 53 # Revert to last known good state 54 @facts = @last_good_facts.clone 55 end 56 end 57 58 59 # If you do not supply a specific fact all facts will be returned 60 if fact.nil? 61 return @facts 62 else 63 @facts.include?(fact) ? @facts[fact] : nil 64 end 65 end
get_facts()
click to toggle source
Returns all facts
# File lib/mcollective/facts/base.rb 68 def get_facts 69 get_fact(nil) 70 end
has_fact?(fact)
click to toggle source
Returns true if we know about a specific fact, false otherwise
# File lib/mcollective/facts/base.rb 73 def has_fact?(fact) 74 get_fact(nil).include?(fact) 75 end