forked from theforeman/foreman
/
hosts_controller_extensions.rb
158 lines (128 loc) · 4.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
module Foreman::Controller::Puppet::HostsControllerExtensions
extend ActiveSupport::Concern
PUPPETMASTER_ACTIONS = [:externalNodes, :lookup]
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)
included do
add_smart_proxy_filters PUPPETMASTER_ACTIONS, :features => ['Puppet']
before_action :find_multiple_for_puppet_host_extensions, :only => MULTIPLE_EDIT_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 MULTIPLE_EDIT_ACTIONS, :edit
set_callback :set_class_variables, :after, :set_puppet_class_variables
end
def select_multiple_environment
end
def update_multiple_environment
# simple validations
if params[:environment].nil? || (id = params["environment"]["id"]).nil?
error _('No environment selected!')
redirect_to(select_multiple_environment_hosts_path)
return
end
ev = Environment.find_by_id(id)
# update the hosts
@hosts.each do |host|
host.environment = (id == 'inherit' && host.hostgroup.present?) ? host.hostgroup.environment : ev
host.save(:validate => false)
end
success _('Updated hosts: changed environment')
redirect_back_or_to hosts_path
end
def environment_from_param
# simple validations
if params[:environment].nil? || (id = params["environment"]["id"]).nil?
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
if proxy_id.present? && !SmartProxy.find_by_id(proxy_id)
error _('Invalid proxy selected!')
redirect_to(redirect_path)
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|
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
if failed_hosts.empty?
if proxy
success _('The %{proxy_type} proxy of the selected hosts was set to %{proxy_name}') % {:proxy_name => proxy.name, :proxy_type => proxy_type}
else
success _('The %{proxy_type} proxy of the selected hosts was cleared.') % {:proxy_type => proxy_type}
end
else
error n_("The %{proxy_type} proxy could not be set for host: %{host_names}.",
"The %{proxy_type} puppet ca proxy could not be set for hosts: %{host_names}",
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
success _('The %{proxy_type} proxy of the selected hosts was set to %{proxy_name}.') % {:proxy_name => proxy.name, :proxy_type => proxy_type}
else
success _('The %{proxy_type} proxy of the selected hosts was cleared.') % {:proxy_type => proxy_type}
end
else
error n_("The %{proxy_type} proxy could not be set for host: %{host_names}.",
"The %{proxy_type} puppet ca proxy could not be set for hosts: %{host_names}",
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 find_multiple_for_puppet_host_extensions
find_multiple
end
end