summaryrefslogtreecommitdiffstats
path: root/lib/puppet/node/environment.rb
blob: 2a314803f745fed4060fe0828cabc44016d9d725 (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
# 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
    # Return the list of valid environments.  Just looks them up in
    # the settings.
    def self.valid
        Puppet.settings.value(:environments).split(",").collect { |e| e.to_sym }
    end

    # Is the provided environment valid?
    def self.valid?(name)
        return false if name.to_s == ""
        valid.include?(name.to_sym)
    end

    @seen = {}

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

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

        raise(ArgumentError, "'%s' is not a valid environment" % name) unless valid?(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
end