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
@@ -1,82 +1,16 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
import chalk = require('chalk');
import { defaultMenuSettings } from '../../../../globals';
import { getTestStacksInfo } from '../../../../common/testservers';
const tto = require('terminal-table-output').create();
type Stack = {
name: string;
status: string;
drift_status: string;
branch: string;
updated: string;
created: string;
};
module.exports = {
name: 'status',
alias: ['s'],
description: 'Show status for available testservers (s)',
hidden: false,
run: async (toolbox: GluegunMenuToolbox) => {
const { system, strings } = toolbox;
const stacks = JSON.parse(
strings.trim(await system.run(`aws cloudformation list-stacks`))
);
const teststacks: Stack[] = 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);
})
);
const curratedStacks: Stack[] = teststacks.filter(Boolean);
const testStacks = await getTestStacksInfo(toolbox);
const status = (status: string) => {
switch (status) {
case 'UPDATE_FAILED':
@@ -88,7 +22,7 @@ module.exports = {
return `(${chalk.yellow('IN PROGRESS')})`;
case 'UPDATE_COMPLETE':
case 'CREATE_COMPLETE':
return `(${chalk.greenBright('OK')})`;
return `(${chalk.green('OK')})`;
default:
return `(${chalk.red.bold('?')})`;
}
@@ -142,18 +76,20 @@ module.exports = {
.pushrow(['| status', 'stack', 'branch', 'time since update', 'url'])
.line('-');
// Sort on stackname
curratedStacks.sort((a, b) => {
testStacks.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
for (const stack of curratedStacks) {
for (const stack of testStacks) {
const t = new Date(stack.updated);
tto.pushrow([
status(stack.status),
stack.name,
ellipsis(stack.branch, 24),
!isNaN(t.getTime()) ? timeSince(t.getTime()) : timeSince((new Date(stack.created)).getTime()),
!isNaN(t.getTime())
? timeSince(t.getTime())
: timeSince(new Date(stack.created).getTime()),
stackUrl(stack.name)
]);
}