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
23
24
25
26
27
28
29
30
# File 'lib/rubber/environment.rb', line 15

def initialize(config_root)
  @config_root = config_root
  @config_files = ["#{@config_root}/rubber.yml"]
  @config_files += Dir["#{@config_root}/rubber-*.yml"].sort

  # add a config file for current env only so that you can override
  #things for specific envs
  @config_files -= Dir["#{@config_root}/rubber-*-env.yml"]
  env_yml = "#{@config_root}/rubber-#{Rubber.env}-env.yml"
  @config_files << env_yml if File.exist?(env_yml)
  
  @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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubber/environment.rb', line 64

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



55
56
57
# File 'lib/rubber/environment.rb', line 55

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

#current_full_hostObject



51
52
53
# File 'lib/rubber/environment.rb', line 51

def current_full_host
  Socket::gethostname
end

#current_hostObject



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

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

#known_rolesObject



39
40
41
42
43
44
45
# File 'lib/rubber/environment.rb', line 39

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



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

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