From b12bc676bd5f99843b224839bc32504b47f0599e Mon Sep 17 00:00:00 2001 From: Momo Kornher Date: Thu, 3 Aug 2023 15:57:43 +0200 Subject: [PATCH] fix(cli): specifying --quiet does not suppress asset building and publishing logs (#26493) > We're currently maintaining a patched version of CDK to use with [SST](https://docs.sst.dev/) because this flag isn't fully respected. When the quiet flag is specified, the docker build and docker push shell commands still print to the terminal. This PR propagates the quiet flag down to the cdk-assets's container-images handler. Replaces #26265 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk/lib/util/asset-publishing.ts | 1 + .../cdk-assets/lib/private/asset-handler.ts | 9 ++++++++- packages/cdk-assets/lib/private/docker.ts | 15 ++++++++++++--- .../lib/private/handlers/container-images.ts | 19 ++++++++++++++----- .../cdk-assets/lib/private/handlers/index.ts | 6 +++--- packages/cdk-assets/lib/publishing.ts | 11 ++++++++++- 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/packages/aws-cdk/lib/util/asset-publishing.ts b/packages/aws-cdk/lib/util/asset-publishing.ts index 7b4c4f943d9ff..47d83eb5c691c 100644 --- a/packages/aws-cdk/lib/util/asset-publishing.ts +++ b/packages/aws-cdk/lib/util/asset-publishing.ts @@ -54,6 +54,7 @@ export async function publishAssets( publishInParallel: options.parallel ?? true, buildAssets: options.buildAssets ?? true, publishAssets: true, + quiet: options.quiet, }); await publisher.publish(); if (publisher.hasFailures) { diff --git a/packages/cdk-assets/lib/private/asset-handler.ts b/packages/cdk-assets/lib/private/asset-handler.ts index 5baa32d9d52f4..baafb3cd0317e 100644 --- a/packages/cdk-assets/lib/private/asset-handler.ts +++ b/packages/cdk-assets/lib/private/asset-handler.ts @@ -28,4 +28,11 @@ export interface IHandlerHost { readonly dockerFactory: DockerFactory; emitMessage(type: EventType, m: string): void; -} \ No newline at end of file +} + +export interface IHandlerOptions { + /** + * Suppress all output + */ + readonly quiet?: boolean; +} diff --git a/packages/cdk-assets/lib/private/docker.ts b/packages/cdk-assets/lib/private/docker.ts index 4dae410047497..27b7f1ed9b911 100644 --- a/packages/cdk-assets/lib/private/docker.ts +++ b/packages/cdk-assets/lib/private/docker.ts @@ -21,6 +21,12 @@ interface BuildOptions { readonly outputs?: string[]; readonly cacheFrom?: DockerCacheOption[]; readonly cacheTo?: DockerCacheOption; + readonly quiet?: boolean; +} + +interface PushOptions { + readonly tag: string; + readonly quiet?: boolean; } export interface DockerCredentialsConfig { @@ -101,7 +107,10 @@ export class Docker { ...options.cacheTo ? ['--cache-to', this.cacheOptionToFlag(options.cacheTo)] : [], '.', ]; - await this.execute(buildCommand, { cwd: options.directory }); + await this.execute(buildCommand, { + cwd: options.directory, + quiet: options.quiet, + }); } /** @@ -128,8 +137,8 @@ export class Docker { await this.execute(['tag', sourceTag, targetTag]); } - public async push(tag: string) { - await this.execute(['push', tag]); + public async push(options: PushOptions) { + await this.execute(['push', options.tag], { quiet: options.quiet }); } /** diff --git a/packages/cdk-assets/lib/private/handlers/container-images.ts b/packages/cdk-assets/lib/private/handlers/container-images.ts index 670c813dd8b20..3b21e922aec66 100644 --- a/packages/cdk-assets/lib/private/handlers/container-images.ts +++ b/packages/cdk-assets/lib/private/handlers/container-images.ts @@ -3,7 +3,7 @@ import { DockerImageDestination } from '@aws-cdk/cloud-assembly-schema'; import type * as AWS from 'aws-sdk'; import { DockerImageManifestEntry } from '../../asset-manifest'; import { EventType } from '../../progress'; -import { IAssetHandler, IHandlerHost } from '../asset-handler'; +import { IAssetHandler, IHandlerHost, IHandlerOptions } from '../asset-handler'; import { Docker } from '../docker'; import { replaceAwsPlaceholders } from '../placeholders'; import { shell } from '../shell'; @@ -21,7 +21,8 @@ export class ContainerImageAssetHandler implements IAssetHandler { constructor( private readonly workDir: string, private readonly asset: DockerImageManifestEntry, - private readonly host: IHandlerHost) { + private readonly host: IHandlerHost, + private readonly options: IHandlerOptions) { } public async build(): Promise { @@ -36,7 +37,9 @@ export class ContainerImageAssetHandler implements IAssetHandler { ecr: initOnce.ecr, }); - const builder = new ContainerImageBuilder(dockerForBuilding, this.workDir, this.asset, this.host); + const builder = new ContainerImageBuilder(dockerForBuilding, this.workDir, this.asset, this.host, { + quiet: this.options.quiet, + }); const localTagName = await builder.build(); if (localTagName === undefined || this.host.aborted) { return; } @@ -70,7 +73,7 @@ export class ContainerImageAssetHandler implements IAssetHandler { if (this.host.aborted) { return; } this.host.emitMessage(EventType.UPLOAD, `Push ${initOnce.imageUri}`); - await dockerForPushing.push(initOnce.imageUri); + await dockerForPushing.push({ tag: initOnce.imageUri, quiet: this.options.quiet }); } private async initOnce(options: { quiet?: boolean } = {}): Promise { @@ -120,12 +123,17 @@ export class ContainerImageAssetHandler implements IAssetHandler { } } +interface ContainerImageBuilderOptions { + readonly quiet?: boolean; +} + class ContainerImageBuilder { constructor( private readonly docker: Docker, private readonly workDir: string, private readonly asset: DockerImageManifestEntry, - private readonly host: IHandlerHost) { + private readonly host: IHandlerHost, + private readonly options: ContainerImageBuilderOptions) { } async build(): Promise { @@ -188,6 +196,7 @@ class ContainerImageBuilder { outputs: source.dockerOutputs, cacheFrom: source.cacheFrom, cacheTo: source.cacheTo, + quiet: this.options.quiet, }); } diff --git a/packages/cdk-assets/lib/private/handlers/index.ts b/packages/cdk-assets/lib/private/handlers/index.ts index 1a46247674468..2b3c767eb4963 100644 --- a/packages/cdk-assets/lib/private/handlers/index.ts +++ b/packages/cdk-assets/lib/private/handlers/index.ts @@ -1,14 +1,14 @@ import { ContainerImageAssetHandler } from './container-images'; import { FileAssetHandler } from './files'; import { AssetManifest, DockerImageManifestEntry, FileManifestEntry, IManifestEntry } from '../../asset-manifest'; -import { IAssetHandler, IHandlerHost } from '../asset-handler'; +import { IAssetHandler, IHandlerHost, IHandlerOptions } from '../asset-handler'; -export function makeAssetHandler(manifest: AssetManifest, asset: IManifestEntry, host: IHandlerHost): IAssetHandler { +export function makeAssetHandler(manifest: AssetManifest, asset: IManifestEntry, host: IHandlerHost, options: IHandlerOptions): IAssetHandler { if (asset instanceof FileManifestEntry) { return new FileAssetHandler(manifest.directory, asset, host); } if (asset instanceof DockerImageManifestEntry) { - return new ContainerImageAssetHandler(manifest.directory, asset, host); + return new ContainerImageAssetHandler(manifest.directory, asset, host, options); } throw new Error(`Unrecognized asset type: '${asset}'`); diff --git a/packages/cdk-assets/lib/publishing.ts b/packages/cdk-assets/lib/publishing.ts index 9e38308cd7e66..03cf2683f3293 100644 --- a/packages/cdk-assets/lib/publishing.ts +++ b/packages/cdk-assets/lib/publishing.ts @@ -45,6 +45,13 @@ export interface AssetPublishingOptions { * @default true */ readonly publishAssets?: boolean; + + /** + * Whether to print publishing logs + * + * @default true + */ + readonly quiet?: boolean; } /** @@ -240,7 +247,9 @@ export class AssetPublishing implements IPublishProgress { if (existing) { return existing; } - const ret = makeAssetHandler(this.manifest, asset, this.handlerHost); + const ret = makeAssetHandler(this.manifest, asset, this.handlerHost, { + quiet: this.options.quiet, + }); this.handlerCache.set(asset, ret); return ret; }