Class: Dizby::BasicSpawnTunnel

Inherits:
AbstractTunnel show all
Defined in:
lib/dizby/tunnel/basic_spawn.rb

Instance Attribute Summary

Attributes inherited from AbstractTunnel

#local_port, #remote_port

Instance Method Summary collapse

Methods inherited from AbstractTunnel

#close, #open_ssh, #read_ports

Constructor Details

#initialize(server, strategy, command, user, host) ⇒ BasicSpawnTunnel

Returns a new instance of BasicSpawnTunnel.


7
8
9
10
11
# File 'lib/dizby/tunnel/basic_spawn.rb', line 7

def initialize(server, strategy, command, user, host)
  @command = command

  super(server, strategy, user, host)
end

Instance Method Details

#get_and_write_ports(ssh, output)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dizby/tunnel/basic_spawn.rb', line 13

def get_and_write_ports(ssh, output)
  @command.set_dynamic_mode unless @tunnel.server_port

  @channel = ssh.open_channel do |ch|
    ch.exec @command.to_cmd do |_, success|
      fail SpawnError, 'could not spawn host' unless success

      # it is already triggered if the port is set
      get_remote_server_port(ch) if @command.dynamic?
    end
  end

  ssh.loop { !@channel[:triggered] } if @command.dynamic?
  @channel.eof!

  super
end

#get_remote_server_port(ch)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dizby/tunnel/basic_spawn.rb', line 31

def get_remote_server_port(ch)
  ch[:data] = ''
  ch[:triggered] = false

  ch.on_data { |_, data| ch[:data] << data }
  ch.on_extended_data { |_, _, data| @server.log(data.inspect) }

  ch.on_process do |_|
    if !ch[:triggered] && ch[:data] =~ /Running on port (\d+)\./
      @strategy.instance_variable_set(:@server_port, $~[1])
      ch[:triggered] = true
    end
  end
end

#wait(ssh)


46
47
48
# File 'lib/dizby/tunnel/basic_spawn.rb', line 46

def wait(ssh)
  ssh.loop { @channel.active? }
end