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
84 def self.from_data_hash(data)
85   new_facts = allocate
86   new_facts.initialize_from_hash(data)
87   new_facts
88 end
new(name, values = {}) click to toggle source
   # File lib/puppet/node/facts.rb
35 def initialize(name, values = {})
36   @name = name
37   @values = values
38 
39   add_timestamp
40 end

Public Instance Methods

==(other) click to toggle source
   # File lib/puppet/node/facts.rb
79 def ==(other)
80   return false unless self.name == other.name
81   values == other.values
82 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
66 def add_extra_values(extra_values)
67   @values.merge!(extra_values)
68   nil
69 end
add_local_facts() click to toggle source
   # File lib/puppet/node/facts.rb
29 def add_local_facts
30   values["clientcert"] = Puppet.settings[:certname]
31   values["clientversion"] = Puppet.version.to_s
32   values["clientnoop"] = Puppet.settings[:noop]
33 end
add_timestamp() click to toggle source
    # File lib/puppet/node/facts.rb
115 def add_timestamp
116   @timestamp = Time.now
117 end
initialize_from_hash(data) click to toggle source
   # File lib/puppet/node/facts.rb
42 def initialize_from_hash(data)
43   @name = data['name']
44   @values = data['values']
45   # Timestamp will be here in YAML, e.g. when reading old reports
46   timestamp = @values.delete('_timestamp')
47   # Timestamp will be here in JSON
48   timestamp ||= data['timestamp']
49 
50   if timestamp.is_a? String
51     @timestamp = Time.parse(timestamp)
52   else
53     @timestamp = timestamp
54   end
55 
56   self.expiration = data['expiration']
57   if expiration.is_a? String
58     self.expiration = Time.parse(expiration)
59   end
60 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
73 def sanitize
74   values.each do |fact, value|
75     values[fact] = sanitize_fact value
76   end
77 end
to_data_hash() click to toggle source
    # File lib/puppet/node/facts.rb
 90 def to_data_hash
 91   result = {
 92     'name' => name,
 93     'values' => values
 94   }
 95 
 96   if @timestamp
 97     if @timestamp.is_a? Time
 98       result['timestamp'] = @timestamp.iso8601(9)
 99     else
100       result['timestamp'] = @timestamp
101     end
102   end
103 
104   if expiration
105     if expiration.is_a? Time
106       result['expiration'] = expiration.iso8601(9)
107     else
108       result['expiration'] = expiration
109     end
110   end
111 
112   result
113 end
to_yaml() click to toggle source
    # File lib/puppet/node/facts.rb
119 def to_yaml
120   facts_to_display = Psych.parse_stream(YAML.dump(self))
121   quote_special_strings(facts_to_display)
122 end

Private Instance Methods

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