Class: Aerosol::Connection

Inherits:
Object
  • Object
show all
Includes:
Dockly::Util::DSL, Dockly::Util::Logger::Mixin
Defined in:
lib/aerosol/connection.rb

Instance Method Summary collapse

Instance Method Details

#with_connection(overridden_host = nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aerosol/connection.rb', line 11

def with_connection(overridden_host=nil, &block)
  actual_host = overridden_host || host
  unless actual_host.is_a?(String)
    actual_host = actual_host.address
  end

  if jump
    info "connecting to gateway #{jump[:user] || user}@#{jump[:host]}"
    gateway = nil
    Timeout.timeout(20) do
      gateway = Net::SSH::Gateway.new(jump[:host], jump[:user] || user, :forward_agent => true)
    end

    begin
      info "connecting to #{user}@#{actual_host} through gateway"
      gateway.ssh(actual_host, user, &block)
    ensure
      info "shutting down gateway connection"
      gateway.shutdown!
    end
  else
    info "connecting to #{user}@#{actual_host}"
    Net::SSH.start(actual_host, user, &block)
  end
rescue Timeout::Error => ex
  error "Timeout error #{ex.message}"
  error ex.backtrace.join("\n")
end