Class: AwsUtils::Ec2Info

Inherits:
Object
  • Object
show all
Defined in:
lib/awsutils/ec2info.rb

Instance Method Summary collapse

Constructor Details

#initializeEc2Info

Returns a new instance of Ec2Info.



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/awsutils/ec2info.rb', line 671

def initialize()

   if (ARGV[0] == "") || (ARGV[0] == nil)

     puts "Please specify a search term (Host Name or Instance ID)"
     exit 1

   end

   args = ARGV

   if args.include?("-s") || args.include?("--short")

     args.delete("-s")
     args.delete("--short")

	  describe_instance_short(args)

   else

	  describe_instance(args)

   end


	
end

Instance Method Details

#connectObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/awsutils/ec2info.rb', line 24

def connect()

	conn = Fog::Compute.new(:provider => 'AWS')
	
	if $DEBUG
	
		puts "Inspect connection result:"
		puts conn.inspect
		
	end
	
	#puts conn.inspect
	
	return conn
	
end

#describe_instance(search_term) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/awsutils/ec2info.rb', line 185

def describe_instance(search_term)

	conn = connect()

   instance_ids = Array.new

   instance_ids = get_instance_id(conn,search_term)

   instance_attribute_index = Array.new

   conn.servers.first.class.attributes.each do |cur_attr|

     instance_attribute_index << cur_attr

   end

   if $DEBUG

     puts "Built attribute index: #{instance_attribute_index.inspect}"

   end

   col_green = "\033[32;1m" # Style: Bold; Color: Green
   col_red = "\033[31;1m" # Style: Bold; Color: Red
   col_blinking_red = "\033[31;5m"

   key_color = "\033[30;1m" # Style: Bold; Color: Default
   reset_color = "\033[0m"

   instance_ids.each do |instance_id|
	
	  instance = conn.servers.get(instance_id)

     puts "#{key_color}NAME:#{reset_color} #{instance.tags['Name']}"
     puts ""

     instance_attribute_index.delete("vpc_id")

     instance_attribute_index.each do |instance_attribute|

       if $DEBUG

         puts "Instance attribute: #{instance_attribute}"

       end

       case instance_attribute
       when :id

         puts "#{key_color}ID:#{reset_color} #{instance.id}"

       when :ami_launch_index

         puts "#{key_color}AMI Launch Index:#{reset_color} #{instance.ami_launch_index}"

       when :availability_zone

         puts "#{key_color}Availability Zone:#{reset_color} #{instance.availability_zone}"

       when :block_device_mapping

         puts "#{key_color}Block Devices:#{reset_color} "
         
         instance.block_device_mapping.each do |vol|

           vol_obj = conn.volumes.get(vol['volumeId'])

           puts "\t#{key_color}Size:#{reset_color} #{vol_obj.size} GB"
           puts "\t#{key_color}Volume ID:#{reset_color} #{vol_obj.id}"
           puts "\t#{key_color}Device Name:#{reset_color} #{vol_obj.device}"

           if (vol_obj.delete_on_termination == true)

             puts "\t#{key_color}Delete on Termination:#{reset_color} " +
               "#{col_red}YES#{reset_color}"

           else

             puts "\t#{key_color}Delete on Termination:#{reset_color} " +
               "#{col_green}NO#{reset_color}"

           end

           puts "\t#{key_color}Attach Time:#{reset_color} #{vol_obj.attached_at}"
           puts "\t#{key_color}Creation Time:#{reset_color} #{vol_obj.created_at}"

           if (vol_obj.snapshot_id != nil)

             snap_obj = conn.snapshots.get(vol_obj.snapshot_id)

             print "\t#{key_color}Snapshot ID:#{reset_color} #{vol_obj.snapshot_id}"

             if (defined?snap_obj.created_at)

               puts " (Created: #{snap_obj.created_at})"

             end

             if (defined?snap_obj.tags) && (snap_obj.tags != {})

               puts "\t\t#{key_color}Tags:#{reset_color}"

               snap_obj.tags.each do |snap_tag,snap_tag_value|

                 puts "\t\t\t#{key_color}#{snap_tag}:#{reset_color} #{snap_tag_value}"

               end

             end

           end

           if (vol_obj.state != "in-use")

             puts "\t#{key_color}State:#{reset_color} #{vol_obj.state}"

           end

           if (vol['status'] == "attached")

             status_color = col_green

           else

             status_color = col_red

           end

           puts "\t#{key_color}Status:#{reset_color} #{status_color}#{vol['status']}#{reset_color}"

           if (vol_obj.tags != {})

             puts "\t#{key_color}Tags:#{reset_color}"

             vol_obj.tags.each do |vol_tag,vol_tag_value|

               puts "\t\t#{key_color}#{vol_tag}:#{reset_color} #{vol_tag_value}"

             end

           end

           if (vol != instance.block_device_mapping.last)

             puts "\t---------------------------------------"

           end

         end

       when :client_token

         if (instance.client_token != nil)

           puts "#{key_color}Client Token:#{reset_color} #{instance.client_token}"

         elsif $DEBUG

           puts "#{key_color}Client Token:#{reset_color} N/A"

         end

       when :dns_name

         puts "#{key_color}DNS Name:#{reset_color} #{instance.dns_name}"

       when :groups

         instance.groups.each do |group_id|

           group = conn.security_groups.get(group_id)

           if (group != nil)

             puts "#{key_color}Security Group:#{reset_color} #{group.name}"
             puts "\t#{key_color}Description:#{reset_color} #{group.description}"
             puts "\t#{key_color}ID:#{reset_color} #{group.group_id}"

           end

           break if group != nil

         end

       when :flavor_id
         
         instance_flavor = conn.flavors.get(instance.flavor_id)

         puts "#{key_color}Flavor:#{reset_color} #{instance_flavor.id}"

         puts "\t#{key_color}Name:#{reset_color} #{instance_flavor.name}"
         puts "\t#{key_color}Architecture:#{reset_color} #{instance_flavor.bits} bit"
         puts "\t#{key_color}Cores:#{reset_color} #{instance_flavor.cores}"
         puts "\t#{key_color}Instance Storage (in /mnt):#{reset_color} #{instance_flavor.disk} GB"
         puts "\t#{key_color}RAM:#{reset_color} #{instance_flavor.ram} MB"

       when :image_id

         puts "#{key_color}Image ID:#{reset_color} #{instance.image_id}"
         
         image_obj = conn.images.get(instance.image_id)

         if (defined?image_obj.name)
           
           puts "\t#{key_color}Name:#{reset_color} #{image_obj.name}"

         end

         if (defined?image_obj.description)

           puts "\t#{key_color}Description:#{reset_color} #{image_obj.description}"

         end

         if (defined?image_obj.location)
           
           puts "\t#{key_color}Location:#{reset_color} #{image_obj.location}"

         end

         if (defined?image_obj.architecture)
           
           puts "\t#{key_color}Arch:#{reset_color} #{image_obj.architecture}"

         end
         
         if (defined?image_obj.tags) && (image_obj.tags != {})

           puts "\t#{key_color}Tags:#{reset_color}"

           image_obj.tags.each do |image_tag,image_tag_value|

             puts "\t\t#{key_color}#{image_tag}: #{image_tag_value}"

           end

         end


       when :kernel_id

         puts "#{key_color}Kernel ID:#{reset_color} #{instance.kernel_id}"

       when :key_name

         puts "#{key_color}SSH Key:#{reset_color} #{instance.key_name}"

       when :created_at

         puts "#{key_color}Created Date:#{reset_color} #{instance.created_at}"

       when :monitoring

         puts "#{key_color}Monitoring:#{reset_color} #{instance.monitoring}"

       when :placement_group

         if (instance.placement_group != nil)

           puts "#{key_color}Placement Group:#{reset_color} #{instance.placement_group}"

         elsif $DEBUG

           puts "#{key_color}Placement Group:#{reset_color} N/A"

         end

       when :platform

         if (instance.platform != nil)

           puts "#{key_color}Platform:#{reset_color} #{instance.platform}"

         elsif $DEBUG

           puts "#{key_color}Platform:#{reset_color} N/A"

         end

       when :product_codes

         if (instance.product_codes.count > 0)

           puts "#{key_color}Product Codes:#{reset_color} #{instance.product_codes.join(',')}"

         elsif $DEBUG

           puts "#{key_color}Product Codes:#{reset_color} N/A"

         end

       when :private_dns_name

         puts "#{key_color}Private DNS Name:#{reset_color} #{instance.private_dns_name}"

       when :private_ip_address

         puts "#{key_color}Private IP Address:#{reset_color} #{instance.private_ip_address}"

       when :public_ip_address

         if conn.addresses.get(instance.public_ip_address)

           puts "#{key_color}Public IP Address:#{reset_color} " +
             "#{instance.public_ip_address} (#{col_green}STATIC#{reset_color})"

         else

           puts "#{key_color}Public IP Address:#{reset_color} " +
             "#{instance.public_ip_address} (#{col_red}DYNAMIC#{reset_color})"

         end

       when :ramdisk_id

         if (instance.ramdisk_id != nil)

           puts "#{key_color}Ramdisk ID:#{reset_color} #{instance.ramdisk_id}"

         elsif $DEBUG

           puts "#{key_color}Ramdisk ID:#{reset_color} N/A"

         end

       when :reason

         if (instance.reason != nil)

           puts "#{key_color}State Reason:#{reset_color} #{instance.reason}"

         elsif $DEBUG

           puts "#{key_color}State Reason:#{reset_color} N/A"

         end

       when :root_device_name

         if (instance.root_device_name != nil)

           puts "#{key_color}Root Device Name:#{reset_color} #{instance.root_device_name}"

         elsif $DEBUG

           puts "#{key_color}Root Device Name:#{reset_color} N/A"

         end

       when :root_device_type

         if (instance.root_device_type != nil)

           puts "#{key_color}Root Device Type:#{reset_color} #{instance.root_device_type}"

         elsif $DEBUG

           puts "#{key_color}Root Device Name:#{reset_color} N/A"

         end

       when :security_group_ids

         if (instance.security_group_ids != nil)

           puts "#{key_color}Security Group IDs:#{reset_color} #{instance.security_group_ids}"

         elsif $DEBUG

           puts "#{key_color}Security Group IDs:#{reset_color} N/A"

         end

       when :state

         state_color = get_state_color(instance.state)

         puts "#{key_color}State:#{reset_color} #{state_color}#{instance.state}#{reset_color}"

       when :state_reason

         if (instance.state_reason != {})

           puts "#{key_color}State Reason Code:#{reset_color} #{instance.state_reason["Code"]}"

         elsif $DEBUG

           puts "#{key_color}State Reason Code:#{reset_color} N/A"

         end

       when :subnet_id

         if (instance.subnet_id != nil)

           puts "#{key_color}Subnet ID:#{reset_color} #{instance.subnet_id}"

         elsif $DEBUG

           puts "#{key_color}Subnet ID:#{reset_color} N/A"

         end

       when :tenancy

         puts "#{key_color}Tenancy:#{reset_color} #{instance.tenancy}"

       when :tags

         if (instance.tags.count > 0)

           puts "#{key_color}Tags:#{reset_color} "

           instance.tags.each do |tag,value|

             puts "\t#{key_color}#{tag}:#{reset_color} #{value}"

           end

         else

           puts "#{key_color}Tags:#{reset_color} None"

         end

       when :user_data

         if (instance.user_data != nil)

           puts "#{key_color}User Data:#{reset_color} #{instance.user_data}"

         elsif $DEBUG

           puts "#{key_color}User Data:#{reset_color} N/A"

         end

       when :vpc_id

         if (instance.vpc_id != nil)

           puts "#{key_color}VPC ID:#{reset_color} #{instance.vpc_id}"

         elsif $DEBUG

           puts "#{key_color}VPC ID: #{reset_color} N/A"

         end

       else

         if instance.respond_to? :instance_attribute

           puts "#{key_color}#{instance_attribute.to_s}:#{reset_color} #{instance.instance_attribute}"

         else
           
           puts "#{key_color}#{instance_attribute.to_s}:#{reset_color} <NULL>"

         end

       end

     end

     if instance.instance_initiated_shutdown_behavior == nil
       
       puts "#{key_color}Shutdown Behavior:#{reset_color} Do nothing"

     else

       puts "#{key_color}Shutdown Behavior:#{reset_color} " +
         "#{instance.instance_initiated_shutdown_behavior}"

     end

     if (instance_id != instance_ids.last)

       puts "------------------------------------------------------------------------------------"

     end

   end
	
end

#describe_instance_short(search_term) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/awsutils/ec2info.rb', line 136

def describe_instance_short(search_term)

  conn = connect()

  instance_ids = Array.new

  instance_ids = get_instance_id(conn,search_term)

  instance_attribute_index = Array.new

  conn.servers.first.class.attributes.each do |cur_attr|

    instance_attribute_index << cur_attr

  end

  key_color = "\033[30;1m" # Style: Bold; Color: Default
  reset_color = "\033[0m"

  printf("#{key_color}%-50s %-11s %-12s %-13s %-10s %s#{reset_color}\n",
         "Name",
         "Zone",
         "ID",
         "Group",
         "Flavor",
         "State"
        )

  instance_ids.each do |instance_id|

    inst = conn.servers.get(instance_id)
    
    s_color = get_state_color(inst.state)
    f_color = get_flavor_color(inst.flavor_id)

    printf("%-50s %-11s %-12s %-13s #{f_color}%-10s#{reset_color} #{s_color}%s#{reset_color}",
           inst.tags['Name'],
           inst.availability_zone,
           inst.id,
           inst.groups.first,
           inst.flavor_id,
           inst.state
          )

  end


end

#get_flavor_color(flavor) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/awsutils/ec2info.rb', line 94

def get_flavor_color(flavor)

	case flavor
      when "t1.micro"
        fcolor = "1;33"
      when "m1.large"
        fcolor = "0;33"
      when "m1.xlarge"
        fcolor = "0;34"
      when "m2.2xlarge"
        fcolor = "0;35"
      when "m2.4xlarge"
        fcolor = "0;31"
      when "m2.xlarge"
        fcolor = "0;36"
      else
        fcolor = "0"
	end

    return "\033[#{fcolor}m"

end

#get_instance_id(conn, search_terms) ⇒ Object



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
# File 'lib/awsutils/ec2info.rb', line 41

def get_instance_id(conn,search_terms)

  if $DEBUG

    puts "Entering get_instance_id"
    puts "Search Term: #{search_terms.inspect}"

  end

  instance_ids = Array.new

  search_terms.each do |search_term|

    if (/^i-/ =~ search_term) && (!(/\./ =~ search_term))

      instance_ids[0] = search_term

    else

      conn.servers.each do |server|

        if (server.tags.has_key?("Name")) \
          && (server.tags["Name"] != "") \
          && (/#{search_term}/i =~ server.tags["Name"])

            instance_ids << server.id

        end

      end

    end

  end

  if (instance_ids.count < 1)

    puts "No instances by that Name/ID in this account"

    exit 1

  end

  if $DEBUG

    puts "Found instances: #{instance_ids.inspect}"

  end

  return instance_ids

end

#get_state_color(state) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/awsutils/ec2info.rb', line 117

def get_state_color(state)

	case state
      when "running"
        scolor="1;32"
      when "stopped"
        scolor="1;31"
      when "starting"
        scolor="5;32"
      when "stopping"
        scolor="5;31"
      else
        scolor="0"
	end

    return "\033[#{scolor}m"

end