aws-cache 0.0.07 → 0.0.08

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjM4Njk1ZjI2ZjgzMzZkYzYxYjhjZGJiNDIwMDY5MWYzNzUxZmM2OQ==
4
+ ZDUxM2E4MTcyYTE3NTQyNGFiNjg4ZGM3YTM0NDlhZmIyMDE4OWE0NA==
5
5
  data.tar.gz: !binary |-
6
- ZGY0NTMwZjg1NjZjMDU1NzBmODczMzZiZmNlNGNjN2IyOGQxOWYwZQ==
6
+ ZDYwNzZmNTdkYzZlNTYyMDI0ZDUzNjUyZTMyMTgwNTg2NDZjYzZmMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWRjMmQ3NjdlZDNlZDVjMWViY2MxNTI0OTU5ODE1YWRkNDczMWFkYjA0Yzc2
10
- MTA2ZTEzOTQ1NGMwZThmYWUzM2RkNTc5NWZiNTY3N2E3MTdkZDU4MGNmM2Ri
11
- NGQ3ZWZiY2ExZTI3OWVkMjc0Yjk4NDIyMGFlMTEwYWIyYWU4NjE=
9
+ NjE5MzQ2Zjc5ZThlMTg5OTAzMDJjNTA4YTczNGQ5MGQ4ZDkwMzZkODA4NmRk
10
+ OWRmMDE0YWMzYmRiNWEzZjRiZjhhOGIwNGU4ZmVkY2NlODVkMGE2NzBiZTUx
11
+ MzliYjYwMjIxYzNjN2IxNmZiNzg5NDM5MzliZDVmZmNmMzA0Yjk=
12
12
  data.tar.gz: !binary |-
13
- ZDdkMGMwNzhjYWNlMzA1MWFjMDQ3YmU4MjJiNmRjZTgxNTQ3MjBjYWFlMGNk
14
- ZTYwZjczYzljMDYxY2UyNzg5NzUxY2MyZDk2NTA0Y2E3MTAxZjg1MTNiODAx
15
- MzZlYjAwZTEwMGU1OWUyMjFjYWJmZmE5YzVlYzFkNjhiODdhMGM=
13
+ ZmM4MzczZTdkMTVlNjczYzJlM2Y3YTU0NGZhODFlNDgyMWM0OWM2Y2M1ZWRk
14
+ ZjI1Y2M0NzFmZTM1NGQyZGMwNTVlODcwMDc0OWY5M2EzZDhmNmNjYWRjZTQy
15
+ OGMwNzYwZGEzYTA4YjEwNTRhMGZjMDJiNmZhMmExZDk3NzRhZjk=
@@ -1,4 +1,4 @@
1
1
  module AwsCacheVersion
2
2
  # Please follow semantic versioning (semver.org).
3
- VERSION = '0.0.07'
3
+ VERSION = '0.0.08'
4
4
  end
@@ -4,6 +4,7 @@ require 'aws-sdk'
4
4
  require 'yaml'
5
5
  require 'pry'
6
6
  require 'aws-cache-version'
7
+ require 'awesome_print'
7
8
 
8
9
  class AwsCache
9
10
  # Please follow semantic versioning (semver.org).
@@ -20,162 +21,119 @@ class AwsCache
20
21
  @region = optional_element(opts, ['region'], 'us-east-1')
21
22
  end
22
23
 
23
- def ec2_instances
24
- instances = cache_get('aws_ec2_instances', 300) do
25
- instances = {}
26
-
27
- ec2 = Aws::EC2::Client.new(region: @region)
28
- pages = ec2.describe_instances
29
- pages.each do |page|
30
- page =
31
- page.data[:reservations].each do |res|
32
- res[:instances].each do |instance|
33
- list_to_hash!(instance, [:tags], :key)
34
- list_to_hash!(instance, [:block_device_mappings], :device_name)
35
- instances[instance[:instance_id]] = instance
36
- end
37
- end
24
+ def stack_auto_scaling_groups(stack_name)
25
+ autoscaling_groups = Array.new()
26
+ output = self.list_stack_resources(stack_name)
27
+ output.each do |entry|
28
+ if entry[:resource_type] == "AWS::AutoScaling::AutoScalingGroup"
29
+ autoscaling_groups.push(entry)
38
30
  end
39
-
40
- instances
41
31
  end
32
+ return autoscaling_groups
33
+ end
42
34
 
43
- return instances
35
+ def list_stack_resources( stack_name)
36
+ output = cache_get_2("list_stack_resources-#{stack_name}", 300) do
37
+ aws_object = Aws::CloudFormation::Client.new(region: @region)
38
+ pages = aws_object.list_stack_resources(stack_name: stack_name)
39
+ output = process_page( 'stack_resource_summaries', pages)
40
+ end
41
+ return output
44
42
  end
45
43
 
46
- def stack_instances(stack_name)
47
- instances = self.ec2_instances
48
- stack_instances = {}
49
- instances.each do |id, instance|
50
- if stack_name == optional_element(instance, [:tags,'StackName',:value], '')
51
- stack_instances[id] = instance
44
+
45
+ def get_sub_stacks( stack_name)
46
+ substacks = Array.new()
47
+ stacks = self.list_stack_resources(stack_name)
48
+ stacks.each do |entry|
49
+ if entry[:resource_type] == "AWS::CloudFormation::Stack"
50
+ self.describe_stacks.each do |stack|
51
+ if entry[:physical_resource_id] == stack[:stack_id]
52
+ substacks.push(stack)
53
+ end
54
+ end
52
55
  end
53
56
  end
54
- return stack_instances
55
- end
56
-
57
- def auto_scaling_groups
58
- groups = cache_get('aws_auto_scaling_groups', 300) do
59
- groups = {}
60
-
61
- autoscaling = Aws::AutoScaling::Client.new(region: @region)
62
- pages = autoscaling.describe_auto_scaling_groups
63
- pages.each do |page|
64
- page.data[:auto_scaling_groups].each do |group|
65
- instances = Hash.new()
66
- list_to_hash!(group, [:instances], :instance_id)
67
- list_to_hash!(group, [:tags], :key)
68
- groups[group[:auto_scaling_group_name]] = group
69
- end
57
+ return substacks
58
+ end
59
+
60
+ def describe_stack(stack_name)
61
+ stacks = self.describe_stacks
62
+ stacks.each do |stack|
63
+ if stack[:stack_name] == stack_name
64
+ return stack
70
65
  end
71
-
72
- groups
73
66
  end
67
+ return nil
68
+ end
74
69
 
75
- return groups
70
+ def describe_stacks()
71
+ output = cache_get_2('get_stacks', 300) do
72
+ aws_object = Aws::CloudFormation::Client.new(region: @region)
73
+ pages = aws_object.describe_stacks
74
+ output = process_page( 'stacks', pages)
75
+ end
76
+ return output
76
77
  end
77
78
 
78
- def stack_auto_scaling_groups(stack_name)
79
- groups = self.auto_scaling_groups
80
- stack_groups = {}
81
- groups.each do |name, group|
82
- if stack_name == optional_element(group, [:tags,'StackName',:value], '')
83
- stack_groups[name] = group
84
- end
79
+ def describe_snapshots()
80
+ output = cache_get_2('get_snapshots', 300) do
81
+ aws_object = Aws::EC2::Client.new(region: @region)
82
+ pages = aws_object.describe_snapshots
83
+ output = process_page( 'snapshots', pages)
85
84
  end
86
- return stack_groups
87
- end
88
-
89
- def describe_stacks
90
- cloudformation_stacks = cache_get('aws_cloudformation_describe_stacks', 900) do
91
- cloudformation_stacks = {}
92
-
93
- cfn = Aws::CloudFormation::Client.new(region: @region)
94
- pages = cfn.describe_stacks
95
- pages.each do |page|
96
- page.data[:stacks].each do |stack|
97
- list_to_hash!(stack, [:parameters], :parameter_key)
98
- list_to_hash!(stack, [:outputs], :output_key)
99
- list_to_hash!(stack, [:tags], :key)
100
- cloudformation_stacks[stack[:stack_name]] = stack
101
- end
102
- end
103
-
104
- cloudformation_stacks
85
+ return output
86
+ end
87
+
88
+ def get_asg_instances(asg)
89
+ instances = Array.new()
90
+ asg = self.describe_auto_scaling_group(asg)
91
+ asg[:instances].each do |instance|
92
+ instances.push(instance)
105
93
  end
94
+ return instances
95
+ end
106
96
 
107
- return cloudformation_stacks
97
+ def describe_auto_scaling_group(asg)
98
+ asgroups = self.describe_autoscaling_groups()
99
+ asgroups.each do |asgroup|
100
+ if asgroup[:auto_scaling_group_name] == asg then
101
+ return asgroup
102
+ end
103
+ end
104
+ return nil
108
105
  end
109
106
 
110
- def describe_stack(stack_name)
111
- stacks = self.describe_stacks
112
- return stacks[stack_name]
107
+ def describe_autoscaling_groups()
108
+ output = cache_get_2('get_autoscaling_groups', 300) do
109
+ aws_object = Aws::AutoScaling::Client.new(region: @region)
110
+ pages = aws_object.describe_auto_scaling_groups
111
+ output = process_page( 'auto_scaling_groups', pages)
112
+ end
113
+ return output
113
114
  end
114
115
 
115
- def list_stack_resources(stack_name)
116
- stack_resources = cache_get("aws_cloudformation_list_stack_resources:#{stack_name}", 900) do
117
- stack_resources = {}
118
- cfn = Aws::CloudFormation::Client.new(region: @region)
116
+ def describe_instances()
117
+ output = cache_get_2('describe_instances', 300) do
118
+ aws_object = Aws::EC2::Client.new(region: @region)
119
+ pages = aws_object.describe_instances
120
+ output = process_page( 'reservations', pages)
121
+ end
122
+ return output
123
+ end
119
124
 
120
- pages = cfn.list_stack_resources(stack_name: stack_name)
121
- pages.each do |page|
122
- resources = page.data[:stack_resource_summaries]
123
- resources.each do |resource|
124
- stack_resources[resource[:logical_resource_id]] = resource
125
- end
126
- end
127
-
128
- stack_resources
129
- end
130
-
131
- return stack_resources
132
- end
133
-
134
- # One way this is different than describe_stacks is that it includes
135
- # deleted stacks.
136
- def list_stacks
137
- stacks = cache_get('aws_cloudformation_list_stacks', 900) do
138
- # Can't return a hash, because some stacks will appear more
139
- # than once, such as if the stack was deleted and recreated.
140
- stacks = []
141
-
142
- cfn = Aws::CloudFormation::Client.new(region: @region)
143
- pages = cfn.list_stacks
144
- pages.each do |page|
145
- page.data[:stack_summaries].each do |stack|
146
- stacks.push(stack)
147
- end
148
- end
149
-
150
- stacks
151
- end
152
-
153
- return stacks
154
- end
155
-
156
- def get_snapshots()
157
- snapshots = cache_get('aws_ec2_snapshots', 9) do
158
- snapshots = Hash.new()
159
- ec2 = Aws::EC2::Client.new(region: @region)
160
- pages = ec2.describe_snapshots
161
- pages.each do |page|
162
- page.data.each do |data|
163
- data.each do |snap|
164
- if snapshots.has_key?(snap[:volume_id]) then
165
- snapshots[snap[:volume_id]].push(snap)
166
- else
167
- snapshots[snap[:volume_id]] = Array.new()
168
- snapshots[snap[:volume_id]].push(snap)
169
- end
170
- end
125
+ def process_page( key, pages)
126
+ output = Array.new()
127
+ pages.each do |page|
128
+ page.each do |data|
129
+ data.data[key].each do |entry|
130
+ output.push(entry)
171
131
  end
172
132
  end
173
- snapshots
174
- end
175
- return snapshots
133
+ end
134
+ return output
176
135
  end
177
136
 
178
-
179
137
  private
180
138
  def optional_element(hash, keys, default=nil)
181
139
  keys.each do |key|
@@ -198,9 +156,22 @@ class AwsCache
198
156
  return false
199
157
  end
200
158
 
159
+ def cache_get_2(key, ttl)
160
+ vkey = "#{key}_#{@keyspace}"
161
+ output = @redis.get(vkey)
162
+ if output.nil?
163
+ output = yield
164
+ output = YAML.dump(output)
165
+ @redis.setex(vkey, ttl, output )
166
+ end
167
+ return YAML.load(output)
168
+ end
169
+
170
+
201
171
  def cache_get(key, ttl)
202
172
  vkey = "#{key}_#{@keyspace}"
203
173
  hash = @redis.get(vkey)
174
+ ap hash.class.name
204
175
  unless hash.nil?
205
176
  hash = YAML.load(hash)
206
177
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.07
4
+ version: 0.0.08
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen J. Smith
8
+ - Brian J. Schrock
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-06-09 00:00:00.000000000 Z
12
+ date: 2015-06-25 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: aws-sdk