class Fulmar::Domain::Model::Configuration

Loads and prepares the configuration from the yaml file

Attributes

base_path[RW]
debug[R]
environment[RW]
target[RW]

Public Class Methods

new(data, base_path = '', debug = false) click to toggle source
# 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

[](id) click to toggle source

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
[]=(id, value) click to toggle source

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
dependencies(env = nil) click to toggle source

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
each() { |env, target, data| ... } click to toggle source

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
hosts() click to toggle source

Allow access to host list

# File lib/fulmar/domain/model/configuration.rb, line 94
def hosts
  @data[:hosts]
end
key?(key) click to toggle source

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(other) click to toggle source

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
plugins() click to toggle source
# File lib/fulmar/domain/model/configuration.rb, line 64
def plugins
  @data[:plugins] || {}
end
project() click to toggle source

Return the project

# File lib/fulmar/domain/model/configuration.rb, line 60
def project
  @project ||= Fulmar::Domain::Model::Project.new(@data[:project])
end
ready?() click to toggle source

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(environment, target = nil) click to toggle source

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
ssh_user_and_host() click to toggle source

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

absolutize(path, base = @base_path) click to toggle source

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
absolutize_paths() click to toggle source

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
handle_inheritance() click to toggle source

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
local_path?(key) click to toggle source

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
merge_hosts() click to toggle source

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
prepare_data() click to toggle source

Prepares the configuration

# File lib/fulmar/domain/model/configuration.rb, line 115
def prepare_data
  handle_inheritance
  merge_hosts
  absolutize_paths
end