Class: Fulmar::Domain::Model::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/fulmar/domain/model/configuration.rb

Overview

Loads and prepares the configuration from the yaml file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, base_path = '', debug = false) ⇒ Configuration

Returns a new instance of Configuration.



16
17
18
19
20
21
# 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

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



13
14
15
# File 'lib/fulmar/domain/model/configuration.rb', line 13

def base_path
  @base_path
end

#debugObject (readonly)

Returns the value of attribute debug.



14
15
16
# File 'lib/fulmar/domain/model/configuration.rb', line 14

def debug
  @debug
end

#environmentObject

Returns the value of attribute environment.



13
14
15
# File 'lib/fulmar/domain/model/configuration.rb', line 13

def environment
  @environment
end

#targetObject

Returns the value of attribute target.



13
14
15
# File 'lib/fulmar/domain/model/configuration.rb', line 13

def target
  @target
end

Instance Method Details

#[](id) ⇒ Object

Allow access of configuration via array/hash access methods (read access)



24
25
26
# File 'lib/fulmar/domain/model/configuration.rb', line 24

def [](id)
  ready? ? @data[:environments][@environment][@target][id] : nil
end

#[]=(id, value) ⇒ Object

Allow access of configuration via array/hash access methods (write access)



29
30
31
32
33
34
35
36
# 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) ⇒ Object

TODO:

Refactor this to work with the dependencies plugin

Handle dependencies



85
86
87
88
89
90
91
# 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

#eachObject

Allows iterating over all targets from all configured environments



69
70
71
72
73
74
75
# 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

#hostsObject

Allow access to host list



94
95
96
# File 'lib/fulmar/domain/model/configuration.rb', line 94

def hosts
  @data[:hosts]
end

#key?(key) ⇒ Boolean

Checks if a configuration key exists in one of the targets

Returns:

  • (Boolean)


99
100
101
# File 'lib/fulmar/domain/model/configuration.rb', line 99

def key?(key)
  ready? ? @data[:environments][@environment][@target].key?(key) : false
end

#merge(other) ⇒ Object

Merge another configuration into the currently active one Useful for supplying a default configuration, as values are not overwritten. Hashes are merged.

Parameters:

  • other (Hash)


107
108
109
110
# 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

#pluginsObject



64
65
66
# File 'lib/fulmar/domain/model/configuration.rb', line 64

def plugins
  @data[:plugins] || {}
end

#projectObject

Return the project



60
61
62
# File 'lib/fulmar/domain/model/configuration.rb', line 60

def project
  @project ||= Fulmar::Domain::Model::Project.new(@data[:project])
end

#ready?Boolean

Checks if environment and target are set

Returns:

  • (Boolean)


52
53
54
55
56
57
# 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) ⇒ Object

Set the environment and target in one call



39
40
41
42
43
44
45
46
47
48
49
# 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_hostObject

TODO:

Is there another way to do this?

Return the combined user and host



79
80
81
# File 'lib/fulmar/domain/model/configuration.rb', line 79

def ssh_user_and_host
  self[:user].blank? ? self[:hostname] : self[:user] + '@' + self[:hostname]
end