Skip to content

Commit

Permalink
delete logs and add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizencc committed Oct 1, 2024
1 parent 6f2381d commit 890daaf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ integTest(
// assert that the bootstrap bucket is empty
await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName }))
.then(async (result) => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(result.Contents));
expect(result.Contents).toHaveLength(2);
const key = result.Contents![0].Key;
const tags = await fixture.aws.s3.send(new GetObjectTaggingCommand({ Bucket: bootstrapBucketName, Key: key }));
Expand Down
10 changes: 5 additions & 5 deletions packages/aws-cdk/lib/api/garbage-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ interface GarbageCollectorProps {
readonly resolvedEnvironment: cxapi.Environment;

/**
* SDK provider (seeded with default credentials)
*
* Will be used to make SDK calls to CloudFormation, S3, and ECR.
*/
* SDK provider (seeded with default credentials)
*
* Will be used to make SDK calls to CloudFormation, S3, and ECR.
*/
readonly sdkProvider: SdkProvider;

/**
Expand Down Expand Up @@ -201,7 +201,7 @@ export class GarbageCollector {
print(chalk.white(`${taggables.length} taggable assets`));

if (!this.props.dryRun) {
if (deletables.length > 0) {
if (!this.props.tagOnly && deletables.length > 0) {
await this.parallelDelete(s3, bucket, deletables);
}
if (taggables.length > 0) {
Expand Down
95 changes: 54 additions & 41 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,29 +725,7 @@ export class CdkToolkit {
// If there is an '--app' argument and an environment looks like a glob, we
// select the environments from the app. Otherwise, use what the user said.

// By default, glob for everything
const environmentSpecs = userEnvironmentSpecs.length > 0 ? [...userEnvironmentSpecs] : ['**'];

// Partition into globs and non-globs (this will mutate environmentSpecs).
const globSpecs = partition(environmentSpecs, looksLikeGlob);
if (globSpecs.length > 0 && !this.props.cloudExecutable.hasApp) {
if (userEnvironmentSpecs.length > 0) {
// User did request this glob
throw new Error(`'${globSpecs}' is not an environment name. Specify an environment name like 'aws://123456789012/us-east-1', or run in a directory with 'cdk.json' to use wildcards.`);
} else {
// User did not request anything
throw new Error('Specify an environment name like \'aws://123456789012/us-east-1\', or run in a directory with \'cdk.json\'.');
}
}

const environments: cxapi.Environment[] = [
...environmentsFromDescriptors(environmentSpecs),
];

// If there is an '--app' argument, select the environments from the app.
if (this.props.cloudExecutable.hasApp) {
environments.push(...await globEnvironmentsFromStacks(await this.selectStacksForList([]), globSpecs, this.props.sdkProvider));
}
const environments = await this.defineEnvironments(userEnvironmentSpecs);

await Promise.all(environments.map(async (environment) => {
success(' ⏳ Bootstrapping environment %s...', chalk.blue(environment.name));
Expand All @@ -769,15 +747,27 @@ export class CdkToolkit {
* @param options Options for Garbage Collection
*/
public async garbageCollect(userEnvironmentSpecs: string[], options: GarbageCollectionOptions) {
// eslint-disable-next-line no-console
console.log(userEnvironmentSpecs, options);
const environments = await this.defineEnvironments(userEnvironmentSpecs);

await Promise.all(environments.map(async (environment) => {
success(' ⏳ Garbage Collecting environment %s...', chalk.blue(environment.name));
const gc = new GarbageCollector({
sdkProvider: this.props.sdkProvider,
resolvedEnvironment: environment,
bootstrapStackName: options.bootstrapStackName,
isolationDays: options.days,
dryRun: options.dryRun ?? false,
tagOnly: options.tagOnly ?? false,
type: options.type ?? 'all',
});
await gc.garbageCollect();
}));
}

// TODO: copied from bootstrap, make into function
private async defineEnvironments(userEnvironmentSpecs: string[]): Promise<cxapi.Environment[]> {
// By default, glob for everything
const environmentSpecs = userEnvironmentSpecs.length > 0 ? [...userEnvironmentSpecs] : ['**'];

// eslint-disable-next-line no-console
console.log(environmentSpecs);
// Partition into globs and non-globs (this will mutate environmentSpecs).
const globSpecs = partition(environmentSpecs, looksLikeGlob);
if (globSpecs.length > 0 && !this.props.cloudExecutable.hasApp) {
Expand All @@ -794,23 +784,12 @@ export class CdkToolkit {
...environmentsFromDescriptors(environmentSpecs),
];

// If there is an '--app' argument, select the environments from the app.
if (this.props.cloudExecutable.hasApp) {
environments.push(...await globEnvironmentsFromStacks(await this.selectStacksForList([]), globSpecs, this.props.sdkProvider));
}

await Promise.all(environments.map(async (environment) => {
success(' ⏳ Garbage Collecting environment %s...', chalk.blue(environment.name));
const gc = new GarbageCollector({
sdkProvider: this.props.sdkProvider,
resolvedEnvironment: environment,
bootstrapStackName: options.bootstrapStackName,
isolationDays: options.days,
dryRun: options.dryRun ?? false,
tagOnly: options.tagOnly ?? false,
type: options.type ?? 'all',
});
await gc.garbageCollect();
}));
return environments;
}

/**
Expand Down Expand Up @@ -1457,11 +1436,45 @@ export interface DestroyOptions {
readonly ci?: boolean;
}

/**
* Options for the garbage collection
*/
export interface GarbageCollectionOptions {
/**
* Whether to perform a dry run or not.
* A dry run means that isolated objects are printed to stdout
* but not tagged or deleted
*
* @default false
*/
readonly dryRun?: boolean;

/**
* Whether to only perform tagging. If this is set, no deletion happens
*
* @default false
*/
readonly tagOnly?: boolean;

/**
* The type of the assets to be garbage collected.
*
* @default 'all'
*/
readonly type: 'ecr' | 's3' | 'all';

/**
* Elapsed time between an asset being marked as isolated and actually deleted.
*
* @default 0
*/
readonly days: number;

/**
* The stack name of the bootstrap stack.
*
* @default DEFAULT_TOOLKIT_STACK_NAME
*/
readonly bootstrapStackName?: string;
}

Expand Down

0 comments on commit 890daaf

Please sign in to comment.