Class: DiscoveredHostsController

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

Instance Method Summary collapse

Instance Method Details

#auto_complete_searchObject



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

def auto_complete_search
  begin
    @items = Host::Discovered.complete_for(params[:search])
    @items = @items.map do |item|
      category = (['and','or','not','has'].include?(item.to_s.sub(/^.*\s+/,''))) ? 'Operators' : ''
      part = item.to_s.sub(/^.*\b(and|or)\b/i) {|match| match.sub(/^.*\s+/,'')}
      completed = item.to_s.chomp(part)
      {:completed => completed, :part => part, :label => item, :category => category}
    end
  rescue ScopedSearch::QueryNotSupported => e
    @items = [{:error =>e.to_s}]
  end
  render :json => @items
end

#auto_provisionObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/discovered_hosts_controller.rb', line 139

def auto_provision
  @host.transaction do
    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
end

#auto_provision_allObject



155
156
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
# File 'app/controllers/discovered_hosts_controller.rb', line 155

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

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

  Host.transaction do
    overall_errors = ""
    Host::Discovered.all.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
end

#createObject

Importing yaml is restricted to puppetmasters, so instead we take the ip as a parameter and use refresh_facts to get the rest



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

def create
  Taxonomy.no_taxonomy_scope do
    host, imported = Host::Discovered.new(:ip => get_ip_from_env).refresh_facts
    respond_to do |format|
      format.yml {
        if imported
          render :text => _("Imported discovered host"), :status => 200 and return
        else
          render :text => _("Failed to import facts for discovered host"), :status => 400
        end
      }
    end
  end
rescue Exception => e
  logger.warn "Failed to import facts for discovered host: #{e}"
  render :text => _("Failed to import facts for discovered host: %s") % (e), :status => 400
end

#destroyObject



56
57
58
59
# File 'app/controllers/discovered_hosts_controller.rb', line 56

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

#editObject



61
62
63
64
65
66
# File 'app/controllers/discovered_hosts_controller.rb', line 61

def edit
  Host.transaction do
    @host = ::ForemanDiscovery::HostConverter.to_managed(@host) unless @host.nil?
    render :template => 'hosts/edit'
  end
end

#indexObject



18
19
20
21
22
23
24
25
26
# File 'app/controllers/discovered_hosts_controller.rb', line 18

def index
  search = resource_base.search_for(params[:search], :order => params[:order])
  respond_to do |format|
    format.html do
      @hosts = search.includes(:location, :organization, :subnet, :model, :discovery_attribute_set).paginate(:page => params[:page])
    end
    format.json { render :json => search }
  end
end

#multiple_destroyObject



108
109
# File 'app/controllers/discovered_hosts_controller.rb', line 108

def multiple_destroy
end

#rebootObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/discovered_hosts_controller.rb', line 93

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
    process_error :error_msg => _("Failed to reboot host %{hostname} with error %{error_message}") % {:hostname => @host.name, :error_message => e.message},
                  :redirect => :back
end

#refresh_factsObject



85
86
87
88
89
90
91
# File 'app/controllers/discovered_hosts_controller.rb', line 85

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
end

#showObject



48
49
50
51
52
53
54
# File 'app/controllers/discovered_hosts_controller.rb', line 48

def show
  # filter graph time range
  @range = nil

  # summary report text
  @report_summary = nil
end

#submit_multiple_destroyObject



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

def submit_multiple_destroy
  # keep all the ones that were not deleted for notification.
  @hosts.delete_if {|host| host.destroy}

  missed_hosts = @hosts.map(&:name).join('<br/>')
  if @hosts.empty?
    notice _("Destroyed selected hosts")
  else
    error _("The following hosts were not deleted: %s") % missed_hosts
  end
  redirect_to(discovered_hosts_path)
end

#updateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/discovered_hosts_controller.rb', line 68

def update
  Host.transaction do
    @host = ::ForemanDiscovery::HostConverter.to_managed(@host, false, false)
    forward_url_options
    Taxonomy.no_taxonomy_scope do
      if @host.update_attributes(params[:host])
        process_success :success_redirect => host_path(@host), :redirect_xhr => request.xhr?
      else
        taxonomy_scope
        load_vars_for_ajax
        offer_to_overwrite_conflicts
        process_error :object => @host, :render => 'hosts/edit'
      end
    end
  end
end