From d3919f44368e3e9e60736fd90f31118f520747a8 Mon Sep 17 00:00:00 2001 From: elijah Date: Fri, 20 Jun 2014 15:42:51 -0700 Subject: moved json macros to provider_base/lib/macros. requires new unreleased leap_cli --- provider_base/lib/macros/haproxy.rb | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 provider_base/lib/macros/haproxy.rb (limited to 'provider_base/lib/macros/haproxy.rb') diff --git a/provider_base/lib/macros/haproxy.rb b/provider_base/lib/macros/haproxy.rb new file mode 100644 index 00000000..db0d4db8 --- /dev/null +++ b/provider_base/lib/macros/haproxy.rb @@ -0,0 +1,68 @@ +# encoding: utf-8 + +## +## HAPROXY +## + +module LeapCli + module Macro + + # + # creates a hash suitable for configuring haproxy. the key is the node name of the server we are proxying to. + # + # * node_list - a hash of nodes for the haproxy servers + # * stunnel_client - contains the mappings to local ports for each server node. + # * non_stunnel_port - in case self is included in node_list, the port to connect to. + # + # 1000 weight is used for nodes in the same location. + # 100 otherwise. + # + def haproxy_servers(node_list, stunnel_clients, non_stunnel_port=nil) + default_weight = 10 + local_weight = 100 + + # record the hosts_file + hostnames(node_list) + + # create a simple map for node name -> local stunnel accept port + accept_ports = stunnel_clients.inject({}) do |hsh, stunnel_entry| + name = stunnel_entry.first.sub /_[0-9]+$/, '' + hsh[name] = stunnel_entry.last['accept_port'] + hsh + end + + # if one the nodes in the node list is ourself, then there will not be a stunnel to it, + # but we need to include it anyway in the haproxy config. + if node_list[self.name] && non_stunnel_port + accept_ports[self.name] = non_stunnel_port + end + + # create the first pass of the servers hash + servers = node_list.values.inject(Config::ObjectList.new) do |hsh, node| + weight = default_weight + if self['location'] && node['location'] + if self.location['name'] == node.location['name'] + weight = local_weight + end + end + hsh[node.name] = Config::Object[ + 'backup', false, + 'host', 'localhost', + 'port', accept_ports[node.name] || 0, + 'weight', weight + ] + hsh + end + + # if there are some local servers, make the others backup + if servers.detect{|k,v| v.weight == local_weight} + servers.each do |k,server| + server['backup'] = server['weight'] == default_weight + end + end + + return servers + end + + end +end \ No newline at end of file -- cgit v1.2.3 From 26cbd9a70c4b19b591a6f865812f0ad98de2668c Mon Sep 17 00:00:00 2001 From: elijah Date: Sat, 21 Jun 2014 02:52:43 -0700 Subject: haproxy: support read only couchdb mirrors --- provider_base/lib/macros/haproxy.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'provider_base/lib/macros/haproxy.rb') diff --git a/provider_base/lib/macros/haproxy.rb b/provider_base/lib/macros/haproxy.rb index db0d4db8..c0f9ede5 100644 --- a/provider_base/lib/macros/haproxy.rb +++ b/provider_base/lib/macros/haproxy.rb @@ -40,17 +40,18 @@ module LeapCli # create the first pass of the servers hash servers = node_list.values.inject(Config::ObjectList.new) do |hsh, node| weight = default_weight - if self['location'] && node['location'] - if self.location['name'] == node.location['name'] - weight = local_weight - end - end + try { + weight = local_weight if self.location.name == node.location.name + } hsh[node.name] = Config::Object[ 'backup', false, 'host', 'localhost', 'port', accept_ports[node.name] || 0, 'weight', weight ] + if node.services.include?('couchdb') + hsh[node.name]['writable'] = node.couch.mode != 'mirror' + end hsh end -- cgit v1.2.3 From d341c90c1493a78ed0ee2e216797651ff0aebfa9 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 16 Jul 2014 10:32:27 +0200 Subject: haproxy connects to a local couch if available When running a service that requires couch (webapp or mx) on a node that also had couch running the haproxy was confused because it did not have an stunnel port for the local couch. Emit a more useful error and fixed this for webapp and mx --- provider_base/lib/macros/haproxy.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'provider_base/lib/macros/haproxy.rb') diff --git a/provider_base/lib/macros/haproxy.rb b/provider_base/lib/macros/haproxy.rb index c0f9ede5..602ae726 100644 --- a/provider_base/lib/macros/haproxy.rb +++ b/provider_base/lib/macros/haproxy.rb @@ -39,6 +39,10 @@ module LeapCli # create the first pass of the servers hash servers = node_list.values.inject(Config::ObjectList.new) do |hsh, node| + # make sure we have a port to talk to + unless accept_ports[node.name] + error "haproxy needs a local port to talk to when connecting to #{node.name}" + end weight = default_weight try { weight = local_weight if self.location.name == node.location.name @@ -46,7 +50,7 @@ module LeapCli hsh[node.name] = Config::Object[ 'backup', false, 'host', 'localhost', - 'port', accept_ports[node.name] || 0, + 'port', accept_ports[node.name], 'weight', weight ] if node.services.include?('couchdb') @@ -66,4 +70,4 @@ module LeapCli end end -end \ No newline at end of file +end -- cgit v1.2.3