Class: Halb::LoadBalancer

Inherits:
Object
  • Object
show all
Defined in:
lib/halb/load_balancer.rb

Constant Summary collapse

SHOW_ACTIVE_HOSTS_COMMAND =
'ipvsadm -l -n'

Instance Method Summary collapse

Constructor Details

#initialize(host, user, ssh_keys) ⇒ LoadBalancer

Returns a new instance of LoadBalancer.



7
8
9
# File 'lib/halb/load_balancer.rb', line 7

def initialize(host, user, ssh_keys)
  @host, @user, @ssh_keys = host, user, ssh_keys
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/halb/load_balancer.rb', line 11

def active?
  !/TCP[ ]+\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/.match(get_output_of(SHOW_ACTIVE_HOSTS_COMMAND)).nil?
end

#get_output_of(command) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/halb/load_balancer.rb', line 15

def get_output_of(command)
  output=''
  open_connection do |ssh|
    output=ssh.exec!(command)
  end
  output
end

#open_connection(&block) ⇒ Object



47
48
49
# File 'lib/halb/load_balancer.rb', line 47

def open_connection(&block)
  Net::SSH.start(@host, 'root', :keys => @ssh_keys, &block)
end

#perform(params) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/halb/load_balancer.rb', line 37

def perform(params)
  open_connection do |ssh|
    ssh.exec!(params[:command])
    loop do
      break if params[:exit_when].call(ssh)
      sleep(1)
    end
  end
end

#put_in_maintenance(ip_address) ⇒ Object



23
24
25
26
# File 'lib/halb/load_balancer.rb', line 23

def put_in_maintenance(ip_address)
  service_endpoint=service_endpoint_for(ip_address)
  perform(:command => "touch /etc/ha.d/maintenance/#{service_endpoint}", :exit_when => lambda { |ssh| !ssh.exec!(SHOW_ACTIVE_HOSTS_COMMAND).include?(service_endpoint) })
end

#remove_from_maintenance(ip_address) ⇒ Object



28
29
30
31
# File 'lib/halb/load_balancer.rb', line 28

def remove_from_maintenance(ip_address)
  service_endpoint = service_endpoint_for(ip_address)
  perform(:command => "rm /etc/ha.d/maintenance/#{service_endpoint}", :exit_when => lambda { |ssh| ssh.exec!(SHOW_ACTIVE_HOSTS_COMMAND).include?(service_endpoint) })
end

#service_endpoint_for(ip_address) ⇒ Object



33
34
35
# File 'lib/halb/load_balancer.rb', line 33

def service_endpoint_for(ip_address)
  "#{ip_address}:80"
end