awsutils 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5f6821233efc5b2ccae58b7e1704ca710677d7f
4
- data.tar.gz: b410ed5b1d984f78502e6a1644e52b633f23ced6
3
+ metadata.gz: 85c4d31eee5853d0a2671122c63f9545534a9e91
4
+ data.tar.gz: 3f1f387a73456e368d762743ca7ba6c6a9548a9a
5
5
  SHA512:
6
- metadata.gz: dc700cf9f38ef4160a961960c6dec9a2a68841743c829c00fab076bbb6ed8590d4a756257e6cbfd05320cf3ea35bb1a8bf3dfad79592d739c7b03034a9cf9989
7
- data.tar.gz: cf9fd37811b4847d91739efbf9ada4b8b5690c646f164330824d625e9bb62f680cc0dc0339f6510bbe307d89a122b8b28d3e43d446791a648b700d64aab5a545
6
+ metadata.gz: d58121f23d06c6e3f149e4e3694336f3c34317087e3aba2f6793d037e93592f72086b26c95144f43ac17b6bcb606541dc244d8d08d53f66112547caf4040f8bc
7
+ data.tar.gz: 0a47336c477fe8b97faf165c7d5f06fb1d50a0bce6045a5acbb9895f4f1588b6938822d4536ee317acee1e37344b2c97f35eb72e183d9b4fd55d26f431dc78c9
@@ -1,10 +1,27 @@
1
1
  require 'rubygems'
2
2
  require 'fog/aws'
3
+ require 'awesome_print'
3
4
 
4
5
  class String
6
+ def underscore
7
+ gsub(/::/, '/')
8
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
9
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
10
+ .tr('-', '_')
11
+ .downcase
12
+ end
13
+
5
14
  def title_case
6
- return self if self !~ /_/ && self =~ /[A-Z]+.*/
7
- split('_').map { |e| e.capitalize }.join(' ')
15
+ underscore.split('_').map do |word|
16
+ # Recognize certain special cases (e.g. acronyms)
17
+ if %w(iam ebs id dns vpc).include? word.downcase
18
+ word.upcase
19
+ elsif word.casecmp('ids').zero?
20
+ 'IDs'
21
+ else
22
+ word.capitalize
23
+ end
24
+ end.join(' ')
8
25
  end
9
26
  end
10
27
 
@@ -31,14 +48,14 @@ module AwsUtils
31
48
  @ec2 ||= Fog::Compute.new(provider: 'AWS')
32
49
  end
33
50
 
34
- def instance_ids
51
+ def instances
35
52
  @instance_ids ||= begin
36
53
  if $DEBUG
37
54
  puts 'Entering instance_ids'
38
55
  puts "Search Term: #{search_terms.inspect}"
39
56
  end
40
57
 
41
- results = search_terms.each_with_object([]) do |search_term, m|
58
+ instance_ids = search_terms.each_with_object([]) do |search_term, m|
42
59
  if (/^i-/ =~ search_term) && !(/\./ =~ search_term)
43
60
  m << search_term
44
61
  else
@@ -51,14 +68,14 @@ module AwsUtils
51
68
  end
52
69
  end
53
70
 
54
- if results.empty?
71
+ if instance_ids.empty?
55
72
  puts 'No instances by that Name/ID in this account'
56
73
  exit 1
57
74
  end
58
75
 
59
76
  puts "Found instances: #{results.inspect}" if $DEBUG
60
77
 
61
- results
78
+ ec2.servers.all 'instance-id' => instance_ids
62
79
  end
63
80
  end
64
81
 
@@ -133,12 +150,28 @@ module AwsUtils
133
150
  end
134
151
  end
135
152
 
136
- def describe_instance
137
- instance_attribute_index = ec2.servers.first.class.attributes
138
- # ec2.servers.first.class.attributes.reject { |attr| attr == :vpc_id }
153
+ def printkv(key, value)
154
+ key_color = "\033[30;1m" # Style: Bold; Color: Default
155
+ reset_color = "\033[0m"
139
156
 
140
- puts "Built attribute index: #{instance_attribute_index.inspect}" if $DEBUG
157
+ print "#{key_color}#{key.to_s.title_case}:#{reset_color} "
158
+ case
159
+ when value.respond_to?(:to_sym)
160
+ puts value
161
+ when value.respond_to?(:key?)
162
+ puts
163
+ value.each { |k, v| printkv " #{k}", v }
164
+ when (
165
+ value.respond_to?(:join) &&
166
+ value.reject { |item| item.respond_to?(:to_sym) }.empty? # If value only contains strings, do this
167
+ )
168
+ value.join(', ')
169
+ else
170
+ puts value.inspect
171
+ end
172
+ end
141
173
 
174
+ def describe_instance
142
175
  col_green = "\033[32;1m" # Style: Bold; Color: Green
143
176
  col_red = "\033[31;1m" # Style: Bold; Color: Red
144
177
  # col_blinking_red = "\033[31;5m"
@@ -146,22 +179,14 @@ module AwsUtils
146
179
  key_color = "\033[30;1m" # Style: Bold; Color: Default
147
180
  reset_color = "\033[0m"
148
181
 
149
- instance_ids.each do |instance_id|
150
- instance = ec2.servers.get instance_id
151
-
182
+ instances.each do |instance|
152
183
  puts "#{key_color}NAME:#{reset_color} #{instance.tags['Name']}"
153
184
  puts
154
185
 
155
- instance_attribute_index.each do |instance_attribute|
186
+ instance.attributes.each do |instance_attribute, _value|
156
187
  puts "Instance attribute: #{instance_attribute}" if $DEBUG
157
188
 
158
189
  case instance_attribute
159
- when :id
160
- puts "#{key_color}ID:#{reset_color} #{instance.id}"
161
- when :ami_launch_index
162
- puts "#{key_color}AMI Launch Index:#{reset_color} #{instance.ami_launch_index}"
163
- when :availability_zone
164
- puts "#{key_color}Availability Zone:#{reset_color} #{instance.availability_zone}"
165
190
  when :block_device_mapping
166
191
  puts "#{key_color}Block Devices:#{reset_color} "
167
192
 
@@ -230,8 +255,6 @@ module AwsUtils
230
255
  elsif $DEBUG
231
256
  puts "#{key_color}Client Token:#{reset_color} N/A"
232
257
  end
233
- when :dns_name
234
- puts "#{key_color}DNS Name:#{reset_color} #{instance.dns_name}"
235
258
  when :groups
236
259
  instance.groups.each do |group_id|
237
260
  group = ec2.security_groups.get(group_id)
@@ -279,36 +302,12 @@ module AwsUtils
279
302
  puts "\t\t#{key_color}#{image_tag}: #{image_tag_value}"
280
303
  end
281
304
  end
282
- when :kernel_id
283
- puts "#{key_color}Kernel ID:#{reset_color} #{instance.kernel_id}"
284
- when :key_name
285
- puts "#{key_color}SSH Key:#{reset_color} #{instance.key_name}"
286
- when :created_at
287
- puts "#{key_color}Created Date:#{reset_color} #{instance.created_at}"
288
- when :monitoring
289
- puts "#{key_color}Monitoring:#{reset_color} #{instance.monitoring}"
290
- when :placement_group
291
- if instance.placement_group
292
- puts "#{key_color}Placement Group:#{reset_color} #{instance.placement_group}"
293
- elsif $DEBUG
294
- puts "#{key_color}Placement Group:#{reset_color} N/A"
295
- end
296
- when :platform
297
- if instance.platform
298
- puts "#{key_color}Platform:#{reset_color} #{instance.platform}"
299
- elsif $DEBUG
300
- puts "#{key_color}Platform:#{reset_color} N/A"
301
- end
302
305
  when :product_codes
303
306
  if instance.product_codes.any?
304
307
  puts "#{key_color}Product Codes:#{reset_color} #{instance.product_codes.join(',')}"
305
308
  elsif $DEBUG
306
309
  puts "#{key_color}Product Codes:#{reset_color} N/A"
307
310
  end
308
- when :private_dns_name
309
- puts "#{key_color}Private DNS Name:#{reset_color} #{instance.private_dns_name}"
310
- when :private_ip_address
311
- puts "#{key_color}Private IP Address:#{reset_color} #{instance.private_ip_address}"
312
311
  when :public_ip_address
313
312
  if ec2.addresses.get(instance.public_ip_address)
314
313
  puts "#{key_color}Public IP Address:#{reset_color} " \
@@ -317,36 +316,6 @@ module AwsUtils
317
316
  puts "#{key_color}Public IP Address:#{reset_color} " \
318
317
  "#{instance.public_ip_address} (#{col_red}DYNAMIC#{reset_color})"
319
318
  end
320
- when :ramdisk_id
321
- if instance.ramdisk_id
322
- puts "#{key_color}Ramdisk ID:#{reset_color} #{instance.ramdisk_id}"
323
- elsif $DEBUG
324
- puts "#{key_color}Ramdisk ID:#{reset_color} N/A"
325
- end
326
- when :reason
327
- if instance.reason
328
- puts "#{key_color}State Reason:#{reset_color} #{instance.reason}"
329
- elsif $DEBUG
330
- puts "#{key_color}State Reason:#{reset_color} N/A"
331
- end
332
- when :root_device_name
333
- if instance.root_device_name
334
- puts "#{key_color}Root Device Name:#{reset_color} #{instance.root_device_name}"
335
- elsif $DEBUG
336
- puts "#{key_color}Root Device Name:#{reset_color} N/A"
337
- end
338
- when :root_device_type
339
- if instance.root_device_type
340
- puts "#{key_color}Root Device Type:#{reset_color} #{instance.root_device_type}"
341
- elsif $DEBUG
342
- puts "#{key_color}Root Device Name:#{reset_color} N/A"
343
- end
344
- when :security_group_ids
345
- if instance.security_group_ids
346
- puts "#{key_color}Security Group IDs:#{reset_color} #{instance.security_group_ids}"
347
- elsif $DEBUG
348
- puts "#{key_color}Security Group IDs:#{reset_color} N/A"
349
- end
350
319
  when :state
351
320
  state_color = get_state_color(instance.state)
352
321
  puts "#{key_color}State:#{reset_color} #{state_color}#{instance.state}#{reset_color}"
@@ -356,14 +325,6 @@ module AwsUtils
356
325
  elsif $DEBUG
357
326
  puts "#{key_color}State Reason Code:#{reset_color} N/A"
358
327
  end
359
- when :subnet_id
360
- if instance.subnet_id
361
- puts "#{key_color}Subnet ID:#{reset_color} #{instance.subnet_id}"
362
- elsif $DEBUG
363
- puts "#{key_color}Subnet ID:#{reset_color} N/A"
364
- end
365
- when :tenancy
366
- puts "#{key_color}Tenancy:#{reset_color} #{instance.tenancy}"
367
328
  when :tags
368
329
  if instance.tags.any?
369
330
  puts "#{key_color}Tags:#{reset_color} "
@@ -374,21 +335,12 @@ module AwsUtils
374
335
  else
375
336
  puts "#{key_color}Tags:#{reset_color} None"
376
337
  end
377
- when :user_data
378
- if instance.user_data
379
- puts "#{key_color}User Data:#{reset_color} #{instance.user_data}"
380
- elsif $DEBUG
381
- puts "#{key_color}User Data:#{reset_color} N/A"
382
- end
383
- when :vpc_id
384
- if instance.vpc_id
385
- puts "#{key_color}VPC ID:#{reset_color} #{instance.vpc_id}"
386
- elsif $DEBUG
387
- puts "#{key_color}VPC ID: #{reset_color} N/A"
388
- end
389
338
  else
390
- print "#{key_color}#{instance_attribute.to_s.title_case}:#{reset_color} "
391
- puts instance.respond_to?(:instance_attribute) ? '' : '<NULL>'
339
+ if instance.respond_to?(instance_attribute) && !instance.send(instance_attribute).nil?
340
+ printkv instance_attribute, instance.send(instance_attribute)
341
+ else
342
+ printkv instance_attribute, '<NULL>'
343
+ end
392
344
  end
393
345
  end
394
346
 
@@ -399,7 +351,7 @@ module AwsUtils
399
351
  puts "#{key_color}Shutdown Behavior:#{reset_color} Do nothing"
400
352
  end
401
353
 
402
- if instance_id != instance_ids.last
354
+ if instance.id != instances.last.id
403
355
  puts '------------------------------------------------------------------------------------'
404
356
  end
405
357
  end
@@ -1,3 +1,3 @@
1
1
  module AwsUtils
2
- VERSION = '2.0.1'
2
+ VERSION = '2.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awsutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Herot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-26 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler