summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/feature.rb
blob: 2669d1ab188ccfdcd5e0e09803ce94d5c3150710 (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
76
77
78
79
80
81
82
83
84
85
#  Created by Luke Kanies on 2006-11-07.
#  Copyright (c) 2006. All rights reserved.

class Puppet::Util::Feature
    attr_reader :path

    # Create a new feature test.  You have to pass the feature name,
    # and it must be unique.  You can either provide a block that
    # will get executed immediately to determine if the feature
    # is present, or you can pass an option to determine it.
    # Currently, the only supported option is 'libs' (must be
    # passed as a symbol), which will make sure that each lib loads
    # successfully.
    def add(name, options = {})
        method = name.to_s + "?"
        if self.class.respond_to?(method)
            raise ArgumentError, "Feature %s is already defined" % name
        end

        if block_given?
            begin
                result = yield
            rescue => detail
                warn "Failed to load feature test for %s: %s" % [name, detail]
                result = false
            end
            @results[name] = result
        end
        
        meta_def(method) do
            unless @results.include?(name)
                @results[name] = test(name, options)
            end
            @results[name]
        end
    end
    
    # Create a new feature collection.
    def initialize(path)
        @path = path
        @results = {}
        @loader = Puppet::Util::Autoload.new(self, @path)
    end
    
    def load
        @loader.loadall
    end

    def method_missing(method, *args)
        return super unless method.to_s =~ /\?$/

        feature = method.to_s.sub(/\?$/, '')
        @loader.load(feature)

        if respond_to?(method)
            return self.send(method)
        else
            return false
        end
    end

    # Actually test whether the feature is present.  We only want to test when
    # someone asks for the feature, so we don't unnecessarily load
    # files.
    def test(name, options)
        result = true
        if ary = options[:libs]
            ary = [ary] unless ary.is_a?(Array)
            
            ary.each do |lib|
                unless lib.is_a?(String)
                    raise ArgumentError, "Libraries must be passed as strings not %s" % lib.class
                end
            
                begin
                    require lib
                rescue Exception
                    Puppet.debug "Failed to load library '%s' for feature '%s'" % [lib, name]
                    result = false
                end
            end
        end
        result
    end
end