Class: Capistrano::EnvConfig::Environment

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

Defined Under Namespace

Classes: KeyNotSpecified

Constant Summary collapse

BLANK_RE =
/\A[[:space:]]*\z/

Instance Method Summary collapse

Constructor Details

#initialize(roles = fetch( :env_config_roles )) ⇒ Environment

Returns a new instance of Environment.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/capistrano/env_config/environment.rb', line 7

def initialize( roles = fetch( :env_config_roles ) )
  variables = { }

  # Acquire all configuration variables and store them in a dictionary (hash)
  on roles( roles ) do
    output = capture( 'cat /etc/environment' )
    output.each_line do |line|
      captures = line.match( /\s*([A-Z_][A-Z0-9_]*)=(.*)/ )
      if captures&.length == 3
        variables[ captures[1] ] = captures[2]
      end
    end
  end

  @variables = variables
end

Instance Method Details

#delete(key) ⇒ Object

Raises:



38
39
40
41
# File 'lib/capistrano/env_config/environment.rb', line 38

def delete( key )
  raise KeyNotSpecified.new if BLANK_RE === key
  @variables.delete( key.to_s.upcase )
end

#get(key) ⇒ Object

Raises:



24
25
26
27
# File 'lib/capistrano/env_config/environment.rb', line 24

def get( key )
  raise KeyNotSpecified.new if BLANK_RE === key
  return @variables[ key.to_s.upcase ]
end

#listObject



29
30
31
# File 'lib/capistrano/env_config/environment.rb', line 29

def list
  @variables
end

#set(key, value) ⇒ Object

Raises:



33
34
35
36
# File 'lib/capistrano/env_config/environment.rb', line 33

def set( key, value )
  raise KeyNotSpecified.new if BLANK_RE === key
  @variables[ key.to_s.upcase ] = value.to_s
end

#sync(roles = fetch( :env_config_roles )) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/capistrano/env_config/environment.rb', line 43

def sync( roles = fetch( :env_config_roles ) )
  # Concatenate all variables and output them in the appropariate format
  environment = ''
  @variables.each do |key, value|
    environment += "#{key.upcase}=#{value}\n"
  end

  # Upload the variables to all servers
  on roles( roles ) do
    contents = StringIO.new( environment, 'r' )
    upload! contents, '/etc/environment'
  end
end