root/lib/proxy/puppet/environment.rb @ af58b9f1
5da8bfa9 | Ohad Levy | module Proxy::Puppet
|
|
d58db757 | Dominic Cleal | require 'proxy/puppet/initializer'
|
|
5da8bfa9 | Ohad Levy | require 'proxy/puppet/puppet_class'
|
|
require 'puppet'
|
|||
class Environment
|
|||
6c458743 | Greg Sutcliffe | extend Proxy::Log
|
|
5da8bfa9 | Ohad Levy | ||
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
|
|||
d58db757 | Dominic Cleal | Initializer.load
|
|
5da8bfa9 | Ohad Levy | 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
|
|||
d396690f | Greg Sutcliffe | env[:production] = conf[:main][:modulepath] || conf[:master][:modulepath] || '/etc/puppet/modules'
|
|
90a57cf1 | DoubleMalt | logger.warn "No environments found - falling back to defaults (production - #{env[:production]})"
|
|
5da8bfa9 | Ohad Levy | end
|
|
1c60be63 | Greg Sutcliffe | if env.size == 1 and env.keys.first == :master and !env.values.first.include?('$environment')
|
|
# If we only have an entry in [master] it should really be called production
|
|||
logger.warn "Re-writing single 'master' environment as 'production'"
|
|||
env[:production] = env[:master]
|
|||
env.delete :master
|
|||
end
|
|||
6c500f38 | Ohad Levy | ||
d54d0686 | Florent Castelli | new_env = env.clone
|
|
6c500f38 | Ohad Levy | # are we using dynamic puppet environments?
|
|
env.each do|environment, modulepath|
|
|||
ce3bbf11 | Dominic Cleal | next unless modulepath
|
|
# expand $confdir if defined and used in modulepath
|
|||
if modulepath.include?("$confdir")
|
|||
if conf[:main][:confdir]
|
|||
modulepath.gsub!("$confdir", conf[:main][:confdir])
|
|||
else
|
|||
# /etc/puppet is the default if $confdir is not defined
|
|||
modulepath.gsub!("$confdir", "/etc/puppet")
|
|||
2d232cff | Jason Antman | end
|
|
ce3bbf11 | Dominic Cleal | end
|
|
e4ee928b | Daniel Baeurer | # parting modulepath into static and dynamic paths
|
|
staticpath = modulepath.split(":")
|
|||
dynamicpath = modulepath.split(":")
|
|||
722bba2d | Greg Sutcliffe | modulepath.split(":").each do |base_dir|
|
|
if base_dir.include?("$environment")
|
|||
e4ee928b | Daniel Baeurer | # remove this entry from the static paths
|
|
staticpath.delete base_dir
|
|||
else
|
|||
# remove this entry from the dynamic paths
|
|||
dynamicpath.delete base_dir
|
|||
end
|
|||
end
|
|||
# remove or add static environment
|
|||
if staticpath.empty?
|
|||
new_env.delete environment
|
|||
else
|
|||
new_env[environment] = staticpath.join(':')
|
|||
end
|
|||
# create dynamic environments and modulepaths (array of hash)
|
|||
unless dynamicpath.empty?
|
|||
temp_environment = []
|
|||
dynamicpath.each do |base_dir|
|
|||
722bba2d | Greg Sutcliffe | # Dynamic environments - get every directory under the modulepath
|
|
92a8bdef | Greg Sutcliffe | Dir.glob("#{base_dir.gsub(/\$environment(.*)/,"/")}/*").grep(/\/[A-Za-z0-9_]+$/) do |dir|
|
|
6c500f38 | Ohad Levy | e = dir.split("/").last
|
|
e4ee928b | Daniel Baeurer | temp_environment.push({e => base_dir.gsub("$environment", e)})
|
|
end
|
|||
end
|
|||
# group array of hashes, join values (modulepaths) and create dynamic environment => modulepath
|
|||
dynamic_environment = temp_environment.group_by(&:keys).map{|k, v| {k.first => v.flatten.map(&:values).join(':')}}
|
|||
dynamic_environment.each do |h|
|
|||
h.each do |k,v|
|
|||
new_env[k.to_sym] = v
|
|||
6c500f38 | Ohad Levy | end
|
|
end
|
|||
end
|
|||
end
|
|||
d54d0686 | Florent Castelli | new_env.reject { |k, v| k.nil? or v.nil? }
|
|
5da8bfa9 | Ohad Levy | 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
|
|||
af58b9f1 | Rickard von Essen | conf = Puppet.settings.instance_variable_get(:@values)
|
|
eparser = conf[:master] && conf[:master][:parser] == 'future'
|
|||
paths.map {|path| PuppetClass.scan_directory path, eparser}.flatten
|
|||
5da8bfa9 | Ohad Levy | end
|
|
end
|
|||
2d232cff | Jason Antman | end
|