Class: Capissh::ConnectionManager::GatewayConnectionFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/capissh/connection_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(gateway, options) ⇒ GatewayConnectionFactory

Returns a new instance of GatewayConnectionFactory.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/capissh/connection_manager.rb', line 21

def initialize(gateway, options)
  @options = options
  Thread.abort_on_exception = true
  @gateways = {}
  @default_gateway = nil
  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)
      Array(hosts).each do |host|
        # Why is the default only set if there's at least one host?
        # It seems odd enough to be intentional, since it could easily be set outside of the loop.
        @default_gateway ||= gateway_connection
        @gateways[host] = gateway_connection
      end
    end
  else
    @options[:logger].debug "Creating gateway using #{Array(gateway).join(', ')}" if @options[:logger]
    @default_gateway = add_gateway(gateway)
  end
end

Instance Method Details

#add_gateway(gateway) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capissh/connection_manager.rb', line 43

def add_gateway(gateway)
  gateways = ServerDefinition.wrap_list(gateway)

  tunnel = SSH.gateway(gateways.shift, @options)

  gateways.inject(tunnel) do |tunnel, destination|
    @options[:logger].debug "Creating tunnel to #{destination}" if @options[:logger]
    local = local_host(destination, tunnel)
    SSH.gateway(local, @options)
  end
end

#connect_to(server) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/capissh/connection_manager.rb', line 55

def connect_to(server)
  @options[:logger].debug "establishing connection to `#{server}' via gateway" if @options[:logger]
  local = local_host(server, gateway_for(server))
  session = SSH.connect(local, @options)
  session.xserver = server
  session
end

#gateway_for(server) ⇒ Object



67
68
69
# File 'lib/capissh/connection_manager.rb', line 67

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

#local_host(destination, tunnel) ⇒ Object



63
64
65
# File 'lib/capissh/connection_manager.rb', line 63

def local_host(destination, tunnel)
  ServerDefinition.new("127.0.0.1", :user => destination.user, :port => tunnel.open(destination.host, destination.connect_to_port))
end