summaryrefslogtreecommitdiff
path: root/lib/net/ssh
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2008-04-08 16:45:44 -0600
committerJamis Buck <jamis@37signals.com>2008-04-08 16:45:44 -0600
commit793696dbcf7ac3c763b113f611a56052493f318b (patch)
tree2b80cc27f393be1325f3cf292f27e4e3418594c7 /lib/net/ssh
parent73e7a6164730051cca2555ed70b0c34935049514 (diff)
downloadnet-ssh-multi-793696dbcf7ac3c763b113f611a56052493f318b.tar.gz
use an abbreviated session definition syntax (borrowing further from Capistrano)
Diffstat (limited to 'lib/net/ssh')
-rw-r--r--lib/net/ssh/multi.rb8
-rw-r--r--lib/net/ssh/multi/server.rb39
-rw-r--r--lib/net/ssh/multi/session.rb25
3 files changed, 47 insertions, 25 deletions
diff --git a/lib/net/ssh/multi.rb b/lib/net/ssh/multi.rb
index 8f393df..1664d0e 100644
--- a/lib/net/ssh/multi.rb
+++ b/lib/net/ssh/multi.rb
@@ -19,13 +19,13 @@ module Net; module SSH
# session.via 'gateway', 'gateway-user'
#
# # define the servers we want to use
- # session.use 'host1', 'user1'
- # session.use 'host2', 'user2'
+ # session.use 'user1@host1'
+ # session.use 'user2@host2'
#
# # define servers in groups for more granular access
# session.group :app do
- # session.use 'app1', 'user'
- # session.use 'app2', 'user'
+ # session.use 'user@app1'
+ # session.use 'user@app2'
# end
#
# # execute commands on all servers
diff --git a/lib/net/ssh/multi/server.rb b/lib/net/ssh/multi/server.rb
index 3354b0a..a9d01aa 100644
--- a/lib/net/ssh/multi/server.rb
+++ b/lib/net/ssh/multi/server.rb
@@ -6,13 +6,15 @@ module Net; module SSH; module Multi
# need to instantiate one of these directly: instead, you should use
# Net::SSH::Multi::Session#use.
class Server
+ include Comparable
+
# The Net::SSH::Multi::Session instance that manages this server instance.
attr_reader :master
# The host name (or IP address) of the server to connect to.
attr_reader :host
- # The user name to use when connecting to this server.
+ # The user name to use when logging into the server.
attr_reader :user
# The Hash of additional options to pass to Net::SSH when connecting
@@ -26,15 +28,31 @@ module Net; module SSH; module Multi
# Creates a new Server instance with the given connection information. The
# +master+ argument must be a reference to the Net::SSH::Multi::Session
# instance that will manage this server reference. The +options+ hash must
- # conform to the options described for Net::SSH::start, with one addition:
+ # conform to the options described for Net::SSH::start, with two additions:
#
# * :via => a Net::SSH::Gateway instance to use when establishing a
# connection to this server.
- def initialize(master, host, user, options={})
+ # * :user => the name of the user to use when logging into this server.
+ #
+ # The +host+ argument may include the username and port number, in which
+ # case those values take precedence over similar values given in the +options+:
+ #
+ # server = Net::SSH::Multi::Server.new(session, 'user@host:1234')
+ # puts server.user #-> user
+ # puts server.port #-> 1234
+ def initialize(master, host, options={})
@master = master
- @host = host
- @user = user
@options = options.dup
+
+ @user, @host, port = host.match(/^(?:([^;,:=]+)@|)(.*?)(?::(\d+)|)$/)[1,3]
+
+ user_opt, port_opt = @options.delete(:user), @options.delete(:port)
+
+ @user = @user || user_opt || master.default_user
+ port ||= port_opt
+
+ @options[:port] = port.to_i if port
+
@gateway = @options.delete(:via)
@failed = false
end
@@ -51,15 +69,12 @@ module Net; module SSH; module Multi
options[:port] || 22
end
- # Compares the given +server+ to this instance, and returns true if they
- # have the same host, user, and port.
- def eql?(server)
- host == server.host &&
- user == server.user &&
- port == server.port
+ # Gives server definitions a sort order, and allows comparison.
+ def <=>(server)
+ [host, port, user] <=> [server.host, server.port, server.user]
end
- alias :== :eql?
+ alias :eql? :==
# Generates a +Fixnum+ hash value for this object. This function has the
# property that +a.eql?(b)+ implies +a.hash == b.hash+. The
diff --git a/lib/net/ssh/multi/session.rb b/lib/net/ssh/multi/session.rb
index 4c6e1a0..c7462d6 100644
--- a/lib/net/ssh/multi/session.rb
+++ b/lib/net/ssh/multi/session.rb
@@ -20,13 +20,13 @@ module Net; module SSH; module Multi
# session.via 'gateway', 'gateway-user'
#
# # define the servers we want to use
- # session.use 'host1', 'user1'
- # session.use 'host2', 'user2'
+ # session.use 'user1@host1'
+ # session.use 'user2@host2'
#
# # define servers in groups for more granular access
# session.group :app do
- # session.use 'app1', 'user'
- # session.use 'app2', 'user'
+ # session.use 'user@app1'
+ # session.use 'user@app2'
# end
#
# # execute commands on all servers
@@ -65,6 +65,12 @@ module Net; module SSH; module Multi
# :warn if connection errors should cause a warning.
attr_accessor :on_error
+ # 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
+ # set.
+ attr_accessor :default_user
+
# The number of connections that are currently open.
attr_reader :open_connections #:nodoc:
@@ -92,6 +98,7 @@ module Net; module SSH; module Multi
@open_groups = []
@connect_threads = []
@on_error = :fail
+ @default_user = ENV['USER'] || ENV['USERNAME'] || "unknown"
@open_connections = 0
@pending_sessions = []
@@ -171,11 +178,11 @@ module Net; module SSH; module Multi
# a different Net::SSH::Gateway instance (or +nil+) with the :via key in
# the +options+.
#
- # session.use 'host', 'user'
- # session.use 'host2', 'user2', :via => nil
- # session.use 'host3', 'user3', :via => Net::SSH::Gateway.new('gateway.host', 'user')
- def use(host, user, options={})
- server = Server.new(self, host, user, {:via => default_gateway}.merge(options))
+ # session.use 'host'
+ # session.use 'user@host2', :via => nil
+ # session.use 'host3', :user => "user3", :via => Net::SSH::Gateway.new('gateway.host', 'user')
+ def use(host, options={})
+ server = Server.new(self, host, {:via => default_gateway}.merge(options))
exists = servers.index(server)
if exists
server = servers[exists]