Class: Jets::Commands::Delete
- Inherits:
-
Object
- Object
- Jets::Commands::Delete
show all
- Includes:
- AwsServices
- Defined in:
- lib/jets/commands/delete.rb
Instance Method Summary
collapse
#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts
#lookup, #stack_exists?, #stack_in_progress?
included
Constructor Details
#initialize(options) ⇒ Delete
Returns a new instance of Delete.
4
5
6
|
# File 'lib/jets/commands/delete.rb', line 4
def initialize(options)
@options = options
end
|
Instance Method Details
#are_you_sure? ⇒ Boolean
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/jets/commands/delete.rb', line 116
def are_you_sure?
if @options[:yes]
sure = 'y'
else
puts "Are you sure you want to want to delete the #{Jets.config.project_namespace.color(:green)} project? (y/N)"
sure = $stdin.gets
end
unless sure =~ /^y/
puts "Phew! Jets #{Jets.config.project_namespace.color(:green)} project was not deleted."
exit 0
end
end
|
#bucket_exists?(bucket_name) ⇒ Boolean
102
103
104
105
106
107
108
109
110
|
# File 'lib/jets/commands/delete.rb', line 102
def bucket_exists?(bucket_name)
bucket_exists = false
begin
resp = s3.head_bucket(bucket: bucket_name, use_accelerate_endpoint: false)
bucket_exists = true
rescue
end
bucket_exists
end
|
#check_deleteable_status ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/jets/commands/delete.rb', line 133
def check_deleteable_status
return true if !stack_exists?(@parent_stack_name)
resp = cfn.describe_stacks(stack_name: @parent_stack_name)
status = resp.stacks[0].stack_status
return true if status == 'ROLLBACK_COMPLETE'
if status =~ /_IN_PROGRESS$/
puts "The '#{@parent_stack_name}' stack status is #{status}. " \
"It is not in an updateable status. Please wait until the stack is ready and try again.".color(:red)
exit 0
elsif resp.stacks[0].outputs.empty?
puts "The minimal stack failed to create. Please delete the stack first and try again. " \
"You can delete the CloudFormation stack or use the `jets delete` command"
exit 0
else
true
end
end
|
#confirm_project_exists ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/jets/commands/delete.rb', line 46
def confirm_project_exists
retries = 0
begin
cfn.describe_stacks(stack_name: parent_stack_name)
rescue Aws::CloudFormation::Errors::ValidationError
puts "The parent stack #{Jets.config.project_namespace.color(:green)} for the project #{Jets.config.project_name.color(:green)} does not exist. So it cannot be deleted."
exit 0
rescue Aws::CloudFormation::Errors::Throttling => e
retries += 1
seconds = 2 ** retries
puts "WARN: confirm_project_exists #{e.class} #{e.message}".color(:yellow)
puts "Backing off and will retry in #{seconds} seconds."
sleep(seconds)
if seconds > 90 puts "Giving up after #{retries} retries"
else
retry
end
end
end
|
#delete_logs ⇒ Object
40
41
42
43
44
|
# File 'lib/jets/commands/delete.rb', line 40
def delete_logs
puts "Deleting CloudWatch logs"
log = Jets::Commands::Clean::Log.new(mute: true, yes: true)
log.clean
end
|
#empty_s3_bucket ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/jets/commands/delete.rb', line 70
def empty_s3_bucket
return unless s3_bucket_name return unless bucket_exists?(s3_bucket_name)
resp = s3.list_objects(bucket: s3_bucket_name)
if resp.contents.size > 0
objects = resp.contents.map { |item| {key: item.key} }
s3.delete_objects(
bucket: s3_bucket_name,
delete: {
objects: objects,
quiet: false,
}
)
empty_s3_bucket end
end
|
#parent_stack_name ⇒ Object
#run ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/jets/commands/delete.rb', line 8
def run
puts("Deleting project...")
return if @options[:noop]
are_you_sure?
confirm_project_exists
puts "First, deleting objects in s3 bucket #{s3_bucket_name}" if s3_bucket_name
empty_s3_bucket
stack_in_progress?(parent_stack_name)
cfn.delete_stack(stack_name: parent_stack_name)
puts "Deleting #{Jets.config.project_namespace.color(:green)}..."
wait_for_stack if @options[:wait]
delete_logs
puts "Project #{Jets.config.project_namespace.color(:green)} deleted!"
end
|
#s3_bucket_name ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/jets/commands/delete.rb', line 89
def s3_bucket_name
return @s3_bucket_name if defined?(@s3_bucket_name)
resp = cfn.describe_stacks(stack_name: parent_stack_name)
outputs = resp.stacks[0].outputs
if outputs.empty?
@s3_bucket_name = false
else
@s3_bucket_name = outputs.find {|o| o.output_key == 'S3Bucket'}.output_value
end
end
|
#wait_for_stack ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/jets/commands/delete.rb', line 32
def wait_for_stack
status = Jets::Cfn::Status.new(@options)
start_time = Time.now
status.wait
took = Time.now - start_time
puts "Time took for deletion: #{status.pretty_time(took).color(:green)}."
end
|