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

#await_sshd_uninterruptedlyObject



14
15
16
17
18
19
20
# 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) ⇒ Object



30
31
32
# File 'lib/quandl/sandbox/server/ssh.rb', line 30

def exec(command)
  exec_through_channel!(command)
end

#exec_through_channel!(command) ⇒ Object



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
62
63
# 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!Object



5
6
7
8
9
10
11
12
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# 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/") ⇒ Object



22
23
24
25
26
27
28
# 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