|
| 1 | +import { |
| 2 | + CloudFormationClient, |
| 3 | + CreateStackRefactorCommand, |
| 4 | + CreateStackRefactorCommandInput, |
| 5 | + DescribeStackRefactorCommand, |
| 6 | + DescribeStackRefactorCommandOutput, |
| 7 | + ExecuteStackRefactorCommand, |
| 8 | + StackRefactorExecutionStatus, |
| 9 | + StackRefactorStatus, |
| 10 | +} from '@aws-sdk/client-cloudformation'; |
| 11 | +import assert from 'node:assert'; |
| 12 | +import { CFNStackStatus, FailedRefactorResponse } from './types'; |
| 13 | +import { pollStackForCompletionState } from './cfn-stack-updater'; |
| 14 | + |
| 15 | +const POLL_ATTEMPTS = 300; |
| 16 | +const POLL_INTERVAL_MS = 12000; |
| 17 | +const COMPLETION_STATE = '_COMPLETE'; |
| 18 | +const FAILED_STATE = '_FAILED'; |
| 19 | +export const UPDATE_COMPLETE = 'UPDATE_COMPLETE'; |
| 20 | +/** |
| 21 | + * Refactors a stack with given source and destination template. |
| 22 | + * @param cfnClient |
| 23 | + * @param createStackRefactorCommandInput |
| 24 | + * @param attempts number of attempts to poll CFN stack for update completion state. The interval between the polls is 1.5 seconds. |
| 25 | + * @returns a tuple containing the success/failed state and the reason if any. |
| 26 | + */ |
| 27 | +export async function tryRefactorStack( |
| 28 | + cfnClient: CloudFormationClient, |
| 29 | + createStackRefactorCommandInput: CreateStackRefactorCommandInput, |
| 30 | + attempts = POLL_ATTEMPTS, |
| 31 | +): Promise<[boolean, FailedRefactorResponse | undefined]> { |
| 32 | + const { StackRefactorId } = await cfnClient.send(new CreateStackRefactorCommand(createStackRefactorCommandInput)); |
| 33 | + assert(StackRefactorId); |
| 34 | + let describeStackRefactorResponse = await pollStackRefactorForCompletionState( |
| 35 | + cfnClient, |
| 36 | + StackRefactorId, |
| 37 | + (_describeStackRefactorResponse: DescribeStackRefactorCommandOutput) => { |
| 38 | + assert(_describeStackRefactorResponse.Status); |
| 39 | + return ( |
| 40 | + _describeStackRefactorResponse.Status.endsWith(COMPLETION_STATE) || _describeStackRefactorResponse.Status.endsWith(FAILED_STATE) |
| 41 | + ); |
| 42 | + }, |
| 43 | + attempts, |
| 44 | + ); |
| 45 | + if (describeStackRefactorResponse.Status !== StackRefactorStatus.CREATE_COMPLETE) { |
| 46 | + return [ |
| 47 | + false, |
| 48 | + { |
| 49 | + status: describeStackRefactorResponse.Status, |
| 50 | + reason: describeStackRefactorResponse.StatusReason, |
| 51 | + stackRefactorId: StackRefactorId, |
| 52 | + }, |
| 53 | + ]; |
| 54 | + } |
| 55 | + await cfnClient.send( |
| 56 | + new ExecuteStackRefactorCommand({ |
| 57 | + StackRefactorId, |
| 58 | + }), |
| 59 | + ); |
| 60 | + describeStackRefactorResponse = await pollStackRefactorForCompletionState( |
| 61 | + cfnClient, |
| 62 | + StackRefactorId, |
| 63 | + (describeStackRefactorResponse: DescribeStackRefactorCommandOutput) => { |
| 64 | + assert(describeStackRefactorResponse.ExecutionStatus); |
| 65 | + return ( |
| 66 | + describeStackRefactorResponse.ExecutionStatus.endsWith(COMPLETION_STATE) || |
| 67 | + describeStackRefactorResponse.ExecutionStatus.endsWith(FAILED_STATE) |
| 68 | + ); |
| 69 | + }, |
| 70 | + attempts, |
| 71 | + ); |
| 72 | + if (describeStackRefactorResponse.ExecutionStatus !== StackRefactorExecutionStatus.EXECUTE_COMPLETE) { |
| 73 | + return [ |
| 74 | + false, |
| 75 | + { |
| 76 | + status: describeStackRefactorResponse.ExecutionStatus, |
| 77 | + stackRefactorId: StackRefactorId, |
| 78 | + reason: describeStackRefactorResponse.ExecutionStatusReason, |
| 79 | + }, |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + const sourceStackName = createStackRefactorCommandInput.StackDefinitions?.[0].StackName; |
| 84 | + const destinationStackName = createStackRefactorCommandInput.StackDefinitions?.[1].StackName; |
| 85 | + assert(sourceStackName); |
| 86 | + assert(destinationStackName); |
| 87 | + const sourceStackStatus = await pollStackForCompletionState(cfnClient, sourceStackName); |
| 88 | + assert(sourceStackStatus === CFNStackStatus.UPDATE_COMPLETE, `${sourceStackName} was not updated successfully.`); |
| 89 | + const destinationStackStatus = await pollStackForCompletionState(cfnClient, destinationStackName); |
| 90 | + assert(destinationStackStatus === CFNStackStatus.UPDATE_COMPLETE, `${destinationStackName} was not updated successfully.`); |
| 91 | + |
| 92 | + return [true, undefined]; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Polls a stack refactor operation for completion state |
| 97 | + * @param cfnClient |
| 98 | + * @param stackRefactorId |
| 99 | + * @param exitCondition a function that determines if the stack refactor operation has reached a completion state. |
| 100 | + * @param attempts number of attempts to poll for completion. |
| 101 | + * @returns the stack status |
| 102 | + */ |
| 103 | +async function pollStackRefactorForCompletionState( |
| 104 | + cfnClient: CloudFormationClient, |
| 105 | + stackRefactorId: string, |
| 106 | + exitCondition: (describeStackRefactorResponse: DescribeStackRefactorCommandOutput) => boolean, |
| 107 | + attempts: number, |
| 108 | +): Promise<DescribeStackRefactorCommandOutput> { |
| 109 | + do { |
| 110 | + const describeStackRefactorResponse = await cfnClient.send( |
| 111 | + new DescribeStackRefactorCommand({ |
| 112 | + StackRefactorId: stackRefactorId, |
| 113 | + }), |
| 114 | + ); |
| 115 | + if (exitCondition(describeStackRefactorResponse)) { |
| 116 | + return describeStackRefactorResponse; |
| 117 | + } |
| 118 | + await new Promise((res) => setTimeout(() => res(''), POLL_INTERVAL_MS)); |
| 119 | + attempts--; |
| 120 | + } while (attempts > 0); |
| 121 | + throw new Error(`Stack refactor ${stackRefactorId} did not reach a completion state within the given time period.`); |
| 122 | +} |
0 commit comments