From 3aa1a63c0877a191da84fac033c3591c428f0998 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Tue, 26 Oct 2010 17:49:12 +0100 Subject: [PATCH] Add :allow_duplicate_servers option to Session and ServerList. --- lib/net/ssh/multi/server_list.rb | 20 +++++++++++++------- lib/net/ssh/multi/session.rb | 9 +++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/net/ssh/multi/server_list.rb b/lib/net/ssh/multi/server_list.rb index da1914f..b90d39f 100644 --- a/lib/net/ssh/multi/server_list.rb +++ b/lib/net/ssh/multi/server_list.rb @@ -13,7 +13,9 @@ class ServerList # Create a new ServerList that wraps the given server list. Duplicate entries # will be discarded. def initialize(list=[]) - @list = list.uniq + options = list.last.is_a?(Hash) ? list.pop : {} + @allow_duplicate_servers = options.delete :allow_duplicate_servers + @list = @allow_duplicate_servers ? list : list.uniq end # Adds the given server to the list, and returns the argument. If an @@ -21,11 +23,15 @@ def initialize(list=[]) # argument is _not_ added, and the existing server record is returned # instead. def add(server) - index = @list.index(server) - if index - server = @list[index] - else + if @allow_duplicate_servers @list.push(server) + else + index = @list.index(server) + if index + server = @list[index] + else + @list.push(server) + end end server end @@ -71,10 +77,10 @@ def flatten end end - result.uniq + @allow_duplicate_servers ? result : result.uniq end alias to_ary flatten end -end; end; end \ No newline at end of file +end; end; end diff --git a/lib/net/ssh/multi/session.rb b/lib/net/ssh/multi/session.rb index 333fdf3..c8fb2d8 100644 --- a/lib/net/ssh/multi/session.rb +++ b/lib/net/ssh/multi/session.rb @@ -142,6 +142,11 @@ class Session # :warn if connection errors should cause a warning. attr_accessor :on_error + # Whether to allow duplicate server definitions. This defaults to false, + # but you may set it to true if you really want to run parallel commands on + # the same server. + attr_accessor :allow_duplicate_servers + # The default user name to use when connecting to a server. If a user name # is not given for a particular server, this value will be used. It defaults # to ENV['USER'] || ENV['USERNAME'], or "unknown" if neither of those are @@ -169,7 +174,7 @@ class Session # session.use ... # end def initialize(options={}) - @server_list = ServerList.new + @server_list = ServerList.new([ { :allow_duplicate_servers => options[:allow_duplicate_servers] } ]) @groups = Hash.new { |h,k| h[k] = ServerList.new } @gateway = nil @open_groups = [] @@ -357,7 +362,7 @@ def servers_for(*criteria) aggregator.concat(servers) end - list.uniq + allow_duplicate_servers ? list : list.uniq end end