Class: Capistrano::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/ssh.rb

Overview

A helper class for dealing with SSH connections.

Defined Under Namespace

Modules: Server

Constant Summary collapse

DEFAULT_PORT =

The default port for SSH.

22

Class Method Summary collapse

Class Method Details

.connect(server, options = {}, &block) ⇒ 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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/capistrano/ssh.rb', line 85

def self.connect(server, options={}, &block)
  methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ]
  password_value = nil
  
  ssh_options = (options[:ssh_options] || {}).dup
  ssh_options[:username] = server.user || options[:user] || ssh_options[:username]
  ssh_options[:port]     = server.port || options[:port] || ssh_options[:port] || DEFAULT_PORT

  begin
    connection_options = ssh_options.merge(
      :password => password_value,
      :auth_methods => ssh_options[:auth_methods] || methods.shift
    )

    connection = Net::SSH.start(server.host, connection_options, &block)
    Server.apply_to(connection, server)

  rescue Net::SSH::AuthenticationFailed
    raise if methods.empty? || ssh_options[:auth_methods]
    password_value = options[:password]
    retry
  end
end