module Quandl::Sandbox::Server::SSH

Public Instance Methods

await_sshd_uninterruptedly() click to toggle source
# File lib/quandl/sandbox/server/ssh.rb, line 14
def await_sshd_uninterruptedly
  t1 = Time.now
  Quandl::Logger.debug("#{instance_id}: await_sshd_uninterruptedly")
  @await_sshd_uninterruptedly ||= Quandl::Sandbox::EC2.auto_retry(3){ exec("hostname") }
  Quandl::Logger.debug "#{instance_id}: await_sshd_uninterruptedly finished (#{t1.elapsed_ms})"
  @await_sshd_uninterruptedly
end
exec(command) click to toggle source
# File lib/quandl/sandbox/server/ssh.rb, line 30
def exec(command)
  exec_through_channel!(command)
end
exec_through_channel!(command) click to toggle source
# File lib/quandl/sandbox/server/ssh.rb, line 34
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[:stdout] = result[:stdout].to_s.strip.rstrip
  result[:stderr] = result[:stderr].to_s.strip.rstrip
  result
end
launch!() click to toggle source
Calls superclass method
# File lib/quandl/sandbox/server/ssh.rb, line 5
def launch!
  # only new servers can be launched
  return false unless new_record
  # launch instance
  super if defined?(super)
  # wait for sshd to start on server
  await_sshd_uninterruptedly
end
ssh(&block) click to toggle source
# File lib/quandl/sandbox/server/ssh.rb, line 65
def ssh(&block)
  # wait for server to launch
  await_instance_uninterruptedly unless running?
  # execute block through tunnel
  output = nil
  Quandl::Sandbox::EC2.gateway.ssh( private_ip_address, ssh_user,
    key_data: Quandl::Sandbox.configuration.key_data, keys_only: true ) do |ssh|
    output = block.call(ssh)
  end
  output
end
upload!(local_path, remote_path="/home/ubuntu/") click to toggle source
# File lib/quandl/sandbox/server/ssh.rb, line 22
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