/
hosts_controller_extensions.rb
96 lines (80 loc) · 2.91 KB
/
hosts_controller_extensions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module ForemanSalt
module Concerns
module HostsControllerExtensions
extend ActiveSupport::Concern
MULTIPLE_EDIT_ACTIONS = %w(select_multiple_salt_environment update_multiple_salt_environment
select_multiple_salt_master update_multiple_salt_master)
module Overrides
def process_hostgroup
@hostgroup = Hostgroup.find(params[:host][:hostgroup_id]) if params[:host][:hostgroup_id].to_i > 0
return head(:not_found) unless @hostgroup
@salt_modules = @host.salt_modules if @host
@salt_environment = @host.salt_environment if @host
@inherited_salt_modules = @hostgroup.all_salt_modules
super
end
private
def load_vars_for_ajax
return unless @host
@obj = @host
@salt_environment = @host.salt_environment if @host
@selected = @host.salt_modules
@salt_modules = @host.salt_environment ? @salt_environment.salt_modules : []
@inherited_salt_modules = @host.hostgroup.all_salt_modules if @host.hostgroup
super
end
end
included do
prepend Overrides
define_action_permission MULTIPLE_EDIT_ACTIONS, :edit
before_action :find_multiple, :only => MULTIPLE_EDIT_ACTIONS
end
def select_multiple_salt_master
end
def update_multiple_salt_master
update_multiple_proxy(_('Salt Master'), :salt_proxy=)
end
def select_multiple_salt_environment
end
def update_multiple_salt_environment
# simple validations
if params[:salt_environment].nil? || (id = params[:salt_environment][:id]).nil?
error _('No salt environment selected!')
redirect_to(select_multiple_salt_environment_hosts_path)
return
end
ev = ForemanSalt::SaltEnvironment.find_by_id(id)
# update the hosts
@hosts.each do |host|
host.salt_environment = ev
host.save(:validate => false)
end
success _('Updated hosts: changed salt environment')
redirect_back_or_to hosts_path
end
private
def find_multiple
# Lets search by name or id and make sure one of them exists first
if params[:host_ids].present?
@hosts = Host.where("id IN (?)", params[:host_ids])
if @hosts.empty?
error _('No hosts were found.')
redirect_to(hosts_path)
return false
end
else
error _('No hosts selected')
redirect_to(hosts_path)
return false
end
@hosts
rescue => error
message = _("Something went wrong while selecting hosts - %s") % error
error(message)
Foreman::Logging.exception(message, error)
redirect_to(hosts_path)
false
end
end
end
end