From 3fbac612317fc863f4d49b9f40bd3a583ff4da76 Mon Sep 17 00:00:00 2001 From: Bernhard Suttner Date: Fri, 24 Nov 2023 20:41:09 +0100 Subject: [PATCH] [PR] Fixes #36946 - cache if tracer/host_tools is installed (cherry picked from commit 954996e6f194033f3df213cf4393ed1780e9666c) --- app/models/katello/host/content_facet.rb | 23 +++++++++++-------- .../katello/host/profiles_uploader.rb | 23 ++++++++++++++----- test/models/host/content_facet_test.rb | 14 +++++++++++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/app/models/katello/host/content_facet.rb b/app/models/katello/host/content_facet.rb index 69f7dcdf43e..2ee737c1171 100644 --- a/app/models/katello/host/content_facet.rb +++ b/app/models/katello/host/content_facet.rb @@ -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 @@ -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 diff --git a/app/services/katello/host/profiles_uploader.rb b/app/services/katello/host/profiles_uploader.rb index 01acdc2495b..c1dcab67480 100644 --- a/app/services/katello/host/profiles_uploader.rb +++ b/app/services/katello/host/profiles_uploader.rb @@ -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) @@ -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? diff --git a/test/models/host/content_facet_test.rb b/test/models/host/content_facet_test.rb index b2f9a087f2f..5ba96b579e0 100644 --- a/test/models/host/content_facet_test.rb +++ b/test/models/host/content_facet_test.rb @@ -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),