Module: Katello::HostsAndHostgroupsHelper

Defined in:
app/helpers/katello/hosts_and_hostgroups_helper.rb

Instance Method Summary collapse

Instance Method Details

#accessible_content_proxies(obj) ⇒ Object



131
132
133
134
135
136
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 131

def accessible_content_proxies(obj)
  list = accessible_resource_records(:smart_proxy).with_content.order(:name).to_a
  current = obj.content_source
  list |= [current] if current.present?
  list
end

#accessible_lifecycle_environments(org, host_or_hostgroup) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 120

def accessible_lifecycle_environments(org, host_or_hostgroup)
  selected = if host_or_hostgroup.is_a?(::Host::Managed)
               host_or_hostgroup.try(:single_lifecycle_environment)
             else
               host_or_hostgroup.lifecycle_environment
             end
  envs = org.kt_environments.readable.order(:name)
  envs |= [selected] if selected.present? && org == selected.organization
  envs
end

#blank_or_inherit_with_id(f, attr) ⇒ Object



39
40
41
42
43
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 39

def blank_or_inherit_with_id(f, attr)
  return true unless f.object.respond_to?(:parent_id) && f.object.parent_id
  inherited_value = f.object.send(attr).try(:id) || ''
  %(<option data-id="#{inherited_value}" value="">#{blank_or_inherit_f(f, attr)}</option>)
end

#content_host_overview_button(host) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 310

def content_host_overview_button(host)
  return [] unless host.content_facet || host.subscription_facet
  [{
    :button => link_to(
      _('Content'),
      "/content_hosts/#{host.id}",
      :title => _("Host content and subscription details"),
      :class => 'btn btn-default'),
    :priority => 900,
  }]
end

#content_options(host, selected_id, object_type, options = {}) ⇒ Object

Generic method to provide a list of options in the UI



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 150

def content_options(host, selected_id, object_type, options = {})
  include_blank = options.fetch(:include_blank, nil)
  include_blank = '<option></option>' if include_blank == true #check for true specifically
  orgs = relevant_organizations(host)
  all_options = []
  orgs.each do |org|
    content_object_options = ""
    accessible_content_objects = case object_type
                                 when :lifecycle_environment
                                   accessible_lifecycle_environments(org, host)
                                 when :content_source
                                   accessible_content_proxies(host)
                                 end
    accessible_content_objects.each do |content_object|
      selected = selected_id == content_object.id ? 'selected' : ''
      content_object_options << %(<option value="#{content_object.id}" class="kt-env" #{selected}>#{h(content_object.name)}</option>)
    end

    if orgs.count > 1
      all_options << %(<optgroup label="#{org.name}">#{content_object_options}</optgroup>)
    else
      all_options << content_object_options
    end
  end

  all_options = all_options.join
  all_options.insert(0, include_blank) if include_blank
  all_options.html_safe
end

#content_source_inherited?(host) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 15

def content_source_inherited?(host)
  !using_hostgroups_page? && host&.content_source.blank? && host&.hostgroup&.content_source.present? && cv_lce_disabled?
end

#content_source_options(host, options = {}) ⇒ Object



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

def content_source_options(host, options = {})
  content_options(
    host,
    fetch_content_source(host, options).try(:id),
    :content_source,
    options
  )
end

#content_view_inherited?(host) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 23

def content_view_inherited?(host)
  !using_hostgroups_page? && host&.content_views&.empty? && host&.hostgroup&.content_view.present? && cv_lce_disabled?
end

#content_views_for_host(host, options) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 198

def content_views_for_host(host, options)
  include_blank = options.fetch(:include_blank, nil)
  if include_blank == true #check for true specifically
    include_blank = '<option></option>'
  end
  lifecycle_environment = fetch_lifecycle_environment(host, options)
  content_view = fetch_content_view(host, options)

  views = []
  if lifecycle_environment
    views = Katello::ContentView.in_environment(lifecycle_environment).ignore_generated.readable.order(:name)
    views |= [content_view] if content_view.present? && content_view.in_environment?(lifecycle_environment)
  elsif content_view
    views = [content_view]
  end
  view_options = views.map do |view|
    selected = content_view.try(:id) == view.id ? 'selected' : ''
    %(<option #{selected} value="#{view.id}">#{h(view.name)}</option>)
  end
  view_options = view_options.join
  view_options.insert(0, include_blank) if include_blank
  view_options.html_safe
end

#cv_lce_disabled?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 11

def cv_lce_disabled?
  edit_action? && !using_discovered_hosts_page?
end

#edit_action?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 7

def edit_action?
  params[:action] == 'edit'
end

#errata_counts(host) ⇒ Object



340
341
342
343
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 340

def errata_counts(host)
  counts = host.content_facet_attributes&.errata_counts || {}
  render partial: 'katello/hosts/errata_counts', locals: { counts: counts, host: host }
end

#fetch_content_source(host_or_hostgroup, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 110

def fetch_content_source(host_or_hostgroup, options = {})
  return host_or_hostgroup.content_source if host_or_hostgroup.content_source_id&.present? && host_or_hostgroup.persisted?
  if host_or_hostgroup.is_a?(::Hostgroup) && host_or_hostgroup.content_facet.present?
    # to handle cloned hostgroups that are new records
    return host_or_hostgroup.content_facet.content_source
  end
  selected_host_group = options.fetch(:selected_host_group, nil)
  return selected_host_group.content_source if selected_host_group.present?
end

#fetch_content_view(host_or_hostgroup, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 99

def fetch_content_view(host_or_hostgroup, options = {})
  return host_or_hostgroup.single_content_view if host_or_hostgroup.try(:single_content_view)
  return host_or_hostgroup.content_view if host_or_hostgroup.try(:content_view)
  if host_or_hostgroup.is_a?(::Hostgroup) && host_or_hostgroup.content_facet.present?
    # to handle cloned hostgroups that are new records
    return host_or_hostgroup.content_facet.content_view
  end
  selected_host_group = options.fetch(:selected_host_group, nil)
  return selected_host_group.content_view if selected_host_group.present?
end

#fetch_inherited_param(id, entity, parent_value) ⇒ Object



273
274
275
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 273

def fetch_inherited_param(id, entity, parent_value)
  id.blank? ? parent_value : entity.find(id)
end

#fetch_lifecycle_environment(host_or_hostgroup, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 88

def fetch_lifecycle_environment(host_or_hostgroup, options = {})
  return host_or_hostgroup.single_lifecycle_environment if host_or_hostgroup.try(:single_lifecycle_environment)
  return host_or_hostgroup.lifecycle_environment if host_or_hostgroup.try(:lifecycle_environment)
  if host_or_hostgroup.is_a?(::Hostgroup) && host_or_hostgroup.content_facet.present?
    # to handle cloned hostgroups that are new records
    return host_or_hostgroup.content_facet.lifecycle_environment
  end
  selected_host_group = options.fetch(:selected_host_group, nil)
  return selected_host_group.lifecycle_environment if selected_host_group.present?
end

#host_checkin_time(host) ⇒ Object



351
352
353
354
355
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 351

def host_checkin_time(host)
  return ''.html_safe unless host.subscription_facet_attributes&.last_checkin

  date_time_relative_value(host.subscription_facet_attributes.last_checkin)
end

#host_hostgroup_kickstart_repository_id(host) ⇒ Object



58
59
60
61
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 58

def host_hostgroup_kickstart_repository_id(host)
  return if host.blank?
  host.content_facet&.kickstart_repository_id
end

#host_registered_time(host) ⇒ Object



345
346
347
348
349
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 345

def host_registered_time(host)
  return ''.html_safe unless host.subscription_facet_attributes&.registered_at

  date_time_relative_value(host.subscription_facet_attributes.registered_at)
end

#host_status_icon(status) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 326

def host_status_icon(status)
  colours = [:green, :yellow, :red]

  colour = colours[status] || :red

  icons = {
    green: "#{colour} host-status pficon pficon-ok status-ok",
    yellow: "#{colour} host-status pficon pficon-info status-warn",
    red: "#{colour} host-status pficon pficon-error-circle-o status-error",
  }

  "<span class=\"#{icons[colour]}\"></span>".html_safe
end

#hosts_change_content_sourceObject



322
323
324
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 322

def hosts_change_content_source
  [{ action: [_('Change Content Source'), '/change_host_content_source', false], priority: 100 }]
end

#kickstart_repo_inheritable?(host) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 27

def kickstart_repo_inheritable?(host)
  host&.kickstart_repository_id.blank?
end

#kickstart_repository_id(host, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 63

def kickstart_repository_id(host, options = {})
  host_ks_repo_id = host_hostgroup_kickstart_repository_id(host)
  ks_repo_options = kickstart_repository_options(host, options)
  # if the kickstart repo id is set in the selected_hostgroup use that
  selected_host_group = options.fetch(:selected_host_group, nil)
  if selected_host_group.try(:kickstart_repository_id).present?
    ks_repo_ids = ks_repo_options.map(&:id)

    if ks_repo_ids.include?(selected_host_group.kickstart_repository_id)
      return selected_host_group.kickstart_repository_id
    elsif host_ks_repo_id && ks_repo_ids.include?(host_ks_repo_id)
      return host_ks_repo_id
    else
      return ks_repo_options.first.try(:id)
    end
  end

  # if the kickstart repo id is set in the host use that
  return host_ks_repo_id if host_ks_repo_id.present?

  if selected_host_group.try(:medium_id).blank? && host.try(:medium_id).blank?
    ks_repo_options.first.try(:id)
  end
end

#kickstart_repository_options(param_host, options = {}) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 235

def kickstart_repository_options(param_host, options = {})
  # this method gets called in 2 places
  # 1) On initial page load or a host group selection. At that point the host object is already
  #  =>  populated and we should just use that.
  # 2) Once you chose a diff os/content source/arch/lifecycle env/cv via the os_selected method.
  #   In case 2 we want it to play by the rules of "one of these params" and
  #   in case 1 we want it to behave as if everything is already set right and
  # We need to figure out the available KS repos in both cases.
  if param_host.present?
    # case 1
    selected_host_group = options.fetch(:selected_host_group, nil)
    host = selected_host_group.present? ? selected_host_group : param_host

    new_host = ::Host.new
    new_host.operatingsystem = param_host.operatingsystem.present? ? param_host.operatingsystem : host.operatingsystem
    new_host.architecture = param_host.architecture.present? ? param_host.architecture : host.architecture

    return [] unless new_host.operatingsystem.is_a?(Redhat)

    if (host.is_a? ::Hostgroup)
      new_host.content_facet = hostgroup_content_facet(host, param_host)
    elsif host.content_facet.present?
      new_host.content_facet = ::Katello::Host::ContentFacet.new(:content_source_id => host.content_source_id)
      if host.single_content_view_environment?
        # assign new_host the same CVE as host
        new_host.content_facet.assign_single_environment(
          :lifecycle_environment => host.content_facet.single_lifecycle_environment,
          :content_view => host.content_facet.single_content_view
        )
      end
    end
    new_host.operatingsystem.kickstart_repos(new_host).map { |repo| OpenStruct.new(repo) }
  else
    # case 2
    os_updated_kickstart_options
  end
end

#kt_ak_labelObject



3
4
5
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 3

def kt_ak_label
  "kt_activation_keys"
end

#lifecycle_environment_inherited?(host) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 19

def lifecycle_environment_inherited?(host)
  !using_hostgroups_page? && host&.lifecycle_environments&.empty? && host&.hostgroup&.lifecycle_environment.present? && cv_lce_disabled?
end

#lifecycle_environment_options(host, options = {}) ⇒ Object



180
181
182
183
184
185
186
187
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 180

def lifecycle_environment_options(host, options = {})
  content_options(
    host,
    fetch_lifecycle_environment(host, options).try(:id),
    :lifecycle_environment,
    options
  )
end

#organizations(host) ⇒ Object



45
46
47
48
49
50
51
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 45

def organizations(host)
  if host.is_a?(::Hostgroup)
    host.organizations
  else
    host.organization ? [host.organization] : []
  end
end

#os_updated_kickstart_options(host = nil) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 277

def os_updated_kickstart_options(host = nil)
  # this method gets called in 1 place Once you chose a diff os/content source/arch/lifecycle env/cv
  # via the os_selected method.
  # In this case we want it play by the rules of "one of these params" and
  # need to figure out the available KS repos for the given params.
  os_selection_params = ["operatingsystem_id", 'content_view_id', 'lifecycle_environment_id',
                         'content_source_id', 'architecture_id']
  view_options = []

  host_params = params[:hostgroup] || params[:host]
  parent = ::Hostgroup.find(host_params[:parent_id]) unless host_params.blank? || host_params[:parent_id].blank?
  if host_params && (parent || os_selection_params.all? { |key| host_params[key].present? })
    if host.nil?
      host = ::Host.new
    end
    host.operatingsystem = fetch_inherited_param(host_params[:operatingsystem_id], ::Operatingsystem, parent&.os)
    host.architecture = fetch_inherited_param(host_params[:architecture_id], ::Architecture, parent&.architecture)
    lifecycle_env = fetch_inherited_param(host_params[:lifecycle_environment_id], ::Katello::KTEnvironment, parent&.lifecycle_environment)
    content_view = fetch_inherited_param(host_params[:content_view_id], ::Katello::ContentView, parent&.content_view)
    content_source = fetch_inherited_param(host_params[:content_source_id], ::SmartProxy, parent&.content_source)

    host.content_facet = Host::ContentFacet.new(:content_source => content_source)
    host.content_facet.assign_single_environment(
      :lifecycle_environment_id => lifecycle_env.id,
      :content_view_id => content_view.id
    )
    if host.operatingsystem.is_a?(Redhat)
      view_options = host.operatingsystem.kickstart_repos(host).map { |repo| OpenStruct.new(repo) }
    end
  end
  view_options
end

#relevant_organizations(host) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 138

def relevant_organizations(host)
  host_orgs = organizations(host)
  if Organization.current
    [Organization.current]
  elsif host_orgs.present?
    host_orgs
  else
    Organization.my_organizations
  end
end

#use_install_media(host, options = {}) ⇒ Object



53
54
55
56
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 53

def use_install_media(host, options = {})
  return true if host&.errors && host.errors.include?(:medium_id) && host.medium.present?
  kickstart_repository_id(host, options).blank?
end

#using_discovered_hosts_page?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 31

def using_discovered_hosts_page?
  controller.controller_name == "discovered_hosts"
end

#using_hostgroups_page?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 35

def using_hostgroups_page?
  controller.controller_name == "hostgroups"
end

#view_to_options(view_options, selected_val, include_blank = false) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/helpers/katello/hosts_and_hostgroups_helper.rb', line 222

def view_to_options(view_options, selected_val, include_blank = false)
  if include_blank == true #check for true specifically
    include_blank = '<option></option>'
  end
  views = view_options.map do |view|
    selected = selected_val == view.id ? 'selected' : ''
    %(<option #{selected} value="#{view.id}">#{h(view.name)}</option>)
  end
  views = views.join
  views.insert(0, include_blank) if include_blank
  views.html_safe
end