mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 08:31:53 -05:00
fix(cloudformation): return stack IDs before provisioning completes (#7491)
This commit is contained in:
1 parent
cfbf86b8a1
commit
af0461af91
103 files changed
+917
-51
No files matched your search
+14
-21
@@ -24,7 +24,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Handles CloudFormation Query-protocol API calls (form-encoded POST, XML response).
|
||||
@@ -119,20 +118,23 @@ public class CloudFormationQueryHandler {
|
||||
List<String> capabilities = extractList(params, "Capabilities.member.");
|
||||
Map<String, String> tags = extractTags(params);
|
||||
|
||||
cfnService.createChangeSet(stackName, "initial-create", "CREATE",
|
||||
templateBody, templateUrl, parameters, capabilities, tags, region);
|
||||
awaitExecution(cfnService.executeChangeSet(stackName, "initial-create", region));
|
||||
|
||||
if (Boolean.parseBoolean(params.getFirst("EnableTerminationProtection"))) {
|
||||
// CreateStackInput carries the same member; honor it so the stack is created protected.
|
||||
cfnService.updateTerminationProtection(stackName, true, region);
|
||||
boolean terminationProtection = Boolean.parseBoolean(params.getFirst("EnableTerminationProtection"));
|
||||
ChangeSet initial = cfnService.createChangeSetForCreateStack(stackName, "initial-create",
|
||||
templateBody, templateUrl, parameters, capabilities, tags, region,
|
||||
terminationProtection);
|
||||
try {
|
||||
cfnService.executeChangeSet(initial.getStackId(), initial.getChangeSetId(), region);
|
||||
} catch (AwsException e) {
|
||||
if (terminationProtection) {
|
||||
cfnService.clearProtectionAfterRejectedCreate(stackName, initial, region);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
Stack stack = cfnService.describeStacks(stackName, region).get(0);
|
||||
String xml = new XmlBuilder()
|
||||
.start("CreateStackResponse", CF_NS)
|
||||
.start("CreateStackResult")
|
||||
.elem("StackId", stack.getStackId())
|
||||
.elem("StackId", initial.getStackId())
|
||||
.end("CreateStackResult")
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
.end("CreateStackResponse")
|
||||
@@ -165,13 +167,12 @@ public class CloudFormationQueryHandler {
|
||||
cfnService.deleteChangeSet(stackName, cs.getChangeSetName(), region);
|
||||
return xmlError("ValidationError", "No updates are to be performed.", 400);
|
||||
}
|
||||
awaitExecution(cfnService.executeChangeSet(stackName, cs.getChangeSetName(), region));
|
||||
cfnService.executeChangeSet(cs.getStackId(), cs.getChangeSetId(), region);
|
||||
|
||||
Stack stack = cfnService.describeStacks(stackName, region).get(0);
|
||||
String xml = new XmlBuilder()
|
||||
.start("UpdateStackResponse", CF_NS)
|
||||
.start("UpdateStackResult")
|
||||
.elem("StackId", stack.getStackId())
|
||||
.elem("StackId", cs.getStackId())
|
||||
.end("UpdateStackResult")
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
.end("UpdateStackResponse")
|
||||
@@ -1134,14 +1135,6 @@ public class CloudFormationQueryHandler {
|
||||
xml.end("Parameters");
|
||||
}
|
||||
|
||||
private void awaitExecution(Future<?> future) {
|
||||
try {
|
||||
future.get();
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Stack execution failed: {0}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private Response xmlError(String code, String message, int status) {
|
||||
String xml = new XmlBuilder()
|
||||
.start("ErrorResponse", CF_NS)
|
||||
|
||||
+40
-5
@@ -234,6 +234,25 @@ public class CloudFormationService implements ResourceProvider {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public void clearProtectionAfterRejectedCreate(String stackName, ChangeSet initial, String region) {
|
||||
String accountId = currentAccount();
|
||||
stacks.computeIfPresent(stackKey(accountId, stackName, region), (key, stack) -> {
|
||||
ChangeSet current = stack.getChangeSets().get(initial.getChangeSetName());
|
||||
if (stack.getStackId().equals(initial.getStackId())
|
||||
&& "REVIEW_IN_PROGRESS".equals(stack.getStatus())
|
||||
&& (current == null && stack.getChangeSets().isEmpty()
|
||||
|| current != null && current.getChangeSetId().equals(initial.getChangeSetId())
|
||||
&& ("AVAILABLE".equals(current.getExecutionStatus())
|
||||
|| "UNAVAILABLE".equals(current.getExecutionStatus())))
|
||||
&& !runningExecutions.containsKey(stack.getStackId())) {
|
||||
stack.setEnableTerminationProtection(false);
|
||||
// Keep this rare rejection cleanup ordered before a concurrent delete unpersists it.
|
||||
persistStack(stack);
|
||||
}
|
||||
return stack;
|
||||
});
|
||||
}
|
||||
|
||||
// ── DescribeStacks ────────────────────────────────────────────────────────
|
||||
|
||||
public List<Stack> describeStacks(String stackName, String region) {
|
||||
@@ -347,7 +366,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
String previousTemplate = previousTemplateOf(stack);
|
||||
created = createChangeSet(stackName, changeSetName, "UPDATE", previousTemplate, null,
|
||||
parameters, capabilities, tags, region, currentAccount(), false, true);
|
||||
parameters, capabilities, tags, region, currentAccount(), false, true, false);
|
||||
}
|
||||
return created;
|
||||
}
|
||||
@@ -365,7 +384,17 @@ public class CloudFormationService implements ResourceProvider {
|
||||
Map<String, String> parameters, List<String> capabilities,
|
||||
Map<String, String> tags, String region) {
|
||||
return createChangeSet(stackName, changeSetName, changeSetType, templateBody, templateUrl,
|
||||
parameters, capabilities, tags, region, regionResolver.getAccountId(), false, false);
|
||||
parameters, capabilities, tags, region, regionResolver.getAccountId(), false, false, false);
|
||||
}
|
||||
|
||||
public ChangeSet createChangeSetForCreateStack(String stackName, String changeSetName,
|
||||
String templateBody, String templateUrl,
|
||||
Map<String, String> parameters, List<String> capabilities,
|
||||
Map<String, String> tags, String region,
|
||||
boolean enableTerminationProtection) {
|
||||
return createChangeSet(stackName, changeSetName, "CREATE", templateBody, templateUrl,
|
||||
parameters, capabilities, tags, region, regionResolver.getAccountId(), false, false,
|
||||
enableTerminationProtection);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -389,7 +418,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
Map<String, String> parameters, List<String> capabilities,
|
||||
Map<String, String> tags, String region) {
|
||||
return createChangeSet(stackName, changeSetName, changeSetType, templateBody, templateUrl,
|
||||
parameters, capabilities, tags, region, regionResolver.getAccountId(), true, false);
|
||||
parameters, capabilities, tags, region, regionResolver.getAccountId(), true, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -407,14 +436,15 @@ public class CloudFormationService implements ResourceProvider {
|
||||
Map<String, String> parameters, List<String> capabilities,
|
||||
Map<String, String> tags, String region, String accountId) {
|
||||
return createChangeSet(stackName, changeSetName, changeSetType, templateBody, templateUrl,
|
||||
parameters, capabilities, tags, region, accountId, false, false);
|
||||
parameters, capabilities, tags, region, accountId, false, false, false);
|
||||
}
|
||||
|
||||
private ChangeSet createChangeSet(String stackName, String changeSetName, String changeSetType,
|
||||
String templateBody, String templateUrl,
|
||||
Map<String, String> parameters, List<String> capabilities,
|
||||
Map<String, String> tags, String region, String accountId,
|
||||
boolean attachToReviewInProgressStack, boolean fromPreviousTemplate) {
|
||||
boolean attachToReviewInProgressStack, boolean fromPreviousTemplate,
|
||||
boolean enableTerminationProtection) {
|
||||
String resolvedTemplate = resolveTemplate(templateBody, templateUrl);
|
||||
requireValidTemplate(resolvedTemplate);
|
||||
boolean isCreateType = "CREATE".equalsIgnoreCase(changeSetType);
|
||||
@@ -511,6 +541,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
target = newStack(stackName,
|
||||
createdStackId != null ? createdStackId : newStackId(stackName, region, accountId),
|
||||
region, accountId);
|
||||
target.setEnableTerminationProtection(enableTerminationProtection);
|
||||
if (tags != null) target.getTags().putAll(tags);
|
||||
// A CREATE change set puts a brand-new stack into REVIEW_IN_PROGRESS. Record the
|
||||
// matching stack-level event (as AWS and LocalStack do) so DescribeStackEvents is
|
||||
@@ -1289,6 +1320,9 @@ public class CloudFormationService implements ResourceProvider {
|
||||
cs.setExecutionStatus(status != null && (status.contains("ROLLBACK") || status.endsWith("_FAILED"))
|
||||
? "EXECUTE_FAILED" : "EXECUTE_COMPLETE");
|
||||
persistStack(stack);
|
||||
} catch (RuntimeException | Error e) {
|
||||
LOG.errorv(e, "Stack {0} execution aborted unexpectedly", stack.getStackName());
|
||||
throw e;
|
||||
} finally {
|
||||
endExecution(stack, claimed.done(), region, accountId);
|
||||
}
|
||||
@@ -1322,6 +1356,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
done.complete(null);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.errorv(e, "Stack {0} execution cleanup failed", stack.getStackName());
|
||||
done.completeExceptionally(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user