Method: Capistrano::SSH.connection_strategy

Defined in:
lib/capistrano/ssh.rb

.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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/capistrano/ssh.rb', line 48

def self.connection_strategy(server, options={}, &block)
  methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ]
  password_value = nil

  # construct the hash of ssh options that should be passed more-or-less
  # directly to Net::SSH. This will be the general ssh options, merged with
  # the server-specific ssh-options.
  ssh_options = (options[:ssh_options] || {}).merge(server.options[:ssh_options] || {})

  # load any SSH configuration files that were specified in the SSH options. This
  # will load from ~/.ssh/config and /etc/ssh_config by default (see Net::SSH
  # for details). Merge the explicitly given ssh_options over the top of the info
  # from the config file.
  ssh_options = Net::SSH.configuration_for(server.host, ssh_options.fetch(:config, true)).merge(ssh_options)

  # Once we've loaded the config, we don't need Net::SSH to do it again.
  ssh_options[:config] = false

  ssh_options[:verbose] = :debug if options[:verbose] && options[:verbose] > 0

  user = server.user || options[:user] || ssh_options[:username] ||
         ssh_options[:user] || ServerDefinition.default_user
  port = server.port || options[:port] || ssh_options[:port]

  # the .ssh/config file might have changed the host-name on us
  host = ssh_options.fetch(:host_name, server.host)

  ssh_options[:port] = port if port

  # delete these, since we've determined which username to use by this point
  ssh_options.delete(:username)
  ssh_options.delete(:user)

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

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