Permalink
Fetching contributors…
Cannot retrieve contributors at this time
150 lines (118 sloc) 4.44 KB
require 'cloud_controller/account_capacity'
require 'uri'
require 'cloud_controller/backends/stagers'
require 'cloud_controller/backends/runners'
require 'cloud_controller/index_stopper'
require 'cloud_controller/backends/instances_reporters'
require 'repositories/service_event_repository'
require 'cloud_controller/config_schemas/api_schema'
require 'cloud_controller/config_schemas/clock_schema'
require 'cloud_controller/config_schemas/worker_schema'
require 'cloud_controller/config_schemas/migrate_schema'
module VCAP::CloudController
class Config
class InvalidConfigPath < StandardError
end
class << self
def load_from_file(file_name, context: :api)
schema_class = schema_class_for_context(context)
config_from_file = schema_class.from_file(file_name)
hash = merge_defaults(config_from_file)
@instance = new(hash, context: context)
end
def config
@instance
end
def schema_class_for_context(context)
const_get("VCAP::CloudController::ConfigSchemas::#{context.capitalize}Schema")
end
private
def merge_defaults(config)
config[:db] ||= {}
config[:db][:database] ||= ENV['DB_CONNECTION_STRING']
sanitize(config)
end
def sanitize(config)
sanitize_staging_auth(config)
config
end
def sanitize_staging_auth(config)
auth = config[:staging][:auth]
auth[:user] = escape_userinfo(auth[:user]) unless valid_in_userinfo?(auth[:user])
auth[:password] = escape_password(auth[:password]) unless valid_in_userinfo?(auth[:password])
end
def escape_password(value)
escape_userinfo(value).gsub(/\"/, '%22')
end
def escape_userinfo(value)
URI.escape(value, "%#{URI::REGEXP::PATTERN::RESERVED}")
end
def valid_in_userinfo?(value)
URI::REGEXP::PATTERN::USERINFO.match(value)
end
end
attr_reader :config_hash
def initialize(config_hash, context: :api)
@config_hash = config_hash
@schema_class = self.class.schema_class_for_context(context)
end
def configure_components
Encryptor.db_encryption_key = get(:db_encryption_key)
if get(:database_encryption)
Encryptor.database_encryption_keys = get(:database_encryption)[:keys]
Encryptor.current_encryption_key_label = get(:database_encryption)[:current_key_label]
end
dependency_locator = CloudController::DependencyLocator.instance
dependency_locator.config = self
run_initializers
ProcessObserver.configure(dependency_locator.stagers, dependency_locator.runners)
InternalApi.configure(self)
@schema_class.configure_components(self)
end
def get(*keys)
invalid_config_path!(keys) unless valid_config_path?(keys, @schema_class.schema)
HashUtils.dig(config_hash, *keys)
end
def set(key, value)
config_hash[key] = value
end
def valid_config_path?(keys, some_schema)
keys.each do |key|
if some_schema.is_a?(Membrane::Schemas::Record) && some_schema.schemas.keys.include?(key)
some_schema = some_schema.schemas[key]
else
invalid_config_path!(keys)
end
end
end
private
def invalid_config_path!(keys)
raise InvalidConfigPath.new(%("#{keys.join('.')}" is not a valid config key))
end
def run_initializers
return if @initialized
run_initializers_in_directory('../../../config/initializers/*.rb')
if @config_hash[:newrelic_enabled]
require 'newrelic_rpm'
# We need to explicitly initialize NewRelic before running our initializers
# When Rails is present, NewRelic adds itself to the Rails initializers instead
# of initializing immediately.
opts = if (Rails.env.test? || Rails.env.development?) && !ENV['NRCONFIG']
{ env: ENV['NEW_RELIC_ENV'] || 'production', monitor_mode: false }
else
{ env: ENV['NEW_RELIC_ENV'] || 'production' }
end
NewRelic::Agent.manual_start(opts)
run_initializers_in_directory('../../../config/newrelic/initializers/*.rb')
end
@initialized = true
end
def run_initializers_in_directory(path)
Dir.glob(File.expand_path(path, __FILE__)).each do |file|
require file
method = File.basename(file).sub('.rb', '').tr('-', '_')
CCInitializers.send(method, @config_hash)
end
end
end
end