]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
add macro to support for haproxy with weights
authorelijah <elijah@riseup.net>
Fri, 26 Jul 2013 09:30:52 +0000 (02:30 -0700)
committerelijah <elijah@riseup.net>
Fri, 26 Jul 2013 09:30:52 +0000 (02:30 -0700)
lib/leap_cli/config/macros.rb
lib/leap_cli/version.rb

index 94ad034c7774a0e9662a65c3f04402430d1bce6a..f46799cb56d1f8bb19029484200073857c039b26 100644 (file)
@@ -151,7 +151,7 @@ module LeapCli; module Config
         nodes = ObjectList.new nodes
       end
       nodes.each_node do |node|
-        @referenced_nodes[node.name] = node
+        @referenced_nodes[node.name] ||= node
       end
       return nodes.values.collect {|node| node.domain.name}
     end
@@ -184,19 +184,6 @@ module LeapCli; module Config
       end
     end
 
-    def known_hosts_file
-      return nil unless @referenced_nodes
-      entries = []
-      @referenced_nodes.each_node do |node|
-        hostnames = [node.name, node.domain.internal, node.domain.full, node.ip_address].join(',')
-        pub_key   = Util::read_file([:node_ssh_pub_key,node.name])
-        if pub_key
-          entries << [hostnames, pub_key].join(' ')
-        end
-      end
-      entries.join("\n")
-    end
-
     ##
     ## STUNNEL
     ##
@@ -229,7 +216,7 @@ module LeapCli; module Config
       hostnames(node_list) # record the hosts
       node_list.values.inject(Config::ObjectList.new) do |hsh, node|
         if node.name != self.name || options[:include_self]
-          hsh["#{node.name}#{port}"] = Config::Object[
+          hsh["#{node.name}_#{port}"] = Config::Object[
             'accept_port', @next_stunnel_port,
             'connect', node.domain.internal,
             'connect_port', stunnel_port(port)
@@ -262,6 +249,63 @@ module LeapCli; module Config
       end
     end
 
+    ##
+    ## HAPROXY
+    ##
+
+    #
+    # creates a hash suitable for configuring haproxy. the key is the node name of the server we are proxying to.
+    #
+    # stunnel_client contains the mappings to local ports for each node.
+    #
+    # 1000 weight is used for nodes in the same location.
+    # 100 otherwise.
+    #
+    def haproxy_servers(node_list, stunnel_clients)
+      default_weight = 10
+      local_weight = 1000
+
+      # 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
+
+      # 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
+
+    ##
+    ## SSH
+    ##
+
     #
     # creates a hash from the ssh key info in users directory, for use in updating authorized_keys file
     #
@@ -278,5 +322,18 @@ module LeapCli; module Config
       hash
     end
 
+    def known_hosts_file
+      return nil unless @referenced_nodes
+      entries = []
+      @referenced_nodes.each_node do |node|
+        hostnames = [node.name, node.domain.internal, node.domain.full, node.ip_address].join(',')
+        pub_key   = Util::read_file([:node_ssh_pub_key,node.name])
+        if pub_key
+          entries << [hostnames, pub_key].join(' ')
+        end
+      end
+      entries.join("\n")
+    end
+
   end
 end; end
index 7f019c49d6b9010fb544348068a81f5a894a4d97..0e96de694dc9d34659643b92e6f9fffcb3d08151 100644 (file)
@@ -1,6 +1,6 @@
 module LeapCli
   unless defined?(LeapCli::VERSION)
-    VERSION = '1.1.3'
+    VERSION = '1.1.4'
     COMPATIBLE_PLATFORM_VERSION = '0.2.3'..'1.99'
     SUMMARY = 'Command line interface to the LEAP platform'
     DESCRIPTION = 'The command "leap" can be used to manage a bevy of servers running the LEAP platform from the comfort of your own home.'