summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/facts/facter.rb
blob: 6376b71ca5d50ec951c62f743976547422754ff6 (plain)
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
require 'puppet/node/facts'
require 'puppet/indirector/code'

class Puppet::Node::Facts::Facter < Puppet::Indirector::Code
    desc "Retrieve facts from Facter.  This provides a somewhat abstract interface
        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.load_fact_plugins
        # LAK:NOTE See http://snurl.com/21zf8  [groups_google_com] 
        x = Puppet[:factpath].split(":").each do |dir|
            load_facts_in_dir(dir)
        end
    end

    def self.load_facts_in_dir(dir)
        return unless FileTest.directory?(dir)

        Dir.chdir(dir) do
            Dir.glob("*.rb").each do |file|
                fqfile = ::File.join(dir, file)
                begin
                    Puppet.info "Loading facts in %s" % [::File.basename(file.sub(".rb",''))]
                    Timeout::timeout(self.timeout) do
                        load file
                    end
                rescue => detail
                    Puppet.warning "Could not load fact file %s: %s" % [fqfile, detail]
                end
            end
        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.load_fact_plugins
    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

    # Look a host's facts up in Facter.
    def find(request)
        result = Puppet::Node::Facts.new(request.key, Facter.to_hash)

        result.add_local_facts
        result.stringify
        result.downcase_if_necessary

        result
    end

    def save(facts)
        raise Puppet::DevError, "You cannot save facts to the code store; it is only used for getting facts from Facter"
    end
end