Class: MCollective::Facts::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/facts/base.rb

Overview

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"}

Direct Known Subclasses

Yaml_facts

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



10
11
12
13
14
15
# File 'lib/mcollective/facts/base.rb', line 10

def initialize
  @mutex = Mutex.new
  @facts = {}
  @last_good_facts = {}
  @last_facts_load = 0
end

Class Method Details

.inherited(klass) ⇒ Object

Registers new fact sources into the plugin manager



18
19
20
21
# File 'lib/mcollective/facts/base.rb', line 18

def self.inherited(klass)
  PluginManager << {:type => "facts_plugin", :class => klass.to_s}
  super
end

Instance Method Details

#force_reload?Boolean

Plugins can override this to provide forced fact invalidation

Returns:

  • (Boolean)


78
79
80
# File 'lib/mcollective/facts/base.rb', line 78

def force_reload?
  false
end

#get_fact(fact = nil) ⇒ Object

Returns the value of a single fact



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mcollective/facts/base.rb', line 24

def get_fact(fact=nil)
  config = Config.instance

  cache_time = config.fact_cache_time || 300

  @mutex.synchronize do
    begin
      if (Time.now.to_i - @last_facts_load > cache_time.to_i) || force_reload?
        Log.debug("Resetting facter cache, now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")

        tfacts = load_facts_from_source

        # Force reset to last known good state on empty facts
        raise "Got empty facts" if tfacts.empty?

        @facts = normalize_facts(tfacts)

        @last_good_facts = @facts.clone
        @last_facts_load = Time.now.to_i
      else
        Log.debug("Using cached facts now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      Log.error("Failed to load facts: #{e.class}: #{e}")

      # Avoid loops where failing fact loads cause huge CPU
      # loops, this way it only retries once every cache_time
      # seconds
      @last_facts_load = Time.now.to_i

      # Revert to last known good state
      @facts = @last_good_facts.clone
    end
  end

  # If you do not supply a specific fact all facts will be returned
  if fact.nil?
    @facts
  else
    @facts.include?(fact) ? @facts[fact] : nil
  end
end

#get_factsObject

Returns all facts



68
69
70
# File 'lib/mcollective/facts/base.rb', line 68

def get_facts # rubocop:disable Naming/AccessorMethodName
  get_fact(nil)
end

#has_fact?(fact) ⇒ Boolean

Returns true if we know about a specific fact, false otherwise

Returns:

  • (Boolean)


73
74
75
# File 'lib/mcollective/facts/base.rb', line 73

def has_fact?(fact)
  get_fact(nil).include?(fact)
end