lib/flapjack/configuration.rb in flapjack-1.6.0 vs lib/flapjack/configuration.rb in flapjack-2.0.0b1

- old
+ new

@@ -1,46 +1,39 @@ #!/usr/bin/env ruby -require 'yaml' -require 'logger' +require 'toml' +require 'active_support/core_ext/hash/indifferent_access' module Flapjack - class Configuration - DEFAULT_CONFIG_PATH = '/etc/flapjack/flapjack_config.yaml' - attr_reader :filename - def initialize(opts = {}) - @logger = opts[:logger] - end - def all - @config_env + @config end def for_redis - return unless @config_env + return unless @config - redis_defaults = {'host' => 'localhost', - 'port' => 6379, - 'path' => nil, - 'db' => 0} + redis_defaults = {'host' => '127.0.0.1', + 'port' => 6379, + 'path' => nil, + 'db' => 0} - @config_env['redis'] = {} unless @config_env.has_key?('redis') + @config['redis'] = {} unless @config.has_key?('redis') - redis = @config_env['redis'] + redis = @config['redis'] redis_defaults.each_pair do |k,v| - next if redis.has_key?(k) && redis[k] && + next if redis.has_key?(k) && !redis[k].nil? && !(redis[k].is_a?(String) && redis[k].empty?) redis[k] = v end redis_path = (redis['path'] || nil) - base_opts = {:db => (redis['db'] || 0)} - base_opts[:driver] = redis['driver'] if redis['driver'] + base_opts = HashWithIndifferentAccess.new({ :db => (redis['db'] || 0) }) + base_opts[:driver] = redis['driver'] unless redis['driver'].nil? redis_config = base_opts.merge( (redis_path ? { :path => redis_path } : { :host => (redis['host'] || '127.0.0.1'), :port => (redis['port'] || 6379)}) ) @@ -48,39 +41,60 @@ redis_config[:password] = redis["password"] if redis["password"] redis_config end - def load(filename) - @filename = nil - @config_env = nil + def load(file_pattern) + @file_pattern = nil + @config = nil - unless File.file?(filename) - @logger.error "Could not find file '#{filename}'" if @logger + config_file_names = Dir.glob(file_pattern) + + if config_file_names.nil? + Flapjack.logger.error { + "Could not load config files using file_pattern '#{file_pattern}'" + } return end - unless defined?(FLAPJACK_ENV) - @logger.error "Environment variable 'FLAPJACK_ENV' is not set" if @logger - return + yaml_file = config_file_names.detect {|f| f.end_with?('.yaml') } + + unless yaml_file.nil? + raise "#{yaml_file} looks like a YAML file. Flapjack v2 config files are now in TOML, " + + "see flapjack.io/docs/2.x/configuration" end - config = YAML::load_file(filename) + config = config_file_names.inject({}) do |config, file_name| + config.merge!(TOML.load_file(file_name)) do |key, old_val, new_val| + if old_val != new_val + Flapjack.logger.error { + "Duplicate configuration setting #{key} in #{file_name}" + } + break + else + new_val + end + end + end - if config.nil? - @logger.error "Could not load config file '#{filename}'" if @logger + if config.nil? || config.empty? + Flapjack.logger.error { + "Could not load config files using file_pattern '#{file_pattern}'" + } return end - @config_env = config[FLAPJACK_ENV] + @config = HashWithIndifferentAccess.new(config) - if @config_env.nil? - @logger.error "No config data for environment '#{FLAPJACK_ENV}' found in '#{filename}'" if @logger + @file_pattern = file_pattern + end + + def reload + if @file_pattern.nil? + Flapjack.logger.error "Cannot reload, config file_pattern not set." return end - @filename = filename + load(@file_pattern) end - end - end