Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Created February 18, 2011 00:36
Show Gist options
  • Save kwilczynski/833047 to your computer and use it in GitHub Desktop.
Save kwilczynski/833047 to your computer and use it in GitHub Desktop.
facts.txt with ability to include other files ...
require 'thread'
require 'facter'
class StaticFact
@@facts = {}
@@mutex = Mutex.new
@@maximum_recursion_level = 4 # Sane default? Hope so ...
@@current_recursion_level = 0
class << self
def load_facts(file='/etc/facts.conf')
parse_file(file) # Parse and load facts from the origin file ...
@@facts
end
private
def parse_file(file)
# We cannot allow for endless recursion ...
return if @@current_recursion_level > @@maximum_recursion_level
return unless File.exists?(file)
File.readlines(file).each do |line|
next if line.match(/^#/) # Skip comments if any ...
next if line.match(/^\n|\r\n/) # Skip empty lines ...
if match = line.match(/^include\s+(.+)$/)
file = File.expand_path(match[1].strip)
files = Dir.glob(file) # Look for wild card pattern ...
if files.size > 1
# When simple pattern was used to include more files then
# we parse each and one of them but do not change current
# recursion level. We consider this a "flat include".
files.each { |f| parse_file(f) }
else
@@mutex.synchronize { @@current_recursion_level += 1 }
parse_file(file) # Parse and load facts from an include file ...
end
elsif match = line.match(/^(.+)=(.+)$/)
@@mutex.synchronize {
@@facts.update({ match[1].strip => match[2].strip })
}
end
end
end
end
end
facts = StaticFact.load_facts
facts.each do |name, value|
Facter.add(name) do
setcode { value }
end
end
facts.clear # Remove all. Schedule for garbage collection ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment