lib/fulmar/domain/service/configuration_service.rb in fulmar-1.10.1 vs lib/fulmar/domain/service/configuration_service.rb in fulmar-2.0.0

- old
+ new

@@ -1,9 +1,10 @@ require 'yaml' require 'pp' require 'fulmar/domain/model/project' -require 'singleton' +require 'fulmar/domain/model/configuration' +require 'active_support/core_ext/hash/keys' module Fulmar module Domain module Service # Loads and prepares the configuration from the yaml file @@ -18,113 +19,29 @@ BLANK_CONFIG = { project: {}, environments: {}, features: {}, hosts: {}, - dependencies: { all: {} } } - include ::Singleton + include Singleton attr_reader :environment, :target - attr_accessor :load_user_config + attr_accessor :load_user_config, :debug def initialize - @environment = nil - @target = nil @load_user_config = true end - # Allow access of configuration via array/hash access methods (read access) - def [](id) - ready? ? configuration[:environments][@environment][@target][id] : nil - end - - # Allow access of configuration via array/hash access methods (write access) - def []=(id, value) - if ready? - configuration[:environments][@environment][@target][id] = value - else - fail 'Environment or target not set. Please set both variables via configuration.environment = \'xxx\' / '\ - 'configuration.target = \'yyy\'' - end - end - - def to_hash - ready? ? configuration[:environments][@environment][@target] : configuration - end - def base_path @base_path ||= lookup_base_path end def configuration @config ||= load_configuration end - def project - @project ||= Fulmar::Domain::Model::Project.new(configuration[:project]) - end - - def method_missing(name) - environment(name) if configuration[:environments][name] - end - - def environment=(env) - @environment = env ? env.to_sym : nil - end - - def target=(target) - @target = target ? target.to_sym : nil - end - - def ssh_user_and_host - self[:user].blank? ? self[:hostname] : self[:user] + '@' + self[:hostname] - end - - def dependencies(env = nil) - if env.nil? || !@config[:dependencies].has_key?(env) - @config[:dependencies][:all] - else - @config[:dependencies][:all].deep_merge(@config[:dependencies][env]) - end - end - - def ready? - return false if @environment.nil? || @target.nil? - fail 'Environment is invalid' if configuration[:environments][@environment].nil? - fail 'Target is invalid' if configuration[:environments][@environment][@target].nil? - true - end - - def feature?(feature) - return configuration[:features].include? feature.to_s unless configuration[:features].nil? - case feature - when :database - any? { |data| data[:type] == 'maria' } - else - false - end - end - - def each - configuration[:environments].each_key do |env| - configuration[:environments][env].each_pair do |target, data| - yield(env, target, data) - end - end - end - - def any? - if block_given? - each { |_env, _target, data| return true if yield(data) } - false - else - configuration[:environments].any? - end - end - # Merge another configuration into the currently active one # Useful for supplying a default configuration, as values are not overwritten. # Hashes are merged. # @param [Hash] other def merge(other) @@ -156,69 +73,34 @@ end File.dirname(fulmar_file) end - # Fills a target with all globally set variables so all necessary information - # is found within each target - def fill_target(env, target) - @config[:environments][env][target] = @config[:environments][:all].deep_merge(@config[:environments][env][target]) if @config[:environments][:all] - - return if @config[:environments][env][target][:host].blank? - - host = @config[:environments][env][target][:host].to_sym - return unless @config[:hosts] && @config[:hosts][host] - @config[:hosts][host].each do - @config[:environments][env][target] = @config[:hosts][host].deep_merge(@config[:environments][env][target]) - end - end - # Loads the configuration from the YAML file and populates all targets def load_configuration - @config = BLANK_CONFIG + config = BLANK_CONFIG config_files.each do |config_file| - @config = @config.deep_merge((YAML.load_file(config_file) || {}).symbolize) + config = config.deep_merge((YAML.load_file(config_file) || {}).deep_symbolize_keys) end - - prepare_environments - prepare_dependencies - # Iterate over all environments and targets to prepare them - @config[:environments].delete(:all) - check_version - @config + check_version(config[:project][:fulmar_version]) + Fulmar::Domain::Model::Configuration.new(config, base_path, @debug) end - def prepare_environments - @config[:environments].each_key do |env| - next if env == :all - @config[:environments][env].each_key do |target| - fill_target(env, target) - check_path(env, target) - end - end - end - + # @todo Move to configuration model if I know what this was or if it is relevant def prepare_dependencies @config[:dependencies].each_pair do |_env, repos| repos.each_pair do |_name, repo| next if repo[:path].blank? full_path = File.expand_path("#{base_path}/#{repo[:path]}") repo[:path] = full_path unless repo[:path][0,1] == '/' end end end - def check_path(env, target) - path = @config[:environments][env][target][:local_path] - return if path.blank? - return if path[0,1] == '/' - @config[:environments][env][target][:local_path] = File.expand_path("#{base_path}/#{path}") - end - - def check_version - return if @config[:project][:fulmar_version].nil? - unless Gem::Dependency.new('', @config[:project][:fulmar_version]).match?('', Fulmar::VERSION) - fail "Project requires another version of fulmar: #{@config[:project][:fulmar_version]}" + def check_version(version) + return if version.nil? + unless Gem::Dependency.new('', version).match?('', Fulmar::VERSION) + fail "Project requires a newer version of fulmar: #{version}" end end end end end