Class: DiscoveredHostsController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, Foreman::Controller::AutoCompleteSearch, Foreman::Controller::DiscoveredExtensions, Foreman::Controller::Parameters::DiscoveredHost, Foreman::Controller::TaxonomyMultiple
Defined in:
app/controllers/discovered_hosts_controller.rb

Instance Method Summary collapse

Instance Method Details

#auto_provisionObject



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/discovered_hosts_controller.rb', line 161

def auto_provision
  if rule = find_discovery_rule(@host)
    if perform_auto_provision(@host, rule)
      process_success :success_msg => _("Host %{host} was provisioned with rule %{rule}") % {:host => @host.name, :rule => rule.name}, :success_redirect => discovered_hosts_path
    else
      errors = @host.errors.full_messages.join(' ')
      logger.warn "Failed to auto provision host %s: %s" % [@host.name, errors]
      process_error :error_msg => _("Failed to auto provision host %s: %s") % [@host.name, errors], :redirect => :back
    end
  else
    process_success :success_msg => _("No rule found for host %s") % @host.name, :success_redirect => :back
  end
end

#destroyObject



54
55
56
57
# File 'app/controllers/discovered_hosts_controller.rb', line 54

def destroy
  @host.destroy
  redirect_to :action => 'index'
end

#editObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/discovered_hosts_controller.rb', line 59

def edit
  quick = params.delete(:quick_submit)
  @host = ::ForemanDiscovery::HostConverter.to_managed(@host, true, false, discovered_host_params_host) unless @host.nil?
  setup_host_class_variables
  @override_taxonomy = true
  # need to permit this one but don't know how
  if quick
    perform_update(@host, _('Successfully provisioned %s') % @host.name)
  else
    @host.build = true
    render :template => 'discovered_hosts/edit'
  end
end

#indexObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/discovered_hosts_controller.rb', line 30

def index
  @hosts = resource_base_search_and_page.includes([
    :location,
    :organization,
    :model,
    :discovery_attribute_set
  ], {:interfaces => :subnet})
  fact_array = @hosts.collect do |host|
    [host.id, Hash[host.fact_values.joins(:fact_name).where('fact_names.name' => Setting['discovery_fact_column']).pluck(:name, :value)]]
  end
  @host_facts = Hash[fact_array]
end

#model_of_controllerObject



26
27
28
# File 'app/controllers/discovered_hosts_controller.rb', line 26

def model_of_controller
  Host::Discovered
end

#multiple_auto_provisionObject



147
148
# File 'app/controllers/discovered_hosts_controller.rb', line 147

def multiple_auto_provision
end

#multiple_destroyObject



141
142
# File 'app/controllers/discovered_hosts_controller.rb', line 141

def multiple_destroy
end

#multiple_rebootObject



144
145
# File 'app/controllers/discovered_hosts_controller.rb', line 144

def multiple_reboot
end

#perform_update(host, success_message = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/discovered_hosts_controller.rb', line 81

def perform_update(host, success_message = nil)
  Taxonomy.no_taxonomy_scope do
    ::ForemanDiscovery::HostConverter.set_build_clean_facts(host)
    ::ForemanDiscovery::HostConverter.unused_ip_for_host(host)
    if host.save
      host_path = Setting['host_details_ui'] ? host_details_page_path(host) : host_path(host)
      success_options = { :success_redirect => host_path, :redirect_xhr => request.xhr? }
      success_options[:success_msg] = success_message if success_message
      process_success success_options
    else
      taxonomy_scope
      load_vars_for_ajax
      offer_to_overwrite_conflicts
      process_error :object => host, :render => 'discovered_hosts/edit'
    end
  end
end

#rebootObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/discovered_hosts_controller.rb', line 111

def reboot
  unless @host.is_a?(::Host::Discovered)
    process_error :error_msg => _("Host of type %s can not be rebooted") % @host.type, :redirect => :back
  end

  if @host.reboot
    process_success :success_msg => _("Rebooting host %s") % @host.name, :success_redirect => :back
  else
    process_error :error_msg => _("Failed to reboot host %s") % @host.name, :redirect => :back
  end
rescue => e
  exception = Foreman::WrappedException.new(e, N_("Failed to reboot host %{hostname} with error %{error_message}"), :hostname => @host.name, :error_message => e.message)
  Foreman::Logging.exception exception.message, e
  process_error :error_msg => exception.message, :redirect => :back
end

#refresh_factsObject



99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/discovered_hosts_controller.rb', line 99

def refresh_facts
  if @host.is_a?(::Host::Discovered) && @host.refresh_facts
    process_success :success_msg => _("Facts refreshed for %s") % @host.name, :success_redirect => :back
  else
    process_error :error_msg => _("Failed to refresh facts for %s") % @host.name, :redirect => :back
  end
rescue => e
  exception = Foreman::WrappedException.new(e, N_("Failed to refresh facts for %{hostname} with error %{error_message}"), :hostname => @host.name, :error_message => e.message)
  Foreman::Logging.exception exception.message, e
  process_error :error_msg => exception.message, :redirect => :back
end

#resource_classObject



202
203
204
# File 'app/controllers/discovered_hosts_controller.rb', line 202

def resource_class
  Host::Discovered
end

#showObject



43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/discovered_hosts_controller.rb', line 43

def show
  # filter graph time range
  @range = nil
  # summary report text
  @report_summary = nil
  resolver = ForemanDiscovery::FactToCategoryResolver.new(@host)
  @categories_names = ForemanDiscovery::FactToCategoryResolver::CATEGORIES_NAMES
  @categories = resolver.categories
  @interfaces = resolver.interfaces
end

#submit_multiple_auto_provisionObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/controllers/discovered_hosts_controller.rb', line 175

def submit_multiple_auto_provision
  result = true
  error_message = _("Errors during auto provisioning: %s")

  if Host::Discovered.count == 0
    error_message = _("No discovered hosts to provision")
    result = false
  end

  overall_errors = ""
  @hosts.each do |discovered_host|
    if rule = find_discovery_rule(discovered_host)
      result &= perform_auto_provision(discovered_host, rule)
      unless discovered_host.errors.empty?
        errors = discovered_host.errors.full_messages.join(' ')
        logger.warn "Failed to auto provision host %s: %s" % [discovered_host.name, errors]
        overall_errors << "#{discovered_host.name}: #{errors} "
      end
    end
  end
  if result
    process_success :success_msg => _("Discovered hosts are provisioning now"), :success_redirect => :back
  else
    process_error :error_msg => error_message % overall_errors, :redirect => :back
  end
end

#submit_multiple_destroyObject



150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/discovered_hosts_controller.rb', line 150

def submit_multiple_destroy
  # keep all the ones that were not deleted for notification.
  missed_hosts = @hosts.select {|host| !host.destroy }
  if missed_hosts
    success _("Destroyed selected hosts")
  else
    error _("The following hosts were not deleted: %s") % missed_hosts
  end
  redirect_to(discovered_hosts_path)
end

#submit_multiple_rebootObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/discovered_hosts_controller.rb', line 127

def submit_multiple_reboot
  error_message = perform_reboot_all(@hosts)

  if error_message
    process_error :error_msg => error_message, :redirect => :back
  else
    process_success :success_msg => _("Discovered hosts are rebooting now"), :success_redirect => :back
  end
rescue => e
  exception = Foreman::WrappedException.new(e, N_("Failed to reboot hosts with error %s"), e.message)
  Foreman::Logging.exception exception.message, e
  process_error :error_msg => exception.message, :redirect => :back
end

#updateObject



73
74
75
76
77
78
79
# File 'app/controllers/discovered_hosts_controller.rb', line 73

def update
  @host = ::ForemanDiscovery::HostConverter.to_managed(@host, true, true, managed_host_params_host)
  forward_url_options

  @override_taxonomy = true
  perform_update(@host)
end