Class: Ec2ssh::App

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2ssh.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = "~/.ec2ssh", account = :default) ⇒ App

Returns a new instance of App.



17
18
19
# File 'lib/ec2ssh.rb', line 17

def initialize(file = "~/.ec2ssh", =:default)
  @config = read_aws_config(file, )
end

Instance Method Details

#select_instance(instances = []) ⇒ Object



21
22
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ec2ssh.rb', line 21

def select_instance(instances=[])
  # TODO: Ansi colors https://github.com/JEG2/highline/blob/master/examples/ansi_colors.rb
  instances = get_all_ec2_instances
  table_rows = []

  instances.each do |i|
    if i[:aws_state] == "running"
      table_rows << ['', i[:tags]["Name"], i[:aws_instance_id], i[:aws_groups].join(','), i[:ssh_key_name], i[:aws_private_ip_address], i[:dns_name], i[:architecture], i[:aws_instance_type]]
    end
  end

  # sort tables by tag name
  table_rows.sort! do |a, b|
    a[1] <=> b[1]
  end

  # give them numbers
  table_rows.count.times do |i|
    table_rows[i][0] = i + 1
  end

  table_header = ['', 'Name', 'Instance ID', 'SecGroup', 'Key', 'Internal IP', 'Public DNS', 'Arch', 'Type']

  table = Text::Table.new :rows => table_rows, :head => table_header

  # output table
  puts table

  input = ask(">>  ")
  options = input.split

  input_host = options[0]
  input_user = options[1]
  input_key  = options[2]

  if input_host =~ /^\d+$/
    host = table_rows[input_host.to_i - 1]
  else
    host = table_rows.find { |h| h[1] == input_host }
  end

  host_ssh_key    = host[4]
  host_private_ip = host[5]
  host_public_dns = host[6]

  default_user = @config[:default_user] || Etc.getlogin

  template = @config[:template] || "ssh #{default_user}@<public_dns>"

  # <instance> remains for compatibility with upstream
  command = template.gsub("<instance>", host_public_dns).
                     gsub("<public_dns>", host_public_dns).
                     gsub("<private_ip>", host_private_ip)

  # interpolate ssh user
  if input_user.blank?
    command.gsub!("<user>", default_user)
  else
    command.gsub!("<user>", input_user)
  end

  # interpolate ssh key
  if input_key.blank?
    command.gsub!("<key>", "")
  else
    command.gsub!("<key>", "-i ~/.ssh/#{input_key}.pem")
  end

  puts "!!! #{command}"
  exec(command)
end