Allow to delete retained resources

The EFS and the Network stack (when provided) are always protected from
deletion. This allows to perform blue/green deployments, since those
stacks can be pointed to by blue and green environments respectively,
without being destroyed when switching to the new environment and
deleting the old one.

However, there are cases where those resources are not wanted anymore
and the cluster is actually intended to be destroyed completely. This is
the case for example of an automated testing environment or a
development one.

Allow the retained stack to be deleted explicitly by running:

make delete-all-including-retained-stack

Feature: Issue 13865
Change-Id: Ibc965b60a99bba2b661c3d2f6a64b839b9d717d9
diff --git a/README.md b/README.md
index 69bf466..4c91754 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,9 @@
 To build gerrit and related-components' images
 [Docker](https://www.docker.com/)
 
+To manipulate aws cloudformation outputs
+[jq](https://stedolan.github.io/jq/)
+
 ## Templates
 
 * [Standalone Gerrit master sandbox with LDAP authentication](/single-master/README.md)
diff --git a/dual-master/Makefile b/dual-master/Makefile
index 2b6e9bf..1da24a9 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -484,6 +484,47 @@
 						$(optional_replication_targets_deletion) \
 						delete-cluster wait-for-cluster-deletion
 
+confirm-persistent-stack-deletion:
+	@echo ""
+	@echo "* * * * WARNING * * * * this is going to completely destroy the stack, including git data."
+	@echo ""
+	@echo -n "Are you sure you want to continue? [y/N] " && read ans && [ $${ans:-N} = y ]
+
+delete-all-including-retained-stack: confirm-persistent-stack-deletion delete-all delete-git-persistent-stack delete-network-persistent-stack
+
+delete-git-persistent-stack:
+
+	$(eval EFS_STACK_NAME := $(shell $(AWS_FC_COMMAND) list-stacks --stack-status-filter CREATE_COMPLETE --query "StackSummaries[*].StackName" | jq -r '.[]| select(startswith("$(CLUSTER_STACK_NAME)-GitFileSystemPermanentStack"))'))
+
+	$(if $(EFS_STACK_NAME), \
+		$(AWS_FC_COMMAND) delete-stack \
+			--stack-name $(EFS_STACK_NAME) \
+			--region $(AWS_REGION) && \
+		echo "*** Wait for Git persistent stack '$(EFS_STACK_NAME)' deletion" && \
+		$(AWS_FC_COMMAND) wait stack-delete-complete \
+			--stack-name $(EFS_STACK_NAME) \
+			--region $(AWS_REGION) && \
+		echo "*** Git persistent stack '$(EFS_STACK_NAME)' deleted" \
+		, \
+		echo "No Git persistent stack found. Nothing to do." \
+	)
+
+delete-network-persistent-stack:
+	$(eval NETWORK_STACK_NAME=$(shell $(AWS_FC_COMMAND) list-stacks --stack-status-filter CREATE_COMPLETE --query "StackSummaries[*].StackName" | jq -r '.[]| select(startswith("$(CLUSTER_STACK_NAME)-ECSTaskNetworkStack"))'))
+
+	$(if $(NETWORK_STACK_NAME), \
+		$(AWS_FC_COMMAND) delete-stack \
+			--stack-name $(NETWORK_STACK_NAME) \
+			--region $(AWS_REGION) && \
+		echo "*** Wait for Network stack '$(NETWORK_STACK_NAME)' deletion" && \
+		$(AWS_FC_COMMAND) wait stack-delete-complete \
+			--stack-name $(NETWORK_STACK_NAME) \
+			--region $(AWS_REGION) && \
+		echo "*** Network stack '$(NETWORK_STACK_NAME)' deleted" \
+		, \
+		echo "No network stack found. Nothing to do." \
+	)
+
 gerrit-publish:
 ifeq ($(MULTISITE_ENABLED),true)
 	$(MAKE) -C ../gerrit gerrit-publish RECIPE=dual-master PLUGINS="$(MULTI_SITE_PLUGINS)" PLUGINS_LIBS_LINKS="$(MULTI_SITE_PLUGINS_LIBS_LINKS)" MAVEN_LIBS="$(MULTI_SITE_MAVEN_LIBS)"
diff --git a/dual-master/README.md b/dual-master/README.md
index 17d4be2..5d59b1b 100644
--- a/dual-master/README.md
+++ b/dual-master/README.md
@@ -329,6 +329,31 @@
 * SSL certificates
 * ECR repositories
 * EFS stack
+* VPC and subnets (if created as part of this deployment, rather than externally
+provided)
+
+Note that you can completely delete the stack, including explicitly retained
+resources such as the EFS Git filesystem, VPC and subnets, by issuing the more
+aggressive command:
+
+```
+make [AWS_REGION=a-valid-aws-region] [AWS_PREFIX=some-cluster-prefix] delete-all-including-retained-stack
+```
+
+Note that this will execute a prompt to confirm your choice:
+
+```
+* * * * WARNING * * * * this is going to completely destroy the stack, including git data.
+
+Are you sure you want to continue? [y/N]
+```
+
+If you want to automate this programmatically you can just pipe the `yes`
+command to the make:
+
+```
+yes | make [AWS_REGION=a-valid-aws-region] [AWS_PREFIX=some-cluster-prefix] delete-all-including-retained-stack
+```
 
 ### Access your Gerrit instances