Skip to content

Commit

Permalink
Merge branch 'main' into fix-iotevents-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jan 15, 2025
2 parents ae2f1f4 + 3a711b0 commit 491a849
Show file tree
Hide file tree
Showing 22 changed files with 472 additions and 233 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cli-lib-alpha/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class AwsCdkCli implements IAwsCdkCli {
return new AwsCdkCli(async (args) => changeDir(
() => runCli(args, async (sdk, config) => {
const env = await prepareDefaultEnvironment(sdk);
const context = await prepareContext(config, env);
const context = await prepareContext(config.settings, config.context.all, env);

return withEnv(async() => createAssembly(await producer.produce(context)), env);
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/bootstrap/bootstrap-props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BootstrapSource } from './bootstrap-environment';
import { Tag } from '../../cdk-toolkit';
import { Tag } from '../../tags';
import { StringWithoutPlaceholders } from '../util/placeholders';

export const BUCKET_NAME_OUTPUT = 'BucketName';
Expand Down
45 changes: 22 additions & 23 deletions packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { minimatch } from 'minimatch';
import * as semver from 'semver';
import { error, info, warning } from '../../logging';
import { ToolkitError } from '../../toolkit/error';
import { info } from '../../logging';
import { AssemblyError, ToolkitError } from '../../toolkit/error';
import { flatten } from '../../util';

export enum DefaultSelection {
Expand Down Expand Up @@ -134,7 +134,7 @@ export class CloudAssembly {
}
}

private selectMatchingStacks(
protected selectMatchingStacks(
stacks: cxapi.CloudFormationStackArtifact[],
patterns: string[],
extend: ExtendedStackSelection = ExtendedStackSelection.None,
Expand Down Expand Up @@ -170,7 +170,7 @@ export class CloudAssembly {
}
}

private extendStacks(
protected extendStacks(
matched: cxapi.CloudFormationStackArtifact[],
all: cxapi.CloudFormationStackArtifact[],
extend: ExtendedStackSelection = ExtendedStackSelection.None,
Expand Down Expand Up @@ -231,6 +231,10 @@ export class StackCollection {
return this.stackArtifacts.map(s => s.id);
}

public get hierarchicalIds(): string[] {
return this.stackArtifacts.map(s => s.hierarchicalId);
}

public reversed() {
const arts = [...this.stackArtifacts];
arts.reverse();
Expand All @@ -241,14 +245,17 @@ export class StackCollection {
return new StackCollection(this.assembly, this.stackArtifacts.filter(predicate));
}

public concat(other: StackCollection): StackCollection {
return new StackCollection(this.assembly, this.stackArtifacts.concat(other.stackArtifacts));
public concat(...others: StackCollection[]): StackCollection {
return new StackCollection(this.assembly, this.stackArtifacts.concat(...others.map(o => o.stackArtifacts)));
}

/**
* Extracts 'aws:cdk:warning|info|error' metadata entries from the stack synthesis
*/
public processMetadataMessages(options: MetadataMessageOptions = {}) {
public async validateMetadata(
failAt: 'warn' | 'error' | 'none' = 'error',
logger: (level: 'info' | 'error' | 'warn', msg: cxapi.SynthesisMessage) => Promise<void> = async () => {},
) {
let warnings = false;
let errors = false;

Expand All @@ -257,33 +264,25 @@ export class StackCollection {
switch (message.level) {
case cxapi.SynthesisMessageLevel.WARNING:
warnings = true;
printMessage(warning, 'Warning', message.id, message.entry);
await logger('warn', message);
break;
case cxapi.SynthesisMessageLevel.ERROR:
errors = true;
printMessage(error, 'Error', message.id, message.entry);
await logger('error', message);
break;
case cxapi.SynthesisMessageLevel.INFO:
printMessage(info, 'Info', message.id, message.entry);
await logger('info', message);
break;
}
}
}

if (errors && !options.ignoreErrors) {
throw new ToolkitError('Found errors');
if (errors && failAt != 'none') {
throw new AssemblyError('Found errors');
}

if (options.strict && warnings) {
throw new ToolkitError('Found warnings (--strict mode)');
}

function printMessage(logFn: (s: string) => void, prefix: string, id: string, entry: cxapi.MetadataEntry) {
logFn(`[${prefix} at ${id}] ${entry.data}`);

if (options.verbose && entry.trace) {
logFn(` ${entry.trace.join('\n ')}`);
}
if (warnings && failAt === 'warn') {
throw new AssemblyError('Found warnings (--strict mode)');
}
}
}
Expand Down Expand Up @@ -380,7 +379,7 @@ function includeUpstreamStacks(
}
}

function sanitizePatterns(patterns: string[]): string[] {
export function sanitizePatterns(patterns: string[]): string[] {
let sanitized = patterns.filter(s => s != null); // filter null/undefined
sanitized = [...new Set(sanitized)]; // make them unique
return sanitized;
Expand Down
36 changes: 19 additions & 17 deletions packages/aws-cdk/lib/api/cxapp/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as cxapi from '@aws-cdk/cx-api';
import * as fs from 'fs-extra';
import * as semver from 'semver';
import { debug, warning } from '../../logging';
import { Configuration, PROJECT_CONFIG, USER_DEFAULTS } from '../../settings';
import { Configuration, PROJECT_CONFIG, Settings, USER_DEFAULTS } from '../../settings';
import { ToolkitError } from '../../toolkit/error';
import { loadTree, some } from '../../tree';
import { splitBySize } from '../../util/objects';
Expand All @@ -22,7 +22,7 @@ export interface ExecProgramResult {
/** Invokes the cloud executable and returns JSON output */
export async function execProgram(aws: SdkProvider, config: Configuration): Promise<ExecProgramResult> {
const env = await prepareDefaultEnvironment(aws);
const context = await prepareContext(config, env);
const context = await prepareContext(config.settings, config.context.all, env);

const build = config.settings.get(['build']);
if (build) {
Expand All @@ -44,7 +44,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
return { assembly: createAssembly(app), lock };
}

const commandLine = await guessExecutable(appToArray(app));
const commandLine = await guessExecutable(app);

const outdir = config.settings.get(['output']);
if (!outdir) {
Expand Down Expand Up @@ -166,16 +166,19 @@ export function createAssembly(appDir: string) {
*
* @param context The context key/value bash.
*/
export async function prepareDefaultEnvironment(aws: SdkProvider): Promise<{ [key: string]: string }> {
export async function prepareDefaultEnvironment(
aws: SdkProvider,
logFn: (msg: string, ...args: any) => any = debug,
): Promise<{ [key: string]: string }> {
const env: { [key: string]: string } = { };

env[cxapi.DEFAULT_REGION_ENV] = aws.defaultRegion;
debug(`Setting "${cxapi.DEFAULT_REGION_ENV}" environment variable to`, env[cxapi.DEFAULT_REGION_ENV]);
await logFn(`Setting "${cxapi.DEFAULT_REGION_ENV}" environment variable to`, env[cxapi.DEFAULT_REGION_ENV]);

const accountId = (await aws.defaultAccount())?.accountId;
if (accountId) {
env[cxapi.DEFAULT_ACCOUNT_ENV] = accountId;
debug(`Setting "${cxapi.DEFAULT_ACCOUNT_ENV}" environment variable to`, env[cxapi.DEFAULT_ACCOUNT_ENV]);
await logFn(`Setting "${cxapi.DEFAULT_ACCOUNT_ENV}" environment variable to`, env[cxapi.DEFAULT_ACCOUNT_ENV]);
}

return env;
Expand All @@ -186,35 +189,33 @@ export async function prepareDefaultEnvironment(aws: SdkProvider): Promise<{ [ke
* The merging of various configuration sources like cli args or cdk.json has already happened.
* We now need to set the final values to the context.
*/
export async function prepareContext(config: Configuration, env: { [key: string]: string | undefined}) {
const context = config.context.all;

const debugMode: boolean = config.settings.get(['debug']) ?? true;
export async function prepareContext(settings: Settings, context: {[key: string]: any}, env: { [key: string]: string | undefined}) {
const debugMode: boolean = settings.get(['debug']) ?? true;
if (debugMode) {
env.CDK_DEBUG = 'true';
}

const pathMetadata: boolean = config.settings.get(['pathMetadata']) ?? true;
const pathMetadata: boolean = settings.get(['pathMetadata']) ?? true;
if (pathMetadata) {
context[cxapi.PATH_METADATA_ENABLE_CONTEXT] = true;
}

const assetMetadata: boolean = config.settings.get(['assetMetadata']) ?? true;
const assetMetadata: boolean = settings.get(['assetMetadata']) ?? true;
if (assetMetadata) {
context[cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT] = true;
}

const versionReporting: boolean = config.settings.get(['versionReporting']) ?? true;
const versionReporting: boolean = settings.get(['versionReporting']) ?? true;
if (versionReporting) { context[cxapi.ANALYTICS_REPORTING_ENABLED_CONTEXT] = true; }
// We need to keep on doing this for framework version from before this flag was deprecated.
if (!versionReporting) { context['aws:cdk:disable-version-reporting'] = true; }

const stagingEnabled = config.settings.get(['staging']) ?? true;
const stagingEnabled = settings.get(['staging']) ?? true;
if (!stagingEnabled) {
context[cxapi.DISABLE_ASSET_STAGING_CONTEXT] = true;
}

const bundlingStacks = config.settings.get(['bundlingStacks']) ?? ['**'];
const bundlingStacks = settings.get(['bundlingStacks']) ?? ['**'];
context[cxapi.BUNDLING_STACKS] = bundlingStacks;

debug('context:', context);
Expand Down Expand Up @@ -257,7 +258,8 @@ const EXTENSION_MAP = new Map<string, CommandGenerator>([
* verify if registry associations have or have not been set up for this
* file type, so we'll assume the worst and take control.
*/
async function guessExecutable(commandLine: string[]) {
export async function guessExecutable(app: string) {
const commandLine = appToArray(app);
if (commandLine.length === 1) {
let fstat;

Expand Down Expand Up @@ -300,7 +302,7 @@ function contextOverflowCleanup(location: string | undefined, assembly: cxapi.Cl
}
}

function spaceAvailableForContext(env: { [key: string]: string }, limit: number) {
export function spaceAvailableForContext(env: { [key: string]: string }, limit: number) {
const size = (value: string) => value != null ? Buffer.byteLength(value) : 0;

const usedSpace = Object.entries(env)
Expand Down
12 changes: 6 additions & 6 deletions packages/aws-cdk/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { AssetManifest, IManifestEntry } from 'cdk-assets';
import * as chalk from 'chalk';
import type { SdkProvider } from './aws-auth/sdk-provider';
import { type DeploymentMethod, deployStack, DeployStackResult, destroyStack } from './deploy-stack';
import { EnvironmentAccess } from './environment-access';
import { type EnvironmentResources } from './environment-resources';
import type { Tag } from '../cdk-toolkit';
import { debug, warning } from '../logging';
import { EnvironmentAccess } from './environment-access';
import type { Tag } from '../tags';
import { HotswapMode, HotswapPropertyOverrides } from './hotswap/common';
import {
loadCurrentTemplate,
Expand All @@ -25,10 +25,6 @@ import {
Template,
uploadStackTemplateAssets,
} from './util/cloudformation';
import { StackActivityMonitor, StackActivityProgress } from './util/cloudformation/stack-activity-monitor';
import { StackEventPoller } from './util/cloudformation/stack-event-poller';
import { RollbackChoice } from './util/cloudformation/stack-status';
import { makeBodyParameter } from './util/template-body-parameter';
import { AssetManifestBuilder } from '../util/asset-manifest-builder';
import {
buildAssets,
Expand All @@ -38,6 +34,10 @@ import {
type PublishAssetsOptions,
PublishingAws,
} from '../util/asset-publishing';
import { StackActivityMonitor, StackActivityProgress } from './util/cloudformation/stack-activity-monitor';
import { StackEventPoller } from './util/cloudformation/stack-event-poller';
import { RollbackChoice } from './util/cloudformation/stack-status';
import { makeBodyParameter } from './util/template-body-parameter';
import { formatErrorMessage } from '../util/error';

const BOOTSTRAP_STACK_VERSION_FOR_ROLLBACK = 23;
Expand Down
24 changes: 24 additions & 0 deletions packages/aws-cdk/lib/api/util/string-manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@ export function leftPad(s: string, n: number, char: string) {
const padding = Math.max(0, n - s.length);
return char.repeat(padding) + s;
}

/**
* Formats time in milliseconds (which we get from 'Date.getTime()')
* to a human-readable time; returns time in seconds rounded to 2
* decimal places.
*/
export function formatTime(num: number): number {
return roundPercentage(millisecondsToSeconds(num));
}

/**
* Rounds a decimal number to two decimal points.
* The function is useful for fractions that need to be outputted as percentages.
*/
function roundPercentage(num: number): number {
return Math.round(100 * num) / 100;
}

/**
* Given a time in milliseconds, return an equivalent amount in seconds.
*/
function millisecondsToSeconds(num: number): number {
return num / 1000;
}
Loading

0 comments on commit 491a849

Please sign in to comment.