fix create/delete when there exists broken stacks

This commit is contained in:
Anders Gustafsson
2023-04-12 15:44:51 +02:00
parent 87bbc14dad
commit feb8342bc1
5 changed files with 109 additions and 225 deletions
+73
View File
@@ -0,0 +1,73 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
export type Stack = {
name: string;
status: string;
drift_status: string;
branch: string;
updated: string;
created: string;
};
export const getTestStacksInfo = async (
toolbox: GluegunMenuToolbox
): Promise<Stack[]> => {
const { strings, system } = toolbox;
const stacks = JSON.parse(
strings.trim(await system.run(`aws cloudformation list-stacks`))
);
const allStacks = await Promise.all(
stacks.StackSummaries.map(async item => {
if (
item.StackName.indexOf('photowall-test-') > -1 &&
[
'CREATE_COMPLETE',
'UPDATE_COMPLETE',
'UPDATE_IN_PROGRESS',
'UPDATE_FAILED',
'CREATE_FAILED',
'CREATE_IN_PROGRESS',
'ROLLBACK_COMPLETE'
].includes(item.StackStatus)
) {
try {
const pipeline = JSON.parse(
strings.trim(
await system.run(
`aws codepipeline get-pipeline --name ${item.StackName}Pipeline`
)
)
);
const sourceStage = pipeline.pipeline.stages.filter(stage => {
return stage.name === 'Source';
});
const branch =
sourceStage[0].actions[0].configuration.Branch ??
sourceStage[0].actions[0].configuration.BranchName;
const lastUpdated = pipeline.metadata.updated;
return {
name: item.StackName,
status: item.StackStatus,
drift_status: item.DriftInformation.StackDriftStatus,
branch: branch,
updated: lastUpdated,
created: item.CreationTime
};
} catch (e) {
return {
name: item.StackName,
status: item.StackStatus,
drift_status: '',
branch: '',
updated: '',
created: item.CreationTime
};
}
}
return Promise.resolve(null);
})
) as Stack[] | null;
return allStacks.filter(Boolean);
};