Class: Jets::Commands::Delete

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/jets/commands/delete.rb

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

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

Returns:

  • (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

Returns:

  • (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_statusObject

All CloudFormation states listed here: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html

Returns resp so we can use it to grab data about the stack without calling api again.



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)

  # Assumes stack exists
  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?
    # This Happens when the miminal stack fails at the very beginning.
    # There is no s3 bucket at all.  User should delete the stack.
    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_existsObject



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
    # Aws::CloudFormation::Errors::ValidationError is thrown when the stack
    # does not exist
    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 # 2 ** 6 is 64 so will give up after 6 retries
      puts "Giving up after #{retries} retries"
    else
      retry
    end
  end
end

#delete_logsObject



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_bucketObject



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 # Happens when minimal stack fails to build
  return unless bucket_exists?(s3_bucket_name)

  resp = s3.list_objects(bucket: s3_bucket_name)
  if resp.contents.size > 0
    # IE: objects = [{key: "objectkey1"}, {key: "objectkey2"}]
    objects = resp.contents.map { |item| {key: item.key} }
    s3.delete_objects(
      bucket: s3_bucket_name,
      delete: {
        objects: objects,
        quiet: false,
      }
    )
    empty_s3_bucket # keep deleting objects until bucket is empty
  end
end

#parent_stack_nameObject



112
113
114
# File 'lib/jets/commands/delete.rb', line 112

def parent_stack_name
  Jets::Naming.parent_stack_name
end

#runObject



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

  # Must first remove all objects from s3 bucket in order to delete stack
  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_nameObject



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_stackObject



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