Facter - Host Fact Detection and Reporting
Copyright 2011 Puppet Labs Inc
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
A util module for facter containing helper methods
Return a fact object by name. If you use this, you still have to call ‘value’ on it to retrieve the actual value.
# File lib/facter.rb, line 91 def self.[](name) collection.fact(name) 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.
# File lib/facter.rb, line 113 def self.add(name, options = {}, &block) collection.add(name, options, &block) end
Clear all facts. Mostly used for testing.
# File lib/facter.rb, line 158 def self.clear Facter.flush Facter.reset end
Clear all messages. Used only in testing. Can’t add to self.clear because we don’t want to warn multiple times for items that are warnonce’d
# File lib/facter.rb, line 165 def self.clear_messages @@messages.clear end
module methods
# File lib/facter.rb, line 54 def self.collection unless defined?(@collection) and @collection @collection = Facter::Util::Collection.new end @collection end
Add some debugging
# File lib/facter.rb, line 67 def self.debug(string) if string.nil? return end if self.debugging? puts GREEN + string + RESET end end
Set debugging on or off.
# File lib/facter.rb, line 170 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
# File lib/facter.rb, line 76 def self.debugging? @@debug != 0 end
# File lib/facter.rb, line 117 def self.each # Make sure all facts are loaded. collection.load_all collection.each do |*args| yield(*args) end end
Load all of the default facts, and then everything from disk.
# File lib/facter.rb, line 233 def self.loadfacts collection.load_all end
Allow users to call fact names directly on the Facter class, either retrieving the value or comparing it to an existing value.
# File lib/facter.rb, line 129 def method_missing(name, *args) question = false if name.to_s =~ %r\?$/ question = true name = name.to_s.sub(%r\?$/,'') 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
Remove them all.
# File lib/facter.rb, line 228 def self.reset @collection = nil end
Register a directory to search through.
# File lib/facter.rb, line 240 def self.search(*dirs) @search_path += dirs end
Return our registered search directories.
# File lib/facter.rb, line 245 def self.search_path @search_path.dup end
show the timing information
# File lib/facter.rb, line 81 def self.show_time(string) puts "#{GREEN}#{string}#{RESET}" if string and Facter.timing? end
Set timing on or off.
# File lib/facter.rb, line 196 def self.timing(bit) if bit case bit when TrueClass; @@timing = 1 when Fixnum if bit > 0 @@timing = 1 else @@timing = 0 end end else @@timing = 0 end end
# File lib/facter.rb, line 85 def self.timing? @@timing != 0 end
Return the version of the library.
# File lib/facter.rb, line 62 def self.version return FACTERVERSION end
# File lib/facter.rb, line 212 def self.warn(msg) if Facter.debugging? and msg and not msg.empty? msg = [msg] unless msg.respond_to? :each msg.each { |line| Kernel.warn line } end end
Warn once.
# File lib/facter.rb, line 220 def self.warnonce(msg) if msg and not msg.empty? and @@messages[msg].nil? @@messages[msg] = true Kernel.warn(msg) end end