class Fulmar::Infrastructure::Service::TunnelService
Opens an ssh tunnel to a remote host so other services can access mysql for example
Attributes
host[R]
local_port[R]
remote_port[R]
Public Class Methods
new(host, port, remote_host = 'localhost')
click to toggle source
# File lib/fulmar/infrastructure/service/tunnel_service.rb, line 10 def initialize(host, port, remote_host = 'localhost') @host = host @remote_port = port @remote_host = remote_host.nil? ? 'localhost' : remote_host @local_port = 0 @tunnel_pid = 0 end
Public Instance Methods
close()
click to toggle source
# File lib/fulmar/infrastructure/service/tunnel_service.rb, line 24 def close Process.kill 'TERM', @tunnel_pid if @tunnel_pid > 0 @local_port = 0 @tunnel_pid = 0 end
free_port()
click to toggle source
# File lib/fulmar/infrastructure/service/tunnel_service.rb, line 34 def free_port (60_000..61_000).each do |port| begin socket = TCPSocket.new('localhost', port) socket.close rescue Errno::ECONNREFUSED return port end end fail 'Cannot find an open local port' end
open()
click to toggle source
# File lib/fulmar/infrastructure/service/tunnel_service.rb, line 18 def open @local_port = free_port @tunnel_pid = Process.spawn "ssh #{@host} -q -L #{@local_port}:#{@remote_host}:#{@remote_port} -N" sleep 1 end
open?()
click to toggle source
# File lib/fulmar/infrastructure/service/tunnel_service.rb, line 30 def open? @tunnel_pid > 0 end