Skip to content

Commit

Permalink
Merge branch 'main' into gh-24880
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] committed Jun 26, 2023
2 parents 2e752a6 + 939dc84 commit 32887c6
Show file tree
Hide file tree
Showing 283 changed files with 2,116 additions and 514 deletions.
Empty file added .gitmodules
Empty file.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
"lerna": "^7.0.1",
"nx": "^16.3.2",
"patch-package": "^6.5.1",
"semver": "^6.3.0",
"semver": "^7.5.2",
"standard-version": "^9.5.0",
"typescript": "~4.9.5"
"ts-node": "^10.9.1",
"typescript": "~5.1.3"
},
"resolutions": {
"colors": "1.4.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk-testing/cli-integ/lib/corking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export class MemoryStream extends stream.Writable {
this.parts.splice(0, this.parts.length);
}

public async flushTo(strm: NodeJS.WritableStream) {
public async flushTo(strm: NodeJS.WritableStream): Promise<void> {
const flushed = strm.write(this.buffer());
if (!flushed) {
return new Promise(ok => strm.once('drain', ok));
}
return;
}

public toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"inlineSourceMap": true,
"inlineSources": true,
"alwaysStrict": true,
"charset": "utf8",
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"inlineSourceMap": true,
"inlineSources": true,
"alwaysStrict": true,
"charset": "utf8",
"declaration": true,
"experimentalDecorators": true,
"incremental": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1068,15 +1068,17 @@ export class FargateComputeEnvironment extends ManagedComputeEnvironmentBase imp
const stack = Stack.of(scope);
const computeEnvironmentName = stack.splitArn(fargateComputeEnvironmentArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;

class Import extends ManagedComputeEnvironmentBase implements IFargateComputeEnvironment {
class Import extends Resource implements IFargateComputeEnvironment {
public readonly computeEnvironmentArn = fargateComputeEnvironmentArn;
public readonly computeEnvironmentName = computeEnvironmentName;
public readonly enabled = true;
public readonly maxvCpus = 1;
public readonly connections = { } as any;
public readonly securityGroups = [];
public readonly tags: TagManager = new TagManager(TagType.MAP, 'AWS::Batch::ComputeEnvironment');
}

return new Import(scope, id, {
vpc: undefined as any,
});
return new Import(scope, id);
}

public readonly computeEnvironmentName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,12 @@ describe('FargateComputeEnvironment', () => {
ComputeEnvironmentName: 'maxPropsFargateCE',
});
});

test('can be imported from arn', () => {
// WHEN
const ce = FargateComputeEnvironment.fromFargateComputeEnvironmentArn(stack, 'import', 'arn:aws:batch:us-east-1:123456789012:compute-environment/ce-name');

// THEN
expect(ce.computeEnvironmentArn).toEqual('arn:aws:batch:us-east-1:123456789012:compute-environment/ce-name');
});
});
55 changes: 53 additions & 2 deletions packages/@aws-cdk/cdk-cli-wrapper/lib/cdk-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DefaultCdkOptions, DeployOptions, DestroyOptions, SynthOptions, ListOptions, StackActivityProgress } from './commands';
import { exec } from './utils';
import { ChildProcess } from 'child_process';
import { DefaultCdkOptions, DeployOptions, DestroyOptions, SynthOptions, ListOptions, StackActivityProgress, HotswapMode } from './commands';
import { exec, watch } from './utils';

/**
* AWS CDK CLI operations
Expand Down Expand Up @@ -30,6 +31,11 @@ export interface ICdk {
* cdk synth fast
*/
synthFast(options: SynthFastOptions): void;

/**
* cdk watch
*/
watch(options: DeployOptions): ChildProcess;
}

/**
Expand Down Expand Up @@ -176,6 +182,7 @@ export class CdkCliWrapper implements ICdk {
...options.changeSetName ? ['--change-set-name', options.changeSetName] : [],
...options.toolkitStackName ? ['--toolkit-stack-name', options.toolkitStackName] : [],
...options.progress ? ['--progress', options.progress] : ['--progress', StackActivityProgress.EVENTS],
...options.deploymentMethod ? ['--method', options.deploymentMethod] : [],
...this.createDefaultArguments(options),
];

Expand All @@ -186,6 +193,50 @@ export class CdkCliWrapper implements ICdk {
});
}

public watch(options: DeployOptions): ChildProcess {
let hotswap: string;
switch (options.hotswap) {
case HotswapMode.FALL_BACK:
hotswap = '--hotswap-fallback';
break;
case HotswapMode.HOTSWAP_ONLY:
hotswap = '--hotswap';
break;
default:
hotswap = '--hotswap-fallback';
break;
}
const deployCommandArgs: string[] = [
'--watch',
...renderBooleanArg('ci', options.ci),
...renderBooleanArg('execute', options.execute),
...renderBooleanArg('exclusively', options.exclusively),
...renderBooleanArg('force', options.force),
...renderBooleanArg('previous-parameters', options.usePreviousParameters),
...renderBooleanArg('rollback', options.rollback),
...renderBooleanArg('staging', options.staging),
...renderBooleanArg('logs', options.traceLogs),
hotswap,
...options.reuseAssets ? renderArrayArg('--reuse-assets', options.reuseAssets) : [],
...options.notificationArns ? renderArrayArg('--notification-arns', options.notificationArns) : [],
...options.parameters ? renderMapArrayArg('--parameters', options.parameters) : [],
...options.outputsFile ? ['--outputs-file', options.outputsFile] : [],
...options.requireApproval ? ['--require-approval', options.requireApproval] : [],
...options.changeSetName ? ['--change-set-name', options.changeSetName] : [],
...options.toolkitStackName ? ['--toolkit-stack-name', options.toolkitStackName] : [],
...options.progress ? ['--progress', options.progress] : ['--progress', StackActivityProgress.EVENTS],
...options.deploymentMethod ? ['--method', options.deploymentMethod] : [],
...this.createDefaultArguments(options),
];

return watch([this.cdk, 'deploy', ...deployCommandArgs], {
cwd: this.directory,
verbose: this.showOutput,
env: this.env,
});

}

/**
* cdk destroy
*/
Expand Down
47 changes: 47 additions & 0 deletions packages/@aws-cdk/cdk-cli-wrapper/lib/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,53 @@ export interface DeployOptions extends DefaultCdkOptions {
* @default StackActivityProgress.EVENTS
*/
readonly progress?: StackActivityProgress;

/**
* Whether this 'deploy' command should actually delegate to the 'watch' command.
*
* @default false
*/
readonly watch?: boolean;

/**
* Whether to perform a 'hotswap' deployment.
* A 'hotswap' deployment will attempt to short-circuit CloudFormation
* and update the affected resources like Lambda functions directly.
*
* @default - `HotswapMode.FALL_BACK` for regular deployments, `HotswapMode.HOTSWAP_ONLY` for 'watch' deployments
*/
readonly hotswap?: HotswapMode;

/**
* Whether to show CloudWatch logs for hotswapped resources
* locally in the users terminal
*
* @default - false
*/
readonly traceLogs?: boolean;

/**
* Deployment method
*/
readonly deploymentMethod?: DeploymentMethod;
}
export type DeploymentMethod = 'direct' | 'change-set';

export enum HotswapMode {
/**
* Will fall back to CloudFormation when a non-hotswappable change is detected
*/
FALL_BACK = 'fall-back',

/**
* Will not fall back to CloudFormation when a non-hotswappable change is detected
*/
HOTSWAP_ONLY = 'hotswap-only',

/**
* Will not attempt to hotswap anything and instead go straight to CloudFormation
*/
FULL_DEPLOYMENT = 'full-deployment',
}

/**
Expand Down
21 changes: 20 additions & 1 deletion packages/@aws-cdk/cdk-cli-wrapper/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Helper functions for CDK Exec
import { spawnSync } from 'child_process';
import { spawn, spawnSync } from 'child_process';

/**
* Our own execute function which doesn't use shells and strings.
Expand Down Expand Up @@ -37,3 +37,22 @@ export function exec(commandLine: string[], options: { cwd?: string, json?: bool
throw new Error('Command output is not JSON');
}
}

/**
* For use with `cdk deploy --watch`
*/
export function watch(commandLine: string[], options: { cwd?: string, verbose?: boolean, env?: any } = { }) {
const proc = spawn(commandLine[0], commandLine.slice(1), {
stdio: ['ignore', 'pipe', options.verbose ? 'inherit' : 'pipe'], // inherit STDERR in verbose mode
env: {
...process.env,
...options.env,
},
cwd: options.cwd,
});
proc.on('error', (err: Error) => {
throw err;
});

return proc;
}
34 changes: 33 additions & 1 deletion packages/@aws-cdk/cdk-cli-wrapper/test/cdk-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as child_process from 'child_process';
import { CdkCliWrapper } from '../lib/cdk-wrapper';
import { RequireApproval, StackActivityProgress } from '../lib/commands';
let spawnSyncMock: jest.SpyInstance;
let spawnMock: jest.SpyInstance;

beforeEach(() => {
spawnSyncMock = jest.spyOn(child_process, 'spawnSync').mockReturnValue({
Expand All @@ -12,6 +13,11 @@ beforeEach(() => {
output: ['stdout', 'stderr'],
signal: null,
});
spawnMock = jest.spyOn(child_process, 'spawn').mockImplementation(jest.fn(() => {
return {
on: jest.fn(() => {}),
} as unknown as child_process.ChildProcess;
}));
});

afterEach(() => {
Expand Down Expand Up @@ -317,7 +323,33 @@ test('default synth', () => {
);
});

test('synth arguments', () => {
test('watch arguments', () => {
// WHEN
const cdk = new CdkCliWrapper({
directory: '/project',
env: {
KEY: 'value',
},
});
cdk.watch({
app: 'node bin/my-app.js',
stacks: ['test-stack1'],
});

// THEN
expect(spawnMock).toHaveBeenCalledWith(
expect.stringMatching(/cdk/),
['deploy', '--watch', '--hotswap-fallback', '--progress', 'events', '--app', 'node bin/my-app.js', 'test-stack1'],
expect.objectContaining({
env: expect.objectContaining({
KEY: 'value',
}),
cwd: '/project',
}),
);
});

test('destroy arguments', () => {
// WHEN
const cdk = new CdkCliWrapper({
directory: '/project',
Expand Down
Loading

0 comments on commit 32887c6

Please sign in to comment.