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.



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

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

#auto_scaling_groupsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aws-cache.rb', line 57

def auto_scaling_groups
  groups = cache_get('aws_auto_scaling_groups', 300) do
    groups = {}
  
    autoscaling = Aws::AutoScaling::Client.new(region: @region)
    pages = autoscaling.describe_auto_scaling_groups
    pages.each do |page|
      page.data[:auto_scaling_groups].each do |group|
 instances = Hash.new()
 list_to_hash!(group, [:instances], :instance_id)
 list_to_hash!(group, [:tags], :key)
 groups[group[:auto_scaling_group_name]] = group
  end
    end
  
    groups
  end

  return groups
end

#describe_stack(stack_name) ⇒ Object



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

def describe_stack(stack_name)
  stacks = self.describe_stacks
  return stacks[stack_name]
end

#describe_stacksObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/aws-cache.rb', line 89

def describe_stacks
  cloudformation_stacks = cache_get('aws_cloudformation_describe_stacks', 900) do
    cloudformation_stacks = {}
  
    cfn = Aws::CloudFormation::Client.new(region: @region)
    pages = cfn.describe_stacks
    pages.each do |page|
      page.data[:stacks].each do |stack|
 list_to_hash!(stack, [:parameters], :parameter_key)
 list_to_hash!(stack, [:outputs], :output_key)
 list_to_hash!(stack, [:tags], :key)
 cloudformation_stacks[stack[:stack_name]] = stack
  end
    end
  
    cloudformation_stacks
  end

  return cloudformation_stacks
end

#ec2_instancesObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aws-cache.rb', line 23

def ec2_instances
  instances = cache_get('aws_ec2_instances', 300) do
    instances = {}
  
    ec2 = Aws::EC2::Client.new(region: @region)
    pages = ec2.describe_instances
    pages.each do |page|
     page = 
      page.data[:reservations].each do |res|
        res[:instances].each do |instance|
   list_to_hash!(instance, [:tags], :key)
   list_to_hash!(instance, [:block_device_mappings], :device_name)
   instances[instance[:instance_id]] = instance
 end
  end
    end
  
    instances
  end

  return instances
end

#get_snapshotsObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/aws-cache.rb', line 156

def get_snapshots()
  snapshots = cache_get('aws_ec2_snapshots', 9) do
    snapshots = Hash.new()
    ec2 = Aws::EC2::Client.new(region: @region)
    pages = ec2.describe_snapshots
    pages.each do |page|
      page.data.each do |data|
        data.each do |snap|
          if snapshots.has_key?(snap[:volume_id]) then
            snapshots[snap[:volume_id]].push(snap)
          else
            snapshots[snap[:volume_id]] = Array.new()
            snapshots[snap[:volume_id]].push(snap)
          end
        end
      end
    end
    snapshots
 end
  return snapshots
end

#list_stack_resources(stack_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aws-cache.rb', line 115

def list_stack_resources(stack_name)
  stack_resources = cache_get("aws_cloudformation_list_stack_resources:#{stack_name}", 900) do
    stack_resources = {}
    cfn = Aws::CloudFormation::Client.new(region: @region)

    pages = cfn.list_stack_resources(stack_name: stack_name)
    pages.each do |page|
      resources = page.data[:stack_resource_summaries]
  resources.each do |resource|
 stack_resources[resource[:logical_resource_id]] = resource
  end
    end
  
    stack_resources
  end

  return stack_resources
end

#list_stacksObject

One way this is different than describe_stacks is that it includes deleted stacks.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/aws-cache.rb', line 136

def list_stacks
  stacks = cache_get('aws_cloudformation_list_stacks', 900) do
    # Can't return a hash, because some stacks will appear more
    # than once, such as if the stack was deleted and recreated.
    stacks = []
  
    cfn = Aws::CloudFormation::Client.new(region: @region)
    pages = cfn.list_stacks
    pages.each do |page|
      page.data[:stack_summaries].each do |stack|
 stacks.push(stack)
  end
    end
  
    stacks
  end

  return stacks
end

#stack_auto_scaling_groups(stack_name) ⇒ Object



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

def stack_auto_scaling_groups(stack_name)
  groups = self.auto_scaling_groups
  stack_groups = {}
  groups.each do |name, group|
    if stack_name == optional_element(group, [:tags,'StackName',:value], '')
      stack_groups[name] = group
    end
  end
  return stack_groups
end

#stack_instances(stack_name) ⇒ Object



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

def stack_instances(stack_name)
  instances = self.ec2_instances
  stack_instances = {}
  instances.each do |id, instance|
    if stack_name == optional_element(instance, [:tags,'StackName',:value], '')
      stack_instances[id] = instance
    end
  end
  return stack_instances
end