Project

General

Profile

Download (6.69 KB) Statistics
| Branch: | Tag: | Revision:
cab0d8c6 Ohad Levy
class ConfigTemplate < ActiveRecord::Base
include Authorization
ffbb8214 Amos Benari
include Taxonomix
9c864cb6 Ohad Levy
audited :allow_mass_assignment => true
feacea35 Amos Benari
self.auditing_enabled = !(File.basename($0) == "rake" && ARGV.include?("db:migrate"))
0fa5d146 Dominic Cleal
attr_accessible :name, :template, :template_kind, :template_kind_id, :snippet, :template_combinations_attributes, :operatingsystems, :operatingsystem_ids, :audit_comment
f2c78d4a Joseph Magen
validates :name, :presence => true, :uniqueness => true
validates :name, :template, :presence => true
validates :template_kind_id, :presence => true, :unless => Proc.new {|t| t.snippet }
ff8cc704 Joseph Mitchell Magen
before_destroy EnsureNotUsedBy.new(:hostgroups, :environments, :os_default_templates)
cab0d8c6 Ohad Levy
has_many :hostgroups, :through => :template_combinations
has_many :environments, :through => :template_combinations
has_many :template_combinations, :dependent => :destroy
belongs_to :template_kind
cdf02336 Ohad Levy
accepts_nested_attributes_for :template_combinations, :allow_destroy => true, :reject_if => lambda {|tc| tc[:environment_id].blank? and tc[:hostgroup_id].blank? }
cab0d8c6 Ohad Levy
has_and_belongs_to_many :operatingsystems
has_many :os_default_templates
bb181ce2 Ohad Levy
before_save :check_for_snippet_assoications, :remove_trailing_chars
ffbb8214 Amos Benari
# with proc support, default_scope can no longer be chained
# include all default scoping here
default_scope lambda {
with_taxonomy_scope do
93ce3a18 Joseph Mitchell Magen
order("config_templates.name")
ffbb8214 Amos Benari
end
}
cab0d8c6 Ohad Levy
47819d54 Ohad Levy
scoped_search :on => :name, :complete_value => true, :default_order => true
scoped_search :on => :snippet, :complete_value => true, :complete_value => {:true => true, :false => false}
scoped_search :on => :template

scoped_search :in => :operatingsystems, :on => :name, :rename => :operatingsystem, :complete_value => true
scoped_search :in => :environments, :on => :name, :rename => :environment, :complete_value => true
scoped_search :in => :hostgroups, :on => :name, :rename => :hostgroup, :complete_value => true
scoped_search :in => :template_kind, :on => :name, :rename => :kind, :complete_value => true

218bd6e0 Justin Sherrill
class Jail < Safemode::Jail
allow :name
end

47819d54 Ohad Levy
def to_param
c4038d6f Ohad Levy
"#{id}-#{name.parameterize}"
47819d54 Ohad Levy
end

2fa95807 Joseph Mitchell Magen
# TODO: review if we can improve SQL
def self.template_ids_for(hosts)
hosts.with_os.map do |host|
host.configTemplate.try(:id)
end.uniq.compact
end

69f9cb82 Ohad Levy
def self.find_template opts = {}
eed31c34 Ohad Levy
raise ::Foreman::Exception.new(N_("Must provide template kind")) unless opts[:kind]
raise ::Foreman::Exception.new(N_("Must provide an operating systems")) unless opts[:operatingsystem_id]
69f9cb82 Ohad Levy
# first filter valid templates to our OS and requested template kind.
017e1049 Ohad Levy
templates = ConfigTemplate.joins(:operatingsystems, :template_kind).where('operatingsystems.id' => opts[:operatingsystem_id], 'template_kinds.name' => opts[:kind])

69f9cb82 Ohad Levy
# once a template has been matched, we no longer look for others.

if opts[:hostgroup_id] and opts[:environment_id]
# try to find a full match to our host group and environment
017e1049 Ohad Levy
template = templates.joins(:hostgroups, :environments).where("hostgroups.id" => opts[:hostgroup_id], "environments.id" => opts[:environment_id]).first
69f9cb82 Ohad Levy
end

if opts[:hostgroup_id]
# try to find a match with our hostgroup only
017e1049 Ohad Levy
template ||= templates.joins(:hostgroups).where("hostgroups.id" => opts[:hostgroup_id]).first
69f9cb82 Ohad Levy
end

if opts[:environment_id]
# search for a template based only on our environment
017e1049 Ohad Levy
template ||= templates.joins(:environments).where("environments.id" => opts[:environment_id]).first
69f9cb82 Ohad Levy
end

# fall back to the os default template
017e1049 Ohad Levy
template ||= templates.joins(:os_default_templates).where("os_default_templates.operatingsystem_id" => opts[:operatingsystem_id]).first
69f9cb82 Ohad Levy
template.is_a?(ConfigTemplate) ? template : nil
bcd1f70a Paul Kelly
end

def enforce_permissions operation
# We get called again with the operation being set to create
return true if operation == "edit" and new_record?

return true if User.current and User.current.allowed_to?("#{operation}_templates".to_sym)
69f9cb82 Ohad Levy
93117958 Bryan Kearney
errors.add :base, (_("You do not have permission to %s this template") % operation)
bcd1f70a Paul Kelly
false
69f9cb82 Ohad Levy
end

0856182e Ohad Levy
def self.build_pxe_default(renderer)
if (proxies = SmartProxy.tftp_proxies).empty?
93117958 Bryan Kearney
error_msg = _("No TFTP proxies defined, can't continue")
0856182e Ohad Levy
end

0fa5d146 Dominic Cleal
if (default_template = ConfigTemplate.find_by_name("PXELinux global default")).nil?
error_msg = _("Could not find a Configuration Template with the name \"PXELinux global default\", please create one.")
0856182e Ohad Levy
end

if error_msg.empty?
begin
@profiles = pxe_default_combos
menu = renderer.render_safe(default_template.template, [:default_template_url], {:profiles => @profiles})
rescue => e
93117958 Bryan Kearney
error_msg = _("failed to process template: %s" % e)
0856182e Ohad Levy
end
end

return [422, error_msg] unless error_msg.empty?

error_msgs = []
proxies.each do |proxy|
begin
tftp = ProxyAPI::TFTP.new(:url => proxy.url)
tftp.create_default({:menu => menu})

@profiles.each do |combo|
combo[:hostgroup].operatingsystem.pxe_files(combo[:hostgroup].medium, combo[:hostgroup].architecture).each do |bootfile_info|
for prefix, path in bootfile_info do
tftp.fetch_boot_file(:prefix => prefix.to_s, :path => path)
end
end
end
rescue => exc
error_msgs << "#{proxy}: #{exc.message}"
end
end

unless error_msgs.empty?
93117958 Bryan Kearney
msg = _("There was an error creating the PXE Default file: %s") % error_msgs.join(",")
0856182e Ohad Levy
return [500, msg]
end

93117958 Bryan Kearney
return [200, _("PXE Default file has been deployed to all Smart Proxies")]
0856182e Ohad Levy
end

e2d3654e Joseph Mitchell Magen
def skip_strip_attrs
['template']
end

cab0d8c6 Ohad Levy
private

# check if our template is a snippet, and remove its associations just in case they were selected.
def check_for_snippet_assoications
return unless snippet
self.hostgroups.clear
self.environments.clear
self.template_combinations.clear
self.operatingsystems.clear
self.template_kind = nil
end
bb181ce2 Ohad Levy
def remove_trailing_chars
0bc57174 Ohad Levy
self.template.gsub!("\r","") unless template.empty?
bb181ce2 Ohad Levy
end
0856182e Ohad Levy
# get a list of all hostgroup, template combinations that a pxemenu will be
# generated for
def self.pxe_default_combos
combos = []
160e109a Ohad Levy
ConfigTemplate.joins(:template_kind).where("template_kinds.name" => "provision").includes(:template_combinations => [:environment, {:hostgroup => [ :operatingsystem, :architecture, :medium]}]).each do |template|
0856182e Ohad Levy
template.template_combinations.each do |combination|
hostgroup = combination.hostgroup
if hostgroup and hostgroup.operatingsystem and hostgroup.architecture and hostgroup.medium
combos << {:hostgroup => hostgroup, :template => template}
end
end
end
combos
end
cab0d8c6 Ohad Levy
end