Class: TonnelRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/dblink/tonnel_runner.rb

Constant Summary collapse

SSH_TONNEL_COMMAND =
"ssh -N -R 0.0.0.0:0:localhost:{{local_port}} -i {{pem}} -o StrictHostKeyChecking=no share@dblink.tk"
PEM_FILE =
"#{BASE_DIR}/share.pem"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_port, tmp_dir:, verbose: true) ⇒ TonnelRunner

Returns a new instance of TonnelRunner.

[View source]

7
8
9
10
11
# File 'lib/dblink/tonnel_runner.rb', line 7

def initialize(local_port, tmp_dir:, verbose: true)
  @local_port = local_port.to_i
  @verbose = verbose
  @tmp_dir = tmp_dir
end

Instance Attribute Details

#local_portObject (readonly)

Returns the value of attribute local_port.


5
6
7
# File 'lib/dblink/tonnel_runner.rb', line 5

def local_port
  @local_port
end

#remote_portObject (readonly)

Returns the value of attribute remote_port.


5
6
7
# File 'lib/dblink/tonnel_runner.rb', line 5

def remote_port
  @remote_port
end

#runner_pidObject (readonly)

Returns the value of attribute runner_pid.


5
6
7
# File 'lib/dblink/tonnel_runner.rb', line 5

def runner_pid
  @runner_pid
end

#runner_threadObject (readonly)

Returns the value of attribute runner_thread.


5
6
7
# File 'lib/dblink/tonnel_runner.rb', line 5

def runner_thread
  @runner_thread
end

Instance Method Details

#runObject

[View source]

13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dblink/tonnel_runner.rb', line 13

def run
  ssh_command = SSH_TONNEL_COMMAND.gsub('{{local_port}}', @local_port.to_s)

  puts "Copying pem file to tmp folder" if @verbose

  tmp_pem = @tmp_dir + '/share.pem'
  File.open(tmp_pem, 'w') do |file|
    file.write(File.read(PEM_FILE))
  end
  File.chmod(0600, tmp_pem)

  ssh_command.gsub!('{{pem}}', tmp_pem)

  if @verbose
    puts "EXEC #{ssh_command}"
  end
  Open3.popen3(ssh_command) {|stdin, stdout, stderr, wait_thr|
    @runner_pid = wait_thr.pid # pid of the started process.
    #p "process_started: #{pid}"
    Thread.new do
      begin
        while line = stdout.gets
          puts "STDOUT: #{line}" if @verbose
        end
      rescue Exception => error
        puts error.message
        puts error.backtrace
      end
    end
    Thread.new do
      begin
        while line = stderr.gets
          #puts "STDERR: #{line}"
          if line =~ /Allocated port/
            @remote_port = line.match(/Allocated port (\d+)\s/)[1].to_i
            #puts "Remote port: #{@remote_port}"
          end
        end
      rescue Exception => error
        puts error.message
        puts error.backtrace
      end
    end
    exit_status = wait_thr.value # Process::Status object returned.
    puts "SSH tonnel: process stoped: #{exit_status}" if @verbose
  }

  self
end

#run_asyncObject

[View source]

63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dblink/tonnel_runner.rb', line 63

def run_async
  @runner_thread = Thread.new do
    begin
      run
    rescue Exception => error
      puts error.message
      puts error.backtrace
    end
  end

  self
end

#stop_asyncObject

[View source]

76
77
78
79
80
# File 'lib/dblink/tonnel_runner.rb', line 76

def stop_async
  if @runner_pid && System.process_alive?(@runner_pid, verbose: @verbose)
    System.stop_process(@runner_pid)
  end
end

#wait_remote_port_allocatedObject

[View source]

82
83
84
85
86
87
88
89
# File 'lib/dblink/tonnel_runner.rb', line 82

def wait_remote_port_allocated
  while @remote_port.nil?
    #print '.'
    sleep 0.1
  end

  @remote_port
end