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.



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

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.



79
80
81
# File 'lib/loom/facts/fact_set.rb', line 79

def host_spec
  @host_spec
end

Class Method Details

.create_for_host(host_spec, shell, loom_config) ⇒ Object



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/loom/facts/fact_set.rb', line 27

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



120
121
122
# File 'lib/loom/facts/fact_set.rb', line 120

def facts
  @fact_map.dup
end

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



109
110
111
112
113
114
115
116
117
# File 'lib/loom/facts/fact_set.rb', line 109

def get(fact_name)
  v = @fact_map[fact_name.to_sym]
  dup = v.dup rescue v
  if dup.nil?
    Loom::Facts::EMPTY
  else
    dup
  end
end

#hostnameObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/loom/facts/fact_set.rb', line 94

def hostname
  Loom.log.debug(<<NB
`facts.hostname ` is actually the name by which SSH knows the host, not
necessarily the actual host name. e.g. it may be an ip address, /etc/hosts
alias, or ssh config alias. Use `facts.sshname` for the same value without this
warning.
NB
    )
  sshname
end

#merge(facts) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/loom/facts/fact_set.rb', line 81

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

#sshnameObject



105
106
107
# File 'lib/loom/facts/fact_set.rb', line 105

def sshname
  host_spec.hostname
end

#to_sObject



124
125
126
# File 'lib/loom/facts/fact_set.rb', line 124

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