Class: Halb::AbstractLoadBalancer

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

Direct Known Subclasses

HAProxy, LoadBalancer

Instance Method Summary collapse

Constructor Details

#initialize(host, user, ssh_keys) ⇒ AbstractLoadBalancer

Returns a new instance of AbstractLoadBalancer.



5
6
7
# File 'lib/halb/abstract_load_balancer.rb', line 5

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

Instance Method Details

#get_output_of(host = @host, command) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/halb/abstract_load_balancer.rb', line 9

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

#open_connection(host = @host, &block) ⇒ Object



39
40
41
# File 'lib/halb/abstract_load_balancer.rb', line 39

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

#perform(params) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/halb/abstract_load_balancer.rb', line 29

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(machine) ⇒ Object



17
18
19
20
21
# File 'lib/halb/abstract_load_balancer.rb', line 17

def put_in_maintenance(machine)
  service_endpoint=service_endpoint_for(machine)
  perform(:command => in_maintenance_command(service_endpoint),
          :exit_when => lambda { |ssh| !ssh.exec!(show_active_hosts_command).to_s.include?(service_endpoint) })
end

#remove_from_maintenance(host) ⇒ Object



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

def remove_from_maintenance(host)
  service_endpoint = service_endpoint_for(host)
  perform(:command => out_of_maintenance_command(service_endpoint),
          :exit_when => lambda { |ssh| ssh.exec!(show_active_hosts_command).to_s.include?(service_endpoint) })
end