/
facts.rb
executable file
·92 lines (72 loc) · 2.06 KB
/
facts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'time'
require 'puppet/node'
require 'puppet/indirector'
require 'puppet/util/pson'
# Manage a given node's facts. This either accepts facts and stores them, or
# returns facts for a given node.
class Puppet::Node::Facts
# Set up indirection, so that nodes can be looked for in
# the node sources.
extend Puppet::Indirector
extend Puppet::Util::Pson
# We want to expire any cached nodes if the facts are saved.
module NodeExpirer
def save(instance, key = nil, options={})
Puppet::Node.indirection.expire(instance.name, options)
super
end
end
indirects :facts, :terminus_setting => :facts_terminus, :extend => NodeExpirer
attr_accessor :name, :values
def add_local_facts
values["clientcert"] = Puppet.settings[:certname]
values["clientversion"] = Puppet.version.to_s
end
def initialize(name, values = {})
@name = name
@values = values
add_timestamp
end
# Convert all fact values into strings.
def stringify
values.each do |fact, value|
values[fact] = value.to_s
end
end
def ==(other)
return false unless self.name == other.name
strip_internal == other.send(:strip_internal)
end
def self.from_pson(data)
result = new(data['name'], data['values'])
result.timestamp = Time.parse(data['timestamp']) if data['timestamp']
result.expiration = Time.parse(data['expiration']) if data['expiration']
result
end
def to_pson(*args)
result = {
'name' => name,
'values' => strip_internal,
}
result['timestamp'] = timestamp if timestamp
result['expiration'] = expiration if expiration
result.to_pson(*args)
end
# Add internal data to the facts for storage.
def add_timestamp
self.timestamp = Time.now
end
def timestamp=(time)
self.values[:_timestamp] = time
end
def timestamp
self.values[:_timestamp]
end
private
# Strip out that internal data.
def strip_internal
newvals = values.dup
newvals.find_all { |name, value| name.to_s =~ /^_/ }.each { |name, value| newvals.delete(name) }
newvals
end
end