Skip to content

Commit

Permalink
[PR] Fixes #36946 - cache if tracer/host_tools is installed
Browse files Browse the repository at this point in the history
(cherry picked from commit 954996e)
  • Loading branch information
sbernhard authored and nadjaheitmann committed Mar 4, 2024
1 parent 951c1e1 commit 3fbac61
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
23 changes: 13 additions & 10 deletions app/models/katello/host/content_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ContentFacet < Katello::Model
HOST_TOOLS_PACKAGE_NAME = 'katello-host-tools'.freeze
HOST_TOOLS_TRACER_PACKAGE_NAME = 'katello-host-tools-tracer'.freeze
SUBSCRIPTION_MANAGER_PACKAGE_NAME = 'subscription-manager'.freeze
ALL_HOST_TOOLS_PACKAGE_NAMES = [ "python-#{HOST_TOOLS_PACKAGE_NAME}",
"python3-#{HOST_TOOLS_PACKAGE_NAME}",
HOST_TOOLS_PACKAGE_NAME ].freeze
ALL_TRACER_PACKAGE_NAMES = [ "python-#{HOST_TOOLS_TRACER_PACKAGE_NAME}",
"python3-#{HOST_TOOLS_TRACER_PACKAGE_NAME}",
HOST_TOOLS_TRACER_PACKAGE_NAME ].freeze
Expand Down Expand Up @@ -309,22 +312,22 @@ def available_releases
end
end

def tracer_installed?
self.host.installed_packages.where("#{Katello::InstalledPackage.table_name}.name" => ALL_TRACER_PACKAGE_NAMES).any? ||
self.host.installed_debs.where("#{Katello::InstalledDeb.table_name}.name" => ALL_TRACER_PACKAGE_NAMES).any?
def tracer_installed?(force_update_cache: false)
Rails.cache.fetch("#{self.host.id}/tracer_installed", expires_in: 7.days, force: force_update_cache) do
self.host.installed_packages.where("#{Katello::InstalledPackage.table_name}.name" => ALL_TRACER_PACKAGE_NAMES).any? ||
self.host.installed_debs.where("#{Katello::InstalledDeb.table_name}.name" => ALL_TRACER_PACKAGE_NAMES).any?
end
end

def tracer_rpm_available?
::Katello::Rpm.yum_installable_for_host(self.host).where(name: ALL_TRACER_PACKAGE_NAMES).any?
end

def host_tools_installed?
host.installed_packages.where("#{Katello::InstalledPackage.table_name}.name" => [ "python-#{HOST_TOOLS_PACKAGE_NAME}",
"python3-#{HOST_TOOLS_PACKAGE_NAME}",
HOST_TOOLS_PACKAGE_NAME ]).any? ||
host.installed_debs.where("#{Katello::InstalledDeb.table_name}.name" => [ "python-#{HOST_TOOLS_PACKAGE_NAME}",
"python3-#{HOST_TOOLS_PACKAGE_NAME}",
HOST_TOOLS_PACKAGE_NAME ]).any?
def host_tools_installed?(force_update_cache: false)
Rails.cache.fetch("#{self.host.id}/host_tools_installed", expires_in: 7.days, force: force_update_cache) do
self.host.installed_packages.where("#{Katello::InstalledPackage.table_name}.name" => ALL_HOST_TOOLS_PACKAGE_NAMES).any? ||
self.host.installed_debs.where("#{Katello::InstalledDeb.table_name}.name" => ALL_HOST_TOOLS_PACKAGE_NAMES).any?
end
end

def update_errata_status
Expand Down
23 changes: 17 additions & 6 deletions app/services/katello/host/profiles_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ def initialize(profile_string:, host: nil)
@host = host
end

# rubocop:disable Metrics/MethodLength
def upload
if @host.nil?
Rails.logger.warn("Host was not specified; skipping")
return false
elsif @host.content_facet.nil? || @host.content_facet.uuid.nil?
Rails.logger.warn("Host with ID %s has no content facet; skipping" % @host.id)
return false
end

profiles = JSON.parse(@profile_string)
#free the huge string from the memory
@profile_string = 'TRIMMED'.freeze
if @host.nil?
Rails.logger.warn("Host was not specified; continuing")
elsif @host.content_facet.nil? || @host.content_facet.uuid.nil?
Rails.logger.warn("Host with ID %s has no content facet; continuing" % @host.id)
elsif profiles.try(:has_key?, "deb_package_profile")
if profiles.try(:has_key?, "deb_package_profile")
# remove this when deb_package_profile API is removed
payload = profiles.dig("deb_package_profile", "deb_packages") || []
import_deb_package_profile(payload)
Expand All @@ -37,9 +42,15 @@ def upload
module_streams.each do |module_stream_payload|
import_module_streams(module_stream_payload)
end

end

# Just to update the internal cache
@host.content_facet.tracer_installed?(force_update_cache: true)
@host.content_facet.host_tools_installed?(force_update_cache: true)

true
end
# rubocop:enable Metrics/MethodLength

def trigger_applicability_generation
if @host.nil?
Expand Down
14 changes: 14 additions & 0 deletions test/models/host/content_facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ def test_tracer_installed?

host.installed_packages << Katello::InstalledPackage.create!(:name => 'katello-host-tools-tracer', 'nvrea' => 'katello-host-tools-tracer-1.0.x86_64', 'nvra' => 'katello-host-tools-tracer-1.0.x86_64')

# make sure the cache is gets updated
host.reload.content_facet.tracer_installed?(force_update_cache: true)

assert host.reload.content_facet.tracer_installed?
end

def test_host_tools_installed?
refute host.content_facet.host_tools_installed?

host.installed_packages << Katello::InstalledPackage.create!(:name => 'katello-host-tools', 'nvrea' => 'katello-host-tools-1.0.x86_64', 'nvra' => 'katello-host-tools-1.0.x86_64')

# make sure the cache is gets updated
host.reload.content_facet.host_tools_installed?(force_update_cache: true)

assert host.reload.content_facet.host_tools_installed?
end

def test_in_content_view_version_environments
facet_cve = content_facet.content_view_environments.first
first_cvve = {:content_view_version => facet_cve.content_view.version(facet_cve.lifecycle_environment),
Expand Down

0 comments on commit 3fbac61

Please sign in to comment.