Class: Puppet::Node::Facts

Inherits:
Object show all
Extended by:
Indirector, Util::Pson
Defined in:
lib/puppet/node/facts.rb

Overview

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

Defined Under Namespace

Modules: NodeExpirer Classes: ActiveRecord, Couch, Facter, InventoryActiveRecord, InventoryService, Memory, NetworkDevice, Rest, StoreConfigs, Yaml

Constant Summary

Constants included from Indirector

Indirector::BadNameRegexp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Indirector

configure_routes, indirects

Methods included from Util::Pson

pson_create

Constructor Details

#initialize(name, values = {}) ⇒ Facts

Returns a new instance of Facts.



34
35
36
37
38
39
# File 'lib/puppet/node/facts.rb', line 34

def initialize(name, values = {})
  @name = name
  @values = values

  add_timestamp
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/puppet/node/facts.rb', line 26

def name
  @name
end

#valuesObject

Returns the value of attribute values.



26
27
28
# File 'lib/puppet/node/facts.rb', line 26

def values
  @values
end

Class Method Details

.from_pson(data) ⇒ Object



82
83
84
85
86
# File 'lib/puppet/node/facts.rb', line 82

def self.from_pson(data)
  new_facts = allocate
  new_facts.initialize_from_hash(data)
  new_facts
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
80
# File 'lib/puppet/node/facts.rb', line 77

def ==(other)
  return false unless self.name == other.name
  strip_internal == other.send(:strip_internal)
end

#add_local_factsObject



28
29
30
31
32
# File 'lib/puppet/node/facts.rb', line 28

def add_local_facts
  values["clientcert"] = Puppet.settings[:certname]
  values["clientversion"] = Puppet.version.to_s
  values["environment"] ||= Puppet.settings[:environment]
end

#add_timestampObject

Add internal data to the facts for storage.



98
99
100
# File 'lib/puppet/node/facts.rb', line 98

def add_timestamp
  self.timestamp = Time.now
end

#downcase_if_necessaryObject



41
42
43
44
45
46
47
48
# File 'lib/puppet/node/facts.rb', line 41

def downcase_if_necessary
  return unless Puppet.settings[:downcasefacts]

  Puppet.warning "DEPRECATION NOTICE: Fact downcasing is deprecated; please disable (20080122)"
  values.each do |fact, value|
    values[fact] = value.downcase if value.is_a?(String)
  end
end

#initialize_from_hash(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/node/facts.rb', line 50

def initialize_from_hash(data)
  @name = data['name']
  @values = data['values']
  # Timestamp will be here in YAML
  timestamp = data['values']['_timestamp']
  @values.delete_if do |key, val|
    key =~ /^_/
  end

  #Timestamp will be here in pson
  timestamp ||= data['timestamp']
  timestamp = Time.parse(timestamp) if timestamp.is_a? String
  self.timestamp = timestamp

  self.expiration = data['expiration']
  if expiration.is_a? String
    self.expiration = Time.parse(expiration)
  end
end

#stringifyObject

Convert all fact values into strings.



71
72
73
74
75
# File 'lib/puppet/node/facts.rb', line 71

def stringify
  values.each do |fact, value|
    values[fact] = value.to_s
  end
end

#timestampObject



106
107
108
# File 'lib/puppet/node/facts.rb', line 106

def timestamp
  self.values[:_timestamp]
end

#timestamp=(time) ⇒ Object



102
103
104
# File 'lib/puppet/node/facts.rb', line 102

def timestamp=(time)
  self.values[:_timestamp] = time
end

#to_pson(*args) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/puppet/node/facts.rb', line 88

def to_pson(*args)
  {
    'expiration' => expiration,
    'name' => name,
    'timestamp' => timestamp,
    'values' => strip_internal,
  }.to_pson(*args)
end