class Exos::Commands::SSH

Constants

PROTOCOLS

Public Instance Methods

run() click to toggle source
# File lib/exos/commands/ssh.rb, line 23
def run
  # Handle "connection string" style parameter.
  if conn = args.first
    user, str = conn.split("@")

    abort "Malformed parameter: user@[instance-name|instance-id]." if str.nil?

    str.match(/i-[0-9a-f]+/i) ? options.id = str : options.name = str
  else
    user = options.user
  end

  abort "Must specify username with -u or 'user@instance-name' parameter." if user.nil?

  if options.id
    instance = ec2.servers.all("instance-id" => options.id).first
  elsif options.name
    instance = ec2.servers.all("tag:Name" => options.name).first
  else
    instance = ec2.servers.all("tag:Role" => options.role).first
  end

  abort "No instance found. Exiting." if instance.nil?

  puts "Connecting to instance #{ instance.id }..."
  host = "#{ user }@#{ instance.dns_name }"

  if options.proto
    abort "Unknown protocol '#{ options.proto }'." unless PROTOCOLS.include?(options.proto)
    exec "#{ options.proto } #{ host }"
  else
    begin
      exec "mosh #{ host }"
    rescue Errno::ENOENT
      exec "ssh #{ host }"
    end
  end
end