Class: Loom::Facts::FactSet

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/facts/fact_set.rb

Overview

A factset is created by running each registered Loom::Facts::Provider and calling Provider+collect_facts+. See ./fact_file_provider.rb and lib/loomext/facts/facter_provider.rb for examples of fact providers.

Constant Summary collapse

InvalidFactName =
Class.new Loom::LoomError
InvalidFactValue =
Class.new Loom::LoomError
UnmarshalableError =
Class.new Loom::LoomError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host_spec, fact_map) ⇒ FactSet

Returns a new instance of FactSet.



55
56
57
58
59
60
61
62
63
64
# File 'lib/loom/facts/fact_set.rb', line 55

def initialize(host_spec, fact_map)
  raise unless fact_map.is_a? Hash
  validate_facts fact_map

  @fact_map = YAML.load(fact_map.to_yaml).reduce({}) do |memo, tuple|
    memo[tuple.first.to_sym] = tuple.last
    memo
  end
  @host_spec = host_spec
end

Instance Attribute Details

#host_specObject (readonly)

Returns the value of attribute host_spec.



66
67
68
# File 'lib/loom/facts/fact_set.rb', line 66

def host_spec
  @host_spec
end

Class Method Details

.create_for_host(host_spec, shell, loom_config) ⇒ Object



14
15
16
17
18
19
20
21
22
23
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
# File 'lib/loom/facts/fact_set.rb', line 14

def create_for_host(host_spec, shell, loom_config)
  fact_map = {}
  fact_providers = Provider.create_fact_providers(host_spec, shell, loom_config)
  fact_providers.each do |provider|
    next if Provider.disabled_for_host?(host_spec, provider.class)

    namespace = provider.namespace
    Loom.log.debug { "loading facts from provider => #{provider}[#{namespace}]" }

    provider_facts = {}
    begin
      provider.collect_facts.each do |k, v|
        k = k.to_sym
        provider_facts[k] = v
      end

      if namespace
        if fact_map[namespace]
          Loom.log.warn "overriding fact[#{namespace}] with namespace"
        end
        fact_map[namespace] = provider_facts
      else
        provider_facts.each do |k, v|
          if fact_map[k]
            Loom.log.warn "overriding fact[#{k}]"
          end
          Loom.log.debug5(self) { "adding fact => #{k}=#{v.to_s}" }
          fact_map[k] = v
        end
      end
    rescue => e
      Loom.log.error "error executing fact provider #{provider.class} => #{e.message}"
      Loom.log.debug(self) { e.backtrace.join("\n\t") }
      provider.disable(host_spec)
    end
  end

  FactSet.new host_spec, fact_map
end

Instance Method Details

#factsObject



91
92
93
# File 'lib/loom/facts/fact_set.rb', line 91

def facts
  @fact_map.dup
end

#get(fact_name) ⇒ Object Also known as: []



85
86
87
88
# File 'lib/loom/facts/fact_set.rb', line 85

def get(fact_name)
  result = @fact_map[fact_name.to_sym]
  result.dup rescue result
end

#hostnameObject



81
82
83
# File 'lib/loom/facts/fact_set.rb', line 81

def hostname
  host_spec.hostname
end

#merge(facts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/loom/facts/fact_set.rb', line 68

def merge(facts)
  facts = case facts
          when FactSet
            facts.facts
          when Hash
            facts
          else
            raise "unable to merge facts => #{facts.class}:#{facts}"
          end
  merged_facts = @fact_map.merge facts
  FactSet.new @host_spec, merged_facts
end

#to_sObject



95
96
97
# File 'lib/loom/facts/fact_set.rb', line 95

def to_s
  @fact_map.to_a.map { |tuple| tuple.join "=" }.join "\n"
end