Class: Halb::HAProxy

Inherits:
AbstractLoadBalancer show all
Defined in:
lib/halb/ha_proxy.rb

Instance Method Summary collapse

Methods inherited from AbstractLoadBalancer

#get_output_of, #open_connection, #perform, #put_in_maintenance, #remove_from_maintenance

Constructor Details

#initialize(host, user, ssh_keys, cluster_ip, proxy_names) ⇒ HAProxy

Returns a new instance of HAProxy.



3
4
5
6
# File 'lib/halb/ha_proxy.rb', line 3

def initialize(host, user, ssh_keys, cluster_ip, proxy_names)
  super(host, user, ssh_keys)
  @cluster_ip, @proxy_names = cluster_ip, proxy_names
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/halb/ha_proxy.rb', line 13

def active?
  get_output_of(@cluster_ip, 'hostname').strip == @host
end

#in_maintenance_command(machine) ⇒ Object



17
18
19
# File 'lib/halb/ha_proxy.rb', line 17

def in_maintenance_command(machine)
  maintenance_command_for(machine, 'disable')
end

#maintenance_command_for(machine, operation) ⇒ Object



25
26
27
28
29
30
# File 'lib/halb/ha_proxy.rb', line 25

def maintenance_command_for(machine, operation)
  commands = @proxy_names.map do |proxy_name|
    "#{operation} server #{proxy_name}/#{machine}"
  end
  "echo \"#{commands.join(' ; ')}\" | socat stdio /tmp/haproxy.sock"
end

#out_of_maintenance_command(machine) ⇒ Object



21
22
23
# File 'lib/halb/ha_proxy.rb', line 21

def out_of_maintenance_command(machine)
  maintenance_command_for(machine, 'enable')
end

#service_endpoint_for(machine) ⇒ Object



32
33
34
# File 'lib/halb/ha_proxy.rb', line 32

def service_endpoint_for(machine)
  machine
end

#show_active_hosts_commandObject



8
9
10
11
# File 'lib/halb/ha_proxy.rb', line 8

def show_active_hosts_command
  proxy_names_expression = @proxy_names.join('|')
  "echo 'show stat -1' | socat stdio /tmp/haproxy.sock | grep -P '^(#{proxy_names_expression}),.*,UP'"
end