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
|