summaryrefslogtreecommitdiffstats
path: root/lib/puppet/node/environment.rb
blob: 57dc23e9d58fa2fb5b4c1a764259c11e6d6c30ef (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
# Model the environment that a node can operate in.  This class just
# provides a simple wrapper for the functionality around environments.
class Puppet::Node::Environment
    @seen = {}

    # Return an existing environment instance, or create a new one.
    def self.new(name = nil)
        name ||= Puppet.settings.value(:environment)

        raise ArgumentError, "Environment name must be specified" unless name

        symbol = name.to_sym

        return @seen[symbol] if @seen[symbol]

        obj = self.allocate
        obj.send :initialize, symbol
        @seen[symbol] = obj
    end

    attr_reader :name

    # Return an environment-specific setting.
    def [](param)
        Puppet.settings.value(param, self.name)
    end

    def initialize(name)
        @name = name
    end

    def module(name)
        Puppet::Module.each_module(modulepath) do |mod|
            return mod if mod.name == name
        end

        return nil
    end

    def modulepath
        dirs = self[:modulepath].split(File::PATH_SEPARATOR)
        if ENV["PUPPETLIB"]
            dirs = ENV["PUPPETLIB"].split(File::PATH_SEPARATOR) + dirs
        end
        dirs.collect do |dir|
            if dir !~ /^#{File::SEPARATOR}/
                File.join(Dir.getwd, dir)
            else
                dir
            end
        end.find_all do |p|
            p =~ /^#{File::SEPARATOR}/ && FileTest.directory?(p)
        end
    end

    # Return all modules from this environment.
    def modules
        result = []
        Puppet::Module.each_module(modulepath) do |mod|
            result << mod
        end
        result
    end
end