Class: AwsCache

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

Constant Summary collapse

VERSION =

Please follow semantic versioning (semver.org).

'0.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ AwsCache

Returns a new instance of AwsCache.



10
11
12
13
14
15
16
17
# File 'lib/aws-cache.rb', line 10

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_groupsObject



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

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



106
107
108
109
# File 'lib/aws-cache.rb', line 106

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

#describe_stacksObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/aws-cache.rb', line 85

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_instancesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aws-cache.rb', line 19

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/aws-cache.rb', line 111

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_stacksObject

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aws-cache.rb', line 132

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



74
75
76
77
78
79
80
81
82
83
# File 'lib/aws-cache.rb', line 74

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/aws-cache.rb', line 42

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