Class: ProxyMgr::Haproxy::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/proxymgr/haproxy/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Socket

Returns a new instance of Socket.



8
9
10
# File 'lib/proxymgr/haproxy/socket.rb', line 8

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/proxymgr/haproxy/socket.rb', line 6

def path
  @path
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/proxymgr/haproxy/socket.rb', line 45

def connected?
  begin
    with do |socket|
      socket.write "show info"
    end
  rescue Errno::ECONNREFUSED
  end
end

#disable(backend, host) ⇒ Object



23
24
25
# File 'lib/proxymgr/haproxy/socket.rb', line 23

def disable(backend, host)
  write "disable server #{backend}/#{host}"
end

#enable(backend, host) ⇒ Object



19
20
21
# File 'lib/proxymgr/haproxy/socket.rb', line 19

def enable(backend, host)
  write "enable server #{backend}/#{host}"
end

#serversObject



31
32
33
34
35
36
# File 'lib/proxymgr/haproxy/socket.rb', line 31

def servers
  stats.each_with_object([]) do |stat, acc|
    next if %w(FRONTEND BACKEND).include? stat['svname']
    acc << Server.new(self, stat)
  end
end

#shutdown(backend, host) ⇒ Object



27
28
29
# File 'lib/proxymgr/haproxy/socket.rb', line 27

def shutdown(backend, host)
  write "shutdown sessions server #{backend}/#{host}"
end

#statsObject



12
13
14
15
16
17
# File 'lib/proxymgr/haproxy/socket.rb', line 12

def stats
  headers, *rest = write('show stat')
  headers = headers.gsub(/^# /, '').split(',')
  rest.pop
  rest.map { |d| Hash[headers.zip(d.split(','))] }
end

#write(cmd) ⇒ Object



38
39
40
41
42
43
# File 'lib/proxymgr/haproxy/socket.rb', line 38

def write(cmd)
  with do |socket|
    socket.puts(cmd + "\n")
    socket.readlines.map(&:chomp)
  end
end