Module: Quandl::Sandbox::Server::SSH

Extended by:
ActiveSupport::Concern
Included in:
Quandl::Sandbox::Server
Defined in:
lib/quandl/sandbox/server/ssh.rb

Instance Method Summary collapse

Instance Method Details

#ensure_connection_established!Object



5
6
7
# File 'lib/quandl/sandbox/server/ssh.rb', line 5

def ensure_connection_established!
  Sandbox::EC2.auto_retry(3){ exec("hostname") }
end

#exec(command) ⇒ Object



17
18
19
# File 'lib/quandl/sandbox/server/ssh.rb', line 17

def exec(command)
  exec_through_channel!(command)
end

#exec_through_channel!(command) ⇒ Object



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
# File 'lib/quandl/sandbox/server/ssh.rb', line 21

def exec_through_channel!(command)
  t1 = Time.now
  result = { stdout: '', stderr: '' }
  self.ssh do |ssh|
    # run event loop until channel closes
    channel = ssh.open_channel do |ch|
      # command to execute
      ch.exec command do |ch, success|
        # fail fast
        raise "could not execute command" unless success
        # on_data is stdout
        ch.on_data do |c, data|
          $stdout.print data
          result[:stdout] += data
        end
        # on_extended_data is stderr
        ch.on_extended_data do |c, type, data|
          $stderr.print data
          result[:stderr] += data
        end
        # when command completes
        ch.on_close { Quandl::Logger.debug("#{instance_id}: Hasta La Vista! (#{t1.elapsed_ms})") }
      end
    end
    channel.wait
  end
  result
end

#ssh(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/quandl/sandbox/server/ssh.rb', line 50

def ssh(&block)
  # wait for server to be ready
  await_instance_uninterruptedly unless running?
  # establish connection
  Quandl::Logger.debug "#{instance_id}: Waiting for sshd to start ... "
  # execute block through tunnel
  output = nil
  Sandbox::EC2.gateway.ssh( private_ip_address, ssh_user,
    key_data: Sandbox.configuration.key_data, keys_only: true ) do |ssh|
    output = block.call(ssh)
  end
  output
end

#upload!(local_path, remote_path = "/home/ubuntu/") ⇒ Object



9
10
11
12
13
14
15
# File 'lib/quandl/sandbox/server/ssh.rb', line 9

def upload!(local_path, remote_path="/home/ubuntu/")
  ssh do |tunnel|
    tunnel.scp.upload!( local_path, remote_path, recursive: true ) do |ch, name, sent, total|
      Quandl::Logger.debug("#{name}: #{sent}/#{total}")
    end
  end
end