diff --git a/app/helpers/host_description_helper.rb b/app/helpers/host_description_helper.rb index 655dcec5515..b58e1ab3fde 100644 --- a/app/helpers/host_description_helper.rb +++ b/app/helpers/host_description_helper.rb @@ -1,5 +1,5 @@ module HostDescriptionHelper - UI.register_host_description do + ::UI.register_host_description do multiple_actions_provider :base_multiple_actions overview_fields_provider :base_status_overview_fields overview_fields_provider :base_host_overview_fields diff --git a/app/models/concerns/foreman/sti.rb b/app/models/concerns/foreman/sti.rb index 8cc5965ae53..edace2ba1b0 100644 --- a/app/models/concerns/foreman/sti.rb +++ b/app/models/concerns/foreman/sti.rb @@ -1,12 +1,12 @@ module Foreman module STI - def self.prepended(base) - class << base - 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? @@ -18,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) diff --git a/app/models/concerns/nested_ancestry_common.rb b/app/models/concerns/nested_ancestry_common.rb index 37ee67a3418..276696be868 100644 --- a/app/models/concerns/nested_ancestry_common.rb +++ b/app/models/concerns/nested_ancestry_common.rb @@ -102,3 +102,5 @@ def update_matchers lookup_values.update_all(:match => "#{obj_type}=#{title}") end end + +require_dependency 'nested_ancestry_common/search' diff --git a/app/models/hostgroup.rb b/app/models/hostgroup.rb index a05142c662f..1bb9b013ca5 100644 --- a/app/models/hostgroup.rb +++ b/app/models/hostgroup.rb @@ -105,7 +105,7 @@ class Hostgroup < ApplicationRecord property :pxe_loader, String, desc: 'Returns boot loader to be applied on each host within this host group' property :title, String, desc: 'Returns full title of this host group, e.g. Base/CentOS 7' end - class Jail < Safemode::Jail + class Jail < ::Safemode::Jail allow :id, :name, :diskLayout, :puppet_server, :operatingsystem, :architecture, :ptable, :url_for_boot, :params, :puppet_proxy, :puppet_ca_server, :os, :arch, :domain, :subnet, :subnet6, :hosts, :realm, diff --git a/app/models/subnet.rb b/app/models/subnet.rb index 0f53cb2dade..bd2a142f104 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/models/taxonomy.rb b/app/models/taxonomy.rb index 855c667b70a..cd514d39801 100644 --- a/app/models/taxonomy.rb +++ b/app/models/taxonomy.rb @@ -51,7 +51,7 @@ def self.inherited(child) end child.const_set('Jail', jail_class) end - child.send(:include, NestedAncestryCommon::Search) + child.send(:include, ::NestedAncestryCommon::Search) super end diff --git a/app/registries/foreman/plugin/report_scanner_registry.rb b/app/registries/foreman/plugin/report_scanner_registry.rb index f6d23479d88..dea79f8e4b1 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 e69de29bb2d..00000000000 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 00000000000..abcce1b290f --- /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 c5ce22a53b8..36dd6fc52c3 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 8fd08d5d8a5..9f30533eba0 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 bc24b71133f..708d57744c8 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 f67431e2da7..22cf637bdd5 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/proxy_status/puppetca.rb b/app/services/proxy_status/puppetca.rb index c6d6336ffcc..2f812e4dfb0 100644 --- a/app/services/proxy_status/puppetca.rb +++ b/app/services/proxy_status/puppetca.rb @@ -1,5 +1,5 @@ module ProxyStatus - class PuppetCA < Base + class Puppetca < Base def certs fetch_proxy_data('/certs') do api.all.map do |name, properties| @@ -71,4 +71,4 @@ def api_class end end end -ProxyStatus.status_registry.add(ProxyStatus::PuppetCA) +ProxyStatus.status_registry.add(ProxyStatus::Puppetca) 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 57b75a95d4d..00000000000 --- 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/app/services/setting_registry.rb b/app/services/setting_registry.rb index 17e933f7f00..2d596123fa6 100644 --- a/app/services/setting_registry.rb +++ b/app/services/setting_registry.rb @@ -132,18 +132,9 @@ def load_definitions end end - def known_categories - unless @known_descendants == Setting.descendants - @known_descendants = Setting.descendants - @known_categories = @known_descendants.map(&:name) << 'Setting' - @values_loaded_at = nil # force all values to be reloaded - end - @known_categories - end - def load_values(ignore_cache: false) # we are loading only known STIs as we load settings fairly early the first time and plugin classes might not be loaded yet. - settings = Setting.unscoped.where(category: known_categories).where.not(value: nil) + settings = Setting.unscoped.where(category: 'Setting').where.not(value: nil) settings = settings.where('updated_at >= ?', @values_loaded_at) unless ignore_cache || @values_loaded_at.nil? settings.each do |s| unless (definition = find(s.name)) diff --git a/config/application.rb b/config/application.rb index 3578f6292e2..0b6da19afad 100644 --- a/config/application.rb +++ b/config/application.rb @@ -104,7 +104,7 @@ class Application < Rails::Application Dir["#{Rails.root}/config/routes/**/*.rb"].each do |route_file| config.paths['config/routes.rb'] << route_file end - + config.load_defaults 6.0 # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. @@ -115,7 +115,7 @@ class Application < Rails::Application config.autoload_paths += %W(#{config.root}/app/models/compute_resources) config.autoload_paths += %W(#{config.root}/app/models/fact_names) config.autoload_paths += %W(#{config.root}/app/models/lookup_keys) - config.autoload_paths += %W(#{config.root}/app/models/host_status) + # config.autoload_paths += %W(#{config.root}/app/models/host_status) config.autoload_paths += %W(#{config.root}/app/models/operatingsystems) config.autoload_paths += %W(#{config.root}/app/models/parameters) config.autoload_paths += %W(#{config.root}/app/models/taxonomies) diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rb new file mode 100644 index 00000000000..65eefe48a55 --- /dev/null +++ b/config/initializers/zeitwerk.rb @@ -0,0 +1,39 @@ +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/db/migrate/20140902102858_convert_ipam_to_string.rb b/db/migrate/20140902102858_convert_ipam_to_string.rb index 881cb82c9d1..d33d3b5c7f3 100644 --- a/db/migrate/20140902102858_convert_ipam_to_string.rb +++ b/db/migrate/20140902102858_convert_ipam_to_string.rb @@ -1,6 +1,6 @@ class ConvertIpamToString < ActiveRecord::Migration[4.2] def up - change_column :subnets, :ipam, :string, :default => IPAM::MODES[:dhcp], :null => false, :limit => 255 + change_column :subnets, :ipam, :string, :default => ::IPAM::MODES[:dhcp], :null => false, :limit => 255 end def down diff --git a/test/models/subnet/external_ipam_test.rb b/test/models/subnet/external_ipam_test.rb index 4130fc618d3..d471ec50b7a 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 21f13916766..7a3a813aab8 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 07fd06ca915..9d57305aa9d 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