class Tunneler::Ssh

Constants

ACCEPTABLE_CONNECTIVITY_ERRORS
SCP_OPTIONS
SSH_COMMAND_TIMEOUT
SSH_CONNECTIVITY_TIMEOUT
SSH_READY_TIMEOUT

Public Class Methods

default_options() click to toggle source
# File lib/tunneler/ssh.rb, line 18
def self.default_options
  {
    :paranoid => false,
    :keys => [DEFAULT_SSH_KEY_PATH],
    :timeout => self::SSH_COMMAND_TIMEOUT,
  }
end
merge_options(options={}) click to toggle source
# File lib/tunneler/ssh.rb, line 26
def self.merge_options(options={})
  if options.empty?
    Ssh.default_options
  else
    Ssh.default_options.merge(options)
  end
end
new(user, host, options={}) click to toggle source
# File lib/tunneler/ssh.rb, line 12
def initialize(user, host, options={})
  @user = user
  @host = host
  @options = self.class.merge_options(options)
end

Public Instance Methods

scp(local_path, remote_path) click to toggle source
# File lib/tunneler/ssh.rb, line 78
def scp(local_path, remote_path)
  Net::SSH.start(@host, @user, @options) do |ssh|
    ssh.scp.upload!(local_path, remote_path, SCP_OPTIONS)
  end
end
ssh(command, options_override={}) click to toggle source
# File lib/tunneler/ssh.rb, line 65
def ssh(command, options_override={})
  if options_override.empty?
    options = @options
  else
    options = options_override
  end
  output = ""
  Net::SSH.start(@host, @user, @options) do |ssh|
    output = ssh.exec!(command)
  end
  output
end
sshable?() click to toggle source
# File lib/tunneler/ssh.rb, line 43
def sshable?
  begin
    return test_connectivity
  rescue Exception => e
    if ACCEPTABLE_CONNECTIVITY_ERRORS.include?(e.class)
      Tunneler.log "Connectivity error: #{e.class} - #{e.message}", :debug
      return false
    else
      raise e
    end
  end
end
test_connectivity() click to toggle source
# File lib/tunneler/ssh.rb, line 56
def test_connectivity
  options_override = @options.clone
  options_override[:timeout] = self.class::SSH_CONNECTIVITY_TIMEOUT
  options_override[:verbose] = :debug if Tunneler.debug
  if ssh("whoami", options_override).strip == @user
    return true
  end
end
wait_for_ssh_access() click to toggle source
# File lib/tunneler/ssh.rb, line 34
def wait_for_ssh_access
  Timer.timeout("#{@host}: SSH test",self.class::SSH_READY_TIMEOUT) do
    until sshable? do
      Tunneler.log "Waiting between connection attempts", :debug
      sleep 5
    end
  end
end