Class: SportNginAwsAuditor::EC2Instance

Inherits:
Object
  • Object
show all
Extended by:
EC2Wrapper, InstanceHelper
Defined in:
lib/sport_ngin_aws_auditor/ec2_instance.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from EC2Wrapper

#ec2

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InstanceHelper

add_instances_with_tag_to_hash, compare, filter_instance_without_tags, filter_instances_with_tags, gather_instance_tag_date, instance_count_hash, instance_hash, reserved_instance_hash

Constructor Details

#initialize(ec2_instance, tag_name, count = 1) ⇒ EC2Instance

Returns a new instance of EC2Instance.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 13

def initialize(ec2_instance, tag_name, count=1)
  if ec2_instance.class.to_s == "Aws::EC2::Types::ReservedInstances"
    self.id = ec2_instance.reserved_instances_id
    self.name = nil
    self.platform = platform_helper(ec2_instance.product_description)
    self.availability_zone = ec2_instance.availability_zone
    self.instance_type = ec2_instance.instance_type
    self.count = count
    self.stack_name = nil
  elsif ec2_instance.class.to_s == "Aws::EC2::Types::Instance"
    self.id = ec2_instance.instance_id
    self.name = nil
    self.platform = platform_helper((ec2_instance.platform || ''), ec2_instance.vpc_id)
    self.availability_zone = ec2_instance.placement.availability_zone
    self.instance_type = ec2_instance.instance_type
    self.count = count
    self.stack_name = nil

    # go through to see if the tag we're looking for is one of them
    if tag_name
      ec2_instance.tags.each do |tag|
        if tag.key == tag_name
          self.tag_value = tag.value
        end
      end
    end
  end
end

Class Attribute Details

.instancesObject

Returns the value of attribute instances.



9
10
11
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 9

def instances
  @instances
end

.reserved_instancesObject

Returns the value of attribute reserved_instances.



9
10
11
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 9

def reserved_instances
  @reserved_instances
end

Instance Attribute Details

#availability_zoneObject

Returns the value of attribute availability_zone.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def availability_zone
  @availability_zone
end

#countObject

Returns the value of attribute count.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def count
  @count
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def id
  @id
end

#instance_typeObject

Returns the value of attribute instance_type.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def instance_type
  @instance_type
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def name
  @name
end

#platformObject

Returns the value of attribute platform.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def platform
  @platform
end

#stack_nameObject

Returns the value of attribute stack_name.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def stack_name
  @stack_name
end

#tag_valueObject

Returns the value of attribute tag_value.



12
13
14
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 12

def tag_value
  @tag_value
end

Class Method Details

.bucketizeObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 96

def self.bucketize
  buckets = {}
  get_instances.map do |instance|
    name = instance.stack_name || instance.name
    if name
      buckets[name] = [] unless buckets.has_key? name
      buckets[name] << instance
    else
      puts "Could not sort #{instance.id}, as it has no stack_name or name"
    end
  end
  buckets.sort_by{|k,v| k }
end

.get_instances(tag_name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 46

def self.get_instances(tag_name=nil)
  return @instances if @instances
  @instances = ec2.describe_instances.reservations.map do |reservation|
    reservation.instances.map do |instance|
      next unless instance.state.name == 'running'
      new(instance, tag_name)
    end.compact
  end.flatten.compact
  get_more_info
end

.get_reserved_instancesObject



57
58
59
60
61
62
63
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 57

def self.get_reserved_instances
  return @reserved_instances if @reserved_instances
  @reserved_instances = ec2.describe_reserved_instances.reserved_instances.map do |ri|
    next unless ri.state == 'active'
    new(ri, nil, ri.instance_count)
  end.compact
end

Instance Method Details

#no_reserved_instance_tag_valueObject



65
66
67
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 65

def no_reserved_instance_tag_value
  @tag_value
end

#to_sObject



42
43
44
# File 'lib/sport_ngin_aws_auditor/ec2_instance.rb', line 42

def to_s
  "#{platform} #{availability_zone} #{instance_type}"
end