Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Ben-Israel committed Jul 18, 2018
1 parent b208b22 commit 0763282
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
39 changes: 24 additions & 15 deletions packages/@aws-cdk/cdk-assets/lib/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,7 @@ export class Asset extends cdk.Construct {
// resolve full path
this.assetPath = path.resolve(props.path);

if (!fs.existsSync(this.assetPath)) {
throw new Error(`Cannot find asset at ${props.path}`);
}

if (props.packaging === AssetPackaging.ZipDirectory) {
if (!fs.statSync(this.assetPath).isDirectory()) {
throw new Error(`${this.assetPath} is expected to be a directory when asset packaging is 'zip'`);
}
} else if (props.packaging === AssetPackaging.File) {
if (!fs.statSync(this.assetPath).isFile()) {
throw new Error(`${this.assetPath} is expected to be a regular file when asset packaging is 'file'`);
}
} else {
throw new Error(`Unsupported asset packaging format: ${props.packaging}`);
}
validateAssetOnDisk(this.assetPath, props.packaging);

// add parameters for s3 bucket and s3 key. those will be set by
// the toolkit or by CI/CD when the stack is deployed and will include
Expand Down Expand Up @@ -185,3 +171,26 @@ export class ZipDirectoryAsset extends Asset {
super(parent, id, { packaging: AssetPackaging.ZipDirectory, ...props });
}
}

function validateAssetOnDisk(assetPath: string, packaging: AssetPackaging) {
if (!fs.existsSync(assetPath)) {
throw new Error(`Cannot find asset at ${assetPath}`);
}

switch (packaging) {
case AssetPackaging.ZipDirectory:
if (!fs.statSync(assetPath).isDirectory()) {
throw new Error(`${assetPath} is expected to be a directory when asset packaging is 'zip'`);
}
break;

case AssetPackaging.File:
if (!fs.statSync(assetPath).isFile()) {
throw new Error(`${assetPath} is expected to be a regular file when asset packaging is 'file'`);
}
break;

default:
throw new Error(`Unsupported asset packaging format: ${packaging}`);
}
}
7 changes: 7 additions & 0 deletions packages/@aws-cdk/cdk-assets/test/test.asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ export = {
}
});

test.done();
},
'fails if directory not found'(test: Test) {
const stack = new Stack();
test.throws(() => new ZipDirectoryAsset(stack, 'MyDirectory', {
path: '/path/not/found/' + Math.random() * 999999
}));
test.done();
}
};
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/toolkit-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ToolkitInfo {
}) { }

public get bucketUrl() {
return `https://${this.props.bucketName}.s3.amazonaws.com`;
return `https://${this.props.bucketEndpoint}`;
}

public get bucketName() {
Expand Down
30 changes: 5 additions & 25 deletions packages/aws-cdk/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,53 +41,33 @@ async function prepareAsset(asset: AssetMetadataEntry, toolkitInfo: ToolkitInfo)

async function prepareZipAsset(asset: AssetMetadataEntry, toolkitInfo: ToolkitInfo): Promise<CloudFormation.Parameter[]> {
debug('Preparing zip asset from directory:', asset.path);

const staging = await fs.mkdtemp('/tmp/cdk-assets');

try {
const archiveFile = path.join(staging, 'archive.zip');
await zipDirectory(asset.path, archiveFile);

debug('zip archive:', archiveFile);
const data = await fs.readFile(archiveFile);

const { key, changed } = await toolkitInfo.uploadIfChanged(data, {
s3KeyPrefix: 'assets/',
s3KeySuffix: '.zip',
contentType: 'application/zip',
});

const s3url = `s3://${toolkitInfo.bucketName}/${key}`;
if (changed) {
success(` 👜 Asset ${asset.path} (directory zip) uploaded to ${s3url}`);
} else {
success(` 👜 Asset ${asset.path} (directory zip) already exists in ${s3url}`);
}

return [
{ ParameterKey: asset.s3BucketParameter, ParameterValue: toolkitInfo.bucketName },
{ ParameterKey: asset.s3KeyParameter, ParameterValue: key }
];
return await prepareFileAsset(asset, toolkitInfo, 'application/zip');
} finally {
await fs.remove(staging);
}
}

async function prepareFileAsset(asset: AssetMetadataEntry, toolkitInfo: ToolkitInfo): Promise<CloudFormation.Parameter[]> {
async function prepareFileAsset(asset: AssetMetadataEntry, toolkitInfo: ToolkitInfo, contentType?: string): Promise<CloudFormation.Parameter[]> {
debug('Preparing file asset:', asset.path);

const data = await fs.readFile(asset.path);

const { key, changed } = await toolkitInfo.uploadIfChanged(data, {
s3KeyPrefix: 'assets/',
s3KeySuffix: path.extname(asset.path)
s3KeySuffix: path.extname(asset.path),
contentType
});

const s3url = `s3://${toolkitInfo.bucketName}/${key}`;
if (changed) {
success(` 👜 Asset ${asset.path} uploaded to ${s3url}`);
} else {
success(` 👜 Asset ${asset.path} already exists in ${s3url}`);
success(` 👜 Asset ${asset.path} is already up-to-date in ${s3url}`);
}

return [
Expand Down

0 comments on commit 0763282

Please sign in to comment.