Module: Katello::Concerns::HostManagedExtensions

Extended by:
ActiveSupport::Concern
Includes:
ForemanTasks::Concerns::ActionSubject, KatelloUrlsHelper
Defined in:
app/models/katello/concerns/host_managed_extensions.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Modules: ClassMethods, Overrides

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KatelloUrlsHelper

#foreman_settings_url, #repository_url, #subscription_manager_configuration_url

Class Method Details

.available_locksObject



276
277
278
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 276

def self.available_locks
  [:update]
end

Instance Method Details

#advisory_ids(search:) ⇒ Object



592
593
594
595
596
597
598
599
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 592

def advisory_ids(search:)
  errata_scope = ::Katello::Erratum.installable_for_hosts([self])
  ids = errata_scope.search_for(search).pluck(:errata_id)
  if ids.empty?
    fail _("Cannot install errata: No errata found for search term '%s'") % search
  end
  ids
end

#available_module_stream_id_from(name:, stream:, context:) ⇒ Object



335
336
337
338
339
340
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 335

def available_module_stream_id_from(name:, stream:, context:)
  @indexed_available_module_streams ||= Katello::AvailableModuleStream.all.index_by do |available_module_stream|
    "#{available_module_stream.name}-#{available_module_stream.stream}-#{available_module_stream.context}"
  end
  @indexed_available_module_streams["#{name}-#{stream}-#{context}"]&.id
end

#check_host_registrationObject

of included block



190
191
192
193
194
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 190

def check_host_registration
  if subscription_facet
    fail ::Katello::Errors::HostRegisteredException
  end
end

#correct_kickstart_repositoryObject



256
257
258
259
260
261
262
263
264
265
266
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 256

def correct_kickstart_repository
  return unless content_facet

  # If switched from ks repo to install media:
  if medium_id_changed? && medium && content_facet.kickstart_repository
    content_facet.kickstart_repository_id = nil
  # If switched from install media to ks repo:
  elsif content_facet.kickstart_repository && medium
    self.medium = nil
  end
end

#deb_names_for_job_template(action:, search:) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 567

def deb_names_for_job_template(action:, search:)
  actions = %w(install remove update).freeze
  case action
  when 'install'
    deb_installable = ::Katello::Deb.search_for(search).distinct.pluck(:name)
    if deb_installable.empty?
      fail _("No available debs found for search term '%s'. Check the host's content view environments and already-installed debs.") % search
    end
    deb_installable
  when 'remove'
    return [] if search.empty?

    ::Katello::InstalledDeb.search_for(search).distinct.pluck(:name)
  when 'update'
    return [] if search.empty?
    deb_results = ::Katello::InstalledDeb.search_for(search).distinct.pluck(:name)
    if deb_results.empty?
      fail _("No installed debs found for search term '%s'") % search
    end
    deb_results
  else
    fail ::Foreman::Exception.new(N_("deb_names_for_job_template: Action must be one of %s"), actions.join(', '))
  end
end

#errata_statusObject



447
448
449
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 447

def errata_status
  @errata_status ||= get_status(::Katello::ErrataStatus).status
end

#errata_status_label(options = {}) ⇒ Object



451
452
453
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 451

def errata_status_label(options = {})
  @errata_status_label ||= get_status(::Katello::ErrataStatus).to_label(options)
end

#filtered_entitlement_quantity_consumed(pool) ⇒ Object



601
602
603
604
605
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 601

def filtered_entitlement_quantity_consumed(pool)
  entitlements = subscription_facet.candlepin_consumer.filter_entitlements(pool.cp_id)
  return nil if entitlements.empty?
  entitlements.sum { |e| e[:quantity] }
end

#import_enabled_repositories(repos) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 323

def import_enabled_repositories(repos)
  paths = repos.map do |repo|
    if !repo['baseurl'].blank?
      URI(repo['baseurl'].first).path
    else
      logger.warn("System #{name} (#{id}) attempted to bind to unspecific repo (#{repo}).")
      nil
    end
  end
  content_facet.update_repositories_by_paths(paths.compact)
end

#import_module_streams(module_streams) ⇒ Object



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
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 342

def import_module_streams(module_streams)
  # module_streams looks like this
  # {"name"=>"389-ds", "stream"=>"1.4", "version"=>"8030020201203210520", "context"=>"e114a9e7", "arch"=>"x86_64", "profiles"=>[], "installed_profiles"=>[], "status"=>"default", "active"=>false}
  streams = module_streams.map do |module_stream|
    {
      name: module_stream["name"],
      stream: module_stream["stream"],
      context: module_stream["context"],
    }
  end
  if streams.any?
    AvailableModuleStream.insert_all(
      streams,
      unique_by: %w[name stream context],
      returning: %w[id name stream context]
    )
  end
  indexed_module_streams = module_streams.index_by do |module_stream|
    available_module_stream_id_from(
            name: module_stream["name"],
            stream: module_stream["stream"],
            context: module_stream["context"]
          )
  end
  sync_available_module_stream_associations(indexed_module_streams)
end

#import_package_profile(simple_packages) ⇒ Object



280
281
282
283
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 280

def import_package_profile(simple_packages)
  found = import_package_profile_in_bulk(simple_packages)
  sync_package_associations(found.map(&:id).uniq)
end

#import_package_profile_in_bulk(simple_packages) ⇒ Object



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
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 285

def import_package_profile_in_bulk(simple_packages)
  nvreas = simple_packages.map { |sp| sp.nvrea }
  found_nvrea = InstalledPackage.where(:nvrea => nvreas)
  nil_vendor_installed_packages = found_nvrea.where(vendor: nil)
  unless nil_vendor_installed_packages.blank?
    packages_to_update = simple_packages.select { |sp| !sp.vendor.blank? && nil_vendor_installed_packages&.map(&:nvrea)&.include?(sp.nvrea) }
    packages_to_update.each do |simple_package|
      nil_vendor_installed_packages.where(nvrea: simple_package.nvrea).update(vendor: simple_package.vendor)
    end
  end

  found = found_nvrea.select(:id, :nvrea).to_a
  found_nvreas = found.map(&:nvrea)

  new_packages = simple_packages.select { |sp| !found_nvreas.include?(sp.nvrea) }

  installed_packages = []
  new_packages.each do |simple_package|
    installed_packages << InstalledPackage.new(:nvrea => simple_package.nvrea,
                                    :nvra => simple_package.nvra,
                                    :name => simple_package.name,
                                    :epoch => simple_package.epoch,
                                    :version => simple_package.version,
                                    :release => simple_package.release,
                                    :arch => simple_package.arch,
                                    :vendor => simple_package.vendor)
  end
  InstalledPackage.import(installed_packages, validate: false, on_duplicate_key_ignore: true)
  #re-lookup all imported to pickup any duplicates/conflicts
  imported = InstalledPackage.where(:nvrea => installed_packages.map(&:nvrea)).select(:id).to_a

  if imported.count != installed_packages.count
    Rails.logger.warn("Mismatch found in installed package insertion, expected #{installed_packages.count} but only could find #{imported.count}.  This is most likley a bug.")
  end

  (found + imported).flatten
end

#import_tracer_profile(tracer_profile) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 435

def import_tracer_profile(tracer_profile)
  traces = []
  tracer_profile.each do |trace, attributes|
    next if attributes[:helper].blank?

    traces << { host_id: self.id, application: trace, helper: attributes[:helper], app_type: attributes[:type] }
  end
  host_traces.delete_all
  Katello::HostTracer.import(traces, validate: false)
  update_trace_status
end

#package_names_for_job_template(action:, search:, versions: nil) ⇒ Object



507
508
509
510
511
512
513
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 507

def package_names_for_job_template(action:, search:, versions: nil)
  if self&.operatingsystem&.family == 'Debian'
    deb_names_for_job_template(action: action, search: search)
  else
    yum_names_for_job_template(action: action, search: search, versions: versions)
  end
end

#probably_rhel?Boolean

Returns:

  • (Boolean)


480
481
482
483
484
485
486
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 480

def probably_rhel?
  # Get the os name from sub-man facts rather than operatingsystem. This is
  # less likely to have been changed by the user.
  os_name, = facts('distribution::name').values # only query for that one fact, then get its value
  # if this fact isn't there, we can ignore it because the host is not "managed"
  os_name.present? && os_name.start_with?('Red Hat Enterprise Linux')
end

#queue_refresh_content_host_statusObject



205
206
207
208
209
210
211
212
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 205

def queue_refresh_content_host_status
  if !new_record? && !build && self.changes.key?('build')
    queue.create(id: "refresh_content_host_status_#{id}", name: _("Refresh Content Host Statuses for %s") % self,
      priority: 300, action: [self, :refresh_content_host_status])
  else
    true
  end
end

#queue_reset_content_host_statusObject



222
223
224
225
226
227
228
229
230
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 222

def queue_reset_content_host_status
  if should_reset_content_host_status?
    logger.debug "Scheduling host status cleanup"
    queue.create(id: "reset_content_host_status_#{id}", name: _("Mark Content Host Statuses as Unknown for %s") % self,
      priority: 200, action: [self, :reset_katello_status])
  else
    true
  end
end

#refresh_content_host_statusObject



196
197
198
199
200
201
202
203
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 196

def refresh_content_host_status
  if content_facet&.present?
    self.host_statuses.where(type: ::Katello::HostStatusManager::STATUSES.map(&:name)).each do |status|
      status.refresh!
    end
  end
  refresh_global_status
end

#reset_katello_statusObject



214
215
216
217
218
219
220
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 214

def reset_katello_status
  self.host_statuses.where(type: ::Katello::HostStatusManager::STATUSES.map(&:name)).each do |status|
    status.update!(:status => status.class.const_get(:UNKNOWN))
  end
  self.host_statuses.reload
  true
end

#rhel_eos_schedule_indexObject



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 488

def rhel_eos_schedule_index
  return nil unless probably_rhel?
  major = operatingsystem&.major
  return nil unless major
  return "RHEL#{major}" unless major == "7"

  arch_name = architecture&.name
  case arch_name
  when "ppc64le"
    "RHEL7 (POWER9)"
  when "aarch64"
    "RHEL7 (ARM)"
  when "s390x"
    "RHEL7 (System z (Structure A))"
  else
    "RHEL#{major}"
  end
end

#rhel_lifecycle_global_statusObject



455
456
457
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 455

def rhel_lifecycle_global_status
  @rhel_lifecycle_global_status ||= get_status(::Katello::RhelLifecycleStatus).to_global
end

#rhel_lifecycle_statusObject



459
460
461
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 459

def rhel_lifecycle_status
  @rhel_lifecycle_status ||= get_status(::Katello::RhelLifecycleStatus).status
end

#rhel_lifecycle_status_labelObject



463
464
465
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 463

def rhel_lifecycle_status_label
  @rhel_lifecycle_status_label ||= get_status(::Katello::RhelLifecycleStatus).to_label
end

#rhsm_fact_valuesObject



272
273
274
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 272

def rhsm_fact_values
  self.fact_values.joins(:fact_name).where("#{::FactName.table_name}.type = '#{Katello::RhsmFactName}'")
end

#rhsm_organization_labelObject



268
269
270
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 268

def rhsm_organization_label
  self.organization.label
end

#should_reset_content_host_status?Boolean

Returns:

  • (Boolean)


232
233
234
235
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 232

def should_reset_content_host_status?
  return false unless self.is_a?(::Host::Base)
  !new_record? && build && self.changes.key?('build')
end

#sync_available_module_stream_associations(new_available_module_streams) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 369

def sync_available_module_stream_associations(new_available_module_streams)
  new_associated_ids = new_available_module_streams.keys.compact
  upgradable_streams = self.host_available_module_streams.where(:available_module_stream_id => new_associated_ids)
  old_associated_ids = self.available_module_stream_ids
  delete_ids = old_associated_ids - new_associated_ids

  if delete_ids.any?
    self.host_available_module_streams.where(:available_module_stream_id => delete_ids).delete_all
  end

  new_ids = new_associated_ids - old_associated_ids

  hams_to_create = new_ids.map do |new_id|
    module_stream = new_available_module_streams[new_id]
    status = module_stream["status"]
    # Set status to "unknown" only if the active field is in use and set to false and the module is enabled
    if enabled_module_stream_inactive?(module_stream)
      status = "unknown"
    end
    {
      host_id: self.id,
      available_module_stream_id: new_id,
      installed_profiles: module_stream["installed_profiles"],
      status: status,
    }
  end
  HostAvailableModuleStream.insert_all(hams_to_create) if hams_to_create.any?
  upgradable_streams.each do |hams|
    module_stream = new_available_module_streams[hams.available_module_stream_id]
    shared_keys = hams.attributes.keys & module_stream.keys
    module_stream_data = module_stream.slice(*shared_keys)
    if hams.attributes.slice(*shared_keys) != module_stream_data
      hams.update!(module_stream_data)
    end
    # Set status to "unknown" only if the active field is in use and set to false and the module is enabled
    if enabled_module_stream_inactive?(module_stream)
      hams.update!(status: "unknown")
    end
  end
end

#sync_package_associations(new_installed_package_ids) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 410

def sync_package_associations(new_installed_package_ids)
  Katello::Util::Support.active_record_retry do
    old_associated_ids = self.reload.installed_package_ids
    table_name = self.host_installed_packages.table_name

    new_ids = new_installed_package_ids - old_associated_ids
    delete_ids = old_associated_ids - new_installed_package_ids

    queries = []

    if delete_ids.any?
      queries << "DELETE FROM #{table_name} WHERE host_id=#{self.id} AND installed_package_id IN (#{delete_ids.join(', ')})"
    end

    unless new_ids.empty?
      inserts = new_ids.map { |unit_id| "(#{unit_id.to_i}, #{self.id.to_i})" }
      queries << "INSERT INTO #{table_name} (installed_package_id, host_id) VALUES #{inserts.join(', ')}"
    end

    queries.each do |query|
      ActiveRecord::Base.connection.execute(query)
    end
  end
end

#traces_helpers(search:) ⇒ Object



475
476
477
478
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 475

def traces_helpers(search:)
  traces = host_traces.selectable.search_for(search)
  ::Katello::HostTracer.helpers_for(traces)
end

#traces_statusObject



467
468
469
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 467

def traces_status
  @traces_status ||= get_status(::Katello::TraceStatus).status
end

#traces_status_label(options = {}) ⇒ Object



471
472
473
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 471

def traces_status_label(options = {})
  @traces_status_label ||= get_status(::Katello::TraceStatus).to_label(options)
end

#yum_names_for_job_template(action:, search:, versions: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 517

def yum_names_for_job_template(action:, search:, versions: nil)
  actions = %w(install remove update).freeze
  case action
  when 'install'
    yum_installable = ::Katello::Rpm.search_for(search).distinct.pluck(:name)
    if yum_installable.empty?
      fail N_("No available packages found for search term '%s'.") % search
    end
    yum_installable
  when 'remove'
    return [] if search.empty?

    yum_removable = ::Katello::InstalledPackage.search_for(search).distinct.pluck(:name)
    if yum_removable.empty?
      fail N_("Cannot remove package(s): No installed packages found for search term '%s'.") % search
    end
    yum_removable
  when 'update'
    return [] if search.empty?

    versions_by_name_arch = {}
    if versions.present?
      JSON.parse(versions).each do |nvra|
        package_info = ::Katello::Util::Package.parse_nvrea(nvra)
        versions_by_name_arch[[package_info[:name], package_info[:arch]]] = nvra
      end
    end

    # > versions_by_name_arch
    # =>
    # {["glibc-langpack-en", "x86_64"]=>"glibc-langpack-en-2.34-100.el9_4.2.x86_64",
    #  ["crypto-policies", "noarch"]=>"crypto-policies-20221215-1.git9a18988.el9_2.1.noarch"}

    pkg_name_archs = installed_packages.search_for(search).distinct.pluck(:name, :arch)
    if pkg_name_archs.empty?
      fail _("Cannot upgrade packages: No installed packages found for search term '%s'.") % search
    end
    versionless_upgrades = ::Katello::Rpm.where(name: pkg_name_archs.map(&:first)).select(:id, :name, :arch, :evr).order(evr: :desc).group_by { |i| [i.name, i.arch] }
    # Use versions_by_name_arch if a version is specified, otherwise use the latest version. If using the latest version, upgrade by name only, not by name and arch.
    pkg_names_and_nvras = pkg_name_archs.map { |name, arch| versions_by_name_arch[[name, arch]] || versionless_upgrades[[name, arch]]&.first&.name }.compact
    if pkg_names_and_nvras.empty?
      fail _("No upgradable packages found for search term '%s'.") % search
    end
    pkg_names_and_nvras
  else
    fail ::Foreman::Exception.new(N_("package_names_for_job_template: Action must be one of %s"), actions.join(', '))
  end
end

#yum_or_yum_transientObject



607
608
609
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 607

def yum_or_yum_transient
  content_facet&.yum_or_yum_transient || "yum"
end