From 6e93ccc7b5d1f0d4c30d2e4110df072d5786a6ae Mon Sep 17 00:00:00 2001 From: Ladislav Dokoupil Date: Wed, 26 Jun 2024 11:55:46 +0100 Subject: [PATCH] add option to keep failing stacks on AWS --- .../AwsCloudFormationCloudDeploymentManager.java | 14 ++++++++++++-- .../AzureArmTemplateCloudDeploymentManager.java | 7 +++++++ core/src/main/java/sunstone/core/CoreConfig.java | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java b/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java index bfaf1041..e068d1e2 100644 --- a/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java +++ b/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java @@ -11,6 +11,7 @@ import software.amazon.awssdk.services.cloudformation.model.OnFailure; import software.amazon.awssdk.services.cloudformation.model.Parameter; import software.amazon.awssdk.services.cloudformation.waiters.CloudFormationWaiter; +import sunstone.core.CoreConfig; import java.io.Closeable; import java.util.ArrayList; @@ -22,6 +23,8 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import static sunstone.core.SunstoneConfigResolver.getValue; + /** * Purpose: the class handles AWS CloudFormation template - deploy and undeploy the template to and from a stack. *

@@ -69,6 +72,15 @@ public void undeploy(String stack) { CloudFormationClient cfClient = stack2Client.get(stack); CloudFormationWaiter waiter = cfClient.waiter(); + stack2Client.remove(stack); + client2stacks.get(cfClient).remove(stack); + + boolean keepResources = getValue(CoreConfig.KEEP_FAIL_DEPLOY, false); + if (keepResources) { + LOGGER.debug("Stack {} is preserved", stack); + return; + } + DeleteStackRequest stackRequest = DeleteStackRequest.builder() .stackName(stack) .build(); @@ -80,8 +92,6 @@ public void undeploy(String stack) { WaiterResponse waiterResponse = waiter.waitUntilStackDeleteComplete(stacksRequest); LOGGER.debug("Stack {} is deleted {}", stack, waiterResponse.matched().response().orElse(null)); - stack2Client.remove(stack); - client2stacks.get(cfClient).remove(stack); } public void close() { diff --git a/azure/src/main/java/sunstone/azure/impl/AzureArmTemplateCloudDeploymentManager.java b/azure/src/main/java/sunstone/azure/impl/AzureArmTemplateCloudDeploymentManager.java index 60c5a23e..fdbed961 100644 --- a/azure/src/main/java/sunstone/azure/impl/AzureArmTemplateCloudDeploymentManager.java +++ b/azure/src/main/java/sunstone/azure/impl/AzureArmTemplateCloudDeploymentManager.java @@ -13,6 +13,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.slf4j.Logger; +import sunstone.core.CoreConfig; import sunstone.core.TimeoutUtils; import java.io.IOException; @@ -135,8 +136,14 @@ private String parametersFromMap(String template, Map parameters public void undeploy(String rgName) { ResourceGroups rgs = armManager.resourceGroups(); + boolean keepResources = getValue(CoreConfig.KEEP_FAIL_DEPLOY, false); + if (keepResources) { + LOGGER.debug("Azure resource group '{}' is preserved", rgName); + return; + } if (rgs.contain(rgName)) { rgs.deleteByName(rgName); + LOGGER.debug("Azure resource group '{}' is deleted", rgName); } usedRG.remove(rgName); } diff --git a/core/src/main/java/sunstone/core/CoreConfig.java b/core/src/main/java/sunstone/core/CoreConfig.java index 5e92ab61..d9b6c67b 100644 --- a/core/src/main/java/sunstone/core/CoreConfig.java +++ b/core/src/main/java/sunstone/core/CoreConfig.java @@ -4,4 +4,5 @@ public class CoreConfig { public static final String TIMEOUT_FACTOR = "sunstone.timeout.factor"; + public static final String KEEP_FAIL_DEPLOY = "sunstone.fail.keepResources"; }