Class: Capistrano::SSH
- Inherits:
-
Object
- Object
- Capistrano::SSH
- Defined in:
- lib/capistrano/ssh.rb
Overview
A helper class for dealing with SSH connections.
Defined Under Namespace
Modules: Server
Class Method Summary collapse
-
.connect(server, options = {}) ⇒ Object
An abstraction to make it possible to connect to the server via public key without prompting for the password.
-
.connection_strategy(server, options = {}, &block) ⇒ Object
Abstracts the logic for establishing an SSH connection (which includes testing for connection failures and retrying with a password, and so forth, mostly made complicated because of the fact that some of these variables might be lazily evaluated and try to do something like prompt the user, which should only happen when absolutely necessary.
Class Method Details
.connect(server, options = {}) ⇒ Object
An abstraction to make it possible to connect to the server via public key without prompting for the password. If the public key authentication fails this will fall back to password authentication.
server
must be an instance of ServerDefinition.
If a block is given, the new session is yielded to it, otherwise the new session is returned.
If an :ssh_options key exists in options
, it is passed to the Net::SSH constructor. Values in options
are then merged into it, and any connection information in server
is added last, so that server
info takes precedence over options
, which takes precendence over ssh_options.
39 40 41 42 43 44 |
# File 'lib/capistrano/ssh.rb', line 39 def self.connect(server, ={}) connection_strategy(server, ) do |host, user, | connection = Net::SSH.start(host, user, ) Server.apply_to(connection, server) end end |
.connection_strategy(server, options = {}, &block) ⇒ Object
Abstracts the logic for establishing an SSH connection (which includes testing for connection failures and retrying with a password, and so forth, mostly made complicated because of the fact that some of these variables might be lazily evaluated and try to do something like prompt the user, which should only happen when absolutely necessary.
This will yield the hostname, username, and a hash of connection options to the given block, which should return a new connection.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/capistrano/ssh.rb', line 54 def self.connection_strategy(server, ={}, &block) methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ] password_value = nil = (server.[:ssh_options] || {}).merge([:ssh_options] || {}) user = server.user || [:user] || [:username] || ServerDefinition.default_user port = server.port || [:port] || [:port] [:port] = port if port .delete(:username) begin = .merge( :password => password_value, :auth_methods => [:auth_methods] || methods.shift ) yield server.host, user, rescue Net::SSH::AuthenticationFailed raise if methods.empty? || [:auth_methods] password_value = [:password] retry end end |