Class: Rubber::Configuration::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rubber/environment.rb

Overview

Contains the configuration defined in rubber.yml Handles selecting of correct config values based on the host/role passed into bind

Defined Under Namespace

Classes: BoundEnv, HashValueProxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_root) ⇒ Environment

Returns a new instance of Environment.



15
16
17
18
19
20
21
22
# File 'lib/rubber/environment.rb', line 15

def initialize(config_root)
  @config_root = config_root
  @config_files = ["#{@config_root}/rubber.yml"] + Dir["#{@config_root}/rubber-*.yml"].sort
  @items = {}
  @config_files.each { |file| read_config(file) }
  @config_secret = bind().rubber_secret
  read_config(@config_secret) if @config_secret
end

Instance Attribute Details

#config_filesObject (readonly)

Returns the value of attribute config_files.



12
13
14
# File 'lib/rubber/environment.rb', line 12

def config_files
  @config_files
end

#config_rootObject (readonly)

Returns the value of attribute config_root.



11
12
13
# File 'lib/rubber/environment.rb', line 11

def config_root
  @config_root
end

#config_secretObject (readonly)

Returns the value of attribute config_secret.



13
14
15
# File 'lib/rubber/environment.rb', line 13

def config_secret
  @config_secret
end

Class Method Details

.combine(old, new) ⇒ Object

combine old and new into a single value: non-nil wins if other is nil arrays just get unioned hashes also get unioned, but the values of conflicting keys get combined All else, the new value wins



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubber/environment.rb', line 56

def self.combine(old, new)
  return old if new.nil?
  return new if old.nil?
  value = old
  if old.is_a?(Hash) && new.is_a?(Hash)
    value = old.clone
    new.each do |nk, nv|
      value[nk] = combine(value[nk], nv)
    end
  elsif old.is_a?(Array) && new.is_a?(Array)
    value = old | new
  else
    value = new
  end
  return value
end

Instance Method Details

#bind(roles = nil, host = nil) ⇒ Object



47
48
49
# File 'lib/rubber/environment.rb', line 47

def bind(roles = nil, host = nil)
  BoundEnv.new(@items, roles, host)
end

#current_full_hostObject



43
44
45
# File 'lib/rubber/environment.rb', line 43

def current_full_host
  Socket::gethostname
end

#current_hostObject



39
40
41
# File 'lib/rubber/environment.rb', line 39

def current_host
  Socket::gethostname.gsub(/\..*/, '')
end

#known_rolesObject



31
32
33
34
35
36
37
# File 'lib/rubber/environment.rb', line 31

def known_roles
  roles_dir = File.join(@config_root, "role")
  roles = Dir.entries(roles_dir)
  roles.delete_if {|d| d =~ /(^\..*)/}
  roles += @items['roles'].keys
  return roles.compact.uniq
end

#read_config(file) ⇒ Object



24
25
26
27
28
29
# File 'lib/rubber/environment.rb', line 24

def read_config(file)
  LOGGER.debug{"Reading rubber configuration from #{file}"}
  if File.exist?(file)
    @items = Environment.combine(@items, YAML.load_file(file) || {})
  end
end