21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/swaggerless/cleaner.rb', line 21
def clean_unused_deployments(swagger)
apis = @api_gateway_client.get_rest_apis(limit: 500).data
api = apis.items.select { |a| a.name == swagger['info']['title'] }.first
if api
used_deployments = Hash.new
response = @api_gateway_client.get_stages({rest_api_id: api.id})
response.item.each do |stage|
puts "Deployment #{stage.deployment_id} used by #{stage.stage_name}"
used_deployments[stage.deployment_id] = stage.stage_name
end
response = @api_gateway_client.get_deployments({rest_api_id: api.id, limit: 500})
time_threshold = Time.now
response.items.each do |deployment|
if used_deployments[deployment.id] == nil and Time.at(deployment.created_date) < time_threshold
puts "Deployment #{deployment.id} is not used and old enough to be pruned"
@api_gateway_client.delete_deployment({rest_api_id: api.id, deployment_id: deployment.id})
end
end
response = @api_gateway_client.get_deployments({rest_api_id: api.id, limit: 500})
lambda_liases_used = Hash.new
response.items.each do |deployment|
if deployment.description != nil then
first_char = deployment.description.rindex(":")
if first_char
func_alias = deployment.description[deployment.description.rindex(":")+2..-1]
puts "Lambda alias #{func_alias} still used by deployment #{deployment.id}"
lambda_liases_used[func_alias] = deployment.id
end
end
end
configured_lambdas = .get_lambda_map(swagger)
configured_lambdas.each do |functionName, config|
lambda_versions_used = Hash.new
if config[:handler] == nil
next
end
begin
resp = @lambda_client.list_aliases({function_name: functionName, max_items: 500 })
resp.aliases.each do |funcAlias|
if lambda_liases_used[funcAlias.name] == nil then
puts "Deleting alias #{funcAlias.name} for lambda function #{functionName}"
@lambda_client.delete_alias({function_name: functionName, name: funcAlias.name})
else
puts "Lambda version #{funcAlias.function_version} still used by function deployment #{functionName}"
lambda_versions_used[funcAlias.function_version] = funcAlias.name
end
end
resp = @lambda_client.list_versions_by_function({function_name: functionName, max_items: 500})
resp.versions.each do |lambda|
if lambda.version != "$LATEST" and lambda_versions_used[lambda.version] == nil
puts "Deleting lambda version #{lambda.version} of function #{functionName}"
@lambda_client.delete_function({function_name: functionName, qualifier: lambda.version})
end
end
rescue Aws::Lambda::Errors::ResourceNotFoundException
puts "Function #{functionName} does not exist and will be skipped"
end
end
end
end
|