Class: Freighter::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/freighter/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, ssh_conf) ⇒ SSH

Returns a new instance of SSH.



8
9
10
11
12
13
# File 'lib/freighter/ssh.rb', line 8

def initialize(host, ssh_conf)
  @host = host
  user_name = ssh_conf.delete([:user_name])
  @user = OPTIONS.ssh_user || user_name
  @ssh_options = ssh_conf
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



6
7
8
# File 'lib/freighter/ssh.rb', line 6

def thread
  @thread
end

Instance Method Details

#proxyObject



15
16
17
18
# File 'lib/freighter/ssh.rb', line 15

def proxy
  docker_port = OPTIONS.config['connection']['docker']['port']
  Net::SSH::Proxy::SOCKS5.new(@host, docker_port, {user: @user}.merge(@ssh_options))
end

#tunneled_proxy(local_port, use_proxy = false, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/freighter/ssh.rb', line 20

def tunneled_proxy(local_port, use_proxy=false, &block)
  options = use_proxy ? { proxy: proxy } : @ssh_options
  docker_port = OPTIONS.config['docker']['port']

  LOGGER.debug "Connecting\n  host: #{@host}, user: #{@user}, options: #{options.inspect}"
  @thread = Thread.new do
    Thread.current.thread_variable_set(:ssh_tunnel_established, false)

    Net::SSH.start(@host, @user, options) do |session|
      session.forward.local(local_port, "0.0.0.0", docker_port)
      LOGGER.debug "Connected to #{@host} and port #{local_port} is forwarded to host's docker REST API port ##{docker_port}."

      Thread.current.thread_variable_set(:ssh_tunnel_established, true)
      int_pressed = false
      trap("INT") { int_pressed = true }
      session.loop(0.1) { !session.closed? && !int_pressed }
    end
  end

  while @thread.thread_variable_get(:ssh_tunnel_established) != true
    sleep 0.1
  end

  yield
end