diff options
-rw-r--r-- | lib/puppet/indirector/facts/facter.rb | 44 | ||||
-rwxr-xr-x | spec/unit/indirector/facts/facter.rb | 9 |
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/puppet/indirector/facts/facter.rb b/lib/puppet/indirector/facts/facter.rb index 96d22e0c1..5b9a7b3c0 100644 --- a/lib/puppet/indirector/facts/facter.rb +++ b/lib/puppet/indirector/facts/facter.rb @@ -6,6 +6,50 @@ class Puppet::Node::Facts::Facter < Puppet::Indirector::Code between Puppet and Facter. It's only `somewhat` abstract because it always returns the local host's facts, regardless of what you attempt to find." + def self.loaddir(dir, type) + return unless FileTest.directory?(dir) + + Dir.entries(dir).find_all { |e| e =~ /\.rb$/ }.each do |file| + fqfile = ::File.join(dir, file) + begin + Puppet.info "Loading #{type} %s" % ::File.basename(file.sub(".rb",'')) + Timeout::timeout(self.timeout) do + load fqfile + end + rescue => detail + Puppet.warning "Could not load #{type} %s: %s" % [fqfile, detail] + end + end + end + + def self.loadfacts + Puppet[:factpath].split(":").each do |dir| + loaddir(dir, "fact") + end + end + + def self.timeout + timeout = Puppet[:configtimeout] + case timeout + when String: + if timeout =~ /^\d+$/ + timeout = Integer(timeout) + else + raise ArgumentError, "Configuration timeout must be an integer" + end + when Integer: # nothing + else + raise ArgumentError, "Configuration timeout must be an integer" + end + + return timeout + end + + def initialize(*args) + super + self.class.loadfacts + end + def destroy(facts) raise Puppet::DevError, "You cannot destroy facts in the code store; it is only used for getting facts from Facter" end diff --git a/spec/unit/indirector/facts/facter.rb b/spec/unit/indirector/facts/facter.rb index e8ea721d3..1fad4c859 100755 --- a/spec/unit/indirector/facts/facter.rb +++ b/spec/unit/indirector/facts/facter.rb @@ -24,6 +24,11 @@ describe Puppet::Node::Facts::Facter do it "should have its name set to :facter" do Puppet::Node::Facts::Facter.name.should == :facter end + + it "should load facts on initialization" do + Puppet::Node::Facts::Facter.expects(:loadfacts) + Puppet::Node::Facts::Facter.new + end end module TestingCodeFacts @@ -68,3 +73,7 @@ describe Puppet::Node::Facts::Facter, " when destroying facts" do proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) end end + +describe Puppet::Node::Facts::Facter, " when loading facts from the factpath" do + it "should load every fact in each factpath directory" +end |