Class: Exos::Commands::SSH

Inherits:
Command
  • Object
show all
Defined in:
lib/exos/commands/ssh.rb

Constant Summary collapse

PROTOCOLS =
%w(mosh ssh).freeze

Instance Attribute Summary

Attributes inherited from Command

#args, #options

Instance Method Summary collapse

Methods inherited from Command

#initialize

Constructor Details

This class inherits a constructor from Exos::Commands::Command

Instance Method Details

#runObject



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
49
50
51
52
53
54
55
56
57
58
59
60
# 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