From d73e3ad2300e29de25aeaa380ed988c219844927 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 deployments on AWS & Azure --- .../AwsCloudFormationCloudDeploymentManager.java | 16 +++++++++++++--- .../AzureArmTemplateCloudDeploymentManager.java | 9 ++++++++- core/src/main/java/sunstone/core/CoreConfig.java | 1 + 3 files changed, 22 insertions(+), 4 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..5c625b2d 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. *

@@ -48,12 +51,19 @@ String deploy(CloudFormationClient cfClient, String template, Map cfParameters = new ArrayList<>(); parameters.forEach((k, v) -> cfParameters.add(Parameter.builder().parameterKey(k).parameterValue(v).build())); - CreateStackRequest stackRequest = CreateStackRequest.builder() + CreateStackRequest.Builder stackBuilder = CreateStackRequest.builder() .stackName(stackName) .templateBody(template)//templateURL(location) .parameters(cfParameters) - .onFailure(OnFailure.ROLLBACK) - .build(); + .onFailure(OnFailure.ROLLBACK); + + Boolean keepOnFailure = getValue(CoreConfig.KEEP_FAILED_DEPLOY, false); + if (keepOnFailure) { + stackBuilder.onFailure(OnFailure.DO_NOTHING); + LOGGER.debug("Stack will be {} preserved on failure due to {}", stackName, CoreConfig.KEEP_FAILED_DEPLOY); + } + + CreateStackRequest stackRequest = stackBuilder.build(); cfClient.createStack(stackRequest); DescribeStacksRequest stacksRequest = DescribeStacksRequest.builder() diff --git a/azure/src/main/java/sunstone/azure/impl/AzureArmTemplateCloudDeploymentManager.java b/azure/src/main/java/sunstone/azure/impl/AzureArmTemplateCloudDeploymentManager.java index 60c5a23e..f4b38019 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; @@ -92,7 +93,13 @@ void deploy(String template, Map parameters, String group, Strin if (pollStatus != LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) { LOGGER.error("Azure deployment from template {} in \"{}\" group failed", deploymentName, group); AzureUtils.downloadResourceGroupLogs(armManager, group); - undeploy(group); + boolean keepOnFailure = getValue(CoreConfig.KEEP_FAILED_DEPLOY, false); + if (keepOnFailure) { + LOGGER.debug("Resource group {} is preserved due to {}", group, CoreConfig.KEEP_FAILED_DEPLOY); + } else { + undeploy(group); + } + throw new RuntimeException("Deployment failed for group:" + group); } diff --git a/core/src/main/java/sunstone/core/CoreConfig.java b/core/src/main/java/sunstone/core/CoreConfig.java index 5e92ab61..c8838e65 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_FAILED_DEPLOY = "sunstone.fail.keepFailedDeploy"; }