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, #cfn, #lambda, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

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)


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/jets/commands/delete.rb', line 103

def are_you_sure?
  if @options[:sure]
    sure = 'y'
  else
    puts "Are you sure you want to want to delete the #{Jets.config.project_namespace.colorize(:green)} project? (y/N)"
    sure = $stdin.gets
  end

  unless sure =~ /^y/
    puts "Phew! Jets #{Jets.config.project_namespace.colorize(:green)} project was not deleted."
    exit 0
  end
end

#bucket_exists?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
# File 'lib/jets/commands/delete.rb', line 89

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jets/commands/delete.rb', line 120

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.".colorize(: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
# File 'lib/jets/commands/delete.rb', line 46

def confirm_project_exists
  begin
    resp = 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.colorize(:green)} for the project #{Jets.config.project_name.colorize(:green)} does not exist. So it cannot be deleted."
    exit 0
  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, sure: true)
  log.clean
end

#empty_s3_bucketObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jets/commands/delete.rb', line 57

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



99
100
101
# File 'lib/jets/commands/delete.rb', line 99

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.colorize(:green)}..."

  wait_for_stack if @options[:wait]

  delete_logs

  puts "Project #{Jets.config.project_namespace.colorize(:green)} deleted!"
end

#s3_bucket_nameObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jets/commands/delete.rb', line 76

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).green}."
end