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



226
227
228
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 226

def self.available_locks
  [:update]
end

Instance Method Details

#advisory_ids(search:) ⇒ Object



544
545
546
547
548
549
550
551
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 544

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



285
286
287
288
289
290
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 285

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



136
137
138
139
140
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 136

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

#clear_errata_applications_on_rebuildObject



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

def clear_errata_applications_on_rebuild
  errata_applications.delete_all
end

#correct_kickstart_repositoryObject



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

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



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 519

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



399
400
401
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 399

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

#errata_status_label(options = {}) ⇒ Object



403
404
405
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 403

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

#import_enabled_repositories(repos) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 273

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



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

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



230
231
232
233
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 230

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



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/models/katello/concerns/host_managed_extensions.rb', line 235

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



387
388
389
390
391
392
393
394
395
396
397
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 387

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



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

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)


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

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



151
152
153
154
155
156
157
158
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 151

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



168
169
170
171
172
173
174
175
176
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 168

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



142
143
144
145
146
147
148
149
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 142

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



160
161
162
163
164
165
166
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 160

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



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 440

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



407
408
409
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 407

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

#rhel_lifecycle_statusObject



411
412
413
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 411

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

#rhel_lifecycle_status_labelObject



415
416
417
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 415

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

#rhsm_fact_valuesObject



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

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

#rhsm_organization_labelObject



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

def rhsm_organization_label
  self.organization.label
end

#should_reset_content_host_status?Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 178

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



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

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



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

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



427
428
429
430
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 427

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

#traces_statusObject



419
420
421
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 419

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

#traces_status_label(options = {}) ⇒ Object



423
424
425
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 423

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



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 469

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



553
554
555
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 553

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