Class: Jets::Command::Delete

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

Instance Method Summary collapse

Methods included from AwsHelpers

#find_stack, #first_run?

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #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.



16
17
18
# File 'lib/jets/commands/delete/delete_command.rb', line 16

def initialize(options={})
  @options = options
end

Instance Method Details

#are_you_sure?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jets/commands/delete/delete_command.rb', line 113

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

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

#bucket_exists?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/jets/commands/delete/delete_command.rb', line 99

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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jets/commands/delete/delete_command.rb', line 130

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



60
61
62
63
64
65
# File 'lib/jets/commands/delete/delete_command.rb', line 60

def confirm_project_exists
  stack = find_stack(parent_stack_name)
  return if stack
  puts "ERROR: Stack #{parent_stack_name} does not exist".color(:red)
  exit 1
end

#delete_logsObject



54
55
56
57
58
# File 'lib/jets/commands/delete/delete_command.rb', line 54

def delete_logs
  puts "Deleting CloudWatch logs"
  log = Jets::Commands::Clean::Log.new(mute: true, yes: true)
  log.clean
end

#empty_s3_bucketObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jets/commands/delete/delete_command.rb', line 67

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



109
110
111
# File 'lib/jets/commands/delete/delete_command.rb', line 109

def parent_stack_name
  Jets::Names.parent_stack_name
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jets/commands/delete/delete_command.rb', line 20

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

  stack = find_stack(parent_stack_name)
  if @options[:wait]
    wait_for_stack
  end
  Jets::Cfn::Deployment.new(stack_name: stack.stack_id).delete

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

#s3_bucket_nameObject



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

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



46
47
48
49
50
51
52
# File 'lib/jets/commands/delete/delete_command.rb', line 46

def wait_for_stack
  status = Jets::Cfn::Status.new
  start_time = Time.now
  status.wait
  took = Time.now - start_time
  puts "Time took for deletion: #{status.pretty_time(took).color(:green)}."
end