Project

General

Profile

Download (1.78 KB) Statistics
| Branch: | Tag: | Revision:
6e50fa1d Ohad Levy
class Environment < ActiveRecord::Base
c6eee281 Ohad Levy
has_and_belongs_to_many :puppetclasses
has_many :hosts
validates_presence_of :name
validates_uniqueness_of :name
validates_format_of :name, :with => /^\S+$/, :message => "Name cannot contain spaces"
b09b4515 Ohad Levy
default_scope :order => 'name'
09393c6d Ohad Levy
def to_label
name
end

def to_s
name
end
f8e711fe Ohad Levy
# returns an hash of all puppet environments and their relative paths
def self.puppetEnvs
env = Hash.new
# read puppet configuration
conf = Puppet.settings.instance_variable_get(:@values)
6281aac9 Ohad Levy
# query for the environments variable
e9523955 Ohad Levy
unless conf[:main][:environments].nil?
conf[:main][:environments].split(",").each {|e| env[e.to_sym] = conf[e.to_sym][:modulepath]}
else
6281aac9 Ohad Levy
# 0.25 doesn't require the environments variable anymore, scanning for modulepath
conf.keys.each {|p| env[p] = conf[p][:modulepath] unless conf[p][:modulepath].nil?}
# puppetmaster section "might" also returns the modulepath
1ed89594 Ohad Levy
env.delete :main
6281aac9 Ohad Levy
env.delete :puppetmasterd if env.size > 1

if env.size == 0
# fall back to defaults - we probably don't use environments
9c3ffb6b Ohad Levy
env[:production] = conf[:main][:modulepath] || conf[:puppetmasterd][:modulepath] || SETTINGS[:modulepath] || Puppet[:modulepath] || "/etc/puppet/modules"
6281aac9 Ohad Levy
end
e9523955 Ohad Levy
end
f8e711fe Ohad Levy
return env
end

# Imports all Environments and classes from Puppet modules.
c878e775 Ohad Levy
# TODO: compare between current and imported state, so old-unused ones will be deleted
f8e711fe Ohad Levy
def self.importClasses
self.puppetEnvs.each_pair do |e,p|
env = Environment.find_or_create_by_name e.to_s
# if module path contains more than one directory
helper = Array.new
p.split(":").each {|mp| helper += Puppetclass.scanForClasses(mp)}
env.puppetclasses = helper
end
end

6e50fa1d Ohad Levy
end