class Facts

Manage a given node's facts. This either accepts facts and stores them, or returns facts for a given node.

Attributes

name[RW]
timestamp[RW]
values[RW]

Public Class Methods

from_data_hash(data) click to toggle source
   # File lib/puppet/node/facts.rb
85 def self.from_data_hash(data)
86   new_facts = allocate
87   new_facts.initialize_from_hash(data)
88   new_facts
89 end
new(name, values = {}) click to toggle source
   # File lib/puppet/node/facts.rb
36 def initialize(name, values = {})
37   @name = name
38   @values = values
39 
40   add_timestamp
41 end

Public Instance Methods

==(other) click to toggle source
   # File lib/puppet/node/facts.rb
80 def ==(other)
81   return false unless self.name == other.name
82   values == other.values
83 end
add_extra_values(extra_values) click to toggle source

Add extra values, such as facts given to lookup on the command line. The extra values will override existing values. @param extra_values [Hash{String=>Object}] the values to add @api private

   # File lib/puppet/node/facts.rb
67 def add_extra_values(extra_values)
68   @values.merge!(extra_values)
69   nil
70 end
add_local_facts() click to toggle source
   # File lib/puppet/node/facts.rb
30 def add_local_facts
31   values["clientcert"] = Puppet.settings[:certname]
32   values["clientversion"] = Puppet.version.to_s
33   values["clientnoop"] = Puppet.settings[:noop]
34 end
add_timestamp() click to toggle source
    # File lib/puppet/node/facts.rb
116 def add_timestamp
117   @timestamp = Time.now
118 end
initialize_from_hash(data) click to toggle source
   # File lib/puppet/node/facts.rb
43 def initialize_from_hash(data)
44   @name = data['name']
45   @values = data['values']
46   # Timestamp will be here in YAML, e.g. when reading old reports
47   timestamp = @values.delete('_timestamp')
48   # Timestamp will be here in JSON
49   timestamp ||= data['timestamp']
50 
51   if timestamp.is_a? String
52     @timestamp = Time.parse(timestamp)
53   else
54     @timestamp = timestamp
55   end
56 
57   self.expiration = data['expiration']
58   if expiration.is_a? String
59     self.expiration = Time.parse(expiration)
60   end
61 end
sanitize() click to toggle source

Sanitize fact values by converting everything not a string, Boolean numeric, array or hash into strings.

   # File lib/puppet/node/facts.rb
74 def sanitize
75   values.each do |fact, value|
76     values[fact] = sanitize_fact value
77   end
78 end
to_data_hash() click to toggle source
    # File lib/puppet/node/facts.rb
 91 def to_data_hash
 92   result = {
 93     'name' => name,
 94     'values' => values
 95   }
 96 
 97   if @timestamp
 98     if @timestamp.is_a? Time
 99       result['timestamp'] = @timestamp.iso8601(9)
100     else
101       result['timestamp'] = @timestamp
102     end
103   end
104 
105   if expiration
106     if expiration.is_a? Time
107       result['expiration'] = expiration.iso8601(9)
108     else
109       result['expiration'] = expiration
110     end
111   end
112 
113   result
114 end
to_yaml() click to toggle source
    # File lib/puppet/node/facts.rb
120 def to_yaml
121   facts_to_display = Psych.parse_stream(YAML.dump(self))
122   quote_special_strings(facts_to_display)
123 end

Private Instance Methods

quote_special_strings(fact_hash) click to toggle source
    # File lib/puppet/node/facts.rb
127 def quote_special_strings(fact_hash)
128   fact_hash.grep(Psych::Nodes::Scalar).each do |node|
129     next unless node.value =~ /:/
130 
131     node.plain  = false
132     node.quoted = true
133     node.style  = Psych::Nodes::Scalar::DOUBLE_QUOTED
134   end
135 
136   fact_hash.yaml
137 end
sanitize_fact(fact) click to toggle source
    # File lib/puppet/node/facts.rb
139 def sanitize_fact(fact)
140   if fact.is_a? Hash then
141     ret = {}
142     fact.each_pair { |k,v| ret[sanitize_fact k]=sanitize_fact v }
143     ret
144   elsif fact.is_a? Array then
145     fact.collect { |i| sanitize_fact i }
146   elsif fact.is_a? Numeric \
147     or fact.is_a? TrueClass \
148     or fact.is_a? FalseClass \
149     or fact.is_a? String
150     fact
151   else
152     result = fact.to_s
153     # The result may be ascii-8bit encoded without being a binary (low level object.inspect returns ascii-8bit string)
154     if result.encoding == Encoding::ASCII_8BIT
155       begin
156         result = result.encode(Encoding::UTF_8)
157       rescue
158         # return the ascii-8bit - it will be taken as a binary
159         result
160       end
161     end
162     result
163   end
164 end