Class: SSHTunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/shared/ssh_tunnel.rb

Constant Summary collapse

TIMEOUT_SECONDS =

10 seconds was too short for users with poorer connections.

15

Instance Method Summary collapse

Constructor Details

#initialize(host, user, local_port = 2288) ⇒ SSHTunnel

Returns a new instance of SSHTunnel.



8
9
10
# File 'lib/shared/ssh_tunnel.rb', line 8

def initialize(host, user, local_port = 2288)
  @host, @user, @local_port = host, user, local_port
end

Instance Method Details

#connectObject



28
29
30
31
32
33
34
35
36
# File 'lib/shared/ssh_tunnel.rb', line 28

def connect
  @thread.kill if @thread
  @thread = Thread.new do
    Net::SSH.start(@host, @user, { :timeout => TIMEOUT_SECONDS }) do |ssh|
      ssh.forward.local(@local_port, 'localhost', Testbot::SERVER_PORT)
      ssh.loop {  @up = true }
    end
  end
end

#openObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/shared/ssh_tunnel.rb', line 12

def open
  connect

  start_time = Time.now
  while true
    break if @up
    sleep 0.5

    if Time.now - start_time > TIMEOUT_SECONDS
      puts "SSH connection failed, trying again... (#{@user}@#{@host})"
      start_time = Time.now
      connect
    end
  end
end