Class: Puppet::Node::Facts

Inherits:
Object show all
Extended by:
Indirector
Includes:
Util::PsychSupport
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: Facter, Memory, NetworkDevice, 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::PsychSupport

#encode_with, #init_with

Constructor Details

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

Returns a new instance of Facts.



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

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

  add_timestamp
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Class Method Details

.from_data_hash(data) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  return false unless self.name == other.name
  values == other.values
end

#add_local_factsObject



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

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

#add_timestampObject



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

def add_timestamp
  @timestamp = Time.now
end

#initialize_from_hash(data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet/node/facts.rb', line 42

def initialize_from_hash(data)
  @name = data['name']
  @values = data['values']
  # Timestamp will be here in YAML, e.g. when reading old reports
  timestamp = @values.delete('_timestamp')
  # Timestamp will be here in JSON
  timestamp ||= data['timestamp']

  if timestamp.is_a? String
    @timestamp = Time.parse(timestamp)
  else
    @timestamp = timestamp
  end

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

#sanitizeObject

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



64
65
66
67
68
# File 'lib/puppet/node/facts.rb', line 64

def sanitize
  values.each do |fact, value|
    values[fact] = sanitize_fact value
  end
end

#to_data_hashObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet/node/facts.rb', line 81

def to_data_hash
  result = {
    'name' => name,
    'values' => values
  }

  if @timestamp
    if @timestamp.is_a? Time
      result['timestamp'] = @timestamp.iso8601(9)
    else
      result['timestamp'] = @timestamp
    end
  end

  if expiration
    if expiration.is_a? Time
      result['expiration'] = expiration.iso8601(9)
    else
      result['expiration'] = expiration
    end
  end

  result
end