Module: ForemanPuppet::Extensions::HostsControllerExtensions
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb
Constant Summary collapse
- PUPPETMASTER_ACTIONS =
%i[externalNodes].freeze
- PUPPET_AJAX_REQUESTS =
%w[hostgroup_or_environment_selected puppetclass_parameters].freeze
- 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].freeze
Instance Method Summary collapse
-
#environment_from_param ⇒ Object
TODO: seems to be useless.
-
#externalNodes ⇒ Object
returns a yaml file ready to use for puppet external nodes script expected a fqdn parameter to provide hostname to lookup see example script in extras directory will return HTML error codes upon failure rubocop:disable Naming/MethodName.
- #find_multiple_for_puppet_host_extensions ⇒ Object
- #get_environment_for(host, id) ⇒ Object
- #get_environment_id(env_params) ⇒ Object
- #handle_proxy_messages(errors, proxy, proxy_type) ⇒ Object
- #hostgroup_or_environment_selected ⇒ Object
- #puppetclass_parameters ⇒ Object
- #select_multiple_environment ⇒ Object
- #select_multiple_puppet_ca_proxy ⇒ Object
- #select_multiple_puppet_proxy ⇒ Object
- #select_multiple_puppet_proxy_hosts_path(*args) ⇒ Object
- #set_puppet_class_variables ⇒ Object
- #taxonomy_scope_for_puppet_host_extensions ⇒ Object
- #update_multiple_environment ⇒ Object
- #update_multiple_proxy(proxy_type, host_update_method) ⇒ Object
- #update_multiple_puppet_ca_proxy ⇒ Object
- #update_multiple_puppet_proxy ⇒ Object
- #validate_multiple_proxy(redirect_path) ⇒ Object
- #validate_multiple_puppet_ca_proxy ⇒ Object
- #validate_multiple_puppet_proxy ⇒ Object
Instance Method Details
#environment_from_param ⇒ Object
TODO: seems to be useless
112 113 114 115 116 117 118 119 120 121 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 112 def environment_from_param # simple validations if params[:environment].nil? || (id = params['environment']['id']).nil? error _('No environment selected!') redirect_to(foreman_puppet.select_multiple_environment_hosts_path) return end id end |
#externalNodes ⇒ Object
returns a yaml file ready to use for puppet external nodes script expected a fqdn parameter to provide hostname to lookup see example script in extras directory will return HTML error codes upon failure rubocop:disable Naming/MethodName
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 85 def externalNodes certname = params[:name].to_s @host ||= resource_base.find_by certname: certname @host ||= resource_base.friendly.find certname unless @host not_found return end begin respond_to do |format| # don't break lines in yaml to support Ruby < 1.9.3 # Remove the HashesWithIndifferentAccess using 'deep_stringify_keys', # then we turn it into YAML host_info_yaml = @host.info.deep_stringify_keys.to_yaml(line_width: -1) format.html { render html: ActionController::Base.helpers.tag.pre(ERB::Util.html_escape(host_info_yaml)) } format.yml { render plain: host_info_yaml } end rescue StandardError => e Foreman::Logging.exception("Failed to generate external nodes for #{@host}", e) render plain: _('Unable to generate output, Check log files'), status: :precondition_failed end end |
#find_multiple_for_puppet_host_extensions ⇒ Object
222 223 224 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 222 def find_multiple_for_puppet_host_extensions find_multiple end |
#get_environment_for(host, id) ⇒ Object
127 128 129 130 131 132 133 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 127 def get_environment_for(host, id) if id == 'inherit' && host.hostgroup.present? host.hostgroup.environment else ForemanPuppet::Environment.find_by(id: id) end end |
#get_environment_id(env_params) ⇒ Object
123 124 125 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 123 def get_environment_id(env_params) env_params['id'] if env_params end |
#handle_proxy_messages(errors, proxy, proxy_type) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 186 def (errors, proxy, proxy_type) if errors.empty? if proxy success format(_('The %{proxy_type} proxy of the selected hosts was set to %{proxy_name}.'), proxy_name: proxy.name, proxy_type: proxy_type) else success format(_('The %{proxy_type} proxy of the selected hosts was cleared.'), proxy_type: proxy_type) end else error format(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 |
#hostgroup_or_environment_selected ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 38 def hostgroup_or_environment_selected refresh_host set_class_variables(@host) ::Taxonomy.as_taxonomy @organization, @location do if @environment || @hostgroup render partial: 'hosts/form_puppet_enc_tab', locals: { subject: @host, resource_type: :host } else logger.info 'environment_id or hostgroup_id is required to render puppetclasses' end end end |
#puppetclass_parameters ⇒ Object
50 51 52 53 54 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 50 def puppetclass_parameters ::Taxonomy.as_taxonomy @organization, @location do render partial: 'foreman_puppet/puppetclasses/classes_parameters', locals: { obj: refresh_host } end end |
#select_multiple_environment ⇒ Object
56 57 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 56 def select_multiple_environment end |
#select_multiple_puppet_ca_proxy ⇒ Object
207 208 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 207 def select_multiple_puppet_ca_proxy end |
#select_multiple_puppet_proxy ⇒ Object
200 201 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 200 def select_multiple_puppet_proxy end |
#select_multiple_puppet_proxy_hosts_path(*args) ⇒ Object
34 35 36 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 34 def select_multiple_puppet_proxy_hosts_path(*args) foreman_puppet.select_multiple_puppet_proxy_hosts_path(*args) end |
#set_puppet_class_variables ⇒ Object
214 215 216 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 214 def set_puppet_class_variables @environment = @host.puppet&.environment end |
#taxonomy_scope_for_puppet_host_extensions ⇒ Object
218 219 220 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 218 def taxonomy_scope_for_puppet_host_extensions taxonomy_scope end |
#update_multiple_environment ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 59 def update_multiple_environment # simple validations if params[:environment].nil? || (id = params['environment']['id']).nil? error _('No environment selected!') redirect_to(foreman_puppet.select_multiple_environment_hosts_path) return end ev = ForemanPuppet::Environment.find_by(id: id) # update the hosts @hosts.each do |host| puppet = host.puppet || host.build_puppet puppet.environment = id == 'inherit' && host.hostgroup.present? ? host.hostgroup.puppet&.environment : ev host.save(validate: false) end success _('Updated hosts: changed environment') redirect_back_or_to hosts_path end |
#update_multiple_proxy(proxy_type, host_update_method) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 157 def update_multiple_proxy(proxy_type, host_update_method) proxy_id = params[:proxy][:proxy_id] proxy = (SmartProxy.find_by(id: proxy_id) if proxy_id) failed_hosts = {} @hosts.each do |host| host.send(host_update_method, proxy) host.save! rescue StandardError => e failed_hosts[host.name] = e = format(_('Failed to set %{proxy_type} proxy for %{host}.'), host: host, proxy_type: proxy_type) Foreman::Logging.exception(, e) end if failed_hosts.empty? if proxy success format(_('The %{proxy_type} proxy of the selected hosts was set to %{proxy_name}'), proxy_name: proxy.name, proxy_type: proxy_type) else success format(_('The %{proxy_type} proxy of the selected hosts was cleared.'), proxy_type: proxy_type) end else error format(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 |
#update_multiple_puppet_ca_proxy ⇒ Object
210 211 212 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 210 def update_multiple_puppet_ca_proxy update_multiple_proxy(_('Puppet CA'), :puppet_ca_proxy=) end |
#update_multiple_puppet_proxy ⇒ Object
203 204 205 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 203 def update_multiple_puppet_proxy update_multiple_proxy(_('Puppet'), :puppet_proxy=) end |
#validate_multiple_proxy(redirect_path) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 143 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 |
#validate_multiple_puppet_ca_proxy ⇒ Object
139 140 141 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 139 def validate_multiple_puppet_ca_proxy validate_multiple_proxy(select_multiple_puppet_ca_proxy_hosts_path) end |
#validate_multiple_puppet_proxy ⇒ Object
135 136 137 |
# File 'app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb', line 135 def validate_multiple_puppet_proxy validate_multiple_proxy(foreman_puppet.select_multiple_puppet_proxy_hosts_path) end |