foreman/app/controllers/concerns/foreman/controller/puppet/hosts_controller_extensions.rb @ 9d43fc71
007bf254 | Shimon Shtein | module Foreman::Controller::Puppet::HostsControllerExtensions
|
|
extend ActiveSupport::Concern
|
|||
9d43fc71 | Michael Moll | PUPPETMASTER_ACTIONS = [ :externalNodes, :lookup ]
|
|
PUPPET_AJAX_REQUESTS = %w{hostgroup_or_environment_selected puppetclass_parameters}
|
|||
007bf254 | Shimon Shtein | ||
MULTIPLE_EDIT_ACTIONS = %w(select_multiple_environment update_multiple_environment
|
|||
select_multiple_puppet_proxy update_multiple_puppet_proxy
|
|||
select_multiple_puppet_ca_proxy update_multiple_puppet_ca_proxy)
|
|||
PUPPET_MULTIPLE_ACTIONS = %w(multiple_puppetrun update_multiple_puppetrun) + MULTIPLE_EDIT_ACTIONS
|
|||
22a5bf03 | Daniel Lobato Garcia | ||
007bf254 | Shimon Shtein | included do
|
|
add_smart_proxy_filters PUPPETMASTER_ACTIONS, :features => ['Puppet']
|
|||
alias_method :find_resource_for_puppet_host_extensions, :find_resource
|
|||
alias_method :ajax_request_for_puppet_host_extensions, :ajax_request
|
|||
before_action :ajax_request_for_puppet_host_extensions, :only => PUPPET_AJAX_REQUESTS
|
|||
before_action :find_resource_for_puppet_host_extensions, :only => [:puppetrun]
|
|||
before_action :taxonomy_scope_for_puppet_host_extensions, :only => PUPPET_AJAX_REQUESTS
|
|||
before_action :find_multiple_for_puppet_host_extensions, :only => PUPPET_MULTIPLE_ACTIONS
|
|||
before_action :validate_multiple_puppet_proxy, :only => :update_multiple_puppet_proxy
|
|||
before_action :validate_multiple_puppet_ca_proxy, :only => :update_multiple_puppet_ca_proxy
|
|||
define_action_permission ['puppetrun', 'multiple_puppetrun', 'update_multiple_puppetrun'], :puppetrun
|
|||
define_action_permission MULTIPLE_EDIT_ACTIONS, :edit
|
|||
set_callback :set_class_variables, :after, :set_puppet_class_variables
|
|||
end
|
|||
def hostgroup_or_environment_selected
|
|||
22a5bf03 | Daniel Lobato Garcia | refresh_host
|
|
set_class_variables(@host)
|
|||
007bf254 | Shimon Shtein | Taxonomy.as_taxonomy @organization, @location do
|
|
22a5bf03 | Daniel Lobato Garcia | if @environment || @hostgroup
|
|
ee360810 | Michael Moll | render :partial => 'puppetclasses/class_selection', :locals => {:obj => @host}
|
|
007bf254 | Shimon Shtein | else
|
|
logger.info "environment_id or hostgroup_id is required to render puppetclasses"
|
|||
end
|
|||
end
|
|||
end
|
|||
def puppetclass_parameters
|
|||
Taxonomy.as_taxonomy @organization, @location do
|
|||
render :partial => "puppetclasses/classes_parameters", :locals => { :obj => refresh_host}
|
|||
end
|
|||
end
|
|||
def multiple_puppetrun
|
|||
deny_access unless Setting[:puppetrun]
|
|||
end
|
|||
def update_multiple_puppetrun
|
|||
return deny_access unless Setting[:puppetrun]
|
|||
if @hosts.map(&:puppetrun!).uniq == [true]
|
|||
cf94f62e | Avi Sharvit | success _("Successfully executed, check reports and/or log files for more details")
|
|
007bf254 | Shimon Shtein | else
|
|
error _("Some or all hosts execution failed, Please check log files for more information")
|
|||
end
|
|||
redirect_back_or_to hosts_path
|
|||
end
|
|||
def select_multiple_environment
|
|||
end
|
|||
def update_multiple_environment
|
|||
# simple validations
|
|||
9d43fc71 | Michael Moll | if params[:environment].nil? || (id = params["environment"]["id"]).nil?
|
|
007bf254 | Shimon Shtein | error _('No environment selected!')
|
|
redirect_to(select_multiple_environment_hosts_path)
|
|||
return
|
|||
end
|
|||
ev = Environment.find_by_id(id)
|
|||
68388bc2 | Michael Moll | # update the hosts
|
|
007bf254 | Shimon Shtein | @hosts.each do |host|
|
|
host.environment = (id == 'inherit' && host.hostgroup.present?) ? host.hostgroup.environment : ev
|
|||
host.save(:validate => false)
|
|||
end
|
|||
cf94f62e | Avi Sharvit | success _('Updated hosts: changed environment')
|
|
007bf254 | Shimon Shtein | redirect_back_or_to hosts_path
|
|
end
|
|||
def environment_from_param
|
|||
# simple validations
|
|||
9d43fc71 | Michael Moll | if params[:environment].nil? || (id = params["environment"]["id"]).nil?
|
|
007bf254 | Shimon Shtein | error _('No environment selected!')
|
|
redirect_to(select_multiple_environment_hosts_path)
|
|||
return
|
|||
end
|
|||
id
|
|||
end
|
|||
def get_environment_id(env_params)
|
|||
env_params['id'] if env_params
|
|||
end
|
|||
def get_environment_for(host, id)
|
|||
if id == 'inherit' && host.hostgroup.present?
|
|||
host.hostgroup.environment
|
|||
else
|
|||
Environment.find_by_id(id)
|
|||
end
|
|||
end
|
|||
def validate_multiple_puppet_proxy
|
|||
validate_multiple_proxy(select_multiple_puppet_proxy_hosts_path)
|
|||
end
|
|||
def validate_multiple_puppet_ca_proxy
|
|||
validate_multiple_proxy(select_multiple_puppet_ca_proxy_hosts_path)
|
|||
end
|
|||
def validate_multiple_proxy(redirect_path)
|
|||
if params[:proxy].nil? || (proxy_id = params[:proxy][:proxy_id]).nil?
|
|||
error _('No proxy selected!')
|
|||
redirect_to(redirect_path)
|
|||
return false
|
|||
end
|
|||
85021506 | Michael Moll | if proxy_id.present? && !SmartProxy.find_by_id(proxy_id)
|
|
007bf254 | Shimon Shtein | error _('Invalid proxy selected!')
|
|
redirect_to(redirect_path)
|
|||
return false
|
|||
end
|
|||
end
|
|||
def update_multiple_proxy(proxy_type, host_update_method)
|
|||
proxy_id = params[:proxy][:proxy_id]
|
|||
if proxy_id
|
|||
proxy = SmartProxy.find_by_id(proxy_id)
|
|||
else
|
|||
proxy = nil
|
|||
end
|
|||
failed_hosts = {}
|
|||
@hosts.each do |host|
|
|||
begin
|
|||
host.send(host_update_method, proxy)
|
|||
host.save!
|
|||
rescue => error
|
|||
failed_hosts[host.name] = error
|
|||
message = _('Failed to set %{proxy_type} proxy for %{host}.') % {:host => host, :proxy_type => proxy_type}
|
|||
Foreman::Logging.exception(message, error)
|
|||
end
|
|||
end
|
|||
if failed_hosts.empty?
|
|||
if proxy
|
|||
cf94f62e | Avi Sharvit | success _('The %{proxy_type} proxy of the selected hosts was set to %{proxy_name}') % {:proxy_name => proxy.name, :proxy_type => proxy_type}
|
|
007bf254 | Shimon Shtein | else
|
|
cf94f62e | Avi Sharvit | success _('The %{proxy_type} proxy of the selected hosts was cleared.') % {:proxy_type => proxy_type}
|
|
007bf254 | Shimon Shtein | end
|
|
else
|
|||
error n_("The %{proxy_type} proxy could not be set for host: %{host_names}.",
|
|||
b9df73be | Ohad Levy | "The %{proxy_type} puppet ca proxy could not be set for hosts: %{host_names}",
|
|
007bf254 | Shimon Shtein | failed_hosts.count) % {:proxy_type => proxy_type, :host_names => failed_hosts.map {|h, err| "#{h} (#{err})"}.to_sentence}
|
|
end
|
|||
redirect_back_or_to hosts_path
|
|||
end
|
|||
def handle_proxy_messages(errors, proxy, proxy_type)
|
|||
if errors.empty?
|
|||
if proxy
|
|||
cf94f62e | Avi Sharvit | success _('The %{proxy_type} proxy of the selected hosts was set to %{proxy_name}.') % {:proxy_name => proxy.name, :proxy_type => proxy_type}
|
|
007bf254 | Shimon Shtein | else
|
|
cf94f62e | Avi Sharvit | success _('The %{proxy_type} proxy of the selected hosts was cleared.') % {:proxy_type => proxy_type}
|
|
007bf254 | Shimon Shtein | end
|
|
else
|
|||
error n_("The %{proxy_type} proxy could not be set for host: %{host_names}.",
|
|||
b9df73be | Ohad Levy | "The %{proxy_type} puppet ca proxy could not be set for hosts: %{host_names}",
|
|
007bf254 | Shimon Shtein | errors.count) % {:proxy_type => proxy_type, :host_names => errors.map {|h, err| "#{h} (#{err})"}.to_sentence}
|
|
end
|
|||
end
|
|||
def select_multiple_puppet_proxy
|
|||
end
|
|||
def update_multiple_puppet_proxy
|
|||
update_multiple_proxy(_('Puppet'), :puppet_proxy=)
|
|||
end
|
|||
def select_multiple_puppet_ca_proxy
|
|||
end
|
|||
def update_multiple_puppet_ca_proxy
|
|||
update_multiple_proxy(_('Puppet CA'), :puppet_ca_proxy=)
|
|||
end
|
|||
def set_puppet_class_variables
|
|||
@environment = @host.environment
|
|||
end
|
|||
def taxonomy_scope_for_puppet_host_extensions
|
|||
taxonomy_scope
|
|||
end
|
|||
def find_multiple_for_puppet_host_extensions
|
|||
find_multiple
|
|||
end
|
|||
end
|