A util module for facter containing helper methods
This makes it possible to disable loading of external facts
Generally, treat Facter as a hash:
require ‘facter’ puts Facter
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 105 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 127 def self.add(name, options = {}, &block) collection.add(name, options, &block) end
Clear all facts. Mostly used for testing.
# File lib/facter.rb, line 172 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 179 def self.clear_messages @@messages.clear end
module methods
# File lib/facter.rb, line 52 def self.collection unless defined?(@collection) and @collection @collection = Facter::Util::Collection.new( Facter::Util::Loader.new, Facter::Util::Config.ext_fact_loader) end @collection end
Add some debugging
# File lib/facter.rb, line 62 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 184 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 79 def self.debugging? @@debug != 0 end
Debug once.
# File lib/facter.rb, line 72 def self.debugonce(msg) if msg and not msg.empty? and @@debug_messages[msg].nil? @@debug_messages[msg] = true debug(msg) end end
# File lib/facter.rb, line 131 def self.each # Make sure all facts are loaded. collection.load_all collection.each do |*args| yield(*args) end end
Facter.json? is meant to provide a lightweight way to check if the JSON "feature" is available.
# File lib/facter.rb, line 94 def self.json? begin require 'json' true rescue LoadError false end end
Load all of the default facts, and then everything from disk.
# File lib/facter.rb, line 247 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 143 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
Remove them all.
# File lib/facter.rb, line 242 def self.reset @collection = nil end
Register a directory to search through.
# File lib/facter.rb, line 254 def self.search(*dirs) @search_path += dirs end
Return our registered search directories.
# File lib/facter.rb, line 259 def self.search_path @search_path.dup end
show the timing information
# File lib/facter.rb, line 84 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 210 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
version is a public API method intended to always provide a fast and lightweight way to determine the version of Facter.
The intent is that software external to Facter be able to determine the Facter version with no side-effects. The expected use is:
require 'facter/version' version = Facter.version
This function has the following ordering precedence. This precedence list is designed to facilitate automated packaging tasks by simply writing to the VERSION file in the same directory as this source file.
1. If a version has been explicitly assigned using the Facter.version= method, return that version. 2. If there is a VERSION file, read the contents, trim any trailing whitespace, and return that version string. 3. Return the value of the Facter::FACTERVERSION constant hard-coded into the source code.
If there is no VERSION file, the method must return the version string of the nearest parent version that is an officially released version. That is to say, if a branch named 3.1.x contains 25 patches on top of the most recent official release of 3.1.1, then the version method must return the string “3.1.1” if no “VERSION” file is present.
By design the version identifier is not intended to vary during the life a process. There is no guarantee provided that writing to the VERSION file while a Puppet process is running will cause the version string to be updated. On the contrary, the contents of the VERSION are cached to reduce filesystem accesses.
The VERSION file is intended to be used by package maintainers who may be applying patches or otherwise changing the software version in a manner that warrants a different software version identifier. The VERSION file is intended to be managed and owned by the release process and packaging related tasks, and as such should not reside in version control. The FACTERVERSION constant is intended to be version controlled in history.
Ideally, this behavior will allow package maintainers to precisely specify the version of the software they’re packaging as in the following example:
$ git describe > lib/facter/VERSION $ ruby -r facter -e 'puts Facter.version' 1.6.14-6-g66f2c99
@api public
@return [String] containing the facter version, e.g. “1.6.14”
# File lib/facter/version.rb, line 56 def self.version version_file = File.join(File.dirname(__FILE__), 'VERSION') return @facter_version if @facter_version if version = read_version_file(version_file) @facter_version = version end @facter_version ||= FACTERVERSION end
# File lib/facter/version.rb, line 65 def self.version=(version) @facter_version = version end
Generated with the Darkfish Rdoc Generator 2.