foreman/app/controllers/hosts_controller.rb @ 459e0feb
2f077f63 | Ohad Levy | class HostsController < ApplicationController
|
|
0265427b | Ohad Levy | before_filter :require_login, :except => [ :query, :externalNodes, :lookup ]
|
|
before_filter :require_ssl, :except => [ :query, :externalNodes, :lookup ]
|
|||
0e12dbfc | Ohad Levy | before_filter :find_hosts, :only => :query
|
|
b09b4515 | Ohad Levy | before_filter :ajax_methods, :only => [:environment_selected, :architecture_selected, :os_selected]
|
|
52538203 | Ohad Levy | before_filter :find_multiple, :only => [:multiple_actions, :update_multiple_parameters,
|
|
6b728fdc | Ashay Humane | :select_multiple_hostgroup, :select_multiple_environment, :multiple_parameters]
|
|
cdd8bb6f | Paul Kelly | ||
459e0feb | Paul Kelly | helper :hosts, :reports
|
|
0e12dbfc | Ohad Levy | ||
b09b4515 | Ohad Levy | def index
|
|
@search = Host.search(params[:search])
|
|||
@hosts = @search.paginate :page => params[:page]
|
|||
a393106d | Ohad Levy | end
|
|
8e4c0917 | Ohad Levy | ||
72e65b31 | Ohad Levy | def show
|
|
# filter graph time range
|
|||
b09b4515 | Ohad Levy | @range = (params["range"].empty? ? 7 : params["range"].to_i)
|
|
72e65b31 | Ohad Levy | range = @range.days.ago
|
|
@host = Host.find params[:id]
|
|||
c6f1b718 | Ohad Levy | graphs = @host.graph(range)
|
|
# runtime graph
|
|||
data = { :labels => graphs[:runtime_labels], :values => graphs[:runtime] }
|
|||
options = { :title => "Runtime"}
|
|||
@runtime_graph = setgraph(GoogleVisualr::AnnotatedTimeLine.new, data, options)
|
|||
# resource graph
|
|||
data = { :labels => graphs[:resources_labels], :values => graphs[:resources] }
|
|||
options = { :title => "Resource", :width => 800, :height => 300, :legend => 'bottom'}
|
|||
@resource_graph = setgraph(GoogleVisualr::LineChart.new, data, options)
|
|||
# summary report text
|
|||
72e65b31 | Ohad Levy | @report_summary = Report.summarise(range, @host)
|
|
end
|
|||
b09b4515 | Ohad Levy | def new
|
|
@host = Host.new
|
|||
@host.host_parameters.build
|
|||
end
|
|||
dad78fb6 | Paul Kelly | # Clone the host
|
|
def clone
|
|||
@original = Host.find(params[:id])
|
|||
@host = @original.clone
|
|||
load_vars_for_ajax
|
|||
# Clone any parameters as well
|
|||
@original.host_parameters.each{|param| @host.host_parameters << param.clone}
|
|||
flash[:error_customisation] = {:header_message => nil, :class => "flash notice", :id => nil,
|
|||
19bd108a | Paul Kelly | :message => "The following fields will need reviewing:" }
|
|
dad78fb6 | Paul Kelly | @host.valid?
|
|
render :action => :new
|
|||
end
|
|||
b09b4515 | Ohad Levy | def create
|
|
@host = Host.new(params[:host])
|
|||
if @host.save
|
|||
flash[:foreman_notice] = "Successfully created host."
|
|||
redirect_to @host
|
|||
else
|
|||
003ae9a1 | Ohad Levy | load_vars_for_ajax
|
|
b09b4515 | Ohad Levy | render :action => 'new'
|
|
end
|
|||
end
|
|||
def edit
|
|||
@host = Host.find(params[:id])
|
|||
dad78fb6 | Paul Kelly | load_vars_for_ajax
|
|
b09b4515 | Ohad Levy | end
|
|
def update
|
|||
@host = Host.find(params[:id])
|
|||
if @host.update_attributes(params[:host])
|
|||
flash[:foreman_notice] = "Successfully updated host."
|
|||
redirect_to @host
|
|||
else
|
|||
003ae9a1 | Ohad Levy | load_vars_for_ajax
|
|
b09b4515 | Ohad Levy | render :action => 'edit'
|
|
end
|
|||
end
|
|||
def destroy
|
|||
@host = Host.find(params[:id])
|
|||
if @host.destroy
|
|||
flash[:foreman_notice] = "Successfully destroyed host."
|
|||
else
|
|||
flash[:foreman_error] = @host.errors.full_messages.join("<br>")
|
|||
end
|
|||
redirect_to hosts_url
|
|||
end
|
|||
# form AJAX methods
|
|||
def environment_selected
|
|||
if params[:environment_id].to_i > 0 and @environment = Environment.find(params[:environment_id])
|
|||
render :partial => 'puppetclasses/class_selection', :locals => {:obj => (@host ||= Host.new)}
|
|||
else
|
|||
return head(:not_found)
|
|||
end
|
|||
end
|
|||
def architecture_selected
|
|||
assign_parameter "architecture"
|
|||
end
|
|||
def os_selected
|
|||
assign_parameter "operatingsystem"
|
|||
end
|
|||
ad36b317 | Ohad Levy | #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
|
|||
8e4c0917 | Ohad Levy | def externalNodes
|
|
fce2cbc0 | Ohad Levy | # check our parameters and look for a host
|
|
bc2625b5 | Ohad Levy | if params[:id] and @host = Host.find(params[:id])
|
|
elsif params["name"] and @host = Host.find(:first,:conditions => ["name = ?",params["name"]])
|
|||
38a6bbd9 | Ohad Levy | else
|
|
bc2625b5 | Ohad Levy | render :text => '404 Not Found', :status => 404 and return
|
|
end
|
|||
begin
|
|||
respond_to do |format|
|
|||
format.html { render :text => @host.info.to_yaml.gsub("\n","<br>") }
|
|||
format.yml { render :text => @host.info.to_yaml }
|
|||
38a6bbd9 | Ohad Levy | end
|
|
bc2625b5 | Ohad Levy | rescue
|
|
# failed
|
|||
logger.warn "Failed to generate external nodes for #{@host.name} with #{$!}"
|
|||
render :text => 'Unable to generate output, Check log files', :status => 412 and return
|
|||
8e4c0917 | Ohad Levy | end
|
|
end
|
|||
0ce8fa05 | Ohad Levy | ||
e1e9e09b | Ohad Levy | def puppetrun
|
|
host = Host.find params[:id]
|
|||
88bce962 | Ohad Levy | if GW::Puppet.run host.name
|
|
e1e9e09b | Ohad Levy | render :text => "Successfully executed, check log files for more details"
|
|
else
|
|||
render :text => "Failed, check log files"
|
|||
end
|
|||
end
|
|||
8613dec9 | Ohad Levy | def setBuild
|
|
host = Host.find params[:id]
|
|||
if host.setBuild != false
|
|||
6874bbd9 | Paul Kelly | flash[:foreman_notice] = "Enabled #{host.name} for rebuild on next boot"
|
|
8613dec9 | Ohad Levy | else
|
|
1fe671cf | Ohad Levy | flash[:foreman_error] = "Failed to enable #{host.name} for installation"
|
|
8613dec9 | Ohad Levy | end
|
|
redirect_to :back
|
|||
end
|
|||
b0d3f4ee | Ohad Levy | ||
# generates a link to Puppetmaster RD graphs
|
|||
def rrdreport
|
|||
9c3ffb6b | Ohad Levy | if SETTINGS[:rrd_report_url].nil? or (host=Host.find(params[:id])).last_report.nil?
|
|
b0d3f4ee | Ohad Levy | render :text => "Sorry, no graphs for this host"
|
|
else
|
|||
render :partial => "rrdreport", :locals => { :host => host}
|
|||
end
|
|||
end
|
|||
767bbf03 | Ohad Levy | # shows the last report for a host
|
|
def report
|
|||
# is it safe to assume that the biggest ID is the last report?
|
|||
bc4f0284 | Roberto M | redirect_to :controller => "reports", :action => "show", :id => Report.maximum('id', :conditions => {:host_id => params[:id]})
|
|
767bbf03 | Ohad Levy | end
|
|
cdd8bb6f | Paul Kelly | # shows all reports for a certain host
|
|
767bbf03 | Ohad Levy | def reports
|
|
@host = Host.find(params[:id])
|
|||
459e0feb | Paul Kelly | # set defaults search order - cant use default scope due to bug in AR
|
|
# http://github.com/binarylogic/searchlogic/issues#issue/17
|
|||
params[:search] ||= {}
|
|||
params[:search][:order] ||= "ascend_by_reported_at"
|
|||
@search = Report.search(params[:search])
|
|||
@reports = @search.paginate(:page => params[:page], :conditions => {:host_id => @host}, :include => :host)
|
|||
767bbf03 | Ohad Levy | end
|
|
0e12dbfc | Ohad Levy | def query
|
|
fdc22cbd | Ohad Levy | if @verbose
|
|
@hosts.map! do |host|
|
|||
hash = {}
|
|||
h = Host.find_by_name host
|
|||
hash[host] = h.info
|
|||
facts = {}
|
|||
h.fact_values.each {|fv| facts[fv.fact_name.name] = fv.value}
|
|||
hash[host]["facts"]= facts
|
|||
hash
|
|||
end
|
|||
end
|
|||
0e12dbfc | Ohad Levy | respond_to do |format|
|
|
b09b4515 | Ohad Levy | format.html
|
|
format.yml { render :text => @hosts.to_yaml }
|
|||
0e12dbfc | Ohad Levy | end
|
|
end
|
|||
456d537a | Martin Englund | def facts
|
|
@host = Host.find(params[:id])
|
|||
end
|
|||
5af52b3b | Ohad Levy | def storeconfig_klasses
|
|
@host = Host.find(params[:id])
|
|||
end
|
|||
52538203 | Ohad Levy | # multiple host selection methods
|
|
# present the available actions to the user
|
|||
def multiple_actions
|
|||
end
|
|||
def multiple_parameters
|
|||
@parameters = Parameter.find(:all, :select => "DISTINCT name", :conditions => {:host_id => @hosts })
|
|||
end
|
|||
def reset_multiple
|
|||
session[:selected] = []
|
|||
flash[:foreman_notice] = 'Selection cleared.'
|
|||
redirect_to hosts_path and return
|
|||
end
|
|||
def update_multiple_parameters
|
|||
if params[:name].empty?
|
|||
flash[:foreman_notice] = "No parameters were allocted to the selected hosts, can't mass assign."
|
|||
redirect_to hosts_path and return
|
|||
end
|
|||
@skipped_parameters = {}
|
|||
counter = 0
|
|||
@hosts.each do |host|
|
|||
skipped = []
|
|||
params[:name].each do |name, value|
|
|||
next if value.empty?
|
|||
if host_param = host.host_parameters.find_by_name(name)
|
|||
counter += 1 if host_param.update_attribute(:value, value)
|
|||
else
|
|||
skipped << name
|
|||
end
|
|||
@skipped_parameters[host.name] = skipped unless skipped.empty?
|
|||
end
|
|||
end
|
|||
session[:selected] = []
|
|||
if @skipped_parameters.empty?
|
|||
flash[:foreman_notice] = 'Updated all hosts!'
|
|||
redirect_to(hosts_path) and return
|
|||
else
|
|||
flash[:foreman_notice] = "#{counter} Parameters updated, see below for more information"
|
|||
end
|
|||
end
|
|||
def select_multiple_hostgroup
|
|||
end
|
|||
def update_multiple_hostgroup
|
|||
# simple validations
|
|||
if (id=params["hostgroup"]["id"]).empty?
|
|||
flash[:foreman_error] = 'No Hostgroup selected!'
|
|||
91dcf7dc | Ohad Levy | redirect_to(select_multiple_hostgroup_hosts) and return
|
|
52538203 | Ohad Levy | end
|
|
if (hg = Hostgroup.find id).nil?
|
|||
flash[:foreman_error] = 'Empty Hostgroup selected!'
|
|||
91dcf7dc | Ohad Levy | redirect_to(select_multiple_hostgroup_hosts) and return
|
|
52538203 | Ohad Levy | end
|
|
#update the hosts
|
|||
Host.find(session[:selected]).each do |host|
|
|||
host.hostgroup=hg
|
|||
host.save(perform_validation = false)
|
|||
end
|
|||
session[:selected] = []
|
|||
flash[:foreman_notice] = 'Updated hosts: Changed Hostgroup'
|
|||
redirect_to(hosts_path)
|
|||
end
|
|||
6b728fdc | Ashay Humane | def select_multiple_environment
|
|
end
|
|||
def update_multiple_environment
|
|||
# simple validations
|
|||
if (id=params["environment"]["id"]).empty?
|
|||
flash[:foreman_error] = 'No Environment selected!'
|
|||
redirect_to(select_multiple_environment_hosts_path) and return
|
|||
end
|
|||
if (ev = Environment.find id).nil?
|
|||
flash[:foreman_error] = 'Empty Environment selected!'
|
|||
redirect_to(select_multiple_environment_hosts_path) and return
|
|||
end
|
|||
#update the hosts
|
|||
Host.find(session[:selected]).each do |host|
|
|||
host.environment=ev
|
|||
host.save(perform_validation = false)
|
|||
end
|
|||
session[:selected] = []
|
|||
flash[:foreman_notice] = 'Updated hosts: Changed Environment'
|
|||
redirect_to(hosts_path)
|
|||
end
|
|||
52538203 | Ohad Levy | # AJAX method to update our session each time a host has been selected
|
|
# we are using AJAX and JS as the user might select multiple hosts across different pages (or requests).
|
|||
def save_checkbox
|
|||
return unless request.xhr?
|
|||
session[:selected] ||= []
|
|||
case params[:is_checked]
|
|||
when "true"
|
|||
session[:selected] << params[:box]
|
|||
when "false"
|
|||
session[:selected].delete params[:box]
|
|||
end
|
|||
render :nothing => true
|
|||
end
|
|||
0e12dbfc | Ohad Levy | private
|
|
def find_hosts
|
|||
06160506 | Ohad Levy | fact, klass = params[:fact], params[:class]
|
|
if fact.empty? and klass.empty?
|
|||
0e12dbfc | Ohad Levy | render :text => '404 Not Found', :status => 404 and return
|
|
end
|
|||
b09b4515 | Ohad Levy | ||
06160506 | Ohad Levy | @hosts = []
|
|
5859a64c | Ohad Levy | counter = 0
|
|
06160506 | Ohad Levy | ||
fdc22cbd | Ohad Levy | @verbose = params[:verbose] == "yes"
|
|
72a5a1de | Ohad Levy | case params[:state]
|
|
when "out_of_sync"
|
|||
state = "out_of_sync"
|
|||
when "all"
|
|||
state = "all"
|
|||
when "active", nil
|
|||
state = "recent"
|
|||
else
|
|||
raise invalid_request and return
|
|||
end
|
|||
06160506 | Ohad Levy | # TODO: rewrite this part, my brain stopped working
|
|
# it should be possible for a one join
|
|||
fact.each do |f|
|
|||
# split facts based on name => value pairs
|
|||
5539907c | Ohad Levy | q = f.split("-seperator-")
|
|
06160506 | Ohad Levy | invalid_request unless q.size == 2
|
|
72a5a1de | Ohad Levy | list = Host.with_fact(*q).send(state).map(&:name)
|
|
5859a64c | Ohad Levy | @hosts = counter == 0 ? list : @hosts & list
|
|
counter +=1
|
|||
06160506 | Ohad Levy | end unless fact.nil?
|
|
klass.each do |k|
|
|||
f217c950 | Ohad Levy | list = Host.with_class(k).send(state).map(&:name)
|
|
5859a64c | Ohad Levy | @hosts = counter == 0 ? list : @hosts & list
|
|
counter +=1
|
|||
06160506 | Ohad Levy | end unless klass.nil?
|
|
render :text => '404 Not Found', :status => 404 and return if @hosts.empty?
|
|||
0e12dbfc | Ohad Levy | end
|
|
b09b4515 | Ohad Levy | def ajax_methods
|
|
return head(:method_not_allowed) unless request.xhr?
|
|||
@host = Host.find(params[:id]) unless params[:id].empty?
|
|||
end
|
|||
def assign_parameter name
|
|||
if params["#{name}_id"].to_i > 0 and eval("@#{name} = #{name.capitalize}.find(params['#{name}_id'])")
|
|||
render :partial => name, :locals => {:host => (@host ||= Host.new)}
|
|||
else
|
|||
return head(:not_found)
|
|||
end
|
|||
end
|
|||
dad78fb6 | Paul Kelly | def load_vars_for_ajax
|
|
return unless @host
|
|||
@environment = @host.environment
|
|||
@architecture = @host.architecture
|
|||
@operatingsystem = @host.operatingsystem
|
|||
end
|
|||
52538203 | Ohad Levy | def find_multiple
|
|
if session[:selected].empty?
|
|||
flash[:foreman_error] = 'No Hosts selected'
|
|||
redirect_to(hosts_path)
|
|||
else
|
|||
@hosts = Host.find(session[:selected], :order => "hostgroup_id ASC")
|
|||
end
|
|||
end
|
|||
2f077f63 | Ohad Levy | end
|