Class: GV::Valley::Balancer

Inherits:
Bedrock::Service
  • Object
show all
Includes:
Common::PipeHelper
Defined in:
lib/gv/valley/balancer.rb

Constant Summary collapse

INDENT =
"        "

Instance Method Summary collapse

Instance Method Details

#reload(&block) ⇒ Object

reloads haproxy config



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gv/valley/balancer.rb', line 16

def reload &block
  
  @block = block
  
  indicate "Loading Haproxy config"
  
  config = File.read("#{GV::Valley.root}/scripts/haproxy.cfg")
  target_file = "/etc/haproxy/haproxy.cfg"
  acl = ""
  backend = ""
  
  App.all.each do |app|
    
    app["domains"].each do |domain|
      acl << "#{INDENT}use_backend b_#{app["name"]} if { hdr(host) -i #{domain} }\n"
    end
    
    backend << "backend b_#{app["name"]}\n"
    app["ps"]["web"]["containers"].each do |container|
      host = container['HostConfig']['PortBindings']["#{App::PORT}/tcp"].first
      backend << "#{INDENT}server srv_#{container['ID'][0..6]} #{host['HostIp']}:#{host['HostPort']}\n"
    end
    
  end
  
  config.gsub!(/^\#FRONT$/,acl) 
  config.gsub!(/^\#BACK$/,backend) 
  
  pipe "rm #{target_file}"
  File.open(target_file,File::RDWR|File::CREAT){|f| f.puts config }
    
  pipe "chmod 0770 #{target_file}"
  pipe "chgrp haproxy #{target_file}"
  pipe "service haproxy reload", &block
  
end