Module: Awsam::Ec2

Defined in:
lib/awsam/ec2.rb

Constant Summary collapse

LOOKUP_TAGS =
["Name", "aws:autoscaling:groupName", "AlsoKnownAs"].freeze

Class Method Summary collapse

Class Method Details

.find(ec2, instance_id) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/awsam/ec2.rb', line 24

def self.find(ec2, instance_id)
  if instance_id =~ /^i-[0-9a-f]{8,17}$/
    find_by_instance_id(ec2, instance_id)
  else
    find_by_tag(ec2, instance_id)
  end
end

.find_by_instance_id(ec2, instance_id) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/awsam/ec2.rb', line 32

def self.find_by_instance_id(ec2, instance_id)
  begin
    resp = ec2.describe_instances(:instance_ids => [instance_id])
    resp.reservations.length > 0 ? resp.reservations[0].instances.first : nil
  rescue => e
    puts "error describing instance: #{instance_id}: #{e}"
    exit 1
  end
end

.find_by_tag(ec2, instance_id) ⇒ Object



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/awsam/ec2.rb', line 42

def self.find_by_tag(ec2, instance_id)
  results = []

  params = {
    :filters => [{
                   :name => "resource-type",
                   :values => ["instance"]
                 }]
  }
  resp = ec2.describe_tags(params)
  resp.tags.each do |tag|
    if LOOKUP_TAGS.include?(tag.key) &&
        tag.value.downcase.include?(instance_id.downcase)
      results << tag
    end
  end

  if !results || results.length == 0
    puts "No tags by this name are available in your account"
    exit 1
  end

  results.uniq! { |a| a.resource_id }
  results.sort! { |a,b| a.value <=> b.value }

  rmap = {}
  params = {
    :instance_ids => results.map{|a| a.resource_id},
    :filters => [{
                   :name => "instance-state-name",
                   :values => ["running"]
                 }]
  }
  resp = ec2.describe_instances(params)
  resp.reservations.each do |resv|
    resv.instances.each do |inst|
      rmap[inst.instance_id] = inst
    end
  end

  results.reject! { |a| rmap[a.resource_id].nil? }

  if results.length == 0
    puts "No running instances by that tag name are available"
    exit 1
  end

  if $opts[:first_node] || results.length == 1
    node = results.first
  else
    puts "Please select which node you wish to use:"
    puts

    namemax = 0
    instmax = 0
    ipmax = 0
    results.each_with_index do |elem, i|
      inst = rmap[elem.resource_id]
      if elem.value.length > namemax
        namemax = elem.value.length
      end
      if inst.instance_id.length > instmax
        instmax = inst.instance_id.length
      end
      if inst.private_ip_address.length > ipmax
        ipmax = inst.private_ip_address.length
      end
    end

    countmax = results.size.to_s.length
    results.each_with_index do |elem, i|
      inst = rmap[elem.resource_id]

      launchtime = inst.launch_time
      puts "%*d) %-*s (%*s %-*s %-11s %s %s)" %
        [countmax, i + 1,
         namemax, elem.value,
         instmax, inst.instance_id,
         ipmax, inst.private_ip_address,
         inst.instance_type,
         inst.placement.availability_zone,
         launchtime.strftime("%Y-%m-%d")]
    end
    puts "%*s) Quit" % [countmax, "q"]
    puts

    print "> "
    input = $stdin.gets
    puts
    exit unless input =~ /^\d+$/
    sel = input.to_i
    exit unless sel > 0 && sel <= results.size

    node = results[sel - 1]
  end

  return rmap[node.resource_id]
end

.find_instance(acct, instance_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/awsam/ec2.rb', line 11

def self.find_instance(acct, instance_id)
  ec2 = Aws::EC2::Client.new(:access_key_id => acct.access_key,
                             :secret_access_key => acct.secret_key,
                             :region => acct.aws_region)

  unless ec2
    puts "Unable to connect to EC2"
    return nil
  end

  find(ec2, instance_id)
end

.instance_hostname(inst) ⇒ Object



6
7
8
9
# File 'lib/awsam/ec2.rb', line 6

def self.instance_hostname(inst)
  # Always use private IP address
  inst.private_ip_address
end