Skip to content

Commit

Permalink
chore(core): bundle assets in .cdk.staging (aws#8601)
Browse files Browse the repository at this point in the history
Using `/tmp` can be problematic with some Docker setups or in CI.

The directory `.cdk.staging` is already gitignored in the init
templates.

Closes aws#8589


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored Jun 18, 2020
1 parent 1112abb commit 4b68655
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@
"@aws-cdk/cloud-assembly-schema/semver/**",
"@aws-cdk/cloudformation-include/yaml",
"@aws-cdk/cloudformation-include/yaml/**",
"@aws-cdk/core/fs-extra",
"@aws-cdk/core/fs-extra/**",
"@aws-cdk/core/minimatch",
"@aws-cdk/core/minimatch/**",
"@aws-cdk/cx-api/semver",
"@aws-cdk/cx-api/semver/**",
"monocdk-experiment/case",
"monocdk-experiment/case/**",
"monocdk-experiment/fs-extra",
"monocdk-experiment/fs-extra/**",
"monocdk-experiment/jsonschema",
"monocdk-experiment/jsonschema/**",
"monocdk-experiment/minimatch",
Expand Down
31 changes: 12 additions & 19 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as cxapi from '@aws-cdk/cx-api';
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { AssetHashType, AssetOptions } from './assets';
import { BundlingOptions } from './bundling';
import { Construct, ISynthesisSession } from './construct-compat';
import { FileSystem, FingerprintOptions } from './fs';

const STAGING_TMP = '.cdk.staging';

/**
* Initialization properties for `AssetStaging`.
*/
Expand Down Expand Up @@ -118,22 +120,9 @@ export class AssetStaging extends Construct {

// Asset has been bundled
if (this.bundleDir) {
// Try to rename bundling directory to staging directory
try {
fs.renameSync(this.bundleDir, targetPath);
return;
} catch (err) {
// /tmp and cdk.out could be mounted across different mount points
// in this case we will fallback to copying. This can happen in Windows
// Subsystem for Linux (WSL).
if (err.code === 'EXDEV') {
fs.mkdirSync(targetPath);
FileSystem.copyDirectory(this.bundleDir, targetPath, this.fingerprintOptions);
return;
}

throw err;
}
// Move bundling directory to staging directory
fs.moveSync(this.bundleDir, targetPath);
return;
}

// Copy file/directory to staging directory
Expand All @@ -149,8 +138,12 @@ export class AssetStaging extends Construct {
}

private bundle(options: BundlingOptions): string {
// Create temporary directory for bundling
const bundleDir = FileSystem.mkdtemp('cdk-asset-bundle-');
// Temp staging directory in the working directory
const stagingTmp = path.join('.', STAGING_TMP);
fs.ensureDirSync(stagingTmp);

// Create temp directory for bundling inside the temp staging directory
const bundleDir = path.resolve(fs.mkdtempSync(path.join(stagingTmp, 'asset-bundle-')));

let user: string;
if (options.user) {
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@
"ts-mock-imports": "^1.3.0"
},
"dependencies": {
"fs-extra": "^9.0.1",
"minimatch": "^3.0.4",
"@aws-cdk/cx-api": "0.0.0",
"@aws-cdk/cdk-assets-schema": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"constructs": "^3.0.2"
},
"bundledDependencies": [
"fs-extra",
"minimatch"
],
"homepage": "https://github.com/aws/aws-cdk",
Expand Down
11 changes: 10 additions & 1 deletion packages/@aws-cdk/core/test/test.staging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as cxapi from '@aws-cdk/cx-api';
import * as fs from 'fs';
import * as fs from 'fs-extra';
import { Test } from 'nodeunit';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import { App, AssetHashType, AssetStaging, BundlingDockerImage, Stack } from '../lib';

const STUB_INPUT_FILE = '/tmp/docker-stub.input';
Expand All @@ -26,6 +27,7 @@ export = {
fs.unlinkSync(STUB_INPUT_FILE);
}
cb();
sinon.restore();
},

'base case'(test: Test) {
Expand Down Expand Up @@ -103,6 +105,8 @@ export = {
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
const ensureDirSyncSpy = sinon.spy(fs, 'ensureDirSync');
const mkdtempSyncSpy = sinon.spy(fs, 'mkdtempSync');

// WHEN
new AssetStaging(stack, 'Asset', {
Expand All @@ -127,6 +131,11 @@ export = {
'tree.json',
]);

// asset is bundled in a directory inside .cdk.staging
const stagingTmp = path.join('.', '.cdk.staging');
test.ok(ensureDirSyncSpy.calledWith(stagingTmp));
test.ok(mkdtempSyncSpy.calledWith(sinon.match(path.join(stagingTmp, 'asset-bundle-'))));

test.done();
},

Expand Down
2 changes: 2 additions & 0 deletions packages/monocdk-experiment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"license": "Apache-2.0",
"bundledDependencies": [
"case",
"fs-extra",
"jsonschema",
"minimatch",
"semver",
Expand All @@ -89,6 +90,7 @@
"dependencies": {
"case": "1.6.3",
"constructs": "^3.0.2",
"fs-extra": "^9.0.1",
"jsonschema": "^1.2.5",
"minimatch": "^3.0.4",
"semver": "^7.2.2",
Expand Down

0 comments on commit 4b68655

Please sign in to comment.