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



97
98
99
100
101
102
103
104
105
# File 'lib/aws-cache.rb', line 97

def describe_auto_scaling_group(asg)
  asgroups = self.describe_autoscaling_groups()
  asgroups.each do |asgroup|
    if asgroup[:auto_scaling_group_name] == asg then
      return asgroup
    end
  end
  return nil
end

#describe_autoscaling_groupsObject



107
108
109
110
111
112
113
114
# File 'lib/aws-cache.rb', line 107

def describe_autoscaling_groups()
  output = cache_get_2('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_instancesObject



116
117
118
119
120
121
122
123
# File 'lib/aws-cache.rb', line 116

def describe_instances()
  output = cache_get_2('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



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

def describe_snapshots()
  output = cache_get_2('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



60
61
62
63
64
65
66
67
68
# File 'lib/aws-cache.rb', line 60

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



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

def describe_stacks()
  output = cache_get_2('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



88
89
90
91
92
93
94
95
# File 'lib/aws-cache.rb', line 88

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

#get_sub_stacks(stack_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aws-cache.rb', line 45

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



35
36
37
38
39
40
41
42
# File 'lib/aws-cache.rb', line 35

def list_stack_resources( stack_name)
  output = cache_get_2("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



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aws-cache.rb', line 125

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

#stack_auto_scaling_groups(stack_name) ⇒ Object



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

def stack_auto_scaling_groups(stack_name)
  autoscaling_groups = Array.new()
  output = self.list_stack_resources(stack_name)
  output.each do |entry|
    if entry[:resource_type] == "AWS::AutoScaling::AutoScalingGroup"
      autoscaling_groups.push(entry)
    end
  end
  return autoscaling_groups
end