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.



62
63
64
65
66
67
68
69
70
71
# File 'lib/loom/facts/fact_set.rb', line 62

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.



73
74
75
# File 'lib/loom/facts/fact_set.rb', line 73

def host_spec
  @host_spec
end

Class Method Details

.create_for_host(host_spec, shell, loom_config) ⇒ Object



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
53
54
55
56
57
58
59
# File 'lib/loom/facts/fact_set.rb', line 21

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



114
115
116
# File 'lib/loom/facts/fact_set.rb', line 114

def facts
  @fact_map.dup
end

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



103
104
105
106
107
108
109
110
111
# File 'lib/loom/facts/fact_set.rb', line 103

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

#hostnameObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/loom/facts/fact_set.rb', line 88

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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/loom/facts/fact_set.rb', line 75

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



99
100
101
# File 'lib/loom/facts/fact_set.rb', line 99

def sshname
  host_spec.hostname
end

#to_sObject



118
119
120
# File 'lib/loom/facts/fact_set.rb', line 118

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