Class: SportNginAwsAuditor::Stack

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from OpsWorksWrapper

#opsworks

Attributes included from EC2Wrapper

#ec2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws_stack) ⇒ Stack

Returns a new instance of Stack.



13
14
15
16
17
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 13

def initialize(aws_stack)
  @id = aws_stack[:stack_id]
  @name = aws_stack[:name]
  @instances = get_instances.compact
end

Class Attribute Details

.instancesObject

Returns the value of attribute instances.



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

def instances
  @instances
end

.stacksObject

Returns the value of attribute stacks.



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

def stacks
  @stacks
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#instancesObject

Returns the value of attribute instances.



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

def instances
  @instances
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.allObject



42
43
44
45
46
47
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 42

def self.all
  return @stacks if @stacks 
  @stacks = opsworks.describe_stacks.data[:stacks].map do |stack|
    new(stack)
  end.sort! { |a,b| a.name.downcase <=> b.name.downcase }
end

.all_instancesObject



49
50
51
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 49

def self.all_instances
  @all_instances ||= EC2Instance.instance_hash
end

.instances_without_stackObject



53
54
55
56
57
58
59
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 53

def self.instances_without_stack 
  all #simply getting all stacks to make sure instance stack_ids is set
  all_instances.map do |id, instance|
    next if instance.stack_id 
    instance
  end.compact
end

Instance Method Details

#get_instancesObject



19
20
21
22
23
24
25
26
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 19

def get_instances
  return @instances if @instances
  @instances = self.class.opsworks.describe_instances({stack_id: id})[:instances].map do |instance|
    next unless instance[:status].to_s == 'online'
    self.class.all_instances[instance[:ec2_instance_id]].stack_id = id
    self.class.all_instances[instance[:ec2_instance_id]]
  end
end

#pretty_printObject



34
35
36
37
38
39
40
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 34

def pretty_print
  puts "----------------------------------"
  puts "#{@name}"
  puts "----------------------------------"
  print_instances
  puts "\n"
end


28
29
30
31
32
# File 'lib/sport_ngin_aws_auditor/stack.rb', line 28

def print_instances
  EC2Instance.instance_count_hash(self.instances).each do |key,value|
    say "<%= color('#{key}: #{value}', :white) %>"
  end
end