Module: Katello::Concerns::BaseTemplateScopeExtensions

Extended by:
ActiveSupport::Concern, ApipieDSL::Module
Includes:
Katello::ContentSourceHelper
Defined in:
app/lib/katello/concerns/base_template_scope_extensions.rb

Instance Method Summary collapse

Methods included from Katello::ContentSourceHelper

#configure_subman, #missing_content_source, #prepare_ssl_cert, #reconfigure_yggdrasild

Instance Method Details

#configure_host_for_new_content_source(host, ca_cert) ⇒ Object



341
342
343
344
345
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 341

def configure_host_for_new_content_source(host, ca_cert)
  return missing_content_source(host) unless host.content_source

  prepare_ssl_cert(ca_cert) + configure_subman(host.content_source)
end

#errata(id) ⇒ Object



16
17
18
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 16

def errata(id)
  Katello::Erratum.in_repositories(Katello::Repository.readable).with_identifiers(id).map(&:attributes).first.slice!('created_at', 'updated_at')
end

#host_applicable_errata_filtered(host, filter = '') ⇒ Object



176
177
178
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 176

def host_applicable_errata_filtered(host, filter = '')
  host.applicable_errata.includes(:cves).search_for(filter)
end

#host_applicable_errata_ids(host) ⇒ Object



158
159
160
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 158

def host_applicable_errata_ids(host)
  host.applicable_errata.pluck(:errata_id)
end

#host_collections(host) ⇒ Object



108
109
110
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 108

def host_collections(host)
  host.host_collections
end

#host_collections_names(host) ⇒ Object



116
117
118
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 116

def host_collections_names(host)
  host.host_collections.map(&:name)
end

#host_content_facet(host) ⇒ Object



58
59
60
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 58

def host_content_facet(host)
  host.content_facet
end

#host_installable_errata_filtered(host, filter = '') ⇒ Object



185
186
187
188
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 185

def host_installable_errata_filtered(host, filter = '')
  return [] if host.content_facet.nil?
  host.installable_errata.includes(:cves).search_for(filter)
end

#host_installable_errata_ids(host) ⇒ Object



166
167
168
169
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 166

def host_installable_errata_ids(host)
  return [] if host.content_facet.nil?
  host.installable_errata.pluck(:errata_id)
end

#host_latest_applicable_rpm_version(host, package) ⇒ Object



195
196
197
198
199
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 195

def host_latest_applicable_rpm_version(host, package)
  return [] if host.content_facet.nil?
  applicable = ::Katello::Rpm.latest(host.applicable_rpms.where(name: package))
  applicable.present? ? applicable.first.nvra : []
end

#host_latest_installable_rpm_version(host, package) ⇒ Object



206
207
208
209
210
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 206

def host_latest_installable_rpm_version(host, package)
  return [] if host.content_facet.nil?
  installable = ::Katello::Rpm.latest(host.installable_rpms.where(name: package))
  installable.present? ? installable.first.nvra : []
end

#host_products(host) ⇒ Object



74
75
76
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 74

def host_products(host)
  host_subscription_facet(host)&.installed_products
end

#host_products_names(host) ⇒ Object



82
83
84
85
86
87
88
89
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 82

def host_products_names(host)
  products = host_products(host)
  if products
    products.map(&:name)
  else
    []
  end
end

#host_products_names_and_ids(host) ⇒ Object



95
96
97
98
99
100
101
102
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 95

def host_products_names_and_ids(host)
  products = host_products(host)
  if products
    products.collect { |product| "#{product.name} (#{product.cp_product_id})" }
  else
    []
  end
end

#host_redhat_subscription_names(host) ⇒ Object



40
41
42
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 40

def host_redhat_subscription_names(host)
  host.subscriptions.redhat.pluck(:name)
end

#host_redhat_subscriptions_consumed(host) ⇒ Object



48
49
50
51
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 48

def host_redhat_subscriptions_consumed(host)
  presenter = ::Katello::HostSubscriptionsPresenter.new(host)
  presenter.subscriptions.select(&:redhat?).sum(&:quantity_consumed)
end

#host_sla(host) ⇒ Object



66
67
68
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 66

def host_sla(host)
  host_subscription_facet(host)&.service_level
end

#host_subscriptions(host) ⇒ Object



24
25
26
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 24

def host_subscriptions(host)
  host.subscriptions
end

#host_subscriptions_names(host) ⇒ Object



32
33
34
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 32

def host_subscriptions_names(host)
  host.subscriptions.map(&:name)
end

#last_checkin(host) ⇒ Object



233
234
235
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 233

def last_checkin(host)
  host&.subscription_facet&.last_checkin
end

#load_errata_applications(filter_errata_type: nil, include_last_reboot: 'yes', since: nil, up_to: nil, status: nil, host_filter: nil) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 252

def load_errata_applications(filter_errata_type: nil, include_last_reboot: 'yes', since: nil, up_to: nil, status: nil, host_filter: nil)
  result = []

  filter_errata_type = filter_errata_type.presence || 'all'
  search_up_to = up_to.present? ? "ended_at < \"#{up_to}\"" : nil
  search_since = since.present? ? "ended_at > \"#{since}\"" : nil
  search_result = status.present? && status != 'all' ? "result = #{status}" : nil
  labels = 'label ^ (Actions::Katello::Host::Erratum::Install, Actions::Katello::Host::Erratum::ApplicableErrataInstall)'

  new_labels = 'label = Actions::RemoteExecution::RunHostJob AND remote_execution_feature.label ^ (katello_errata_install, katello_errata_install_by_search)'
  labels = [labels, new_labels].map { |label| "(#{label})" }.join(' OR ')

  search = [search_up_to, search_since, search_result, "state = stopped", labels].compact.join(' and ')

  tasks = load_resource(klass: ForemanTasks::Task,
                        permission: 'view_foreman_tasks',
                        joins: [:template_invocation],
                        preload: [:template_invocation],
                        search: search)
  only_host_ids = ::Host.search_for(host_filter).pluck(:id) if host_filter

  # batch of 1_000 records
  tasks.each do |batch|
    @_tasks_input = {}
    @_tasks_errata_cache = {}
    seen_errata_ids = []
    seen_host_ids = []

    batch.each do |task|
      next if skip_task?(task)
      seen_errata_ids = (seen_errata_ids + parse_errata(task)).uniq
      seen_host_ids << get_task_input(task)['host']['id'].to_i if include_last_reboot == 'yes'
    end
    seen_host_ids &= only_host_ids if only_host_ids

    # preload errata in one query for this batch
    preloaded_errata = Katello::Erratum.where(:errata_id => seen_errata_ids).pluck(:errata_id, :errata_type, :issued)
    preloaded_hosts = ::Host.where(:id => seen_host_ids).includes(:reported_data)

    batch.each do |task|
      next if skip_task?(task)
      next unless only_host_ids.nil? || only_host_ids.include?(get_task_input(task)['host']['id'].to_i)
      parse_errata(task).each do |erratum_id|
        current_erratum = preloaded_errata.find { |k, _| k == erratum_id }
        next if current_erratum.nil?
        current_erratum_errata_type = current_erratum[1]
        current_erratum_issued = current_erratum.last

        if filter_errata_type != 'all' && !(filter_errata_type == current_erratum_errata_type)
          next
        end

        hash = {
          :date => task.ended_at,
          :hostname => get_task_input(task)['host']['name'],
          :erratum_id => erratum_id,
          :erratum_type => current_erratum_errata_type,
          :issued => current_erratum_issued,
          :status => task.result,
        }

        if include_last_reboot == 'yes'
          # It is possible that we can't find the host if it has been deleted.
          hash[:last_reboot_time] = preloaded_hosts.find { |k, _| k.id == get_task_input(task)['host']['id'].to_i }&.uptime_seconds&.seconds&.ago
        end

        result << hash
      end
    end
  end

  result
end

#load_pools(search: '', includes: nil, expiring_in_days: nil) ⇒ Object



221
222
223
224
225
226
227
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 221

def load_pools(search: '', includes: nil, expiring_in_days: nil)
  pools = Pool.readable
  if expiring_in_days
    pools = pools.expiring_in_days(expiring_in_days)
  end
  load_resource(klass: pools, search: search, permission: nil, includes: includes)
end

#registered_at(host) ⇒ Object



150
151
152
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 150

def registered_at(host)
  host_subscription_facet(host)&.registered_at
end

#registered_through(host) ⇒ Object



142
143
144
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 142

def registered_through(host)
  host_subscription_facet(host)&.registered_through
end

#sortable_version(version) ⇒ Object



332
333
334
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 332

def sortable_version(version)
  Util::Package.sortable_version(version)
end

#sub_name(pool) ⇒ Object



124
125
126
127
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 124

def sub_name(pool)
  return unless pool
  pool.subscription&.name
end

#sub_sku(pool) ⇒ Object



133
134
135
136
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 133

def sub_sku(pool)
  return unless pool
  pool.subscription&.cp_id
end