root/lib/proxy/puppet/environment.rb @ 083405c8
5da8bfa9 | Ohad Levy | module Proxy::Puppet
|
|
require 'proxy/puppet/puppet_class'
|
|||
require 'puppet'
|
|||
class Environment
|
|||
class << self
|
|||
# return a list of all puppet environments
|
|||
def all
|
|||
6c500f38 | Ohad Levy | puppet_environments.map { |env, path| new(:name => env, :paths => path.split(":")) }
|
|
5da8bfa9 | Ohad Levy | end
|
|
def find name
|
|||
all.each { |e| return e if e.name == name }
|
|||
nil
|
|||
end
|
|||
private
|
|||
def puppet_environments
|
|||
Puppet.clear
|
|||
Puppet[:config] = SETTINGS.puppet_conf if SETTINGS.puppet_conf
|
|||
Puppet.parse_config
|
|||
conf = Puppet.settings.instance_variable_get(:@values)
|
|||
env = { }
|
|||
# query for the environments variable
|
|||
6c500f38 | Ohad Levy | if conf[:main][:environments].nil?
|
|
# 0.25 and newer doesn't require the environments variable anymore, scanning for modulepath
|
|||
5da8bfa9 | Ohad Levy | conf.keys.each { |p| env[p] = conf[p][:modulepath] unless conf[p][:modulepath].nil? }
|
|
# puppetmaster section "might" also returns the modulepath
|
|||
env.delete :main
|
|||
env.delete :puppetmasterd if env.size > 1
|
|||
6c500f38 | Ohad Levy | else
|
|
conf[:main][:environments].split(",").each { |e| env[e.to_sym] = conf[e.to_sym][:modulepath] unless conf[e.to_sym][:modulepath].nil? }
|
|||
5da8bfa9 | Ohad Levy | end
|
|
if env.values.compact.size == 0
|
|||
# fall back to defaults - we probably don't use environments
|
|||
env[:production] = conf[:main][:modulepath] || conf[:master][:modulepath]
|
|||
end
|
|||
6c500f38 | Ohad Levy | ||
# are we using dynamic puppet environments?
|
|||
env.each do|environment, modulepath|
|
|||
0f2cfebb | Ohad Levy | if modulepath and modulepath.include?("$environment")
|
|
2d232cff | Jason Antman | # expand $confdir if defined and used in modulepath
|
|
if conf[:main][:confdir] and modulepath.include?("$confdir")
|
|||
modulepath.sub!("$confdir", conf[:main][:confdir])
|
|||
end
|
|||
6c500f38 | Ohad Levy | # Dynamic environments - get every directory under the modulepath
|
|
modulepath.gsub(/\$environment.*/,"/").split(":").each do |base_dir|
|
|||
Dir.glob("#{base_dir}/*") do |dir|
|
|||
e = dir.split("/").last
|
|||
env[e] = modulepath.gsub("$environment", e)
|
|||
end
|
|||
end
|
|||
# get rid of the main environment
|
|||
env.delete(environment)
|
|||
end
|
|||
end
|
|||
5da8bfa9 | Ohad Levy | env.reject { |k, v| k.nil? or v.nil? }
|
|
end
|
|||
end
|
|||
6c500f38 | Ohad Levy | attr_reader :name, :paths
|
|
5da8bfa9 | Ohad Levy | ||
def initialize args
|
|||
083405c8 | Ohad Levy | @name = args[:name].to_s || raise("Must provide a name")
|
|
@paths= args[:paths] || raise("Must provide a path")
|
|||
5da8bfa9 | Ohad Levy | end
|
|
def to_s
|
|||
name
|
|||
end
|
|||
def classes
|
|||
6c500f38 | Ohad Levy | paths.map {|path| PuppetClass.scan_directory path}.flatten
|
|
5da8bfa9 | Ohad Levy | end
|
|
end
|
|||
2d232cff | Jason Antman | end
|