Skip to content

Commit

Permalink
Support for legacy stacks created with aws-simple v10 or lower is dis…
Browse files Browse the repository at this point in the history
…continued
  • Loading branch information
clebert committed Dec 14, 2022
1 parent 5785e4a commit 27447bf
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 84 deletions.
12 changes: 2 additions & 10 deletions src/list-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const listCommand: CommandModule<
{},
{
readonly 'hosted-zone-name': string | undefined;
readonly 'legacy-app-name': string | undefined;
readonly 'all': boolean;
readonly 'short': boolean;
readonly 'json': boolean;
Expand All @@ -25,10 +24,6 @@ export const listCommand: CommandModule<
describe: `An optional hosted zone name, if not specified it will be determined from the config file`,
string: true,
})
.options(`legacy-app-name`, {
describe: `An optional app name to identify legacy stacks`,
string: true,
})
.options(`all`, {
describe: `List all deployed stacks without filtering by hosted zone name`,
boolean: true,
Expand All @@ -47,14 +42,13 @@ export const listCommand: CommandModule<
.example([
[`npx $0 ${commandName}`],
[`npx $0 ${commandName} --hosted-zone-name example.com`],
[`npx $0 ${commandName} --legacy-app-name example`],
[`npx $0 ${commandName} --all`],
[`npx $0 ${commandName} --short`],
[`npx $0 ${commandName} --json`],
]),

handler: async (args): Promise<void> => {
const {legacyAppName, all, short, json} = args;
const {all, short, json} = args;

const hostedZoneName = all
? undefined
Expand All @@ -74,9 +68,7 @@ export const listCommand: CommandModule<
print.info(`Searching all deployed stacks...`);
}

const stacks = hostedZoneName
? await findStacks({hostedZoneName, legacyAppName})
: await findStacks();
const stacks = await findStacks(hostedZoneName);

if (stacks.length === 0) {
if (!short && !json) {
Expand Down
12 changes: 2 additions & 10 deletions src/purge-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const purgeCommand: CommandModule<
{},
{
readonly 'hosted-zone-name': string | undefined;
readonly 'legacy-app-name': string | undefined;
readonly 'min-age': number;
readonly 'excluded-tags': readonly (string | number)[];
readonly 'yes': boolean;
Expand All @@ -28,10 +27,6 @@ export const purgeCommand: CommandModule<
describe: `An optional hosted zone name, if not specified it will be determined from the config file`,
string: true,
})
.options(`legacy-app-name`, {
describe: `An optional app name to identify legacy stacks`,
string: true,
})
.options(`min-age`, {
describe: `The minimum age (in days) at which a deployed stack is considered expired`,
number: true,
Expand All @@ -50,7 +45,6 @@ export const purgeCommand: CommandModule<
.example([
[`npx $0 ${commandName}`],
[`npx $0 ${commandName} --hosted-zone-name example.com`],
[`npx $0 ${commandName} --legacy-app-name example`],
[`npx $0 ${commandName} --min-age 14`],
[`npx $0 ${commandName} --excluded-tags foo=true bar`],
[`npx $0 ${commandName} --yes`],
Expand All @@ -64,14 +58,12 @@ export const purgeCommand: CommandModule<
throw new Error(`Please specify a hosted zone name.`);
}

const {legacyAppName, minAge: minAgeInDays, excludedTags} = args;
const {minAge: minAgeInDays, excludedTags} = args;

print.warning(`Hosted zone: ${hostedZoneName}`);
print.info(`Searching all expired stacks...`);

const expiredStacks = (
await findStacks({hostedZoneName, legacyAppName})
).filter((stack) =>
const expiredStacks = (await findStacks(hostedZoneName)).filter((stack) =>
isExpired(stack, minAgeInDays, excludedTags.map(String)),
);

Expand Down
31 changes: 0 additions & 31 deletions src/sdk/delete-bucket.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/sdk/delete-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@ import {
DeleteStackCommand,
waitUntilStackDeleteComplete,
} from '@aws-sdk/client-cloudformation';
import {deleteBucket} from './delete-bucket.js';
import {findStack} from './find-stack.js';
import {getOutputValue} from './get-output-value.js';

export async function deleteStack(stackName: string): Promise<void> {
const client = new CloudFormationClient({});

if (stackName.startsWith(`aws-simple--`)) {
const stack = await findStack(stackName);
const bucketName = getOutputValue(stack, `BucketName`);

if (bucketName) {
await deleteBucket(bucketName);
}
}

await client.send(new DeleteStackCommand({StackName: stackName}));

await waitUntilStackDeleteComplete(
Expand Down
5 changes: 0 additions & 5 deletions src/sdk/find-stack-resource-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import {
ListStackResourcesCommand,
} from '@aws-sdk/client-cloudformation';

export interface FindStacksOptions {
readonly hostedZoneName: string;
readonly legacyAppName?: string;
}

export async function findStackResourceIds(
stackName: string,
): Promise<readonly string[]> {
Expand Down
21 changes: 5 additions & 16 deletions src/sdk/find-stacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import {
import {findStack} from './find-stack.js';
import {getOutputValue} from './get-output-value.js';

export interface FindStacksOptions {
readonly hostedZoneName: string;
readonly legacyAppName?: string;
}

export async function findStacks(
options?: FindStacksOptions,
hostedZoneName?: string,
): Promise<readonly Stack[]> {
const client = new CloudFormationClient({});
const stacks: Stack[] = [];
Expand All @@ -33,7 +28,7 @@ export async function findStacks(

return Promise.all(
stacks
.filter((stack) => isMatchingStack(stack, options))
.filter((stack) => isMatchingStack(stack, hostedZoneName))
// For some reason `stack.EnableTerminationProtection` is always
// `undefined`. Describing a single stack instead, does return the correct
// value (true or false) for `EnableTerminationProtection`.
Expand All @@ -43,21 +38,15 @@ export async function findStacks(

function isMatchingStack(
stack: Stack,
options: FindStacksOptions | undefined,
hostedZoneName: string | undefined,
): boolean {
if (!stack.StackName?.startsWith(`aws-simple`)) {
return false;
}

if (!options) {
if (!hostedZoneName) {
return true;
}

const {hostedZoneName, legacyAppName} = options;

return Boolean(
getOutputValue(stack, `HostedZoneName`) === hostedZoneName ||
(legacyAppName &&
stack.StackName.startsWith(`aws-simple--${legacyAppName}--`)),
);
return getOutputValue(stack, `HostedZoneName`) === hostedZoneName;
}

0 comments on commit 27447bf

Please sign in to comment.