Skip to content

Commit

Permalink
Fixes #15025 - do not show foreman_proxy startup messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-d authored and domcleal committed Jun 6, 2016
1 parent 222cf17 commit 18a62b7
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 125 deletions.
2 changes: 1 addition & 1 deletion config.ru
Expand Up @@ -2,4 +2,4 @@ $LOAD_PATH.unshift *Dir[File.expand_path("../lib", __FILE__), File.expand_path("

require 'smart_proxy_main'
::Proxy::PluginInitializer.new(::Proxy::Plugins.instance).initialize_plugins
::Proxy::Plugins.instance.enabled_plugins.each {|p| instance_eval(p.https_rackup)}
::Proxy::Plugins.instance.select {|p| p[:state] == :running && p[:https_enabled]}.each {|p| instance_eval(p[:class].https_rackup)}
8 changes: 5 additions & 3 deletions lib/launcher.rb
Expand Up @@ -17,8 +17,8 @@ def https_enabled?
def http_app
return nil if SETTINGS.http_port.nil?
app = Rack::Builder.new do
::Proxy::Plugins.instance.enabled_plugins.each do |p|
instance_eval(p.http_rackup)
::Proxy::Plugins.instance.select {|p| p[:state] == :running && p[:http_enabled]}.each do |p|
instance_eval(p[:class].http_rackup)
end
end

Expand All @@ -38,7 +38,9 @@ def https_app
nil
else
app = Rack::Builder.new do
::Proxy::Plugins.instance.enabled_plugins.each {|p| instance_eval(p.https_rackup)}
::Proxy::Plugins.instance.select {|p| p[:state] == :running && p[:https_enabled]}.each do |p|
instance_eval(p[:class].https_rackup)
end
end

ssl_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:options]
Expand Down
19 changes: 13 additions & 6 deletions lib/proxy/pluggable.rb
@@ -1,3 +1,5 @@
require 'ostruct'

module Proxy::Pluggable
attr_reader :plugin_name, :version, :after_activation_blk

Expand Down Expand Up @@ -25,7 +27,6 @@ def settings_file(apath = nil)
end

def default_settings(a_hash = {})
@settings = nil
@plugin_default_settings ||= {}
@plugin_default_settings.merge!(a_hash)
end
Expand All @@ -51,6 +52,15 @@ def validate(*settings)
settings.each {|setting| validations << {:name => validator_name, :predicate => predicate, :args => validator_args, :setting => setting} }
end

def override_module_loader_class(a_class_or_a_class_name)
@module_loader_class = case a_class_or_a_class_name
when String
eval(a_class_or_a_class_name)
else
a_class_or_a_class_name
end
end

def load_validators(hash_of_validators)
@custom_validators = hash_of_validators
end
Expand All @@ -75,12 +85,9 @@ def start_services(*di_labels)
# End of DSL
#

attr_writer :settings
def settings
@settings ||= Proxy::Settings.load_plugin_settings(plugin_default_settings, settings_file)
end

def settings=(arg)
@settings = arg
@settings ||= OpenStruct.new
end

def module_loader_class
Expand Down
16 changes: 2 additions & 14 deletions lib/proxy/plugin.rb
Expand Up @@ -37,22 +37,10 @@ class ::Proxy::Plugin
class << self
attr_reader :get_http_rackup_path, :get_https_rackup_path, :get_uses_provider

def enabled
@enabled ||= settings.enabled
end

def http_enabled?
[true,'http'].include?(self.enabled)
end

def http_rackup_path(path)
@get_http_rackup_path = path
end

def https_enabled?
[true,'https'].include?(self.enabled)
end

def https_rackup_path(path)
@get_https_rackup_path = path
end
Expand All @@ -73,11 +61,11 @@ def uses_provider?
end

def http_rackup
(http_enabled? && get_http_rackup_path) ? File.read(get_http_rackup_path) : ""
get_http_rackup_path ? File.read(get_http_rackup_path) : ""
end

def https_rackup
(https_enabled? && get_https_rackup_path) ? File.read(get_https_rackup_path) : ""
get_https_rackup_path ? File.read(get_https_rackup_path) : ""
end
end
end
114 changes: 78 additions & 36 deletions lib/proxy/plugin_initializer.rb
Expand Up @@ -7,17 +7,27 @@ class ::Proxy::PluginGroup

def initialize(a_plugin, providers = [], di_container = ::Proxy::DependencyInjection::Container.new)
@plugin = a_plugin
@state = :starting
@state = :uninitialized # :uninitialized -> :starting -> :running, or :uninitialized -> :disabled, or :uninitialized -> :starting -> :failed
@providers = providers
@di_container = di_container
@http_enabled = false
@https_enabled = false
end

def failed?
@state == :failed
def inactive?
@state == :failed || @state == :disabled
end

def http_enabled?
@http_enabled
end

def https_enabled?
@https_enabled
end

def resolve_providers(all_plugins_and_providers)
return if failed?
return if inactive?
return unless @plugin.uses_provider?

used_providers = [@plugin.settings.use_provider].flatten.map(&:to_sym)
Expand Down Expand Up @@ -47,22 +57,35 @@ def printable_module_names(names)
end

def load_plugin_settings
plugin.module_loader_class.new(plugin, di_container).load_settings
settings = plugin.module_loader_class.new(plugin, di_container).load_settings
update_group_initial_state(settings[:enabled])
rescue Exception => e
fail_group(e)
end

def update_group_initial_state(enabled_setting)
@http_enabled = [true, 'http'].include?(enabled_setting) ? true : false
@https_enabled = [true, 'https'].include?(enabled_setting) ? true : false
@state = (http_enabled? || https_enabled?) ? :starting : :disabled
end

def set_group_state_to_failed
@http_enabled = false
@https_enabled = false
@state = :failed
end

def load_provider_settings
return if failed?
return if inactive?
providers.each do |p|
p.module_loader_class.new(p, di_container).load_settings(plugin.settings.marshal_dump)
p.module_loader_class.new(p, di_container).load_settings(plugin.settings.marshal_dump)
end
rescue Exception => e
fail_group(e)
end

def configure
return if failed?
return if inactive?
members.each {|p| p.module_loader_class.new(p, di_container).configure_plugin }
@state = :running
rescue Exception => e
Expand All @@ -75,7 +98,7 @@ def fail_group(an_exception)
end

def fail_group_with_message(a_message, a_backtrace = nil)
@state = :failed
set_group_state_to_failed
logger.error(a_message, a_backtrace)
members.each do |m|
::Proxy::LogBuffer::Buffer.instance.failed_module(m.plugin_name, a_message)
Expand Down Expand Up @@ -114,50 +137,59 @@ def initialize(plugins)
end

def initialize_plugins
# find all enabled plugins
enabled_plugins = plugins.loaded.select {|plugin| plugin[:class].ancestors.include?(::Proxy::Plugin) && plugin[:class].enabled}
loaded_plugins = plugins.loaded.select {|plugin| plugin[:class].ancestors.include?(::Proxy::Plugin)}

grouped_with_providers = enabled_plugins.map {|p| ::Proxy::PluginGroup.new(p[:class], [], Proxy::DependencyInjection::Container.new)}
grouped_with_providers = loaded_plugins.map {|p| ::Proxy::PluginGroup.new(p[:class], [], Proxy::DependencyInjection::Container.new)}

update_plugin_states(plugins, grouped_with_providers)
plugins.update(current_state_of_modules(plugins.loaded, grouped_with_providers))

# load main plugin settings, as this may affect which providers will be selected
grouped_with_providers.each {|group| group.load_plugin_settings }
grouped_with_providers.each {|group| group.load_plugin_settings}

plugins.update(current_state_of_modules(plugins.loaded, grouped_with_providers))

#resolve provider names to classes
grouped_with_providers.each {|group| group.resolve_providers(plugins.loaded)}

# load provider plugin settings
grouped_with_providers.each {|group| group.load_provider_settings }

plugins.update(current_state_of_modules(plugins.loaded, grouped_with_providers))

# configure each plugin & providers
grouped_with_providers.each {|group| group.configure }

# check prerequisites
all_enabled = all_enabled_plugins_and_providers(grouped_with_providers)
grouped_with_providers.each do |group|
next if group.failed?
next if group.inactive?
group.validate_dependencies_or_fail(all_enabled)
end

update_plugin_states(plugins, grouped_with_providers)
plugins.update(current_state_of_modules(plugins.loaded, grouped_with_providers))
end

def update_plugin_states(all_plugins, all_groups)
to_update = all_plugins.loaded.dup
def current_state_of_modules(all_plugins, all_groups)
to_update = all_plugins.dup
all_groups.each do |group|
group.members.each do |group_member|
# note that providers do not use http_enabled and https_enabled
updated = to_update.find {|loaded_plugin| loaded_plugin[:name] == group.plugin.plugin_name}
updated[:di_container] = group.di_container
updated[:state] = group.state
updated[:http_enabled] = group.http_enabled?
updated[:https_enabled] = group.https_enabled?
group.providers.each do |group_member|
updated = to_update.find {|loaded_plugin| loaded_plugin[:name] == group_member.plugin_name}
updated[:di_container] = group.di_container
updated[:state] = group.state
end
end
all_plugins.update(to_update)
to_update
end

def all_enabled_plugins_and_providers(all_groups)
all_groups.inject({}) do |all, group|
group.members.each {|p| all[p.plugin_name] = p} unless group.failed?
group.members.each {|p| all[p.plugin_name] = p} unless group.inactive?
all
end
end
Expand Down Expand Up @@ -200,8 +232,11 @@ def start_services(services, container)

module ::Proxy::DefaultSettingsLoader
def load_settings(main_plugin_settings = {})
settings_file_config = load_configuration(plugin.settings_file)
merged_with_defaults = plugin.default_settings.merge(settings_file_config)
config_file_settings = load_configuration_file(plugin.settings_file)

merged_with_defaults = plugin.default_settings.merge(config_file_settings)

return merged_with_defaults unless module_enabled?(merged_with_defaults)

# load dependencies before loading custom settings and running validators -- they may need those classes
::Proxy::BundlerHelper.require_groups(:default, plugin.bundler_group_name)
Expand All @@ -211,13 +246,29 @@ def load_settings(main_plugin_settings = {})
settings = load_programmable_settings(config_merged_with_main)

plugin.settings = ::Proxy::Settings::Plugin.new({}, settings)
logger.debug("'#{plugin.plugin_name}' settings: #{used_settings(settings)}")

log_used_settings(settings)

validate_settings(plugin, settings)

settings
end

def module_enabled?(user_settings)
return true if plugin.ancestors.include?(::Proxy::Provider)
!!user_settings[:enabled]
end

def load_configuration_file(settings_file)
begin
settings = Proxy::Settings.read_settings_file(settings_file)
rescue Errno::ENOENT
logger.warn("Couldn't find settings file #{::Proxy::SETTINGS.settings_directory}/#{settings_file}. Using default settings.")
settings = {}
end
settings
end

def merge_settings(provider_settings, main_plugin_settings)
main_plugin_settings.delete(:enabled)
# all modules have 'enabled' setting, we ignore it when looking for duplicate setting names
Expand All @@ -227,20 +278,11 @@ def merge_settings(provider_settings, main_plugin_settings)
provider_settings.merge(main_plugin_settings)
end

def used_settings(settings)
def log_used_settings(settings)
default_settings = plugin.plugin_default_settings
sorted_keys = settings.keys.map(&:to_s).sort.map(&:to_sym) # ruby 1.8.7 doesn't support sorting of symbols
sorted_keys.map {|k| "'%s': %s%s" % [k, settings[k], (default_settings.include?(k) && default_settings[k] == settings[k]) ? " (default)" : ""] }.join(", ")
end

def load_configuration(settings_file)
begin
settings = Proxy::Settings.read_settings_file(settings_file)
rescue Errno::ENOENT
logger.warn("Couldn't find settings file #{::Proxy::SETTINGS.settings_directory}/#{settings_file}. Using default settings.")
settings = {}
end
settings
to_log = sorted_keys.map {|k| "'%s': %s%s" % [k, settings[k], (default_settings.include?(k) && default_settings[k] == settings[k]) ? " (default)" : ""] }.join(", ")
logger.debug "'%s' settings: %s" % [plugin.plugin_name, to_log]
end

def load_programmable_settings(settings)
Expand Down
23 changes: 20 additions & 3 deletions lib/proxy/plugins.rb
Expand Up @@ -15,8 +15,19 @@ def plugin_loaded(a_name, a_version, a_class)
self.loaded += [{:name => a_name, :version => a_version, :class => a_class, :state => :uninitialized}]
end

#
# each element of the list is a hash containing:
#
# :name: module name
# :version: module version
# :class: module class
# :state: one of :unitialized, :loaded, :staring, :running, :disabled, or :failed
# :di_container: dependency injection container used by the module
# :http_enabled: true or false (not used by providers)
# :https_enabled: true or false (not used by providers)
#
def loaded
@loaded ||= [] # {:name, :version, :class, :factory, :state, :di_container}
@loaded ||= []
end

def loaded=(an_array)
Expand All @@ -36,14 +47,20 @@ def find
end
end

def enabled_plugins
loaded.select {|p| p[:state] == :running && p[:class].ancestors.include?(::Proxy::Plugin)}.map{|p| p[:class]}
def select
loaded.select do |plugin|
yield plugin
end
end

#
# below are methods that are going to be removed/deprecated
#

def enabled_plugins
loaded.select {|p| p[:state] == :running && p[:class].ancestors.include?(::Proxy::Plugin)}.map{|p| p[:class]}
end

def plugin_enabled?(plugin_name)
plugin = loaded.find {|p| p[:name] == plugin_name.to_sym}
plugin.nil? ? false : plugin[:state] == :running
Expand Down
1 change: 1 addition & 0 deletions modules/root/root.rb
@@ -1 +1,2 @@
require 'root/root_module_loader'
require 'root/root_plugin'

0 comments on commit 18a62b7

Please sign in to comment.