Class: AwsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-cache.rb

Constant Summary collapse

VERSION =

Please follow semantic versioning (semver.org).

AwsCacheVersion::VERSION

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ AwsCache

Returns a new instance of AwsCache.



13
14
15
16
17
18
19
20
21
22
# File 'lib/aws-cache.rb', line 13

def initialize(opts)
  unless opts.has_key?('port') then opts['port'] = 6379 end
  unless opts.has_key?('host') then opts['host'] = 'aws-cache' end
  @redis = optional_element(opts, ['redis'])
  if @redis.nil?
    @redis = Redis.new(url: "redis://#{opts['host']}:#{opts['port']}/0")
  end
  @keyspace = optional_element(opts, ['keyspace'], AwsCache::VERSION)
  @region = optional_element(opts, ['region'], 'us-east-1')
end

Instance Method Details

#describe_auto_scaling_group(asg) ⇒ Object

Returns Aws::AutoScaling::Types::AutoScalingGroup



90
91
92
93
94
95
96
97
98
99
# File 'lib/aws-cache.rb', line 90

def describe_auto_scaling_group(asg)
  asgroups = self.describe_autoscaling_groups()
  target_asg = asgroups.select do |record|
    record[:auto_scaling_group_name] == asg
  end
  if target_asg then
    return target_asg[0]
  end
  return nil
end

#describe_auto_scaling_instancesObject



129
130
131
132
133
134
135
136
# File 'lib/aws-cache.rb', line 129

def describe_auto_scaling_instances()
  output = cache_get("describe_auto_scaling_instances", 300) do
    aws_object = Aws::AutoScaling::Client.new(region: @region)
    pages = aws_object.describe_auto_scaling_instances()
    output = process_page( 'auto_scaling_instances', pages)
  end
  return output
end

#describe_autoscaling_groupsObject

Returns an Array of Hashes containing auto scaling group structures



140
141
142
143
144
145
146
147
# File 'lib/aws-cache.rb', line 140

def describe_autoscaling_groups()
  output = cache_get('get_autoscaling_groups', 300) do
    aws_object = Aws::AutoScaling::Client.new(region: @region) 
    pages = aws_object.describe_auto_scaling_groups
    output = process_page( 'auto_scaling_groups', pages)
  end
  return output
end

#describe_instance(instance_id) ⇒ Object

Returns a hash describing the instance requested.



25
26
27
28
29
30
31
# File 'lib/aws-cache.rb', line 25

def describe_instance( instance_id)
  instances = self.describe_instances()
  instance = instances.select do |entry|
    entry[:instances][0][:instance_id] == instance_id
  end
  return instance[0]
end

#describe_instancesObject

Returns an Array of Hashes containing instance structures



150
151
152
153
154
155
156
157
# File 'lib/aws-cache.rb', line 150

def describe_instances()
  output = cache_get('describe_instances', 300) do
    aws_object = Aws::EC2::Client.new(region: @region)
    pages = aws_object.describe_instances
    output = process_page( 'reservations', pages)
  end
  return output
end

#describe_snapshotsObject



101
102
103
104
105
106
107
108
# File 'lib/aws-cache.rb', line 101

def describe_snapshots()
  output = cache_get('get_snapshots', 300) do
    aws_object = Aws::EC2::Client.new(region: @region)
    pages = aws_object.describe_snapshots
    output = process_page( 'snapshots', pages)
  end
  return output
end

#describe_stack(stack_name) ⇒ Object

Returns a hash describing the stack requested.



69
70
71
72
73
74
75
76
77
# File 'lib/aws-cache.rb', line 69

def describe_stack(stack_name)
  stacks = self.describe_stacks
  stacks.each do |stack|
    if stack[:stack_name] == stack_name
      return stack
    end
  end
  return nil
end

#describe_stacksObject



119
120
121
122
123
124
125
126
# File 'lib/aws-cache.rb', line 119

def describe_stacks()
  output = cache_get('get_stacks', 300) do
    aws_object = Aws::CloudFormation::Client.new(region: @region)
    pages = aws_object.describe_stacks
    output = process_page( 'stacks', pages)
  end
  return output
end

#get_asg_instances(asg) ⇒ Object

Returns an array of hashes of instances.



80
81
82
83
84
85
86
87
# File 'lib/aws-cache.rb', line 80

def get_asg_instances(asg)
  instances = Array.new()
  asg_instances = self.describe_auto_scaling_group(asg)
  asg_instances[:instances].each do |instance|
    instances.push(instance)
  end
  return instances
end

#get_sub_stacks(stack_name) ⇒ Object

Returns an array of hashes describing the substacks for the selected stack.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aws-cache.rb', line 53

def get_sub_stacks( stack_name)
  substacks = Array.new()
  stacks = self.list_stack_resources(stack_name)
  stacks.each do |entry|
    if entry[:resource_type] == "AWS::CloudFormation::Stack"
      self.describe_stacks.each do |stack|
        if entry[:physical_resource_id] == stack[:stack_id]
          substacks.push(stack)
        end
      end
    end
  end
  return substacks
end

#list_stack_resources(stack_name) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/aws-cache.rb', line 110

def list_stack_resources( stack_name)
  output = cache_get("list_stack_resources-#{stack_name}", 300) do
    aws_object = Aws::CloudFormation::Client.new(region: @region)
    pages = aws_object.list_stack_resources(stack_name: stack_name)
    output = process_page( 'stack_resource_summaries', pages)
  end
  return output
end

#process_page(key, pages) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/aws-cache.rb', line 159

def process_page( key, pages)
  output = Array.new()
  pages.each do |page|
    data = page.send( key)
    data.each do |asg|
      output.push(asg)
    end
  end
  return output
end

#stack_auto_scaling_groups(stack_name) ⇒ Object

Returns an array of hashes describing the autoscaling groups for the selected stack.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aws-cache.rb', line 34

def stack_auto_scaling_groups(stack_name)
  auto_scaling_groups = Array.new()
  stack_resources = self.list_stack_resources(stack_name)
  asg_groups = stack_resources.select do |record|
    record[:resource_type] == "AWS::AutoScaling::AutoScalingGroup"
  end
  auto_scaling_groups.concat(asg_groups)
  substacks = stack_resources.select do |record|
    record[:resource_type] == "AWS::CloudFormation::Stack"
  end
  if substacks.length > 0 then
    substacks.each do |stack|
      auto_scaling_groups.concat(self.stack_auto_scaling_groups(stack[:physical_resource_id]))
    end
  end
  return auto_scaling_groups
end