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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#--
# Copyright 2006 Luke Kanies <luke@madstop.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#--
module Facter
# This is just so the other classes have the constant.
module Util; end
require 'facter/util/fact'
require 'facter/util/collection'
include Comparable
include Enumerable
FACTERVERSION = '1.3.8'
# = Facter
# Functions as a hash of 'facts' you might care about about your
# system, such as mac address, IP address, Video card, etc.
# returns them dynamically
# == Synopsis
#
# Generally, treat <tt>Facter</tt> as a hash:
# == Example
# require 'facter'
# puts Facter['operatingsystem']
#
GREEN = "[0;32m"
RESET = "[0m"
@@debug = 0
# module methods
def self.collection
unless defined?(@collection) and @collection
@collection = Facter::Util::Collection.new
end
@collection
end
# Return the version of the library.
def self.version
return FACTERVERSION
end
# Add some debugging
def self.debug(string)
if string.nil?
return
end
if @@debug != 0
puts GREEN + string + RESET
end
end
# Return a fact object by name. If you use this, you still have to call
# 'value' on it to retrieve the actual value.
def self.[](name)
collection.fact(name)
end
class << self
[:add, :each, :fact, :flush, :list, :to_hash, :value].each do |method|
define_method(method) do |*args|
collection.send(method, *args)
end
end
end
# Add a resolution mechanism for a named fact. This does not distinguish
# between adding a new fact and adding a new way to resolve a fact.
def self.add(name, options = {}, &block)
collection.add(name, options, &block)
end
class << self
# Allow users to call fact names directly on the Facter class,
# either retrieving the value or comparing it to an existing value.
def method_missing(name, *args)
question = false
if name.to_s =~ /\?$/
question = true
name = name.to_s.sub(/\?$/,'')
end
if fact = @collection.fact(name)
if question
value = fact.value.downcase
args.each do |arg|
if arg.to_s.downcase == value
return true
end
end
# If we got this far, there was no match.
return false
else
return fact.value
end
else
# Else, fail like a normal missing method.
raise NoMethodError, "Could not find fact '%s'" % name
end
end
end
# Clear all facts. Mostly used for testing.
def self.clear
Facter.flush
Facter.reset
end
# Set debugging on or off.
def self.debugging(bit)
if bit
case bit
when TrueClass: @@debug = 1
when FalseClass: @@debug = 0
when Fixnum:
if bit > 0
@@debug = 1
else
@@debug = 0
end
when String:
if bit.downcase == 'off'
@@debug = 0
else
@@debug = 1
end
else
@@debug = 0
end
else
@@debug = 0
end
end
# Remove them all.
def self.reset
@collection = nil
end
# Load all of the default facts, and then everything from disk.
def self.loadfacts
Facter.add(:facterversion) do
setcode { FACTERVERSION.to_s }
end
Facter.add(:rubyversion) do
setcode { RUBY_VERSION.to_s }
end
Facter.add(:puppetversion) do
setcode {
begin
require 'puppet'
Puppet::PUPPETVERSION.to_s
rescue LoadError
nil
end
}
end
Facter.add :rubysitedir do
setcode do
version = RUBY_VERSION.to_s.sub(/\.\d+$/, '')
$:.find do |dir|
dir =~ /#{File.join("site_ruby", version)}$/
end
end
end
locals = []
# Now find all our loadable facts
factdirs = [] # All the places to check for facts
# See if we can find any other facts in the regular Ruby lib
# paths
$:.each do |dir|
fdir = File.join(dir, "facter")
if FileTest.exists?(fdir) and FileTest.directory?(fdir)
factdirs.push(fdir)
end
end
# Also check anything in 'FACTERLIB'
if ENV['FACTERLIB']
ENV['FACTERLIB'].split(":").each do |fdir|
factdirs.push(fdir)
end
end
factdirs.each do |fdir|
Dir.glob("#{fdir}/*.rb").each do |file|
# Load here, rather than require, because otherwise
# the facts won't get reloaded if someone calls
# "loadfacts". Really only important in testing, but,
# well, it's important in testing.
begin
load file
rescue => detail
warn "Could not load %s: %s" %
[file, detail]
end
end
end
# Now try to get facts from the environment
ENV.each do |name, value|
if name =~ /^facter_?(\w+)$/i
Facter.add($1) do
setcode { value }
end
end
end
end
Facter.loadfacts
end
|