Module: Katello::Concerns::BaseTemplateScopeExtensions

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

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Methods included from Katello::ContentSourceHelper

#configure_subman, #missing_content_source, #prepare_ssl_cert, #reconfigure_yggdrasild

Instance Method Details

#activation_key_names(host) ⇒ Object



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

def activation_key_names(host)
  host_subscription_facet(host)&.activation_key_names || []
end

#configure_host_for_new_content_source(host, ca_cert) ⇒ Object



249
250
251
252
253
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 249

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



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

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



152
153
154
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 152

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

#host_applicable_errata_ids(host) ⇒ Object



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

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

#host_collections(host) ⇒ Object



76
77
78
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 76

def host_collections(host)
  host.host_collections
end

#host_collections_names(host) ⇒ Object



84
85
86
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 84

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

#host_content_facet(host) ⇒ Object



26
27
28
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 26

def host_content_facet(host)
  host.content_facet
end

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



161
162
163
164
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 161

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



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

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



171
172
173
174
175
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 171

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



182
183
184
185
186
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 182

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



42
43
44
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 42

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

#host_products_names(host) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 50

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

#host_products_names_and_ids(host) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 63

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_sla(host) ⇒ Object



34
35
36
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 34

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

#last_checkin(host) ⇒ Object



209
210
211
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 209

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: 'all', host_filter: nil) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 224

def load_errata_applications(filter_errata_type: nil, include_last_reboot: 'yes', since: nil, up_to: nil, status: 'all', host_filter: nil)
  load_errata_applications_from_db(
    filter_errata_type: filter_errata_type,
    include_last_reboot: include_last_reboot,
    since: since,
    up_to: up_to,
    status: status,
    host_filter: host_filter
  )
end

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

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



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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 270

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

  authorized_hosts = ::Host::Managed.authorized('view_hosts').select(:id)

  # Build scoped search query for date and status filters
  search_conditions = []
  search_conditions << "applied_at > \"#{since}\"" if since.present?
  search_conditions << "applied_at < \"#{up_to}\"" if up_to.present?

  if status.present? && status != 'all'
    return [] if status.to_s.casecmp('pending').zero?
    search_conditions << "status = #{status.to_s.downcase}"
  end

  applications = if search_conditions.any?
                   Katello::ErrataApplication.where(host_id: authorized_hosts)
                     .search_for(search_conditions.join(' and '))
                 else
                   Katello::ErrataApplication.where(host_id: authorized_hosts)
                 end

  if host_filter.present?
    applications = applications.where(host_id: ::Host.search_for(host_filter).select(:id))
  end

  # Preload hosts
  if include_last_reboot == 'yes'
    applications = applications.includes(host: :reported_data)
  else
    applications = applications.includes(:host)
  end

  # Fetch host_ids and errata_ids in a single query
  application_data = applications.pluck(:host_id, :errata_ids)
  host_ids = application_data.map(&:first).uniq
  all_errata_ids = application_data.map(&:last).flatten.uniq

  # Preload all errata to avoid N+1 queries (join on errata_id string)
  errata_by_errata_id = Katello::Erratum.where(errata_id: all_errata_ids).index_by(&:errata_id)

  # Fetch applicability only for errata that were applied
  applicable_data = Katello::Host::ContentFacet
    .where(host_id: host_ids)
    .joins(:applicable_errata)
    .where('katello_errata.errata_id' => all_errata_ids)
    .pluck(:host_id, 'katello_errata.errata_id')

  applicable_errata_map =
    applicable_data
      .group_by(&:first)
      .transform_values { |pairs| pairs.map(&:last) }

  # Process each application record
  applications.find_each(batch_size: 1000) do |app|
    # Get applicable erratum IDs for this host (from pre-built map)
    applicable_erratum_ids = applicable_errata_map[app.host_id] || []

    # Create one output row per erratum
    app.errata_ids.each do |erratum_string_id|
      erratum = errata_by_errata_id[erratum_string_id]

      # If erratum doesn't exist in DB (deleted/disabled repo), still create output with limited data
      hash = if erratum
               {
                 date: app.applied_at,
                 hostname: app.host.name,
                 erratum_id: erratum.errata_id,
                 erratum_type: erratum.errata_type,
                 issued: erratum.issued,
                 severity: erratum.severity,
                 status: app.status,
                 still_applicable: applicable_erratum_ids.include?(erratum_string_id),
               }
             else
               # Erratum no longer in system, but we still have the application record
               {
                 date: app.applied_at,
                 hostname: app.host.name,
                 erratum_id: erratum_string_id,
                 erratum_type: nil,
                 issued: nil,
                 severity: nil,
                 status: app.status,
                 still_applicable: false,
               }
             end

      # Filter by errata type if specified (skip if we don't have the erratum object or type doesn't match)
      next if filter_errata_type.present? && filter_errata_type != 'all' && (erratum.nil? || erratum.errata_type != filter_errata_type)

      if include_last_reboot == 'yes'
        hash[:last_reboot_time] = app.host&.uptime_seconds&.seconds&.ago
      end

      result << hash
    end
  end

  result
end

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



197
198
199
200
201
202
203
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 197

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



118
119
120
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 118

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

#registered_through(host) ⇒ Object



110
111
112
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 110

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

#sortable_version(version) ⇒ Object



240
241
242
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 240

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

#sub_name(pool) ⇒ Object



92
93
94
95
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 92

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

#sub_sku(pool) ⇒ Object



101
102
103
104
# File 'app/lib/katello/concerns/base_template_scope_extensions.rb', line 101

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