Class: Aptible::CLI::Helpers::Tunnel
- Inherits:
-
Object
- Object
- Aptible::CLI::Helpers::Tunnel
- Defined in:
- lib/aptible/cli/helpers/tunnel.rb
Instance Method Summary collapse
-
#initialize(env, ssh_cmd, socket_path) ⇒ Tunnel
constructor
A new instance of Tunnel.
- #port ⇒ Object
- #start(desired_port = 0) ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(env, ssh_cmd, socket_path) ⇒ Tunnel
Returns a new instance of Tunnel.
26 27 28 29 30 |
# File 'lib/aptible/cli/helpers/tunnel.rb', line 26 def initialize(env, ssh_cmd, socket_path) @env = env @ssh_cmd = ssh_cmd @socket_path = socket_path end |
Instance Method Details
#port ⇒ Object
78 79 80 81 |
# File 'lib/aptible/cli/helpers/tunnel.rb', line 78 def port raise 'You must call #start before calling #port!' if @local_port.nil? @local_port end |
#start(desired_port = 0) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aptible/cli/helpers/tunnel.rb', line 32 def start(desired_port = 0) @local_port = desired_port @local_port = random_local_port if @local_port.zero? tunnel_cmd = @ssh_cmd + [ '-L', "#{@local_port}:#{@socket_path}", '-o', 'ExitOnForwardFailure=yes' ] out_read, out_write = IO.pipe err_read, err_write = IO.pipe @pid = Process.spawn(@env, *tunnel_cmd, SPAWN_OPTS .merge(in: :close, out: out_write, err: err_write)) # Wait for the tunnel to come up before returning. The other end # will send a message on stdout to indicate that the tunnel is ready. [out_write, err_write].map(&:close) begin out_read.readline rescue EOFError stop e = 'Tunnel did not come up, is something else listening on port ' \ "#{@local_port}?\n#{err_read.read}" raise e ensure [out_read, err_read].map(&:close) end end |
#stop ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/aptible/cli/helpers/tunnel.rb', line 62 def stop raise 'You must call #start before calling #stop' if @pid.nil? begin Process.kill(STOP_SIGNAL, @pid) rescue Errno::ESRCH nil # Dear Rubocop: I know what I'm doing. end wait end |
#wait ⇒ Object
72 73 74 75 76 |
# File 'lib/aptible/cli/helpers/tunnel.rb', line 72 def wait Process.wait @pid rescue Errno::ECHILD nil end |