Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(toolkit): request response #32926

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/@aws-cdk/toolkit/lib/io/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { formatSdkLoggerContent } from 'aws-cdk/lib/api/aws-auth/sdk-logger';
import { ToolkitAction } from '../types';
import { IIoHost, IoMessage, IoRequest } from './io-host';
import { trace } from './messages';
import { withCorkedLogging } from 'aws-cdk/lib/logging';
import { ToolkitError } from '../api/errors';
import chalk from 'chalk';

export function withAction(ioHost: IIoHost, action: ToolkitAction) {
return {
Expand Down Expand Up @@ -109,3 +112,37 @@ export function asSdkLogger(ioHost: IIoHost, action: ToolkitAction): Logger {
}
};
}

let TESTING = false;

export function markTesting() {
TESTING = true;
}
Comment on lines +118 to +120
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assume this would still be necessary when we test


/**
* Ask the user for a yes/no confirmation
*
* Automatically fail the confirmation in case we're in a situation where the confirmation
* cannot be interactively obtained from a human at the keyboard.
*/
export async function askUserConfirmation(
ioHost: IIoHost,
concurrency: number,
motivation: string,
question: string,
) {
await withCorkedLogging(async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#32898 needs to be implemented before we can get rid of this, right?

// only talk to user if STDIN is a terminal (otherwise, fail)
if (!TESTING && !process.stdin.isTTY) {
throw new ToolkitError(`${motivation}, but terminal (TTY) is not attached so we are unable to get a confirmation from the user`);
}

// only talk to user if concurrency is 1 (otherwise, fail)
if (concurrency > 1) {
throw new ToolkitError(`${motivation}, but concurrency is greater than 1 so we are unable to get a confirmation from the user`);
}

const confirmed = await ioHost.requestResponse<any, boolean>(prompt(`${chalk.cyan(question)} (y/n)?`));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not too sure what to do here. prompt currently is nothing and a build failure atm. should this be written in aws-cdk/lib/logging.ts?

if (!confirmed) { throw new ToolkitError('Aborted by user'); }
});
}
22 changes: 13 additions & 9 deletions packages/@aws-cdk/toolkit/lib/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { StackAssembly } from './api/cloud-assembly/stack-assembly';
import { ICloudAssemblySource } from './api/cloud-assembly/types';
import { ToolkitError } from './api/errors';
import { IIoHost } from './io/io-host';
import { asSdkLogger, withAction } from './io/logger';
import { askUserConfirmation, asSdkLogger, withAction } from './io/logger';
import { data, error, highlight, info, success, warning } from './io/messages';
import { Timer } from './io/timer';
import { StackSelectionStrategy, ToolkitAction } from './types';
Expand Down Expand Up @@ -235,6 +235,7 @@ export class Toolkit {
// const currentTemplate = await deployments.readCurrentTemplate(stack);
// if (printSecurityDiff(currentTemplate, stack, requireApproval)) {
// await askUserConfirmation(
// ioHost,
// concurrency,
// '"--require-approval" is enabled and stack includes security-sensitive updates',
// 'Do you wish to deploy these changes',
Expand Down Expand Up @@ -314,6 +315,7 @@ export class Toolkit {
await ioHost.notify(warning(`${motivation}. Rolling back first (--force).`));
} else {
await askUserConfirmation(
ioHost,
concurrency,
motivation,
`${motivation}. Roll back first and then proceed with deployment`,
Expand All @@ -339,6 +341,7 @@ export class Toolkit {
await ioHost.notify(warning(`${motivation}. Proceeding with regular deployment (--force).`));
} else {
await askUserConfirmation(
ioHost,
concurrency,
motivation,
`${motivation}. Perform a regular deployment`,
Expand Down Expand Up @@ -384,14 +387,15 @@ export class Toolkit {
[`❌ ${chalk.bold(stack.stackName)} failed:`, ...(e.name ? [`${e.name}:`] : []), e.message].join(' '),
);
} finally {
if (options.cloudWatchLogMonitor) {
const foundLogGroupsResult = await findCloudWatchLogGroups(await this.sdkProvider('deploy'), stack);
options.cloudWatchLogMonitor.addLogGroups(
foundLogGroupsResult.env,
foundLogGroupsResult.sdk,
foundLogGroupsResult.logGroupNames,
);
}
// @TODO
// if (options.cloudWatchLogMonitor) {
// const foundLogGroupsResult = await findCloudWatchLogGroups(await this.sdkProvider('deploy'), stack);
// options.cloudWatchLogMonitor.addLogGroups(
// foundLogGroupsResult.env,
// foundLogGroupsResult.sdk,
// foundLogGroupsResult.logGroupNames,
// );
// }
// If an outputs file has been specified, create the file path and write stack outputs to it once.
// Outputs are written after all stacks have been deployed. If a stack deployment fails,
// all of the outputs from successfully deployed stacks before the failure will still be written.
Expand Down
Loading