Class: Jets::Cfn::Teardown::Bucket
Instance Method Summary
collapse
#log
#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2
#output_value, #stack_exists?
included, #reset_cache!
Instance Method Details
#are_you_sure? ⇒ Boolean
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/jets/cfn/teardown/bucket.rb', line 65
def are_you_sure?
if @options[:yes]
sure = "y"
else
log.debug "Are you sure you want to delete the #{Jets.project.namespace.color(:green)} project? (y/N)"
sure = $stdin.gets
end
unless /^y/.match?(sure)
log.debug "Phew! Jets #{Jets.project.namespace.color(:green)} project was not deleted."
exit 0
end
end
|
#bucket_exists? ⇒ Boolean
57
58
59
60
61
62
63
|
# File 'lib/jets/cfn/teardown/bucket.rb', line 57
def bucket_exists?
return false unless s3_bucket
s3.head_bucket(bucket: s3_bucket, use_accelerate_endpoint: false)
true
rescue Aws::S3::Errors::NotFound
false
end
|
#delete_objects(call_count = 0) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/jets/cfn/teardown/bucket.rb', line 12
def delete_objects(call_count = 0)
return unless bucket_exists?
log.info "Emptying s3 bucket #{s3_bucket}" if s3_bucket && call_count == 0
resp = s3.list_objects(bucket: s3_bucket)
if resp.contents.size > 0
objects = resp.contents.map { |item| {key: item.key} }
s3.delete_objects(
bucket: s3_bucket,
delete: {
objects: objects,
quiet: false
}
)
delete_objects(call_count + 1) end
end
|
#empty! ⇒ Object
7
8
9
10
|
# File 'lib/jets/cfn/teardown/bucket.rb', line 7
def empty!
delete_objects
wait_for_emptiness
end
|
#wait_for_emptiness ⇒ Object
Think there can be a race condition since delete_objects is async So we wait for the bucket to be empty Before we continue with the delete stack
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/jets/cfn/teardown/bucket.rb', line 36
def wait_for_emptiness
return unless bucket_exists?
@empty_bucket_checks ||= 0
max_checks = 5
while @empty_bucket_checks < max_checks
is_empty = s3.list_objects(bucket: s3_bucket).contents.empty?
if is_empty
log.debug "Bucket '#{s3_bucket}' is empty."
return
else
log.debug "Bucket '#{s3_bucket}' is not empty. Waiting..."
sleep 2
@empty_bucket_checks += 1
end
end
log.debug "Timeout: Bucket '#{s3_bucket}' is not empty after waiting."
end
|