1
- # A Facter plugin that loads facts from /etc/facter/facts.d
2
- # and /etc/puppetlabs/facter/facts.d.
1
+ # @summary
2
+ # A Facter plugin that loads facts from /etc/facter/facts.d
3
+ # and /etc/puppetlabs/facter/facts.d.
3
4
#
4
5
# Facts can be in the form of JSON, YAML or Text files
5
6
# and any executable that returns key=value pairs.
13
14
# fact scripts more often than is needed
14
15
class Facter ::Util ::DotD
15
16
require 'yaml'
16
-
17
+ # These will be nil if Puppet is not available.
17
18
def initialize ( dir = '/etc/facts.d' , cache_file = File . join ( Puppet [ :libdir ] , 'facts_dot_d.cache' ) )
18
19
@dir = dir
19
20
@cache_file = cache_file
20
21
@cache = nil
21
22
@types = { '.txt' => :txt , '.json' => :json , '.yaml' => :yaml }
22
23
end
23
24
25
+ # entries
24
26
def entries
25
27
Dir . entries ( @dir ) . reject { |f | f =~ %r{^\. |\. ttl$} } . sort . map { |f | File . join ( @dir , f ) }
26
28
rescue
27
29
[ ]
28
30
end
29
31
32
+ # fact_type
33
+ # @param file
30
34
def fact_type ( file )
31
35
extension = File . extname ( file )
32
36
@@ -37,6 +41,8 @@ def fact_type(file)
37
41
type
38
42
end
39
43
44
+ # txt_parser
45
+ # @param file
40
46
def txt_parser ( file )
41
47
File . readlines ( file ) . each do |line |
42
48
next unless line =~ %r{^([^=]+)=(.+)$}
@@ -51,6 +57,8 @@ def txt_parser(file)
51
57
Facter . warn ( "Failed to handle #{ file } as text facts: #{ e . class } : #{ e } " )
52
58
end
53
59
60
+ # json_parser
61
+ # @param file
54
62
def json_parser ( file )
55
63
begin
56
64
require 'json'
@@ -68,6 +76,8 @@ def json_parser(file)
68
76
Facter . warn ( "Failed to handle #{ file } as json facts: #{ e . class } : #{ e } " )
69
77
end
70
78
79
+ # yaml_parser
80
+ # @param file
71
81
def yaml_parser ( file )
72
82
require 'yaml'
73
83
@@ -80,6 +90,8 @@ def yaml_parser(file)
80
90
Facter . warn ( "Failed to handle #{ file } as yaml facts: #{ e . class } : #{ e } " )
81
91
end
82
92
93
+ # script_parser
94
+ # @param file
83
95
def script_parser ( file )
84
96
result = cache_lookup ( file )
85
97
ttl = cache_time ( file )
@@ -110,19 +122,24 @@ def script_parser(file)
110
122
Facter . debug ( e . backtrace . join ( "\n \t " ) )
111
123
end
112
124
125
+ # cache_save
113
126
def cache_save!
114
127
cache = load_cache
115
128
File . open ( @cache_file , 'w' , 0o600 ) { |f | f . write ( YAML . dump ( cache ) ) }
116
129
rescue # rubocop:disable Lint/HandleExceptions
117
130
end
118
131
132
+ # cache_store
133
+ # @param file
119
134
def cache_store ( file , data )
120
135
load_cache
121
136
122
137
@cache [ file ] = { :data => data , :stored => Time . now . to_i }
123
138
rescue # rubocop:disable Lint/HandleExceptions
124
139
end
125
140
141
+ # cache_lookup
142
+ # @param file
126
143
def cache_lookup ( file )
127
144
cache = load_cache
128
145
@@ -140,6 +157,8 @@ def cache_lookup(file)
140
157
return nil
141
158
end
142
159
160
+ # cache_time
161
+ # @param file
143
162
def cache_time ( file )
144
163
meta = file + '.ttl'
145
164
@@ -148,6 +167,7 @@ def cache_time(file)
148
167
return 0
149
168
end
150
169
170
+ # load_cache
151
171
def load_cache
152
172
@cache ||= if File . exist? ( @cache_file )
153
173
YAML . load_file ( @cache_file )
@@ -161,6 +181,7 @@ def load_cache
161
181
return @cache
162
182
end
163
183
184
+ # create
164
185
def create
165
186
entries . each do |fact |
166
187
type = fact_type ( fact )
0 commit comments