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



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

def self.available_locks
  [:update]
end

Instance Method Details

#advisory_ids(search:) ⇒ Object



585
586
587
588
589
590
591
592
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 585

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



326
327
328
329
330
331
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 326

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



181
182
183
184
185
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 181

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

#correct_kickstart_repositoryObject



247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 247

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



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 560

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



440
441
442
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 440

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

#errata_status_label(options = {}) ⇒ Object



444
445
446
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 444

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

#import_enabled_repositories(repos) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 314

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



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

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



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

def import_package_profile(simple_packages)
  found = import_package_profile_in_bulk(simple_packages)
  sync_package_associations(simple_packages, found)
end

#import_package_profile_in_bulk(simple_packages) ⇒ Object



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

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, :nvrea).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



428
429
430
431
432
433
434
435
436
437
438
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 428

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



500
501
502
503
504
505
506
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 500

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)


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

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



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

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



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

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



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

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



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

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



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 481

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



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

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

#rhel_lifecycle_statusObject



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

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

#rhel_lifecycle_status_labelObject



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

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

#rhsm_fact_valuesObject



263
264
265
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 263

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

#rhsm_organization_labelObject



259
260
261
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 259

def rhsm_organization_label
  self.organization.label
end

#should_reset_content_host_status?Boolean

Returns:

  • (Boolean)


223
224
225
226
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 223

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



360
361
362
363
364
365
366
367
368
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
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 360

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(simple_packages, found_packages) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 401

def sync_package_associations(simple_packages, found_packages)
  new_package_ids = found_packages.map(&:id).uniq

  # Build validated package associations records to insert or update (fallback to nil if invalid)
  # O(n) for n packages
  packages_by_nvra = found_packages.index_by(&:nvrea)
  records_to_upsert = simple_packages.filter_map do |simple_package|
    package = packages_by_nvra[simple_package.nvrea]
    next unless package

    # Validate persistence value or set to nil (we skip activerecord validation as part of bulk operations below)
    persistence = Katello::HostInstalledPackage::PERSISTENCE_VALUES.include?(simple_package.persistence) ? simple_package.persistence : nil

    { host_id: self.id, installed_package_id: package.id, persistence: persistence }
  end

  # Apply db operations in a transaction with retry logic
  # O(n) for n packages existing + new
  Katello::Util::Support.active_record_retry do
    old_associated_ids = self.reload.installed_package_ids
    delete_ids = old_associated_ids - new_package_ids

    self.host_installed_packages.where(installed_package_id: delete_ids).delete_all unless delete_ids.empty?
    Katello::HostInstalledPackage.upsert_all(records_to_upsert, unique_by: [:host_id, :installed_package_id]) unless records_to_upsert.empty?
  end
end

#traces_helpers(search:) ⇒ Object



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

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

#traces_statusObject



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

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

#traces_status_label(options = {}) ⇒ Object



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

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



510
511
512
513
514
515
516
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
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 510

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



594
595
596
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 594

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