Module: HostsHelper

Constant Summary

Constants included from HostsAndHostgroupsHelper

HostsAndHostgroupsHelper::INHERIT_TEXT

Instance Method Summary collapse

Methods included from AuthorizeHelper

#authorized_for, #authorizer, #can_create?, #can_edit?

Methods included from BmcHelper

#boot_actions, #power_actions, #power_status

Methods included from HostsNicHelper

#nic_subnet_field, #suggest_new_link

Methods included from ComputeResourcesVmsHelper

#available_actions, #common_available_actions, #default_available_actions, #libvirt_networks, #new_vm?, #openstack_available_actions, #ovirt_fake_vms_count, #ovirt_storage_domains_for_select, #ovirt_vms_data, #prop, #security_group_not_selected, #security_groups_for_vpc, #security_groups_selectable, #show_vm_name?, #spice_data_attributes, #subnet_vpc_hash, #supports_spice_xpi?, #vm_associate_action, #vm_console, #vm_console_action, #vm_delete_action, #vm_host_action, #vm_import_action, #vm_power_actions, #vms_table, #vmware_vm_hypervisor_name, #vpc_security_group_hash, #vsphere_networks, #vsphere_resource_pools, #vsphere_scsi_controllers

Methods included from HostsAndHostgroupsHelper

#arch_oss, #domain_subnets, #multiple_filter, #os_media, #os_ptable, #realm_field, #visible_compute_profiles

Methods included from AncestryHelper

#label_with_link

Methods included from OperatingsystemsHelper

#icon, #label, #os_default_templates_for_form, #os_habtm_family, #os_image_tag, #os_name

Methods included from CommonParametersHelper

#authorized_resource_parameters, #authorized_via_my_scope, #can_create_parameter?, #find_parameters_to_view, #hidden_value_field, #lookup_key_field, #omit_help, #omit_help_body, #omit_help_title, #parameter_value_content, #parameter_value_field, #parameters_title

Instance Method Details

#allocation_text_f(f) ⇒ Object


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'app/helpers/hosts_helper.rb', line 324

def allocation_text_f(f)
  active = 'Size'
  active = 'None' if f.object.allocation.to_i == 0
  active = 'Full' if f.object.allocation == f.object.capacity
  text_f f, :allocation, :class => "input-mini", :label => _("Allocation (GB)"), :label_size => "col-md-2",
  :readonly => (active == 'Size') ? false : true,
  :help_inline => ((:span, :class => 'btn-group', :'data-toggle' => 'buttons-radio') do
    [N_('None'), N_('Size'), N_('Full')].collect do |label|
       :button, _(label), :type => 'button', :href => '#',
        :name => 'allocation_radio_btn',
        :class => (label == active) ? 'btn btn-default active' : 'btn btn-default',
        :onclick => "tfm.computeResource.libvirt.allocationSwitcher(this, '#{label}');",
        :data => { :toggle => 'button' },
        :id => (label == 'Full') ? 'btnAllocationFull' : nil
    end.join(' ').html_safe
  end)
end

#args_for_compute_resource_partial(host) ⇒ Object


307
308
309
310
311
312
313
# File 'app/helpers/hosts_helper.rb', line 307

def args_for_compute_resource_partial(host)
  args = {}
  args[:arch] = host.try(:architecture_id) || (params[:host] && params[:host][:architecture_id])
  args[:os] = host.try(:operatingsystem_id) || (params[:host] && params[:host][:operatingsystem_id])
  args[:selected_cluster] = vm_attrs['cluster'] if defined?(vm_attrs)
  args
end

#build_duration(host) ⇒ Object


246
247
248
249
# File 'app/helpers/hosts_helper.rb', line 246

def build_duration(host)
  return _('N/A') if host.initiated_at.nil? || host.installed_at.nil?
  distance_of_time_in_words(host.initiated_at, host.installed_at, include_seconds: true)
end

389
390
391
392
393
394
395
# File 'app/helpers/hosts_helper.rb', line 389

def build_error_link(type, id)
  case type
    when :templates
      link_to_if_authorized(_("Edit"), hash_for_edit_provisioning_template_path(:id => id).merge(:auth_object => id),
        :class => "btn btn-default btn-xs pull-right", :title => _("Edit %s" % type))
  end
end

#build_state(build) ⇒ Object


378
379
380
# File 'app/helpers/hosts_helper.rb', line 378

def build_state(build)
  build.state ? 'warning' : 'danger'
end

#compute_specific_js(compute_resource, js_name) ⇒ Object


39
40
41
# File 'app/helpers/hosts_helper.rb', line 39

def compute_specific_js(compute_resource, js_name)
  javascript_include_tag("compute_resources/#{compute_resource.provider.downcase}/#{js_name}.js")
end

#conflict_objects(errors) ⇒ Object

we ignore interfaces.conflict because they are always registered in host errors as well


292
293
294
# File 'app/helpers/hosts_helper.rb', line 292

def conflict_objects(errors)
  errors.keys.map(&:to_s).select { |key| key =~ /conflict$/ && key != 'interfaces.conflict' }.map(&:to_sym)
end

#date(ts = nil) ⇒ Object


185
186
187
188
# File 'app/helpers/hosts_helper.rb', line 185

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

#days_ago(time) ⇒ Object


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

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

#delete_host_dialog(host) ⇒ Object


279
280
281
282
283
284
285
286
287
288
289
# File 'app/helpers/hosts_helper.rb', line 279

def delete_host_dialog(host)
  if host.compute?
    if Setting[:destroy_vm_on_host_delete]
      _("Are you sure you want to delete host %s? This will delete the VM and its disks, and is irreversible. This behavior can be changed via global setting \"Destroy associated VM on host delete\".") % host.name
    else
      _("Are you sure you want to delete host %s? It is irreversible, but VM and its disks will not be deleted. This behavior can be changed via global setting \"Destroy associated VM on host delete\".") % host.name
    end
  else
    _("Are you sure you want to delete host %s? This action is irreversible.") % host.name
  end
end

#flags_for_nic(nic) ⇒ Object


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

def flags_for_nic(nic)
  flags = ""
  flags += "<i class=\"nic-flag glyphicon glyphicon glyphicon-tag\" title=\"#{_('Primary')}\"></i>" if nic.primary?
  flags += "<i class=\"nic-flag glyphicon glyphicon glyphicon-hdd\" title=\"#{_('Provisioning')}\"></i>" if nic.provision?
  flags.html_safe
end

#has_conflicts?(errors) ⇒ Boolean

Returns:

  • (Boolean)

296
297
298
299
300
301
# File 'app/helpers/hosts_helper.rb', line 296

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

#has_dhcp_lease_errors?(errors) ⇒ Boolean

Returns:

  • (Boolean)

303
304
305
# File 'app/helpers/hosts_helper.rb', line 303

def has_dhcp_lease_errors?(errors)
  errors.include?(:dhcp_lease_error)
end

#host_breadcrumbObject


425
426
427
# File 'app/helpers/hosts_helper.rb', line 425

def host_breadcrumb
  breadcrumbs(resource_url: "/api/v2/hosts?thin=true'")
end

#host_global_status_class(status) ⇒ Object


125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/hosts_helper.rb', line 125

def host_global_status_class(status)
  case status
    when HostStatus::Global::OK
      'status-ok'
    when HostStatus::Global::WARN
      'status-warn'
    when HostStatus::Global::ERROR
      'status-error'
    else
      'status-question'
  end
end

#host_global_status_icon_class(status) ⇒ Object


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/helpers/hosts_helper.rb', line 110

def host_global_status_icon_class(status)
  icon_class = case status
               when HostStatus::Global::OK
                 'pficon-ok'
               when HostStatus::Global::WARN
                 'pficon-info'
               when HostStatus::Global::ERROR
                 'pficon-error-circle-o'
               else
                 'pficon-help'
               end

  "host-status #{icon_class} #{host_global_status_class(status)}"
end

#host_global_status_icon_class_for_host(host) ⇒ Object


104
105
106
107
108
# File 'app/helpers/hosts_helper.rb', line 104

def host_global_status_icon_class_for_host(host)
  options = {}
  options[:last_reports] = @last_reports unless @last_reports.nil?
  host_global_status_icon_class(host.build_global_status(options).status)
end

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

def host_global_status_link_class(status)
  if status.status_link
    case status.to_global
    when HostStatus::Global::OK
      'status-ok-link'
    when HostStatus::Global::WARN
      'status-warn-link'
    when HostStatus::Global::ERROR
      'status-error-link'
    else
      'status-question-link'
    end
  else
    host_global_status_class(status.to_global)
  end
end

#host_taxonomy_select(f, taxonomy) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/hosts_helper.rb', line 48

def host_taxonomy_select(f, taxonomy)
  taxonomy_id = "#{taxonomy.to_s.downcase}_id"
  selected_taxonomy = @host.new_record? ? taxonomy.current.try(:id) : @host.send(taxonomy_id)
  label = _(taxonomy.to_s)
  select_opts = { :include_blank => !@host.managed? || @host.send(taxonomy_id).nil?,
                  :selected => selected_taxonomy }
  html_opts = { :disabled => !@host.new_record?,
                :class => 'host-taxonomy-select',
                :label => label,
                :'data-host-id' => @host.id,
                :'data-url' => process_taxonomy_hosts_path,
                :help_inline => :indicator,
                :required => true }
  html_opts[:label_help] = (_("%s can be changed using bulk action on the All Hosts page") % taxonomy) unless @host.new_record?

  select_f f, taxonomy_id.to_sym, taxonomy.send("my_#{taxonomy.to_s.downcase.pluralize}"), :id, :to_label,
    select_opts, html_opts
end

#host_title_actions(host) ⇒ Object


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

def host_title_actions(host)
  actions = []
  UI::HostDescription.reduce_providers(:title_actions).each do |provider|
    actions += send(provider, host)
  end
  title_actions(
    prioritized_members(actions, :action)
  )
end

#inherited_by_default?(field, host) ⇒ Boolean

Returns:

  • (Boolean)

397
398
399
400
401
402
# File 'app/helpers/hosts_helper.rb', line 397

def inherited_by_default?(field, host)
  return false unless host.hostgroup && host.hostgroup_id_was.nil?
  return false if params[:action] == 'clone'
  return true unless params[:host]
  !params[:host][field]
end

#interface_flags(nic) ⇒ Object


369
370
371
372
373
374
375
376
# File 'app/helpers/hosts_helper.rb', line 369

def interface_flags(nic)
  primary_class = nic.primary? ? "active" : ""
  provision_class = nic.provision? ? "active" : ""

  status = "<i class=\"glyphicon glyphicon glyphicon-tag primary-flag #{primary_class}\" title=\"#{_('Primary')}\"></i>"
  status += "<i class=\"glyphicon glyphicon glyphicon-hdd provision-flag #{provision_class}\" title=\"#{_('Provisioning')}\"></i>"
  status.html_safe
end

#last_report_column(record) ⇒ Object


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

def last_report_column(record)
  time = record.last_report? ? date_time_relative_value(record.last_report) : ""
  link_to_if_authorized(time,
    hash_for_host_config_report_path(:host_id => record.to_param, :id => "last"),
    last_report_tooltip(record))
end

#last_report_tooltip(record) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/hosts_helper.rb', line 81

def last_report_tooltip(record)
  opts = { :rel => "twipsy" }
  date = record.last_report.nil? ? '' : date_time_absolute_value(record.last_report) + ", "
  if @last_report_ids[record.id]
    opts["data-original-title"] = date + _("view last report details")
  else
    opts.merge!(:disabled => true, :class => "disabled", :onclick => 'return false')
    opts["data-original-title"] = date + _("report already deleted") unless record.last_report.nil?
  end
  opts
end

358
359
360
361
362
363
364
365
366
367
# File 'app/helpers/hosts_helper.rb', line 358

def link_status(nic)
  return '' if nic.new_record?

  if nic.link
    status = '<i class="glyphicon glyphicon glyphicon-arrow-up interface-up" title="' + _('Interface is up') + '"></i>'
  else
    status = '<i class="glyphicon glyphicon glyphicon-arrow-down interface-down" title="' + _('Interface is down') + '"></i>'
  end
  status.html_safe
end

#lookup_values_data(host_or_hostgroup) ⇒ Object


435
436
437
438
439
440
441
442
443
444
# File 'app/helpers/hosts_helper.rb', line 435

def lookup_values_data(host_or_hostgroup)
  host_or_hostgroup.lookup_values.map do |item|
    {
      :id => item.id,
      :lookup_key_id => item.lookup_key_id,
      :value => item.value,
      :errors => item.errors.to_hash,
    }
  end
end

#multiple_actionsObject


163
164
165
166
167
168
169
# File 'app/helpers/hosts_helper.rb', line 163

def multiple_actions
  actions = []
  UI::HostDescription.reduce_providers(:multiple_actions).each do |provider|
    actions += send(provider)
  end
  prioritized_members(actions, :action)
end

#multiple_actions_selectObject


171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/helpers/hosts_helper.rb', line 171

def multiple_actions_select
  select_action_button(_("Select Action"), {:id => 'submit_multiple'},
    multiple_actions.map do |action|
      # If the action array has 3 entries, the third one is whether to use a modal dialog or not
      modal = (action.size == 3) ? action[3] : true
      if modal
        link_to_function(action[0], "tfm.hosts.table.buildModal(this, '#{action[1]}')", :'data-dialog-title' => _("%s - The following hosts are about to be changed") % action[0])
      else
        link_to_function(action[0], "tfm.hosts.table.buildRedirect('#{action[1]}')")
      end
    end.flatten
  )
end

#multiple_proxy_select(form, proxy_feature) ⇒ Object


404
405
406
407
408
409
410
411
412
# File 'app/helpers/hosts_helper.rb', line 404

def multiple_proxy_select(form, proxy_feature)
  selectable_f form,
    :proxy_id,
    [[_("Select desired %s proxy") % _(proxy_feature), "disabled"]] +
    [[_("*Clear %s proxy*") % _(proxy_feature), ""]] +
    SmartProxy.with_features(proxy_feature).map { |p| [p.name, p.id] },
    {},
    {:label => _(proxy_feature), :onchange => "tfm.hosts.table.toggleMultipleOkButton(this)" }
end

#name_column(host) ⇒ Object

method that reformat the hostname column by adding the status icons


94
95
96
97
98
99
100
101
102
# File 'app/helpers/hosts_helper.rb', line 94

def name_column(host)
  style = host_global_status_icon_class_for_host(host)
  displayable_statuses = host.host_statuses.select { |status| status.relevant? && !status.substatus? }
  tooltip = displayable_statuses.sort_by(&:type).map { |status| "#{_(status.name)}: #{_(status.to_label)}" }.join(', ')

  content = (:span, "", {:rel => "twipsy", :class => style, :"data-original-title" => tooltip})
  content += link_to("  #{host}", host_path(host))
  content
end

#name_field(host) ⇒ Object


241
242
243
244
# File 'app/helpers/hosts_helper.rb', line 241

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

#overview_fields(host) ⇒ Object


251
252
253
254
255
256
257
# File 'app/helpers/hosts_helper.rb', line 251

def overview_fields(host)
  fields = []
  UI::HostDescription.reduce_providers(:overview_fields).each do |provider|
    fields += send(provider, host)
  end
  prioritized_members(fields, :field)
end

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


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

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

#power_status_visible?Boolean

Returns:

  • (Boolean)

421
422
423
# File 'app/helpers/hosts_helper.rb', line 421

def power_status_visible?
  SETTINGS[:unattended] && Setting[:host_power_status]
end

#prioritized_members(list, value_key) ⇒ Object


429
430
431
432
433
# File 'app/helpers/hosts_helper.rb', line 429

def prioritized_members(list, value_key)
  list.
    sort_by { |member| member[:priority] }.
    map { |member_hash| member_hash[value_key] }
end

#provider_partial(compute_resource, partial) ⇒ Object


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

def provider_partial(compute_resource, partial)
  return nil unless compute_resource

  compute_resource_name = compute_resource.provider.downcase
  "compute_resources_vms/form/#{compute_resource_name}/#{partial}"
end

#provider_partial_exist?(compute_resource, partial) ⇒ Boolean

Returns:

  • (Boolean)

9
10
11
12
13
14
15
16
# File 'app/helpers/hosts_helper.rb', line 9

def provider_partial_exist?(compute_resource, partial)
  return false unless compute_resource

  compute_resource_name = compute_resource.provider.downcase
  ActionController::Base.view_paths.any? do |path|
    File.exist?(File.join(path, 'compute_resources_vms', 'form', compute_resource_name, "_#{partial}.html.erb"))
  end
end

#provision_method_partial(provision_method, partial) ⇒ Object


33
34
35
36
37
# File 'app/helpers/hosts_helper.rb', line 33

def provision_method_partial(provision_method, partial)
  return nil unless provision_method

  "hosts/provision_method/#{provision_method}/#{partial}"
end

#provision_method_partial_exist?(provision_method, partial) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
28
29
30
31
# File 'app/helpers/hosts_helper.rb', line 25

def provision_method_partial_exist?(provision_method, partial)
  return false unless provision_method

  ActionController::Base.view_paths.any? do |path|
    File.exist?(File.join(path, 'hosts', 'provision_method', provision_method, "_#{partial}.html.erb"))
  end
end

414
415
416
417
418
419
# File 'app/helpers/hosts_helper.rb', line 414

def randomize_mac_link
  if NameGenerator.random_based?
    link_to_function(icon_text('random'), 'randomizeName()', :class => 'btn btn-default',
      :title => _('Generate new random name. Visit Settings to disable this feature.'))
  end
end

351
352
353
354
355
356
# File 'app/helpers/hosts_helper.rb', line 351

def remove_interface_link(f)
  remove_child_link('x', f, {:rel => 'twipsy',
                             :'data-title' => _('remove network interface'),
                             :'data-placement' => 'left',
                             :class => 'fr label label-danger'})
end

#reports_showObject


228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/helpers/hosts_helper.rb', line 228

def reports_show
  return if @host.reports.empty?
  number_of_days = days_ago(@host.reports.order(:reported_at).first.reported_at)
  width = [number_of_days.to_s.size + 2, 4].max

  form_tag @host, :id => 'days_filter', :method => :get, :class => "form form-inline" do
    (:span, (_("Found %{count} reports from the last %{days} days") %
      { :days => select(nil, 'range', 1..number_of_days,
        {:selected => @range}, {:style => "float:none; width: #{width}em;", :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


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/helpers/hosts_helper.rb', line 203

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 => '#EC971F'},
   {:label => _("Skipped"), :data => skipped, :color => '#80699B'},
   {:label => _("Restarted"), :data => restarted, :color => '#4572A7'}]
end

#review_build_button(form, status) ⇒ Object


382
383
384
385
386
387
# File 'app/helpers/hosts_helper.rb', line 382

def review_build_button(form, status)
  form.submit(_("Build"),
    :class => "btn btn-#{status} submit",
    :title => (status == 'warning') ? _('Build') : _('Errors occurred, build may fail')
  )
end

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


219
220
221
222
223
224
225
226
# File 'app/helpers/hosts_helper.rb', line 219

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

Returns:

  • (Boolean)

159
160
161
# File 'app/helpers/hosts_helper.rb', line 159

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

#selected?(host) ⇒ Boolean

Returns:

  • (Boolean)

198
199
200
201
# File 'app/helpers/hosts_helper.rb', line 198

def selected?(host)
  return false if host.nil? || !host.is_a?(Host::Base) || session[:selected].nil?
  session[:selected].include?(host.id.to_s)
end

#show_appropriate_host_buttons(host) ⇒ Object


315
316
317
318
319
320
321
322
# File 'app/helpers/hosts_helper.rb', line 315

def show_appropriate_host_buttons(host)
  priority_buttons = []
  UI::HostDescription.reduce_providers(:overview_buttons).each do |provider|
    priority_buttons += send(provider, host)
  end

  prioritized_members(priority_buttons, :button)
end

#state(s) ⇒ Object


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

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

#subnets_ipam_data(field) ⇒ Object

helper method to provide data attribute if subnets has ipam enabled / disabled


343
344
345
346
347
348
349
# File 'app/helpers/hosts_helper.rb', line 343

def subnets_ipam_data(field)
  data = {}
  domain_subnets(field).each do |subnet|
    data[subnet.id] = { :ipam => subnet.ipam? }
  end
  data
end

#template_path(opts = {}) ⇒ Object


190
191
192
193
194
195
196
# File 'app/helpers/hosts_helper.rb', line 190

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

#value_hash_cache(host) ⇒ Object


43
44
45
46
# File 'app/helpers/hosts_helper.rb', line 43

def value_hash_cache(host)
  @value_hash_cache ||= {}
  @value_hash_cache[host.id] ||= HostInfoProviders::PuppetInfo.new(host).inherited_puppetclass_parameters
end