Class: SSHTunnel

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SSHTunnel.



6
7
8
# File 'lib/shared/ssh_tunnel.rb', line 6

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

Instance Method Details

#connectObject



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

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

#openObject



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

def open
  connect
  
  start_time = Time.now
  while true
    break if @up
    sleep 0.5
    
    if Time.now - start_time > 5
      puts "SSH connection failed, trying again..."
      start_time = Time.now
      connect
    end
  end
end