Module: Katello::Concerns::HostManagedExtensions

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

Defined Under Namespace

Modules: ClassMethods, Overrides

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KatelloUrlsHelper

#foreman_settings_url, #subscription_manager_configuration_url

Class Method Details

.available_locksObject



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

def self.available_locks
  [:update]
end

Instance Method Details

#advisory_ids(search:) ⇒ Object



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

def advisory_ids(search:)
  ::Katello::Erratum.installable_for_hosts([self]).search_for(search).pluck(:errata_id)
end

#check_host_registrationObject



174
175
176
177
178
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 174

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

#correct_kickstart_repositoryObject



238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 238

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

#errata_statusObject



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

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

#errata_status_label(options = {}) ⇒ Object



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

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

#filtered_entitlement_quantity_consumed(pool) ⇒ Object



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

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



305
306
307
308
309
310
311
312
313
314
315
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 305

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



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

def import_module_streams(module_streams)
  streams = {}
  module_streams.each do |module_stream|
    stream = AvailableModuleStream.create_or_find_by!(name: module_stream["name"],
                                         context: module_stream["context"],
                                         stream: module_stream["stream"])
    streams[stream.id] = module_stream
  end
  sync_available_module_stream_associations(streams)
end

#import_package_profile(simple_packages) ⇒ Object



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

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



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

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



390
391
392
393
394
395
396
397
398
399
400
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 390

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



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

def package_names_for_job_template(action:, search:, versions: nil)
  actions = %w(install remove update).freeze
  case action
  when 'install'
    ::Katello::Rpm.yum_installable_for_host(self).search_for(search).distinct.pluck(:name)
  when 'remove'
    return [] if search.empty?

    installed_packages.search_for(search).distinct.pluck(:name)
  when 'update'
    return [] if search.empty?

    versions_by_name = {}
    if versions.present?
      JSON.parse(versions).each do |nvra|
        nvra =~ /([^.]*)-\d+\.[-.\w]*/
        versions_by_name[Regexp.last_match(1)] = nvra
      end
    end
    pkg_names = installed_packages.search_for(search).distinct.pluck(:name)
    upgrades = ::Katello::Rpm.installable_for_hosts([self]).select(:id, :name, :nvra, :evr).order(evr: :desc).group_by(&:name)
    pkg_names.map { |p| versions_by_name[p] || upgrades[p]&.first&.nvra }.compact
  else
    fail ::Foreman::Exception.new(N_("package_names_for_job_template: Action must be one of %s"), actions.join(', '))
  end
end

#purpose_addons_statusObject



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

def purpose_addons_status
  @purpose_addons_status ||= get_status(::Katello::PurposeAddonsStatus).status
end

#purpose_addons_status_label(options = {}) ⇒ Object



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

def purpose_addons_status_label(options = {})
  @purpose_addons_status_label ||= get_status(::Katello::PurposeAddonsStatus).to_label(options)
end

#purpose_role_statusObject



438
439
440
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 438

def purpose_role_status
  @purpose_role_status ||= get_status(::Katello::PurposeRoleStatus).status
end

#purpose_role_status_label(options = {}) ⇒ Object



442
443
444
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 442

def purpose_role_status_label(options = {})
  @purpose_role_status_label ||= get_status(::Katello::PurposeRoleStatus).to_label(options)
end

#purpose_sla_statusObject



430
431
432
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 430

def purpose_sla_status
  @purpose_sla_status ||= get_status(::Katello::PurposeSlaStatus).status
end

#purpose_sla_status_label(options = {}) ⇒ Object



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

def purpose_sla_status_label(options = {})
  @purpose_sla_status_label ||= get_status(::Katello::PurposeSlaStatus).to_label(options)
end

#purpose_statusObject



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

def purpose_status
  @purpose_status ||= get_status(::Katello::PurposeStatus).status
end

#purpose_status_label(options = {}) ⇒ Object



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

def purpose_status_label(options = {})
  @purpose_status_label ||= get_status(::Katello::PurposeStatus).to_label(options)
end

#purpose_usage_statusObject



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

def purpose_usage_status
  @purpose_usage_status ||= get_status(::Katello::PurposeUsageStatus).status
end

#purpose_usage_status_label(options = {}) ⇒ Object



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

def purpose_usage_status_label(options = {})
  @purpose_usage_status_label ||= get_status(::Katello::PurposeUsageStatus).to_label(options)
end

#queue_refresh_content_host_statusObject



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

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])
  end
end

#queue_reset_content_host_statusObject



210
211
212
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 210

def queue_reset_content_host_status
  should_reset_content_host_status? && reset_content_host_status
end

#refresh_content_host_statusObject



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

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_content_host_statusObject



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

def 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])
end

#reset_katello_statusObject



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

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

#rhsm_fact_valuesObject



254
255
256
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 254

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

#rhsm_organization_labelObject



250
251
252
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 250

def rhsm_organization_label
  self.organization.label
end

#should_reset_content_host_status?Boolean

Returns:

  • (Boolean)


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

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

#subscription_global_statusObject



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

def subscription_global_status
  @subscription_global_status ||= get_status(::Katello::SubscriptionStatus).to_global
end

#subscription_statusObject



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

def subscription_status
  @subscription_status ||= get_status(::Katello::SubscriptionStatus).status
end

#subscription_status_label(options = {}) ⇒ Object



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

def subscription_status_label(options = {})
  @subscription_status_label ||= get_status(::Katello::SubscriptionStatus).to_label(options)
end

#sync_available_module_stream_associations(new_available_module_streams) ⇒ Object



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

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

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

  new_ids = new_available_module_streams.keys - old_associated_ids
  new_ids.each 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
    self.host_available_module_streams.create!(host_id: self.id,
                                               available_module_stream_id: new_id,
                                               installed_profiles: module_stream["installed_profiles"],
                                               status: status)
  end

  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



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'app/models/katello/concerns/host_managed_extensions.rb', line 365

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



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

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

#traces_statusObject



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

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

#traces_status_label(options = {}) ⇒ Object



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

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