From 4348f96f0a8743126c42613cbe1f4ade28890db5 Mon Sep 17 00:00:00 2001 From: Alexander Olofsson <alexander.olofsson@liu.se> Date: Mon, 7 Oct 2024 15:30:02 +0200 Subject: [PATCH] Prepare for zeitwerk --- .../host_finder_extensions.rb | 40 +++++++++++++++++++ .../concerns/host_finder_extensions.rb | 38 ------------------ lib/foreman_uuid_boot/engine.rb | 16 ++++---- 3 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 app/services/foreman/unattended_installation/concerns/foreman_uuid_boot/host_finder_extensions.rb delete mode 100644 app/services/foreman/unattended_installation/concerns/host_finder_extensions.rb diff --git a/app/services/foreman/unattended_installation/concerns/foreman_uuid_boot/host_finder_extensions.rb b/app/services/foreman/unattended_installation/concerns/foreman_uuid_boot/host_finder_extensions.rb new file mode 100644 index 0000000..9b251df --- /dev/null +++ b/app/services/foreman/unattended_installation/concerns/foreman_uuid_boot/host_finder_extensions.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module ForemanUuidBoot + module HostFinderExtensions + def search + host = super + host ||= find_host_by_uuid unless token_from_params.present? + + host + end + + private + + def find_host_by_uuid + return unless (uuid = query_params['uuid']) + + facets = ForemanUuidBoot::UuidbootHostFacet.where(uuid: uuid).order(:created_at) + if facets.any? + Rails.logger.warn("Multiple hosts found with #{uuid}, choosing the most recent") if facets.count > 1 + + return facets.last.host.reload + end + + return unless SETTINGS[:uuidboot_factsearch] + + # Fallback fact search + fact_name_id = FactName.where(name: 'uuid').map(&:id) + return unless fact_name_id.any? + + query = { fact_values: { fact_name_id: fact_name_id, value: uuid } } + hosts = Host.joins(:fact_values).where(query).order(:created_at) + + Rails.logger.warn("Multiple hosts found with #{uuid}, choosing the most recent") if hosts.count > 1 + + return unless hosts.present? + + hosts.last.reload + end + end +end diff --git a/app/services/foreman/unattended_installation/concerns/host_finder_extensions.rb b/app/services/foreman/unattended_installation/concerns/host_finder_extensions.rb deleted file mode 100644 index 54c159b..0000000 --- a/app/services/foreman/unattended_installation/concerns/host_finder_extensions.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -module HostFinderExtensions - def search - host = super - host ||= find_host_by_uuid unless token_from_params.present? - - host - end - - private - - def find_host_by_uuid - return unless (uuid = query_params['uuid']) - - facets = ForemanUuidBoot::UuidbootHostFacet.where(uuid: uuid).order(:created_at) - if facets.any? - Rails.logger.warn("Multiple hosts found with #{uuid}, choosing the most recent") if facets.count > 1 - - return facets.last.host.reload - end - - return unless SETTINGS[:uuidboot_factsearch] - - # Fallback fact search - fact_name_id = FactName.where(name: 'uuid').map(&:id) - return unless fact_name_id.any? - - query = { fact_values: { fact_name_id: fact_name_id, value: uuid } } - hosts = Host.joins(:fact_values).where(query).order(:created_at) - - Rails.logger.warn("Multiple hosts found with #{uuid}, choosing the most recent") if hosts.count > 1 - - return unless hosts.present? - - hosts.last.reload - end -end diff --git a/lib/foreman_uuid_boot/engine.rb b/lib/foreman_uuid_boot/engine.rb index 5f9ebc8..d6ca6bf 100644 --- a/lib/foreman_uuid_boot/engine.rb +++ b/lib/foreman_uuid_boot/engine.rb @@ -13,13 +13,15 @@ module ForemanUuidBoot end end - initializer 'foreman_uuid_boot.register_plugin', before: :finisher_hook do |_app| - Foreman::Plugin.register :foreman_uuid_boot do - requires_foreman '>= 2.5' - - register_facet ForemanUuidBoot::UuidbootHostFacet, :uuidboot_facet do - configure_host do - set_dependent_action :destroy + initializer 'foreman_uuid_boot.register_plugin', before: :finisher_hook do |app| + app.reloader.to_prepare do + Foreman::Plugin.register :foreman_uuid_boot do + requires_foreman '>= 3.12' + + register_facet ForemanUuidBoot::UuidbootHostFacet, :uuidboot_facet do + configure_host do + set_dependent_action :destroy + end end end end -- GitLab