@@ -6,7 +6,7 @@ import * as fs from 'fs-extra';
6
6
import { NonInteractiveIoHost } from './non-interactive-io-host' ;
7
7
import type { ToolkitServices } from './private' ;
8
8
import { assemblyFromSource } from './private' ;
9
- import type { DeployResult } from './types' ;
9
+ import type { DeployResult , DestroyResult , RollbackResult } from './types' ;
10
10
import type { BootstrapEnvironments , BootstrapOptions , BootstrapResult , EnvironmentBootstrapResult } from '../actions/bootstrap' ;
11
11
import { BootstrapSource } from '../actions/bootstrap' ;
12
12
import { AssetBuildTime , type DeployOptions } from '../actions/deploy' ;
@@ -796,7 +796,7 @@ export class Toolkit extends CloudAssemblySourceBuilder {
796
796
*
797
797
* Rolls back the selected stacks.
798
798
*/
799
- public async rollback ( cx : ICloudAssemblySource , options : RollbackOptions ) : Promise < void > {
799
+ public async rollback ( cx : ICloudAssemblySource , options : RollbackOptions ) : Promise < RollbackResult > {
800
800
const ioHelper = asIoHelper ( this . ioHost , 'rollback' ) ;
801
801
const assembly = await assemblyFromSource ( ioHelper , cx ) ;
802
802
return this . _rollback ( assembly , 'rollback' , options ) ;
@@ -805,16 +805,20 @@ export class Toolkit extends CloudAssemblySourceBuilder {
805
805
/**
806
806
* Helper to allow rollback being called as part of the deploy or watch action.
807
807
*/
808
- private async _rollback ( assembly : StackAssembly , action : 'rollback' | 'deploy' | 'watch' , options : RollbackOptions ) : Promise < void > {
808
+ private async _rollback ( assembly : StackAssembly , action : 'rollback' | 'deploy' | 'watch' , options : RollbackOptions ) : Promise < RollbackResult > {
809
809
const ioHelper = asIoHelper ( this . ioHost , action ) ;
810
810
const synthSpan = await ioHelper . span ( SPAN . SYNTH_ASSEMBLY ) . begin ( { stacks : options . stacks } ) ;
811
811
const stacks = await assembly . selectStacksV2 ( options . stacks ) ;
812
812
await this . validateStacksMetadata ( stacks , ioHelper ) ;
813
813
await synthSpan . end ( ) ;
814
814
815
+ const ret : RollbackResult = {
816
+ stacks : [ ] ,
817
+ } ;
818
+
815
819
if ( stacks . stackCount === 0 ) {
816
820
await ioHelper . notify ( IO . CDK_TOOLKIT_E6001 . msg ( 'No stacks selected' ) ) ;
817
- return ;
821
+ return ret ;
818
822
}
819
823
820
824
let anyRollbackable = false ;
@@ -839,6 +843,16 @@ export class Toolkit extends CloudAssemblySourceBuilder {
839
843
anyRollbackable = true ;
840
844
}
841
845
await rollbackSpan . end ( ) ;
846
+
847
+ ret . stacks . push ( {
848
+ environment : {
849
+ account : stack . environment . account ,
850
+ region : stack . environment . region ,
851
+ } ,
852
+ stackName : stack . stackName ,
853
+ stackArn : stackResult . stackArn ,
854
+ result : stackResult . notInRollbackableState ? 'already-stable' : 'rolled-back' ,
855
+ } ) ;
842
856
} catch ( e : any ) {
843
857
await ioHelper . notify ( IO . CDK_TOOLKIT_E6900 . msg ( `\n ❌ ${ chalk . bold ( stack . displayName ) } failed: ${ formatErrorMessage ( e ) } ` , { error : e } ) ) ;
844
858
throw new ToolkitError ( 'Rollback failed (use --force to orphan failing resources)' ) ;
@@ -847,14 +861,16 @@ export class Toolkit extends CloudAssemblySourceBuilder {
847
861
if ( ! anyRollbackable ) {
848
862
throw new ToolkitError ( 'No stacks were in a state that could be rolled back' ) ;
849
863
}
864
+
865
+ return ret ;
850
866
}
851
867
852
868
/**
853
869
* Destroy Action
854
870
*
855
871
* Destroys the selected Stacks.
856
872
*/
857
- public async destroy ( cx : ICloudAssemblySource , options : DestroyOptions ) : Promise < void > {
873
+ public async destroy ( cx : ICloudAssemblySource , options : DestroyOptions ) : Promise < DestroyResult > {
858
874
const ioHelper = asIoHelper ( this . ioHost , 'destroy' ) ;
859
875
const assembly = await assemblyFromSource ( ioHelper , cx ) ;
860
876
return this . _destroy ( assembly , 'destroy' , options ) ;
@@ -863,18 +879,23 @@ export class Toolkit extends CloudAssemblySourceBuilder {
863
879
/**
864
880
* Helper to allow destroy being called as part of the deploy action.
865
881
*/
866
- private async _destroy ( assembly : StackAssembly , action : 'deploy' | 'destroy' , options : DestroyOptions ) : Promise < void > {
882
+ private async _destroy ( assembly : StackAssembly , action : 'deploy' | 'destroy' , options : DestroyOptions ) : Promise < DestroyResult > {
867
883
const ioHelper = asIoHelper ( this . ioHost , action ) ;
868
884
const synthSpan = await ioHelper . span ( SPAN . SYNTH_ASSEMBLY ) . begin ( { stacks : options . stacks } ) ;
869
885
// The stacks will have been ordered for deployment, so reverse them for deletion.
870
886
const stacks = ( await assembly . selectStacksV2 ( options . stacks ) ) . reversed ( ) ;
871
887
await synthSpan . end ( ) ;
872
888
889
+ const ret : DestroyResult = {
890
+ stacks : [ ] ,
891
+ } ;
892
+
873
893
const motivation = 'Destroying stacks is an irreversible action' ;
874
894
const question = `Are you sure you want to delete: ${ chalk . red ( stacks . hierarchicalIds . join ( ', ' ) ) } ` ;
875
895
const confirmed = await ioHelper . requestResponse ( IO . CDK_TOOLKIT_I7010 . req ( question , { motivation } ) ) ;
876
896
if ( ! confirmed ) {
877
- return ioHelper . notify ( IO . CDK_TOOLKIT_E7010 . msg ( 'Aborted by user' ) ) ;
897
+ await ioHelper . notify ( IO . CDK_TOOLKIT_E7010 . msg ( 'Aborted by user' ) ) ;
898
+ return ret ;
878
899
}
879
900
880
901
const destroySpan = await ioHelper . span ( SPAN . DESTROY_ACTION ) . begin ( {
@@ -890,18 +911,31 @@ export class Toolkit extends CloudAssemblySourceBuilder {
890
911
stack,
891
912
} ) ;
892
913
const deployments = await this . deploymentsForAction ( action ) ;
893
- await deployments . destroyStack ( {
914
+ const result = await deployments . destroyStack ( {
894
915
stack,
895
916
deployName : stack . stackName ,
896
917
roleArn : options . roleArn ,
897
918
} ) ;
919
+
920
+ ret . stacks . push ( {
921
+ environment : {
922
+ account : stack . environment . account ,
923
+ region : stack . environment . region ,
924
+ } ,
925
+ stackName : stack . stackName ,
926
+ stackArn : result . stackArn ,
927
+ stackExisted : result . stackArn !== undefined ,
928
+ } ) ;
929
+
898
930
await ioHelper . notify ( IO . CDK_TOOLKIT_I7900 . msg ( chalk . green ( `\n ✅ ${ chalk . blue ( stack . displayName ) } : ${ action } ed` ) , stack ) ) ;
899
931
await singleDestroySpan . end ( ) ;
900
932
} catch ( e : any ) {
901
933
await ioHelper . notify ( IO . CDK_TOOLKIT_E7900 . msg ( `\n ❌ ${ chalk . blue ( stack . displayName ) } : ${ action } failed ${ e } ` , { error : e } ) ) ;
902
934
throw e ;
903
935
}
904
936
}
937
+
938
+ return ret ;
905
939
} finally {
906
940
await destroySpan . end ( ) ;
907
941
}
0 commit comments