awsutils 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'awsutils/ec2sg'
4
+
5
+ module AwsUtils
6
+
7
+ class Ec2DeleteSecurityGroup < Ec2SecurityGroup
8
+
9
+ def references
10
+
11
+ @references ||= begin
12
+
13
+ references = []
14
+
15
+ connection.security_groups.each do |group|
16
+ group.ip_permissions.each do |ip_perm|
17
+ ip_perm["groups"].each do |src_grp|
18
+ if src_grp["groupName"] == @opts[:security_group]
19
+
20
+ options = {
21
+ "IpPermissions" => [
22
+ {
23
+ "FromPort" => ip_perm["fromPort"],
24
+ "Groups" => [
25
+ {
26
+ "GroupName" => @opts[:security_group],
27
+ "UserId" => @opts[:owner_group_id]
28
+ }
29
+ ],
30
+ "IpProtocol" => ip_perm["ipProtocol"],
31
+ "IpRanges" => [],
32
+ "ToPort" => ip_perm["toPort"]
33
+ }
34
+ ]
35
+ }
36
+
37
+ references << {
38
+ "group_name" => group.name,
39
+ "options" => options
40
+ }
41
+
42
+ end # if src_grp["groupName"] == @opts[:security_group]
43
+ end # ip_perm["groups"].each do |src_grp|
44
+ end # group.ip_permissions.each do |ip_perm|
45
+ end # connection.security_groups.each do |group|
46
+
47
+ references
48
+ end # @references ||= begin
49
+ end # def references
50
+
51
+ def delete_group_refs
52
+
53
+ references.each do |ref|
54
+
55
+ puts "Removing rule: " + ref.inspect
56
+
57
+ connection.revoke_security_group_ingress(
58
+ ref["group_name"],
59
+ ref["options"]
60
+ )
61
+
62
+ end
63
+
64
+ end
65
+
66
+ def initialize( args )
67
+ @opts = Ec2SecurityGroup.parse_opts( args )
68
+ end
69
+
70
+ def name
71
+ @opts[:security_group]
72
+ end
73
+
74
+ def run
75
+
76
+ if ! exist?
77
+ puts "Specified group does not exist."
78
+ exit 1
79
+ end
80
+
81
+ if assigned?
82
+ puts "Group is still assigned to one or more instances."
83
+ exit 1
84
+ end
85
+
86
+ delete_group_refs
87
+
88
+ puts "Deleting group #{@opts[:security_group]}."
89
+ connection.delete_security_group( nil,
90
+ connection.security_groups.get(@opts[:security_group]).group_id )
91
+
92
+ end
93
+
94
+ end
95
+
96
+ end
@@ -0,0 +1,701 @@
1
+ require 'rubygems'
2
+ require 'fog'
3
+
4
+ class Array
5
+
6
+ def longest
7
+ length = 0
8
+ val = String.new
9
+ self.each do |a|
10
+ len_new = a.length
11
+ if len_new > length
12
+ length = len_new
13
+ val = a
14
+ end
15
+ end
16
+ return val
17
+ end
18
+ end
19
+
20
+ module AwsUtils
21
+
22
+ class Ec2Info
23
+
24
+ def connect()
25
+
26
+ conn = Fog::Compute.new(:provider => 'AWS')
27
+
28
+ if $DEBUG
29
+
30
+ puts "Inspect connection result:"
31
+ puts conn.inspect
32
+
33
+ end
34
+
35
+ #puts conn.inspect
36
+
37
+ return conn
38
+
39
+ end
40
+
41
+ def get_instance_id(conn,search_terms)
42
+
43
+ if $DEBUG
44
+
45
+ puts "Entering get_instance_id"
46
+ puts "Search Term: #{search_terms.inspect}"
47
+
48
+ end
49
+
50
+ instance_ids = Array.new
51
+
52
+ search_terms.each do |search_term|
53
+
54
+ if (/^i-/ =~ search_term) && (!(/\./ =~ search_term))
55
+
56
+ instance_ids[0] = search_term
57
+
58
+ else
59
+
60
+ conn.servers.each do |server|
61
+
62
+ if (server.tags.has_key?("Name")) \
63
+ && (server.tags["Name"] != "") \
64
+ && (/#{search_term}/i =~ server.tags["Name"])
65
+
66
+ instance_ids << server.id
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ if (instance_ids.count < 1)
77
+
78
+ puts "No instances by that Name/ID in this account"
79
+
80
+ exit 1
81
+
82
+ end
83
+
84
+ if $DEBUG
85
+
86
+ puts "Found instances: #{instance_ids.inspect}"
87
+
88
+ end
89
+
90
+ return instance_ids
91
+
92
+ end
93
+
94
+ def get_flavor_color(flavor)
95
+
96
+ case flavor
97
+ when "t1.micro"
98
+ fcolor = "1;33"
99
+ when "m1.large"
100
+ fcolor = "0;33"
101
+ when "m1.xlarge"
102
+ fcolor = "0;34"
103
+ when "m2.2xlarge"
104
+ fcolor = "0;35"
105
+ when "m2.4xlarge"
106
+ fcolor = "0;31"
107
+ when "m2.xlarge"
108
+ fcolor = "0;36"
109
+ else
110
+ fcolor = "0"
111
+ end
112
+
113
+ return "\033[#{fcolor}m"
114
+
115
+ end
116
+
117
+ def get_state_color(state)
118
+
119
+ case state
120
+ when "running"
121
+ scolor="1;32"
122
+ when "stopped"
123
+ scolor="1;31"
124
+ when "starting"
125
+ scolor="5;32"
126
+ when "stopping"
127
+ scolor="5;31"
128
+ else
129
+ scolor="0"
130
+ end
131
+
132
+ return "\033[#{scolor}m"
133
+
134
+ end
135
+
136
+ def describe_instance_short(search_term)
137
+
138
+ conn = connect()
139
+
140
+ instance_ids = Array.new
141
+
142
+ instance_ids = get_instance_id(conn,search_term)
143
+
144
+ instance_attribute_index = Array.new
145
+
146
+ conn.servers.first.class.attributes.each do |cur_attr|
147
+
148
+ instance_attribute_index << cur_attr
149
+
150
+ end
151
+
152
+ key_color = "\033[30;1m" # Style: Bold; Color: Default
153
+ reset_color = "\033[0m"
154
+
155
+ printf("#{key_color}%-50s %-11s %-12s %-13s %-10s %s#{reset_color}\n",
156
+ "Name",
157
+ "Zone",
158
+ "ID",
159
+ "Group",
160
+ "Flavor",
161
+ "State"
162
+ )
163
+
164
+ instance_ids.each do |instance_id|
165
+
166
+ inst = conn.servers.get(instance_id)
167
+
168
+ s_color = get_state_color(inst.state)
169
+ f_color = get_flavor_color(inst.flavor_id)
170
+
171
+ printf("%-50s %-11s %-12s %-13s #{f_color}%-10s#{reset_color} #{s_color}%s#{reset_color}",
172
+ inst.tags['Name'],
173
+ inst.availability_zone,
174
+ inst.id,
175
+ inst.groups.first,
176
+ inst.flavor_id,
177
+ inst.state
178
+ )
179
+
180
+ end
181
+
182
+
183
+ end
184
+
185
+ def describe_instance(search_term)
186
+
187
+ conn = connect()
188
+
189
+ instance_ids = Array.new
190
+
191
+ instance_ids = get_instance_id(conn,search_term)
192
+
193
+ instance_attribute_index = Array.new
194
+
195
+ conn.servers.first.class.attributes.each do |cur_attr|
196
+
197
+ instance_attribute_index << cur_attr
198
+
199
+ end
200
+
201
+ if $DEBUG
202
+
203
+ puts "Built attribute index: #{instance_attribute_index.inspect}"
204
+
205
+ end
206
+
207
+ col_green = "\033[32;1m" # Style: Bold; Color: Green
208
+ col_red = "\033[31;1m" # Style: Bold; Color: Red
209
+ col_blinking_red = "\033[31;5m"
210
+
211
+ key_color = "\033[30;1m" # Style: Bold; Color: Default
212
+ reset_color = "\033[0m"
213
+
214
+ instance_ids.each do |instance_id|
215
+
216
+ instance = conn.servers.get(instance_id)
217
+
218
+ puts "#{key_color}NAME:#{reset_color} #{instance.tags['Name']}"
219
+ puts ""
220
+
221
+ instance_attribute_index.delete("vpc_id")
222
+
223
+ instance_attribute_index.each do |instance_attribute|
224
+
225
+ if $DEBUG
226
+
227
+ puts "Instance attribute: #{instance_attribute}"
228
+
229
+ end
230
+
231
+ case instance_attribute
232
+ when :id
233
+
234
+ puts "#{key_color}ID:#{reset_color} #{instance.id}"
235
+
236
+ when :ami_launch_index
237
+
238
+ puts "#{key_color}AMI Launch Index:#{reset_color} #{instance.ami_launch_index}"
239
+
240
+ when :availability_zone
241
+
242
+ puts "#{key_color}Availability Zone:#{reset_color} #{instance.availability_zone}"
243
+
244
+ when :block_device_mapping
245
+
246
+ puts "#{key_color}Block Devices:#{reset_color} "
247
+
248
+ instance.block_device_mapping.each do |vol|
249
+
250
+ vol_obj = conn.volumes.get(vol['volumeId'])
251
+
252
+ puts "\t#{key_color}Size:#{reset_color} #{vol_obj.size} GB"
253
+ puts "\t#{key_color}Volume ID:#{reset_color} #{vol_obj.id}"
254
+ puts "\t#{key_color}Device Name:#{reset_color} #{vol_obj.device}"
255
+
256
+ if (vol_obj.delete_on_termination == true)
257
+
258
+ puts "\t#{key_color}Delete on Termination:#{reset_color} " +
259
+ "#{col_red}YES#{reset_color}"
260
+
261
+ else
262
+
263
+ puts "\t#{key_color}Delete on Termination:#{reset_color} " +
264
+ "#{col_green}NO#{reset_color}"
265
+
266
+ end
267
+
268
+ puts "\t#{key_color}Attach Time:#{reset_color} #{vol_obj.attached_at}"
269
+ puts "\t#{key_color}Creation Time:#{reset_color} #{vol_obj.created_at}"
270
+
271
+ if (vol_obj.snapshot_id != nil)
272
+
273
+ snap_obj = conn.snapshots.get(vol_obj.snapshot_id)
274
+
275
+ print "\t#{key_color}Snapshot ID:#{reset_color} #{vol_obj.snapshot_id}"
276
+
277
+ if (defined?snap_obj.created_at)
278
+
279
+ puts " (Created: #{snap_obj.created_at})"
280
+
281
+ end
282
+
283
+ if (defined?snap_obj.tags) && (snap_obj.tags != {})
284
+
285
+ puts "\t\t#{key_color}Tags:#{reset_color}"
286
+
287
+ snap_obj.tags.each do |snap_tag,snap_tag_value|
288
+
289
+ puts "\t\t\t#{key_color}#{snap_tag}:#{reset_color} #{snap_tag_value}"
290
+
291
+ end
292
+
293
+ end
294
+
295
+ end
296
+
297
+ if (vol_obj.state != "in-use")
298
+
299
+ puts "\t#{key_color}State:#{reset_color} #{vol_obj.state}"
300
+
301
+ end
302
+
303
+ if (vol['status'] == "attached")
304
+
305
+ status_color = col_green
306
+
307
+ else
308
+
309
+ status_color = col_red
310
+
311
+ end
312
+
313
+ puts "\t#{key_color}Status:#{reset_color} #{status_color}#{vol['status']}#{reset_color}"
314
+
315
+ if (vol_obj.tags != {})
316
+
317
+ puts "\t#{key_color}Tags:#{reset_color}"
318
+
319
+ vol_obj.tags.each do |vol_tag,vol_tag_value|
320
+
321
+ puts "\t\t#{key_color}#{vol_tag}:#{reset_color} #{vol_tag_value}"
322
+
323
+ end
324
+
325
+ end
326
+
327
+ if (vol != instance.block_device_mapping.last)
328
+
329
+ puts "\t---------------------------------------"
330
+
331
+ end
332
+
333
+ end
334
+
335
+ when :client_token
336
+
337
+ if (instance.client_token != nil)
338
+
339
+ puts "#{key_color}Client Token:#{reset_color} #{instance.client_token}"
340
+
341
+ elsif $DEBUG
342
+
343
+ puts "#{key_color}Client Token:#{reset_color} N/A"
344
+
345
+ end
346
+
347
+ when :dns_name
348
+
349
+ puts "#{key_color}DNS Name:#{reset_color} #{instance.dns_name}"
350
+
351
+ when :groups
352
+
353
+ instance.groups.each do |group_id|
354
+
355
+ group = conn.security_groups.get(group_id)
356
+
357
+ if (group != nil)
358
+
359
+ puts "#{key_color}Security Group:#{reset_color} #{group.name}"
360
+ puts "\t#{key_color}Description:#{reset_color} #{group.description}"
361
+ puts "\t#{key_color}ID:#{reset_color} #{group.group_id}"
362
+
363
+ end
364
+
365
+ break if group != nil
366
+
367
+ end
368
+
369
+ when :flavor_id
370
+
371
+ instance_flavor = conn.flavors.get(instance.flavor_id)
372
+
373
+ puts "#{key_color}Flavor:#{reset_color} #{instance_flavor.id}"
374
+
375
+ puts "\t#{key_color}Name:#{reset_color} #{instance_flavor.name}"
376
+ puts "\t#{key_color}Architecture:#{reset_color} #{instance_flavor.bits} bit"
377
+ puts "\t#{key_color}Cores:#{reset_color} #{instance_flavor.cores}"
378
+ puts "\t#{key_color}Instance Storage (in /mnt):#{reset_color} #{instance_flavor.disk} GB"
379
+ puts "\t#{key_color}RAM:#{reset_color} #{instance_flavor.ram} MB"
380
+
381
+ when :image_id
382
+
383
+ puts "#{key_color}Image ID:#{reset_color} #{instance.image_id}"
384
+
385
+ image_obj = conn.images.get(instance.image_id)
386
+
387
+ if (defined?image_obj.name)
388
+
389
+ puts "\t#{key_color}Name:#{reset_color} #{image_obj.name}"
390
+
391
+ end
392
+
393
+ if (defined?image_obj.description)
394
+
395
+ puts "\t#{key_color}Description:#{reset_color} #{image_obj.description}"
396
+
397
+ end
398
+
399
+ if (defined?image_obj.location)
400
+
401
+ puts "\t#{key_color}Location:#{reset_color} #{image_obj.location}"
402
+
403
+ end
404
+
405
+ if (defined?image_obj.architecture)
406
+
407
+ puts "\t#{key_color}Arch:#{reset_color} #{image_obj.architecture}"
408
+
409
+ end
410
+
411
+ if (defined?image_obj.tags) && (image_obj.tags != {})
412
+
413
+ puts "\t#{key_color}Tags:#{reset_color}"
414
+
415
+ image_obj.tags.each do |image_tag,image_tag_value|
416
+
417
+ puts "\t\t#{key_color}#{image_tag}: #{image_tag_value}"
418
+
419
+ end
420
+
421
+ end
422
+
423
+
424
+ when :kernel_id
425
+
426
+ puts "#{key_color}Kernel ID:#{reset_color} #{instance.kernel_id}"
427
+
428
+ when :key_name
429
+
430
+ puts "#{key_color}SSH Key:#{reset_color} #{instance.key_name}"
431
+
432
+ when :created_at
433
+
434
+ puts "#{key_color}Created Date:#{reset_color} #{instance.created_at}"
435
+
436
+ when :monitoring
437
+
438
+ puts "#{key_color}Monitoring:#{reset_color} #{instance.monitoring}"
439
+
440
+ when :placement_group
441
+
442
+ if (instance.placement_group != nil)
443
+
444
+ puts "#{key_color}Placement Group:#{reset_color} #{instance.placement_group}"
445
+
446
+ elsif $DEBUG
447
+
448
+ puts "#{key_color}Placement Group:#{reset_color} N/A"
449
+
450
+ end
451
+
452
+ when :platform
453
+
454
+ if (instance.platform != nil)
455
+
456
+ puts "#{key_color}Platform:#{reset_color} #{instance.platform}"
457
+
458
+ elsif $DEBUG
459
+
460
+ puts "#{key_color}Platform:#{reset_color} N/A"
461
+
462
+ end
463
+
464
+ when :product_codes
465
+
466
+ if (instance.product_codes.count > 0)
467
+
468
+ puts "#{key_color}Product Codes:#{reset_color} #{instance.product_codes.join(',')}"
469
+
470
+ elsif $DEBUG
471
+
472
+ puts "#{key_color}Product Codes:#{reset_color} N/A"
473
+
474
+ end
475
+
476
+ when :private_dns_name
477
+
478
+ puts "#{key_color}Private DNS Name:#{reset_color} #{instance.private_dns_name}"
479
+
480
+ when :private_ip_address
481
+
482
+ puts "#{key_color}Private IP Address:#{reset_color} #{instance.private_ip_address}"
483
+
484
+ when :public_ip_address
485
+
486
+ if conn.addresses.get(instance.public_ip_address)
487
+
488
+ puts "#{key_color}Public IP Address:#{reset_color} " +
489
+ "#{instance.public_ip_address} (#{col_green}STATIC#{reset_color})"
490
+
491
+ else
492
+
493
+ puts "#{key_color}Public IP Address:#{reset_color} " +
494
+ "#{instance.public_ip_address} (#{col_red}DYNAMIC#{reset_color})"
495
+
496
+ end
497
+
498
+ when :ramdisk_id
499
+
500
+ if (instance.ramdisk_id != nil)
501
+
502
+ puts "#{key_color}Ramdisk ID:#{reset_color} #{instance.ramdisk_id}"
503
+
504
+ elsif $DEBUG
505
+
506
+ puts "#{key_color}Ramdisk ID:#{reset_color} N/A"
507
+
508
+ end
509
+
510
+ when :reason
511
+
512
+ if (instance.reason != nil)
513
+
514
+ puts "#{key_color}State Reason:#{reset_color} #{instance.reason}"
515
+
516
+ elsif $DEBUG
517
+
518
+ puts "#{key_color}State Reason:#{reset_color} N/A"
519
+
520
+ end
521
+
522
+ when :root_device_name
523
+
524
+ if (instance.root_device_name != nil)
525
+
526
+ puts "#{key_color}Root Device Name:#{reset_color} #{instance.root_device_name}"
527
+
528
+ elsif $DEBUG
529
+
530
+ puts "#{key_color}Root Device Name:#{reset_color} N/A"
531
+
532
+ end
533
+
534
+ when :root_device_type
535
+
536
+ if (instance.root_device_type != nil)
537
+
538
+ puts "#{key_color}Root Device Type:#{reset_color} #{instance.root_device_type}"
539
+
540
+ elsif $DEBUG
541
+
542
+ puts "#{key_color}Root Device Name:#{reset_color} N/A"
543
+
544
+ end
545
+
546
+ when :security_group_ids
547
+
548
+ if (instance.security_group_ids != nil)
549
+
550
+ puts "#{key_color}Security Group IDs:#{reset_color} #{instance.security_group_ids}"
551
+
552
+ elsif $DEBUG
553
+
554
+ puts "#{key_color}Security Group IDs:#{reset_color} N/A"
555
+
556
+ end
557
+
558
+ when :state
559
+
560
+ state_color = get_state_color(instance.state)
561
+
562
+ puts "#{key_color}State:#{reset_color} #{state_color}#{instance.state}#{reset_color}"
563
+
564
+ when :state_reason
565
+
566
+ if (instance.state_reason != {})
567
+
568
+ puts "#{key_color}State Reason Code:#{reset_color} #{instance.state_reason["Code"]}"
569
+
570
+ elsif $DEBUG
571
+
572
+ puts "#{key_color}State Reason Code:#{reset_color} N/A"
573
+
574
+ end
575
+
576
+ when :subnet_id
577
+
578
+ if (instance.subnet_id != nil)
579
+
580
+ puts "#{key_color}Subnet ID:#{reset_color} #{instance.subnet_id}"
581
+
582
+ elsif $DEBUG
583
+
584
+ puts "#{key_color}Subnet ID:#{reset_color} N/A"
585
+
586
+ end
587
+
588
+ when :tenancy
589
+
590
+ puts "#{key_color}Tenancy:#{reset_color} #{instance.tenancy}"
591
+
592
+ when :tags
593
+
594
+ if (instance.tags.count > 0)
595
+
596
+ puts "#{key_color}Tags:#{reset_color} "
597
+
598
+ instance.tags.each do |tag,value|
599
+
600
+ puts "\t#{key_color}#{tag}:#{reset_color} #{value}"
601
+
602
+ end
603
+
604
+ else
605
+
606
+ puts "#{key_color}Tags:#{reset_color} None"
607
+
608
+ end
609
+
610
+ when :user_data
611
+
612
+ if (instance.user_data != nil)
613
+
614
+ puts "#{key_color}User Data:#{reset_color} #{instance.user_data}"
615
+
616
+ elsif $DEBUG
617
+
618
+ puts "#{key_color}User Data:#{reset_color} N/A"
619
+
620
+ end
621
+
622
+ when :vpc_id
623
+
624
+ if (instance.vpc_id != nil)
625
+
626
+ puts "#{key_color}VPC ID:#{reset_color} #{instance.vpc_id}"
627
+
628
+ elsif $DEBUG
629
+
630
+ puts "#{key_color}VPC ID: #{reset_color} N/A"
631
+
632
+ end
633
+
634
+ else
635
+
636
+ if instance.respond_to? :instance_attribute
637
+
638
+ puts "#{key_color}#{instance_attribute.to_s}:#{reset_color} #{instance.instance_attribute}"
639
+
640
+ else
641
+
642
+ puts "#{key_color}#{instance_attribute.to_s}:#{reset_color} <NULL>"
643
+
644
+ end
645
+
646
+ end
647
+
648
+ end
649
+
650
+ if instance.instance_initiated_shutdown_behavior == nil
651
+
652
+ puts "#{key_color}Shutdown Behavior:#{reset_color} Do nothing"
653
+
654
+ else
655
+
656
+ puts "#{key_color}Shutdown Behavior:#{reset_color} " +
657
+ "#{instance.instance_initiated_shutdown_behavior}"
658
+
659
+ end
660
+
661
+ if (instance_id != instance_ids.last)
662
+
663
+ puts "------------------------------------------------------------------------------------"
664
+
665
+ end
666
+
667
+ end
668
+
669
+ end
670
+
671
+ def initialize()
672
+
673
+ if (ARGV[0] == "") || (ARGV[0] == nil)
674
+
675
+ puts "Please specify a search term (Host Name or Instance ID)"
676
+ exit 1
677
+
678
+ end
679
+
680
+ args = ARGV
681
+
682
+ if args.include?("-s") || args.include?("--short")
683
+
684
+ args.delete("-s")
685
+ args.delete("--short")
686
+
687
+ describe_instance_short(args)
688
+
689
+ else
690
+
691
+ describe_instance(args)
692
+
693
+ end
694
+
695
+
696
+
697
+ end
698
+
699
+ end
700
+
701
+ end