Class: Capistrano::Configuration::Connections::GatewayConnectionFactory

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(gateway, options) ⇒ GatewayConnectionFactory

Returns a new instance of GatewayConnectionFactory.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/capistrano/configuration/connections.rb', line 25

def initialize(gateway, options)
  @options = options
  Thread.abort_on_exception = true
  @gateways = {}
  if gateway.is_a?(Hash)
    @options[:logger].debug "Creating multiple gateways using #{gateway.inspect}" if @options[:logger]
    gateway.each do |gw, hosts|
      gateway_connection = add_gateway(gw)
      [*hosts].each do |host|
        @gateways[:default] ||= gateway_connection
        @gateways[host] = gateway_connection
      end
    end
  else
    @options[:logger].debug "Creating gateway using #{[*gateway].join(', ')}" if @options[:logger]
    @gateways[:default] = add_gateway(gateway)
  end
end

Instance Method Details

#add_gateway(gateway) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/capistrano/configuration/connections.rb', line 44

def add_gateway(gateway)
  gateways = [*gateway].collect { |g| ServerDefinition.new(g) }
  tunnel = SSH.connection_strategy(gateways[0], @options) do |host, user, connect_options|
    Net::SSH::Gateway.new(host, user, connect_options)
  end
  (gateways[1..-1]).inject(tunnel) do |tunnel, destination|
    @options[:logger].debug "Creating tunnel to #{destination}" if @options[:logger]
    local_host = ServerDefinition.new("127.0.0.1", :user => destination.user, :port => tunnel.open(destination.host, (destination.port || 22)))
    SSH.connection_strategy(local_host, @options) do |host, user, connect_options|
      Net::SSH::Gateway.new(host, user, connect_options)
    end
  end
end

#connect_to(server) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/capistrano/configuration/connections.rb', line 58

def connect_to(server)
  @options[:logger].debug "establishing connection to `#{server}' via gateway" if @options[:logger]
  local_host = ServerDefinition.new("127.0.0.1", :user => server.user, :port => gateway_for(server).open(server.host, server.port || 22))
  session = SSH.connect(local_host, @options)
  session.xserver = server
  session
end

#gateway_for(server) ⇒ Object



66
67
68
# File 'lib/capistrano/configuration/connections.rb', line 66

def gateway_for(server)
  @gateways[server.host] || @gateways[:default]
end