class Fulmar::Domain::Model::Configuration
Loads and prepares the configuration from the yaml file
Attributes
Public Class Methods
# File lib/fulmar/domain/model/configuration.rb, line 16 def initialize(data, base_path = '', debug = false) @data = data @base_path = base_path @debug = debug prepare_data end
Public Instance Methods
Allow access of configuration via array/hash access methods (read access)
# File lib/fulmar/domain/model/configuration.rb, line 24 def [](id) ready? ? @data[:environments][@environment][@target][id] : nil end
Allow access of configuration via array/hash access methods (write access)
# File lib/fulmar/domain/model/configuration.rb, line 29 def []=(id, value) if ready? @data[:environments][@environment][@target][id] = value else raise 'Environment or target not set. Please set both variables via configuration.environment = \'xxx\' / '\ 'configuration.target = \'yyy\'' end end
Handle dependencies @todo Refactor this to work with the dependencies plugin
# File lib/fulmar/domain/model/configuration.rb, line 85 def dependencies(env = nil) if env.nil? || !@data[:dependencies].key?(env) @data[:dependencies][:all] else @data[:dependencies][:all].deep_merge(@data[:dependencies][env]) end end
Allows iterating over all targets from all configured environments
# File lib/fulmar/domain/model/configuration.rb, line 69 def each @data[:environments].each_key do |env| @data[:environments][env].each_pair do |target, data| yield(env, target, data) end end end
Allow access to host list
# File lib/fulmar/domain/model/configuration.rb, line 94 def hosts @data[:hosts] end
Checks if a configuration key exists in one of the targets
# File lib/fulmar/domain/model/configuration.rb, line 99 def key?(key) ready? ? @data[:environments][@environment][@target].key?(key) : false 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
# File lib/fulmar/domain/model/configuration.rb, line 107 def merge(other) return unless @environment && @target @data[:environments][@environment][@target] = other.deep_merge(@data[:environments][@environment][@target]) end
# File lib/fulmar/domain/model/configuration.rb, line 64 def plugins @data[:plugins] || {} end
Return the project
# File lib/fulmar/domain/model/configuration.rb, line 60 def project @project ||= Fulmar::Domain::Model::Project.new(@data[:project]) end
Checks if environment and target are set
# File lib/fulmar/domain/model/configuration.rb, line 52 def ready? return false if @environment.nil? || @target.nil? raise 'Environment is invalid' if @data[:environments][@environment].nil? raise 'Target is invalid' if @data[:environments][@environment][@target].nil? true end
Set the environment and target in one call
# File lib/fulmar/domain/model/configuration.rb, line 39 def set(environment, target = nil) # For convenience, allow something like "environment:target" as string if environment.class == String fields = environment.split(':') @environment = fields.first.to_sym @target = fields.size > 1 ? fields[1].to_sym : nil else @environment = environment @target = target unless target.nil? end end
Return the combined user and host @todo Is there another way to do this?
# File lib/fulmar/domain/model/configuration.rb, line 79 def ssh_user_and_host self[:user].blank? ? self[:hostname] : self[:user] + '@' + self[:hostname] end
Protected Instance Methods
Prepends the base to the path if it is not already absolute
# File lib/fulmar/domain/model/configuration.rb, line 148 def absolutize(path, base = @base_path) return base if path == '.' (Pathname.new path).absolute? ? path : base + '/' + path end
Check all keys ending with '_path' and prepend either the @base_path or the local_path from the environment
# File lib/fulmar/domain/model/configuration.rb, line 160 def absolutize_paths each do |_env, _target, data| data.each_key do |key| data[:local_path] = absolutize(data[:local_path]) if data[:local_path] if local_path?(key) && data[key].class == String data[key] = absolutize(data[key], data[:local_path] || @base_path) end end end end
Merges the :all-configuration into targets.
- :environments][:all
-
into all targets
- :environments][:all
-
into all targets from environment :something
# File lib/fulmar/domain/model/configuration.rb, line 124 def handle_inheritance global_config = @data[:environments].delete(:all) || {} environments = @data[:environments].keys environments.each do |env| environment_config = @data[:environments][env].delete(:all) || {} targets = @data[:environments][env].keys targets.each do |target| local_config = @data[:environments][env][target] || {} @data[:environments][env][target] = global_config.deep_merge(environment_config).deep_merge(local_config) @data[:environments][env][target][:debug] = @debug end end end
Checks if a key is a local path
# File lib/fulmar/domain/model/configuration.rb, line 154 def local_path?(key) key.to_s.split('_').last == 'path' && !key.to_s.include?('remote') end
Merges the host configuration into the targets referring to it
# File lib/fulmar/domain/model/configuration.rb, line 139 def merge_hosts each do |env, target, data| next if data[:host].nil? host = data[:host].to_sym @data[:environments][env][target] = @data[:hosts][host].deep_merge(data) unless @data[:hosts][host].nil? end end
Prepares the configuration
# File lib/fulmar/domain/model/configuration.rb, line 115 def prepare_data handle_inheritance merge_hosts absolutize_paths end