Class: Wemux::Pair::SshTunnel

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

Direct Known Subclasses

ClientTunnel, HostTunnel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port_offset) ⇒ SshTunnel

Returns a new instance of SshTunnel.



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

def initialize(port_offset)
  @port_offset = port_offset
end

Instance Attribute Details

#port_offsetObject (readonly)

Returns the value of attribute port_offset.



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

def port_offset
  @port_offset
end

Class Method Details

.tunnel_failure!Object



12
13
14
15
# File 'lib/wemux/pair/ssh_tunnel.rb', line 12

def self.tunnel_failure!
  puts "The tunnel failed to open, the port was probably in use, try again."
  exit 1
end

Instance Method Details

#client_portObject



21
22
23
# File 'lib/wemux/pair/ssh_tunnel.rb', line 21

def client_port
  9990 + port_offset
end

#close_tunnelsObject



40
41
42
43
# File 'lib/wemux/pair/ssh_tunnel.rb', line 40

def close_tunnels
  puts "Closing existing tunnels..."
  system "ssh #{ssh_options} -O exit #{ssh_user}@#{ssh_host}" until $? && !$?.success?
end

#host_portObject



17
18
19
# File 'lib/wemux/pair/ssh_tunnel.rb', line 17

def host_port
  1230 + port_offset
end

#openObject



25
26
27
28
29
# File 'lib/wemux/pair/ssh_tunnel.rb', line 25

def open
  close_tunnels
  puts "Opening tunnel..."
  system "ssh #{ssh_options} -f -N #{ssh_forwards} #{ssh_user}@#{ssh_host}"
end

#ssh_optionsObject



31
32
33
34
35
36
37
38
# File 'lib/wemux/pair/ssh_tunnel.rb', line 31

def ssh_options
  [
    "ExitOnForwardFailure yes",
    "ControlPersist yes",
    "ControlPath #{control_path}",
    "ControlMaster auto",
  ].map { |o| "-o \"#{o}\""}.join(" ")
end