lib/codebuild/stack.rb in codebuild-0.5.0 vs lib/codebuild/stack.rb in codebuild-0.6.0
- old
+ new
@@ -43,10 +43,19 @@
IO.write(template_path, YAML.dump(@template))
puts "Generated CloudFormation template at #{template_path.color(:green)}"
return if @options[:noop]
puts "Deploying stack #{@stack_name.color(:green)} with CodeBuild project #{@full_project_name.color(:green)}"
+ @stack = find_stack(@stack_name)
+ if @stack && rollback_complete?(@stack)
+ puts "Existing stack in ROLLBACK_COMPLETE state. Deleting stack before continuing."
+ cfn.delete_stack(stack_name: @stack_name)
+ status.wait
+ status.reset
+ @stack = nil # at this point stack has been deleted
+ end
+
begin
perform
url_info
return unless @options[:wait]
status.wait
@@ -70,8 +79,24 @@
puts "Here's the CloudFormation url to check for more details #{url}"
end
def status
@status ||= Cfn::Status.new(@stack_name)
+ end
+
+ def rollback_complete?(stack)
+ stack.stack_status == 'ROLLBACK_COMPLETE'
+ end
+
+ def find_stack(stack_name)
+ resp = cfn.describe_stacks(stack_name: stack_name)
+ resp.stacks.first
+ rescue Aws::CloudFormation::Errors::ValidationError => e
+ # example: Stack with id demo-web does not exist
+ if e.message =~ /Stack with/ && e.message =~ /does not exist/
+ nil
+ else
+ raise
+ end
end
end
end