From fd1b1a20de6ba23e2f4305ca5b68b7aa03a41d89 Mon Sep 17 00:00:00 2001 From: Oleh Fedorenko Date: Wed, 29 Jun 2022 15:50:21 +0000 Subject: [PATCH] Multiple fixes --- .../compute_resources/foreman/model/vmware.rb | 3 +- app/models/concerns/foreman/sti.rb | 66 +++++++++---------- app/models/subnet.rb | 2 +- .../foreman/plugin/report_scanner_registry.rb | 4 +- .../foreman/plugin/webpack_assets.rb | 0 .../report_scanner/puppet_report_scanner.rb | 20 ++++++ app/services/ipam.rb | 4 +- app/services/ipam/dhcp.rb | 2 +- app/services/ipam/external_ipam.rb | 2 +- app/services/proxy_api/external_ipam.rb | 2 +- .../report_scanner/puppet_report_scanner.rb | 18 ----- config/initializers/zeitwerk.rb | 59 +++++++++++------ test/models/subnet/external_ipam_test.rb | 2 +- test/unit/ipam_test.rb | 2 +- .../puppet_report_scanner_test.rb | 2 +- 15 files changed, 103 insertions(+), 85 deletions(-) delete mode 100644 app/registries/foreman/plugin/webpack_assets.rb create mode 100644 app/services/foreman/report_scanner/puppet_report_scanner.rb delete mode 100644 app/services/report_scanner/puppet_report_scanner.rb diff --git a/app/models/compute_resources/foreman/model/vmware.rb b/app/models/compute_resources/foreman/model/vmware.rb index 7710416db515..a556d6ac9575 100644 --- a/app/models/compute_resources/foreman/model/vmware.rb +++ b/app/models/compute_resources/foreman/model/vmware.rb @@ -1,4 +1,3 @@ -require 'fog_extensions/vsphere/mini_servers' require 'foreman/exception' begin @@ -818,3 +817,5 @@ def cachekey_with_cluster(key, cluster_id = nil) end end end + +require 'fog_extensions/vsphere/mini_servers' diff --git a/app/models/concerns/foreman/sti.rb b/app/models/concerns/foreman/sti.rb index 78b78ae3a7bc..edace2ba1b0a 100644 --- a/app/models/concerns/foreman/sti.rb +++ b/app/models/concerns/foreman/sti.rb @@ -1,13 +1,12 @@ module Foreman module STI - def self.prepended(base) - class << base - cattr_accessor :preloaded, instance_accessor: false - prepend ClassMethods - end + extend ActiveSupport::Concern + + prepended do + cattr_accessor :preloaded, instance_accessor: false end - module ClassMethods + class_methods do # ensures that the correct STI object is created when :type is passed. def new(*attributes, &block) if (h = attributes.first).is_a?(Hash) && (type = h.with_indifferent_access.delete(:type)) && !type.empty? @@ -19,6 +18,33 @@ def new(*attributes, &block) super end + + def descendants + preload_sti unless preloaded + super + end + + # Constantizes all types present in the database. There might be more on + # disk, but that does not matter in practice as far as the STI API is + # concerned. + # + # Assumes store_full_sti_class is true, the default. + def preload_sti + return [] unless base_class.connected? && base_class.table_exists? + types_in_db = base_class + .unscoped + .select(inheritance_column) + .distinct + .pluck(inheritance_column) + .compact + + types_in_db.each do |type| + logger.debug("Preloading STI type #{type}") + type.constantize + end + + self.preloaded = true + end end def save(*args) @@ -28,33 +54,5 @@ def save(*args) ensure self.class.instance_variable_set("@finder_needs_type_condition", :true) if type_changed end - - def descendants - preload_sti unless preloaded - super - end - - # Constantizes all types present in the database. There might be more on - # disk, but that does not matter in practice as far as the STI API is - # concerned. - # - # Assumes store_full_sti_class is true, the default. - def preload_sti - puts "#{self}: #{base_class.connected?} #{base_class.table_exists?}" - return [] unless base_class.connected? && base_class.table_exists? - types_in_db = base_class - .unscoped - .select(inheritance_column) - .distinct - .pluck(inheritance_column) - .compact - - types_in_db.each do |type| - logger.debug("Preloading STI type #{type}") - type.constantize - end - - self.preloaded = true - end end end diff --git a/app/models/subnet.rb b/app/models/subnet.rb index 0f53cb2dade3..bd2a142f1046 100644 --- a/app/models/subnet.rb +++ b/app/models/subnet.rb @@ -269,7 +269,7 @@ def external_ipam? end def external_ipam_proxy(attrs = {}) - @external_ipam_proxy ||= ProxyAPI::ExternalIpam.new({:url => externalipam.url}.merge(attrs)) if external_ipam? + @external_ipam_proxy ||= ProxyAPI::ExternalIPAM.new({:url => externalipam.url}.merge(attrs)) if external_ipam? end def ipam? diff --git a/app/registries/foreman/plugin/report_scanner_registry.rb b/app/registries/foreman/plugin/report_scanner_registry.rb index f6d23479d888..dea79f8e4b1c 100644 --- a/app/registries/foreman/plugin/report_scanner_registry.rb +++ b/app/registries/foreman/plugin/report_scanner_registry.rb @@ -1,10 +1,8 @@ -require_dependency File.expand_path('../../../services/report_scanner/puppet_report_scanner', __dir__) - module Foreman class Plugin class ReportScannerRegistry DEFAULT_REPORT_SCANNERS = [ - ::Foreman::PuppetReportScanner, + ::Foreman::ReportScanner::PuppetReportScanner, ].freeze attr_accessor :report_scanners diff --git a/app/registries/foreman/plugin/webpack_assets.rb b/app/registries/foreman/plugin/webpack_assets.rb deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/app/services/foreman/report_scanner/puppet_report_scanner.rb b/app/services/foreman/report_scanner/puppet_report_scanner.rb new file mode 100644 index 000000000000..abcce1b290fd --- /dev/null +++ b/app/services/foreman/report_scanner/puppet_report_scanner.rb @@ -0,0 +1,20 @@ +module Foreman + module ReportScanner + class PuppetReportScanner + class << self + def identify_origin(report_data) + 'Puppet' if puppet_report?(report_data['logs'] || []) + end + + def add_reporter_data(report, report_data) + # no additional data apart of origin + end + + def puppet_report?(logs) + log = logs.last + log && log['log'].fetch('sources', {}).fetch('source', '') =~ /Puppet/ + end + end + end + end +end diff --git a/app/services/ipam.rb b/app/services/ipam.rb index c5ce22a53b81..36dd6fc52c39 100644 --- a/app/services/ipam.rb +++ b/app/services/ipam.rb @@ -6,7 +6,7 @@ def self.new(type, *args) when IPAM::MODES[:none] IPAM::None.new(*args) when IPAM::MODES[:dhcp] - IPAM::Dhcp.new(*args) + IPAM::DHCP.new(*args) when IPAM::MODES[:db] IPAM::Db.new(*args) when IPAM::MODES[:random_db] @@ -14,7 +14,7 @@ def self.new(type, *args) when IPAM::MODES[:eui64] IPAM::Eui64.new(*args) when IPAM::MODES[:external_ipam] - IPAM::ExternalIpam.new(*args) + IPAM::ExternalIPAM.new(*args) else raise ::Foreman::Exception.new(N_("Unknown IPAM type - can't continue")) end diff --git a/app/services/ipam/dhcp.rb b/app/services/ipam/dhcp.rb index 8fd08d5d8a51..9f30533eba0b 100644 --- a/app/services/ipam/dhcp.rb +++ b/app/services/ipam/dhcp.rb @@ -1,5 +1,5 @@ module IPAM - class Dhcp < Base + class DHCP < Base delegate :dhcp, :dhcp_proxy, :to => :subnet def suggest_ip unless subnet.dhcp? diff --git a/app/services/ipam/external_ipam.rb b/app/services/ipam/external_ipam.rb index bc24b71133f8..708d57744c87 100644 --- a/app/services/ipam/external_ipam.rb +++ b/app/services/ipam/external_ipam.rb @@ -1,5 +1,5 @@ module IPAM - class ExternalIpam < Base + class ExternalIPAM < Base delegate :external_ipam_proxy, :to => :subnet def suggest_ip diff --git a/app/services/proxy_api/external_ipam.rb b/app/services/proxy_api/external_ipam.rb index f67431e2da7f..22cf637bdd59 100644 --- a/app/services/proxy_api/external_ipam.rb +++ b/app/services/proxy_api/external_ipam.rb @@ -1,7 +1,7 @@ require 'uri' module ProxyAPI - class ExternalIpam < ProxyAPI::Resource + class ExternalIPAM < ProxyAPI::Resource def initialize(args) @url = args[:url] + "/ipam" super args diff --git a/app/services/report_scanner/puppet_report_scanner.rb b/app/services/report_scanner/puppet_report_scanner.rb deleted file mode 100644 index 57b75a95d4dc..000000000000 --- a/app/services/report_scanner/puppet_report_scanner.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Foreman - class PuppetReportScanner - class << self - def identify_origin(report_data) - 'Puppet' if puppet_report?(report_data['logs'] || []) - end - - def add_reporter_data(report, report_data) - # no additional data apart of origin - end - - def puppet_report?(logs) - log = logs.last - log && log['log'].fetch('sources', {}).fetch('source', '') =~ /Puppet/ - end - end - end -end diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rb index 00adc62ccc9c..65eefe48a552 100644 --- a/config/initializers/zeitwerk.rb +++ b/config/initializers/zeitwerk.rb @@ -1,20 +1,39 @@ -Rails.autoloaders.each do |autoloader| - autoloader.inflector = Zeitwerk::Inflector.new - autoloader.inflector.inflect( - 'ui' => 'UI', - 'proxy_api' => 'ProxyAPI', - 'sti' => 'STI', - 'dhcp' => 'DHCP', - 'dns' => 'DNS', - 'tftp' => 'TFTP', - 'external_ipam' => 'ExternalIPAM', - 'bmc' => 'BMC', - 'ui_notifications' => 'UINotifications', - 'ipam' => 'IPAM', - 'ssh' => 'SSH', - 'ssh_provision' => 'SSHProvision', - 'ssh_execution_provider' => 'SSHExecutionProvider', - 'keep_current_request_id' => 'KeepCurrentRequestID' - ) -end -# Rails.autoloaders.log! +Rails.autoloaders.main.inflector.inflect( + 'ui' => 'UI', + 'proxy_api' => 'ProxyAPI', + 'sti' => 'STI', + 'dhcp' => 'DHCP', + 'dns' => 'DNS', + 'tftp' => 'TFTP', + 'external_ipam' => 'ExternalIPAM', + 'bmc' => 'BMC', + 'ui_notifications' => 'UINotifications', + 'ipam' => 'IPAM', + 'ssh' => 'SSH', + 'ssh_provision' => 'SSHProvision', + 'ssh_execution_provider' => 'SSHExecutionProvider', + 'keep_current_request_id' => 'KeepCurrentRequestID', + 'ec2' => 'EC2', + 'aws' => 'AWS', + 'gce' => 'GCE', + 'aix' => 'AIX', + 'nxos' => 'NXOS', + 'vrp' => 'VRP', + 'sso' => 'SSO', + 'puppet_ca_certificate' => 'PuppetCACertificate', + 'url_resolver' => 'URLResolver', + 'ztp_record' => 'ZTPRecord', + 'aaaa_record' => 'AAAARecord', + 'ptr4_record' => 'PTR4Record', + 'ptr6_record' => 'PTR6Record' +) + +Rails.autoloaders.main.ignore( + Rails.root.join('lib/core_extensions.rb'), + Rails.root.join('lib/generators') +) +Rails.autoloaders.once.ignore( + Rails.root.join('app/registries/foreman/access_permissions.rb'), + Rails.root.join('app/registries/foreman/settings.rb'), + Rails.root.join('app/registries/foreman/settings') +) diff --git a/test/models/subnet/external_ipam_test.rb b/test/models/subnet/external_ipam_test.rb index 4130fc618d30..d471ec50b7ab 100644 --- a/test/models/subnet/external_ipam_test.rb +++ b/test/models/subnet/external_ipam_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class Subnet::ExternalIpamTest < ActiveSupport::TestCase +class Subnet::ExternalIPAMTest < ActiveSupport::TestCase test 'external ipam is supported for IPv4' do subnet = FactoryBot.build(:subnet_ipv4) assert subnet.supports_ipam_mode?(:external_ipam) diff --git a/test/unit/ipam_test.rb b/test/unit/ipam_test.rb index 21f13916766f..7a3a813aab8a 100644 --- a/test/unit/ipam_test.rb +++ b/test/unit/ipam_test.rb @@ -9,7 +9,7 @@ class IPAMTest < ActiveSupport::TestCase fake_proxy = mock("dhcp_proxy") fake_proxy.stubs(:unused_ip => {'ip' => '192.168.1.25'}) subnet.stubs(:dhcp_proxy => fake_proxy) - ipam = IPAM::Dhcp.new(:subnet => subnet, :mac => '00:11:22:33:44:55') + ipam = IPAM::DHCP.new(:subnet => subnet, :mac => '00:11:22:33:44:55') assert_equal '192.168.1.25', ipam.suggest_ip end end diff --git a/test/unit/report_scanner/puppet_report_scanner_test.rb b/test/unit/report_scanner/puppet_report_scanner_test.rb index 07fd06ca9151..9d57305aa9db 100644 --- a/test/unit/report_scanner/puppet_report_scanner_test.rb +++ b/test/unit/report_scanner/puppet_report_scanner_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class PuppetReportScannerTest < ActiveSupport::TestCase - subject { Foreman::PuppetReportScanner } + subject { Foreman::ReportScanner::PuppetReportScanner } describe '.identify_origin' do it 'returns Puppet if puppet_report' do