Class: Synapse::Haproxy
- Inherits:
-
Object
- Object
- Synapse::Haproxy
- Defined in:
- lib/synapse/haproxy.rb
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#generate_base_config ⇒ Object
generates the global and defaults sections of the config file.
-
#generate_config(watchers) ⇒ Object
generates a new config based on the state of the watchers.
-
#generate_listen_stanza(watcher) ⇒ Object
generates an individual stanza for a particular watcher.
-
#initialize(opts) ⇒ Haproxy
constructor
A new instance of Haproxy.
-
#restart ⇒ Object
restarts haproxy.
-
#update_backends(watchers) ⇒ Object
tries to set active backends via haproxy’s stats socket because we can’t add backends via the socket, we might still need to restart haproxy.
- #update_config(watchers) ⇒ Object
-
#write_config(new_config) ⇒ Object
writes the config.
Constructor Details
#initialize(opts) ⇒ Haproxy
Returns a new instance of Haproxy.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/synapse/haproxy.rb', line 6 def initialize(opts) super() %w{global defaults reload_command}.each do |req| raise ArgumentError, "haproxy requires a #{req} section" if !opts.has_key?(req) end req_pairs = { 'do_writes' => 'config_file_path', 'do_socket' => 'socket_file_path', 'do_reloads' => 'reload_command'} req_pairs.each do |cond, req| if opts[cond] raise ArgumentError, "the `#{req}` option is required when `#{cond}` is true" unless opts[req] end end @opts = opts @restart_required = true end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
5 6 7 |
# File 'lib/synapse/haproxy.rb', line 5 def opts @opts end |
Instance Method Details
#generate_base_config ⇒ Object
generates the global and defaults sections of the config file
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/synapse/haproxy.rb', line 56 def generate_base_config base_config = "# auto-generated by synapse at #{Time.now}\n" %w{global defaults}.each do |section| base_config << "\n#{section}\n" @opts[section].each do |option| base_config << "\t#{option}\n" end end if @opts['extra_sections'] @opts['extra_sections'].each do |title, section| base_config << "\n#{title}\n" section.each do |option| base_config << "\t#{option}\n" end end end return base_config end |
#generate_config(watchers) ⇒ Object
generates a new config based on the state of the watchers
47 48 49 50 51 52 53 |
# File 'lib/synapse/haproxy.rb', line 47 def generate_config(watchers) new_config = generate_base_config + "\n" new_config << watchers.map {|w| generate_listen_stanza(w)}.join("\n") log.debug "synapse: new haproxy config: #{new_config}" return new_config end |
#generate_listen_stanza(watcher) ⇒ Object
generates an individual stanza for a particular watcher
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/synapse/haproxy.rb', line 79 def generate_listen_stanza(watcher) if watcher.backends.empty? log.warn "synapse: no backends found for watcher #{watcher.name}" return "" end stanza = "listen #{watcher.name} localhost:#{watcher.local_port}\n" watcher.listen.each do |line| stanza << "\t#{line}\n" end watcher.backends.shuffle.each do |backend| stanza << "\tserver #{backend['name']} #{backend['host']}:#{backend['port']} #{watcher.}\n" end return stanza end |
#restart ⇒ Object
restarts haproxy
194 195 196 197 198 |
# File 'lib/synapse/haproxy.rb', line 194 def restart res = `#{opts['reload_command']}`.chomp raise "failed to reload haproxy via #{opts['reload_command']}: #{res}" unless $?.success? @restart_required = false end |
#update_backends(watchers) ⇒ Object
tries to set active backends via haproxy’s stats socket because we can’t add backends via the socket, we might still need to restart haproxy
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/synapse/haproxy.rb', line 100 def update_backends(watchers) # first, get a list of existing servers for various backends begin s = UNIXSocket.new(@opts['socket_file_path']) s.write('show stat;') info = s.read() rescue StandardError => e log.warn "synapse: unhandled error reading stats socket: #{e.inspect}" @restart_required = true return end # parse the stats output to get current backends cur_backends = {} info.split("\n").each do |line| next if line[0] == '#' parts = line.split(',') next if ['FRONTEND', 'BACKEND'].include?(parts[1]) cur_backends[parts[0]] ||= [] cur_backends[parts[0]] << parts[1] end # build a list of backends that should be enabled enabled_backends = {} watchers.each do |watcher| enabled_backends[watcher.name] = [] next if watcher.backends.empty? unless cur_backends.include? watcher.name log.debug "synapse: restart required because we added new section #{watcher.name}" @restart_required = true return end watcher.backends.each do |backend| unless cur_backends[watcher.name].include? backend['name'] log.debug "synapse: restart required because we have a new backend #{watcher.name}/#{backend['name']}" @restart_required = true return end enabled_backends[watcher.name] << backend['name'] end end # actually enable the enabled backends, and disable the disabled ones cur_backends.each do |section, backends| backends.each do |backend| if enabled_backends[section].include? backend command = "enable server #{section}/#{backend};" else command = "disable server #{section}/#{backend};" end # actually write the command to the socket begin s = UNIXSocket.new(@opts['socket_file_path']) s.write(command) output = s.read() rescue StandardError => e log.warn "synapse: unknown error writing to socket" @restart_required = true return else unless output == "\n" log.warn "synapse: socket command #{command} failed: #{output}" @restart_required = true return end end end end end |
#update_config(watchers) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/synapse/haproxy.rb', line 28 def update_config(watchers) # if we support updating backends, try that whenever possible if @opts['do_socket'] update_backends(watchers) unless @restart_required else @restart_required = true end # generate a new config new_config = generate_config(watchers) # if we write config files, lets do that and then possibly restart if @opts['do_writes'] write_config(new_config) restart if @opts['do_reloads'] && @restart_required end end |
#write_config(new_config) ⇒ Object
writes the config
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/synapse/haproxy.rb', line 177 def write_config(new_config) begin old_config = File.read(@opts['config_file_path']) rescue Errno::ENOENT => e log.info "synapse: could not open haproxy config file at #{@opts['config_file_path']}" old_config = "" end if old_config == new_config return false else File.open(@opts['config_file_path'],'w') {|f| f.write(new_config)} return true end end |