Module: HostsHelper

Includes:
BmcHelper, ComputeResourcesVmsHelper, HostsAndHostgroupsHelper, OperatingsystemsHelper
Defined in:
app/helpers/hosts_helper.rb

Instance Method Summary collapse

Methods included from BmcHelper

#boot_actions, #power_actions, #power_status

Methods included from ComputeResourcesVmsHelper

#libvirt_networks, #prop, #spice_data_attributes, #supports_spice_xpi?

Methods included from HostsAndHostgroupsHelper

#accessible_domains, #accessible_hostgroups, #arch_oss, #domain_subnets, #hostgroup_name, #ifs_bmc_opts, #interesting_klasses, #model_name, #os_media, #os_ptable, #parent_classes, #puppet_ca, #puppet_master, #puppet_master_fields

Methods included from OperatingsystemsHelper

#icon, #os_habtm_family, #os_name

Methods included from CommonParametersHelper

#authorized_via_my_scope, #parameter_value_field, #parameters_title

Instance Method Details

#args_for_compute_resource_partial(host) ⇒ Object


271
272
273
274
275
# File 'app/helpers/hosts_helper.rb', line 271

def args_for_compute_resource_partial(host)
  { :arch => host.try(:architecture_id)    || (params[:host] && params[:host][:architecture_id]),
    :os   => host.try(:operatingsystem_id) || (params[:host] && params[:host][:operatingsystem_id])
  }
end

#authorized?Boolean


70
71
72
# File 'app/helpers/hosts_helper.rb', line 70

def authorized?
  authorized_for(:hosts, :edit) or authorized_for(:hosts, :destroy)
end

#conflict_objects(errors) ⇒ Object


260
261
262
# File 'app/helpers/hosts_helper.rb', line 260

def conflict_objects errors
  errors.keys.map(&:to_s).grep(/conflict$/).map(&:to_sym)
end

#date(ts = nil) ⇒ Object


100
101
102
103
# File 'app/helpers/hosts_helper.rb', line 100

def date ts=nil
  return _("%s ago") % (time_ago_in_words ts) if ts
  _("N/A")
end

#days_ago(time) ⇒ Object


66
67
68
# File 'app/helpers/hosts_helper.rb', line 66

def days_ago time
  ((Time.now - time) / 1.day).round.to_i
end

#has_conflicts?(errors) ⇒ Boolean


264
265
266
267
268
269
# File 'app/helpers/hosts_helper.rb', line 264

def has_conflicts? errors
  conflict_objects(errors).each do |c|
    return true if errors[c.to_sym].any?
  end
  false
end

#host_title_actions(host, vm) ⇒ Object


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/helpers/hosts_helper.rb', line 222

def host_title_actions(host, vm)
  title_actions(
      button_group(
          link_to_if_authorized(_("Edit"), hash_for_edit_host_path(:id => host), :title => _("Edit your host")),
          if host.build
            link_to_if_authorized(_("Cancel Build"), hash_for_cancelBuild_host_path(:id => host), :disabled => host.can_be_built?,
                                  :title                                                                 => _("Cancel build request for this host"))
          else
            link_to_if_authorized(_("Build"), hash_for_setBuild_host_path(:id => host), :disabled => !host.can_be_built?,
                                  :title                                                       => _("Enable rebuild on next host boot"),
                                  :confirm                                                     => _("Rebuild %s on next reboot?\nThis would also delete all of its current facts and reports") % host)
          end
      ),
      if host.compute_resource_id
        button_group(
            if vm
              html_opts = vm.ready? ? {:confirm => _('Are you sure?'), :class => "btn btn-danger"} : {:class => "btn btn-success"}
              link_to_if_authorized _("Power%s") % state(vm.ready?), hash_for_power_host_path(:power_action => vm.ready? ? :stop : :start), html_opts.merge(:method => :put)
            else
              link_to(_("Unknown Power State"), '#', :disabled => true, :class => "btn btn-warning")
            end +
                link_to_if_authorized(_("Console"), hash_for_console_host_path(), {:disabled => vm.nil? || !vm.ready?, :class => "btn btn-info"})
        )
      end,
      button_group(
        if host.try(:puppet_proxy)
          link_to_if_authorized(_("Run puppet"), hash_for_puppetrun_host_path(:id => host).merge(:auth_action => :edit),
                                :disabled => !Setting[:puppetrun],
                                :title => _("Trigger a puppetrun on a node; requires that puppet run is enabled"))
        end
      ),
      button_group(
          link_to_if_authorized(_("Delete"), hash_for_host_path(:id => host, :auth_action => :destroy),
                                :class => "btn btn-danger", :confirm => _('Are you sure?'), :method => :delete)
      )
  )
end

#last_report_column(record) ⇒ Object


7
8
9
10
11
12
# File 'app/helpers/hosts_helper.rb', line 7

def last_report_column(record)
  time = record.last_report? ? _("%s ago") % time_ago_in_words(record.last_report.getlocal): ""
  link_to_if_authorized(time,
                        hash_for_host_report_path(:host_id => record.to_param, :id => "last"),
                        last_report_tooltip(record))
end

#last_report_tooltip(record) ⇒ Object


14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/hosts_helper.rb', line 14

def last_report_tooltip record
  opts = { :rel => "twipsy" }
  if @last_reports[record.id]
    opts.merge!( "data-original-title" => _("View last report details"))
  else
    opts.merge!(:disabled => true, :class => "disabled", :onclick => 'return false')
    opts.merge!("data-original-title" => _("Report Already Deleted")) unless record.last_report.nil?
  end
  opts
end

#multiple_actions_selectObject


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/hosts_helper.rb', line 78

def multiple_actions_select
  actions = [
    [_('Change Group'), select_multiple_hostgroup_hosts_path, 'pencil'],
    [_('Change Environment'), select_multiple_environment_hosts_path, 'chevron-right'],
    [_('Edit Parameters'), multiple_parameters_hosts_path, 'edit'],
    [_('Delete Hosts'), multiple_destroy_hosts_path, 'trash'],
    [_('Disable Notifications'), multiple_disable_hosts_path, 'eye-close'],
    [_('Enable Notifications'), multiple_enable_hosts_path, 'bullhorn'],
  ]
  actions.insert(1, [_('Build Hosts'), multiple_build_hosts_path, 'fast-forward']) if SETTINGS[:unattended]
  actions <<  [_('Run Puppet'), multiple_puppetrun_hosts_path, 'play'] if Setting[:puppetrun]
  actions <<  [_('Assign Organization'), select_multiple_organization_hosts_path, 'tags'] if SETTINGS[:organizations_enabled]
  actions <<  [_('Assign Location'), select_multiple_location_hosts_path, 'map-marker'] if SETTINGS[:locations_enabled]

   :span, :id => 'submit_multiple' do
    select_action_button( _("Select Action"), actions.map do |action|
      link_to(icon_text(action[2], action[0]) , action[1], :class=>'btn',  :title => _("%s - The following hosts are about to be changed") % action[0])
    end.flatten)
  end

end

#name_column(record) ⇒ Object

method that reformat the hostname column by adding the status icons


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
# File 'app/helpers/hosts_helper.rb', line 26

def name_column(record)
  label = record.host_status
  case label
  when "Pending Installation"
    style ="label-info"
    # TRANSLATORS: host's status: first character of "build"
    short = s_("Build|B")
  when "Alerts disabled"
    style = ""
    # TRANSLATORS: host's status: first character of "disabled"
    short = s_("Disabled|D")
  when "No reports"
    style = ""
    # TRANSLATORS: host's status: first character of "no reports"
    short = s_("No reports|N")
  when "Out of sync"
    style = "label-warning"
    # TRANSLATORS: host's status: first character of "sync" (out of sync)
    short = s_("Sync|S")
  when "Error"
    style = "label-important"
    # TRANSLATORS: host's status: first character of "error"
    short = s_("Error|E")
  when "Active"
    style = "label-info"
    # TRANSLATORS: host's status: first character of "active"
    short = s_("Active|A")
  when "Pending"
    style = "label-warning"
    # TRANSLATORS: host's status: first character of "pending"
    short = s_("Pending|P")
  else
    style = "label-success"
    # TRANSLATORS: host's status: first character of "OK"
    short = s_("OK|O")
  end
  (:span, short, {:rel => "twipsy", :class => "label label-light " + style, :"data-original-title" => _(label)} ) +
    link_to(trunc("  #{record}",32), host_path(record))
end

#name_field(host) ⇒ Object


153
154
155
156
# File 'app/helpers/hosts_helper.rb', line 153

def name_field host
  return if host.name.blank?
  (SETTINGS[:unattended] and host.managed?) ? host.shortname : host.name
end

#overview_fields(host) ⇒ Object


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/helpers/hosts_helper.rb', line 189

def overview_fields host
  fields = [
    [_("Domain"), (link_to(host.domain, hosts_path(:search => "domain = #{host.domain}")) if host.domain)],
    [_("IP Address"), host.ip],
    [_("MAC Address"), host.mac],
    [_("Puppet Environment"), (link_to(host.environment, hosts_path(:search => "environment = #{host.environment}")) if host.environment)],
    [_("Host Architecture"), (link_to(host.arch, hosts_path(:search => "architecture = #{host.arch}")) if host.arch)],
    [_("Operating System"), (link_to(host.os, hosts_path(:search => "os = #{host.os.name}")) if host.os)],
    [_("Host group"), (link_to(host.hostgroup, hosts_path(:search => "hostgroup = #{host.hostgroup}")) if host.hostgroup)],
  ]
  fields += [[_("Location"), (link_to(host.location.name, hosts_path(:search => "location = #{host.location}")) if host.location)]] if SETTINGS[:locations_enabled]
  fields += [[_("Organization"), (link_to(host.organization.name, hosts_path(:search => "organization = #{host.organization}")) if host.organization)]] if SETTINGS[:organizations_enabled]
  if SETTINGS[:login]
    if host.owner_type == _("User")
      fields += [[_("Owner"), (link_to(host.owner, hosts_path(:search => "user.login = #{host.owner.login}")) if host.owner)]]
    else
      fields += [[_("Owner"), host.owner]]
    end
  end
  fields += [[_("Certificate Name"), host.certname]] if Setting[:use_uuid_for_certificates]
  fields
end

#possible_images(cr, arch = nil, os = nil) ⇒ Object


212
213
214
215
216
# File 'app/helpers/hosts_helper.rb', line 212

def possible_images cr, arch = nil, os = nil
  return cr.images unless controller_name == "hosts"
  return [] unless arch && os
  cr.images.where(:architecture_id => arch, :operatingsystem_id => os)
end

#reports_showObject


143
144
145
146
147
148
149
150
151
# File 'app/helpers/hosts_helper.rb', line 143

def reports_show
  return unless @host.reports.size > 0
  form_tag @host, :id => 'days_filter', :method => :get, :class=>"form form-inline" do
    (:span, (_("Reports from the last %{days} days - %{count} reports found") %
      { :days  => select(nil, 'range', 1..days_ago(@host.reports.first.reported_at),
                  {:selected => @range}, {:class=>"span1", :onchange =>"$('#days_filter').submit();$(this).disabled();"}),
        :count => @host.reports.recent(@range.days.ago).count }).html_safe)
  end
end

#resources_chart(timerange = 1.day.ago) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/hosts_helper.rb', line 118

def resources_chart(timerange = 1.day.ago)
  applied, failed, restarted, failed_restarts, skipped = [],[],[],[],[]
  @host.reports.recent(timerange).each do |r|
    applied         << [r.reported_at.to_i*1000, r.applied ]
    failed          << [r.reported_at.to_i*1000, r.failed ]
    restarted       << [r.reported_at.to_i*1000, r.restarted ]
    failed_restarts << [r.reported_at.to_i*1000, r.failed_restarts ]
    skipped         << [r.reported_at.to_i*1000, r.skipped ]
  end
  [{:label=>_("Applied"), :data=>applied,:color =>'#89A54E'},
   {:label=>_("Failed"), :data=>failed,:color =>'#AA4643'},
   {:label=>_("Failed restarts"), :data=>failed_restarts,:color =>'#AA4643'},
   {:label=>_("Skipped"), :data=>skipped,:color =>'#80699B'},
   {:label=>_("Restarted"), :data=>restarted,:color =>'#4572A7'}]
end

#runtime_chart(timerange = 1.day.ago) ⇒ Object


134
135
136
137
138
139
140
141
# File 'app/helpers/hosts_helper.rb', line 134

def runtime_chart(timerange = 1.day.ago)
  config, runtime = [], []
  @host.reports.recent(timerange).each do |r|
    config  << [r.reported_at.to_i*1000, r.config_retrieval]
    runtime << [r.reported_at.to_i*1000, r.runtime]
  end
  [{:label=>_("Config Retrieval"), :data=> config, :color=>'#AA4643'},{:label=>_("Runtime"), :data=> runtime,:color=>'#4572A7'}]
end

#searching?Boolean


74
75
76
# File 'app/helpers/hosts_helper.rb', line 74

def searching?
  params[:search].empty?
end

#selected?(host) ⇒ Boolean


113
114
115
116
# File 'app/helpers/hosts_helper.rb', line 113

def selected? host
  return false if host.nil? or not host.kind_of?(Host::Base) or session[:selected].nil?
  session[:selected].include?(host.id.to_s)
end

#show_appropriate_host_buttons(host) ⇒ Object


277
278
279
280
281
282
283
# File 'app/helpers/hosts_helper.rb', line 277

def show_appropriate_host_buttons(host)
  [ link_to_if_authorized(_("Audits"), hash_for_host_audits_path(:host_id => @host), :title => _("Host audit entries") , :class => 'btn'),
    (link_to_if_authorized(_("Facts"), hash_for_host_facts_path(:host_id => host), :title => _("Browse host facts") , :class => 'btn') if host.facts_hash.present?),
    (link_to_if_authorized(_("Reports"), hash_for_host_reports_path(:host_id => host), :title => _("Browse host reports") , :class => 'btn') if host.reports.present?),
    (link_to(_("YAML"), externalNodes_host_path(:name => host), :title => _("Puppet external nodes YAML dump") , :class => 'btn') if SmartProxy.puppet_proxies.present?)
  ].compact
end

#show_templatesObject


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
185
186
187
# File 'app/helpers/hosts_helper.rb', line 158

def show_templates
  unless SETTINGS[:unattended] and @host.managed?
    return (:div, :class =>"alert") do
      _("Provisioning Support is disabled or this host is not managed")
    end
  end
  begin
    templates = TemplateKind.all.map{|k| @host.configTemplate(:kind => k.name)}.compact
  rescue => e
    return case e.to_s
    when "Must provide an operating systems"
      _("Unable to find templates as this host has no operating system")
    else
      e.to_s
    end
  end

  return _("No template found") if templates.empty?
   :table, :class=>"table table-bordered table-striped" do
    (:th, _("Template Type")) + (:th) +
    templates.sort{|t,x| t.template_kind <=> x.template_kind}.map do |tmplt|
       :tr do
        (:td, _("%s Template") % tmplt.template_kind) +
          (:td,
        link_to_if_authorized(icon_text('pencil'), hash_for_edit_config_template_path(:id => tmplt.to_param), :title => _("Edit"), :rel=>"external") +
        link_to(icon_text('eye-open'), url_for(:controller => '/unattended', :action => tmplt.template_kind.name, :spoof => @host.ip), :title => _("Review"), :"data-provisioning-template" => true ))
      end
    end.join(" ").html_safe
  end
end

#state(s) ⇒ Object


218
219
220
# File 'app/helpers/hosts_helper.rb', line 218

def state s
  s ? ' ' + _("Off") : ' ' + _("On")
end

#template_path(opts = {}) ⇒ Object


105
106
107
108
109
110
111
# File 'app/helpers/hosts_helper.rb', line 105

def template_path opts = {}
  if (t = @host.configTemplate(opts))
    link_to t, edit_config_template_path(t)
  else
    _("N/A")
  end
end