Class: Synapse::Easy::Haproxy

Inherits:
Object
  • Object
show all
Includes:
Fake::Hash
Defined in:
lib/synapse/easy/haproxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Haproxy

Returns a new instance of Haproxy.



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
52
# File 'lib/synapse/easy/haproxy.rb', line 18

def initialize params={}
  @do_writes         ||= true
  @do_reloads        ||= true
  @do_socket         ||= true

  @reload_command    ||= "haproxy -f '#{config_file_path}' -p '#{pid_file_path}' -st $(cat '#{pid_file_path}' 2>/dev/null|| true)"

  @global            ||= []
  @defaults          ||= []
  @extra_sections    ||= {}

  @nbproc            ||= 2
  @global_maxconn    ||= 4096
  @global_log_server   = params[:global_log_server]
  @global_log_server ||= Resolv::Consul.services(:syslog).sample(1).map{|x| "#{x.address}:#{x.port}"}.first
  @global_log_server ||= "127.0.0.1:514"
  @global_log_level  ||= "local0"

  @default_log       ||= "global"
  @default_retries   ||= 3
  @default_maxconn   ||= 2048
  @default_options   ||= [
    "option dontlognull",
    "option redispatch",
    "option tcpka",
  ]
  @default_balance   ||= "roundrobin"
  @default_timeouts  ||= {
    "connect" => 50000,
    "client"  => 72000000,  # 2h Timeout
    "server"  => 72000000,  # 2h Timeout
    "queue"   => 60000,
    "check"   => 10000,
  }
end

Instance Attribute Details

#config_file_pathObject

Returns the value of attribute config_file_path.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def config_file_path
  @config_file_path
end

#default_balanceObject

Returns the value of attribute default_balance.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def default_balance
  @default_balance
end

#default_logObject

Returns the value of attribute default_log.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def default_log
  @default_log
end

#default_maxconnObject

Returns the value of attribute default_maxconn.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def default_maxconn
  @default_maxconn
end

#default_optionsObject

Returns the value of attribute default_options.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def default_options
  @default_options
end

#default_retriesObject

Returns the value of attribute default_retries.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def default_retries
  @default_retries
end

#default_timeoutsObject

Returns the value of attribute default_timeouts.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def default_timeouts
  @default_timeouts
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



17
18
19
# File 'lib/synapse/easy/haproxy.rb', line 17

def defaults
  @defaults
end

#do_reloadsObject

Returns the value of attribute do_reloads.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def do_reloads
  @do_reloads
end

#do_socketObject

Returns the value of attribute do_socket.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def do_socket
  @do_socket
end

#do_writesObject

Returns the value of attribute do_writes.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def do_writes
  @do_writes
end

#extra_sectionsObject

Returns the value of attribute extra_sections.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def extra_sections
  @extra_sections
end

#frontendsObject

Returns the value of attribute frontends.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def frontends
  @frontends
end

#globalObject (readonly)

Returns the value of attribute global.



17
18
19
# File 'lib/synapse/easy/haproxy.rb', line 17

def global
  @global
end

#global_log_levelObject

Returns the value of attribute global_log_level.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def global_log_level
  @global_log_level
end

#global_log_serverObject

Returns the value of attribute global_log_server.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def global_log_server
  @global_log_server
end

#global_maxconnObject

Returns the value of attribute global_maxconn.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def global_maxconn
  @global_maxconn
end

#nbprocObject

Returns the value of attribute nbproc.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def nbproc
  @nbproc
end

#pid_file_pathObject

Returns the value of attribute pid_file_path.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def pid_file_path
  @pid_file_path
end

#reload_commandObject

Returns the value of attribute reload_command.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def reload_command
  @reload_command
end

#socket_file_pathObject

Returns the value of attribute socket_file_path.



11
12
13
# File 'lib/synapse/easy/haproxy.rb', line 11

def socket_file_path
  @socket_file_path
end

#workdirObject

Returns the value of attribute workdir.



17
18
19
# File 'lib/synapse/easy/haproxy.rb', line 17

def workdir
  @workdir
end

Instance Method Details

#config_file_nameObject



63
64
65
# File 'lib/synapse/easy/haproxy.rb', line 63

def config_file_name
  "#{instance_id}.cfg"
end

#instance_id(size = 3) ⇒ Object



53
54
55
# File 'lib/synapse/easy/haproxy.rb', line 53

def instance_id size=3
  @instance_id ||= SecureRandom.hex(size)
end

#pid_file_nameObject



69
70
71
# File 'lib/synapse/easy/haproxy.rb', line 69

def pid_file_name
  "#{instance_id}.pid"
end

#socket_file_nameObject



66
67
68
# File 'lib/synapse/easy/haproxy.rb', line 66

def socket_file_name
  "#{instance_id}.sock"
end

#to_synapseObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/synapse/easy/haproxy.rb', line 98

def to_synapse
  FileUtils.mkdir_p workdir.to_s unless Dir.exists? workdir.to_s
  {
    reload_command: reload_command,
    do_writes: do_writes,
    do_reloads: do_reloads,
    do_socket: do_socket,
    config_file_path: config_file_path,
    socket_file_path: socket_file_path,
    global: global,
    defaults: defaults,
    extra_sections: extra_sections
  }.with_indifferent_access
end