Class: AwsCache
- Inherits:
-
Object
- Object
- AwsCache
- Defined in:
- lib/aws-cache.rb
Constant Summary collapse
- VERSION =
Please follow semantic versioning (semver.org).
AwsCacheVersion::VERSION
Instance Method Summary collapse
- #auto_scaling_groups ⇒ Object
- #describe_stack(stack_name) ⇒ Object
- #describe_stacks ⇒ Object
- #ec2_instances ⇒ Object
-
#initialize(opts) ⇒ AwsCache
constructor
A new instance of AwsCache.
- #list_stack_resources(stack_name) ⇒ Object
-
#list_stacks ⇒ Object
One way this is different than describe_stacks is that it includes deleted stacks.
- #stack_auto_scaling_groups(stack_name) ⇒ Object
- #stack_instances(stack_name) ⇒ Object
Constructor Details
#initialize(opts) ⇒ AwsCache
Returns a new instance of AwsCache.
12 13 14 15 16 17 18 19 |
# File 'lib/aws-cache.rb', line 12 def initialize(opts) @redis = optional_element(opts, ['redis']) if @redis.nil? @redis = Redis.new(url: 'redis://aws-cache:6379/0') end @keyspace = optional_element(opts, ['keyspace'], AwsCache::VERSION) @region = optional_element(opts, ['region'], 'us-east-1') end |
Instance Method Details
#auto_scaling_groups ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/aws-cache.rb', line 55 def auto_scaling_groups groups = cache_get('aws_auto_scaling_groups', 300) do groups = {} autoscaling = Aws::AutoScaling::Client.new(region: @region) page = autoscaling.describe_auto_scaling_groups page.each do |page| page.data[:auto_scaling_groups].each do |group| instances = {} 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
108 109 110 111 |
# File 'lib/aws-cache.rb', line 108 def describe_stack(stack_name) stacks = self.describe_stacks return stacks[stack_name] end |
#describe_stacks ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/aws-cache.rb', line 87 def describe_stacks cloudformation_stacks = cache_get('aws_cloudformation_describe_stacks', 900) do cloudformation_stacks = {} cfn = Aws::CloudFormation::Client.new(region: @region) page = cfn.describe_stacks page.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_instances ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/aws-cache.rb', line 21 def ec2_instances instances = cache_get('aws_ec2_instances', 300) do instances = {} ec2 = Aws::EC2::Client.new(region: @region) page = ec2.describe_instances page.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 |
#list_stack_resources(stack_name) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/aws-cache.rb', line 113 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) page = cfn.list_stack_resources(stack_name: stack_name) page.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_stacks ⇒ Object
One way this is different than describe_stacks is that it includes deleted stacks.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/aws-cache.rb', line 134 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) page = cfn.list_stacks page.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
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/aws-cache.rb', line 76 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
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aws-cache.rb', line 44 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 |