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
20
21
# File 'lib/ec2ssh.rb', line 17

def initialize(file = "~/.ec2ssh", =:default)
  @config = read_aws_config(file, )
  @config[:cache_ttl] ||= 0
  @config[:account] = .to_s
end

Instance Method Details

#select_instance(instances = []) ⇒ Object



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ec2ssh.rb', line 23

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"].blank? ? '' : 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

  puts ">> id-or-name <user> <key> <public or private>"
  input = ask(">> ")
  options = input.split

  # check if last arg is 'public' or 'private'. If so remove it from the options stack and save the value
  pub_priv_override = ''
  if options[-1] =~ /public|private/
    pub_priv_override = options.pop
  end

  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>"

  # if we have a public or private ip override, replace the template variable
  unless pub_priv_override.blank?
    case pub_priv_override
    when 'public'
      template.gsub!("<private_ip>", "<public_dns>")
    when 'private'
      template.gsub!("<public_dns>", "<private_ip>")
    end
  end

  # <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