-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update cdk migrate command to allow for stack name input and a…
…dditional languages (#27134) This change updates use of noctilucent to cdk-from-cfn and installs it from npm. It also adds some additional language support and stack renaming. Not included in this PR: 1. Automated testing - this PR has been manually tested and automated tests will be added in a subsequent PR. 2. Go support - there are still some bugs to work out with go (in cdk-from-cfn) so implementation for it will be added after that's been worked out. 3. The option to compress the app to a zip file 4. The other TODOs listed in the `migrate` file. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
dc87d28
commit 123ab70
Showing
30 changed files
with
172 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,32 @@ OTHER DEALINGS IN THE SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected] - https://www.npmjs.com/package/archiver-utils/v/3.0.4 | MIT | ||
Copyright (c) 2015 Chris Talkington. | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
---------------- | ||
|
||
** [email protected] - https://www.npmjs.com/package/archiver/v/5.3.2 | MIT | ||
Copyright (c) 2012-2014 Chris Talkington, contributors. | ||
|
||
|
@@ -238,7 +264,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH RE | |
|
||
---------------- | ||
|
||
** aws-sdk@2.1447.0 - https://www.npmjs.com/package/aws-sdk/v/2.1447.0 | Apache-2.0 | ||
** aws-sdk@2.1451.0 - https://www.npmjs.com/package/aws-sdk/v/2.1451.0 | Apache-2.0 | ||
AWS SDK for JavaScript | ||
Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
|
@@ -433,6 +459,10 @@ The above copyright notice and this permission notice shall be included in all c | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
|
||
---------------- | ||
|
||
** [email protected] - https://www.npmjs.com/package/cdk-from-cfn/v/0.11.0 | MIT OR Apache-2.0 | ||
|
||
---------------- | ||
|
||
** [email protected] - https://www.npmjs.com/package/chalk/v/4.1.2 | MIT | ||
|
@@ -532,7 +562,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |
|
||
---------------- | ||
|
||
** [email protected].1 - https://www.npmjs.com/package/compress-commons/v/4.1.1 | MIT | ||
** [email protected].2 - https://www.npmjs.com/package/compress-commons/v/4.1.2 | MIT | ||
Copyright (c) 2014 Chris Talkington, contributors. | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
|
@@ -811,7 +841,7 @@ IN THE SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected].2 - https://www.npmjs.com/package/crc32-stream/v/4.0.2 | MIT | ||
** [email protected].3 - https://www.npmjs.com/package/crc32-stream/v/4.0.3 | MIT | ||
Copyright (c) 2014 Chris Talkington, contributors. | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
|
@@ -1367,7 +1397,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected].1 - https://www.npmjs.com/package/https-proxy-agent/v/7.0.1 | MIT | ||
** [email protected].2 - https://www.npmjs.com/package/https-proxy-agent/v/7.0.2 | MIT | ||
|
||
---------------- | ||
|
||
|
@@ -2134,7 +2164,7 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected].0 - https://www.npmjs.com/package/pac-proxy-agent/v/7.0.0 | MIT | ||
** [email protected].1 - https://www.npmjs.com/package/pac-proxy-agent/v/7.0.1 | MIT | ||
|
||
---------------- | ||
|
||
|
@@ -2224,7 +2254,7 @@ THE SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected].0 - https://www.npmjs.com/package/proxy-agent/v/6.3.0 | MIT | ||
** [email protected].1 - https://www.npmjs.com/package/proxy-agent/v/6.3.1 | MIT | ||
|
||
---------------- | ||
|
||
|
@@ -2793,7 +2823,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected].1 - https://www.npmjs.com/package/socks-proxy-agent/v/8.0.1 | MIT | ||
** [email protected].2 - https://www.npmjs.com/package/socks-proxy-agent/v/8.0.2 | MIT | ||
|
||
---------------- | ||
|
||
|
@@ -3375,7 +3405,7 @@ THE SOFTWARE. | |
|
||
---------------- | ||
|
||
** [email protected].0 - https://www.npmjs.com/package/zip-stream/v/4.1.0 | MIT | ||
** [email protected].1 - https://www.npmjs.com/package/zip-stream/v/4.1.1 | MIT | ||
Copyright (c) 2014 Chris Talkington, contributors. | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc'); | ||
baseConfig.ignorePatterns.push('lib/init-templates/**/typescript/**/*.ts'); | ||
baseConfig.ignorePatterns.push('test/integ/cli/sam_cdk_integ_app/**/*.ts'); | ||
baseConfig.ignorePatterns.push('vendor/noctilucent/**/*.ts'); | ||
baseConfig.parserOptions.project = __dirname + '/tsconfig.json'; | ||
module.exports = baseConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,5 +30,3 @@ tsconfig.json | |
junit.xml | ||
|
||
generate.sh | ||
lib/vendor/noctilucent/Dockerfile | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -459,6 +459,10 @@ The above copyright notice and this permission notice shall be included in all c | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
|
||
---------------- | ||
|
||
** [email protected] - https://www.npmjs.com/package/cdk-from-cfn/v/0.11.0 | MIT OR Apache-2.0 | ||
|
||
---------------- | ||
|
||
** [email protected] - https://www.npmjs.com/package/chalk/v/4.1.2 | MIT | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,78 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { initializeProject, availableInitTemplates } from '../../lib/init'; | ||
import * as cdk_from_cfn from 'cdk-from-cfn'; | ||
import { cliInit } from '../../lib/init'; | ||
import { warning } from '../logging'; | ||
import * as nocti from '../vendor/noctilucent'; | ||
|
||
/* eslint-disable @typescript-eslint/no-var-requires */ // Packages don't have @types module | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
const camelCase = require('camelcase'); | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
const decamelize = require('decamelize'); | ||
|
||
/** The list of languages supported by the built-in noctilucent binary. */ | ||
export const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = nocti.supported_languages(); | ||
export const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = cdk_from_cfn.supported_languages(); | ||
|
||
export async function cliMigrate( | ||
inputpath: string = process.cwd() + '/../template.txt', | ||
language = MIGRATE_SUPPORTED_LANGUAGES[0], | ||
generateOnly = false, | ||
outputpath = process.cwd(), | ||
) { | ||
warning('This is an experimental feature. We make no guarantees about the outcome or stability of the functionality.'); | ||
const type = 'default'; // "default" is the default type (and maps to 'app') | ||
const template = (await availableInitTemplates()).find(t => t.hasName(type!)); | ||
if (!template) { | ||
throw new Error(`couldn't find template for ${type} app type, this should never happen`); | ||
} | ||
export interface CliMigrateOptions { | ||
readonly stackName: string; | ||
readonly language?: string; | ||
readonly fromPath?: string; | ||
readonly outputPath?: string; | ||
} | ||
|
||
if (!MIGRATE_SUPPORTED_LANGUAGES.includes(language)) { | ||
throw new Error(`Unsupported language for cdk migrate: ${language}. Supported languages are: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`); | ||
} | ||
export async function cliMigrate(options: CliMigrateOptions) { | ||
warning('This is an experimental feature. We make no guarantees about the outcome or stability of the functionality.'); | ||
|
||
await initializeProject(template, language, true, generateOnly, outputpath); | ||
const template_file = fs.readFileSync(inputpath, 'utf8'); | ||
const generated_app = nocti.transmute(template_file, language); | ||
// TODO: Validate stack name | ||
|
||
// clear out the init'd bin/lib files to replace with our own | ||
delete_files(outputpath + '/lib/'); | ||
const language = options.language ?? 'typescript'; | ||
const outputPath = path.join(options.outputPath ?? process.cwd(), options.stackName); | ||
|
||
// we hardcode everything to be called noctstack still so this works for now. | ||
// Will change this to be much smarter once we can change stack name in noct | ||
const bin_app = `#!/usr/bin/env node | ||
import 'source-map-support/register'; | ||
import * as cdk from 'aws-cdk-lib'; | ||
import { NoctStack } from '../lib/generated_stack'; | ||
const generatedStack = generateStack(options, language); | ||
const stackName = decamelize(options.stackName); | ||
|
||
const app = new cdk.App(); | ||
new NoctStack(app, 'NoctStack', { | ||
/* If you don't specify 'env', this stack will be environment-agnostic. | ||
* Account/Region-dependent features and context lookups will not work, | ||
* but a single synthesized template can be deployed anywhere. */ | ||
try { | ||
fs.rmSync(outputPath, { recursive: true, force: true }); | ||
fs.mkdirSync(outputPath, { recursive: true }); | ||
await cliInit('app', language, true, false, outputPath, options.stackName); | ||
|
||
/* Uncomment the next line to specialize this stack for the AWS Account | ||
* and Region that are implied by the current CLI configuration. */ | ||
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, | ||
let stackFileName: string; | ||
switch (language) { | ||
case 'typescript': | ||
stackFileName = `${outputPath}/lib/${stackName}-stack.ts`; | ||
break; | ||
case 'java': | ||
stackFileName = `${outputPath}/src/main/java/com/myorg/${camelCase(stackName, { pascalCase: true })}Stack.java`; | ||
break; | ||
case 'python': | ||
stackFileName = `${outputPath}/${stackName.replace(/-/g, '_')}/${stackName.replace(/-/g, '_')}_stack.py`; | ||
break; | ||
case 'csharp': | ||
stackFileName = `${outputPath}/src/${camelCase(stackName, { pascalCase: true })}/${camelCase(stackName, { pascalCase: true })}Stack.cs`; | ||
break; | ||
// TODO: Add Go support | ||
default: | ||
throw new Error(`${language} is not supported by CDK Migrate. Please choose from: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`); | ||
} | ||
fs.writeFileSync(stackFileName!, generatedStack); | ||
} catch (error) { | ||
fs.rmSync(outputPath, { recursive: true, force: true }); | ||
throw error; | ||
} | ||
|
||
/* Uncomment the next line if you know exactly what Account and Region you | ||
* want to deploy the stack to. */ | ||
// env: { account: '123456789012', region: 'us-east-1' }, | ||
} | ||
|
||
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */ | ||
});`; | ||
const myname = path.basename(outputpath); | ||
fs.writeFileSync(outputpath + '/lib/' + 'generated_stack.ts', generated_app); | ||
fs.writeFileSync(outputpath + '/bin/' + `${myname}.ts`, bin_app); | ||
function generateStack(options: CliMigrateOptions, language: string) { | ||
const stackName = `${camelCase(decamelize(options.stackName), { pascalCase: true })}Stack`; | ||
// We will add other options here in a future change. | ||
if (options.fromPath) { | ||
return fromPath(stackName, options.fromPath, language); | ||
} | ||
// TODO: replace with actual output for other options. | ||
return ''; | ||
} | ||
|
||
function delete_files(filepath: string) { | ||
fs.readdir(filepath, (err, files) => { | ||
if (err) throw err; | ||
for (const file of files) { | ||
fs.unlink(filepath + file, (cause) => { | ||
if (cause) throw cause; | ||
}); | ||
} | ||
}); | ||
function fromPath(stackName: string, inputPath: string, language: string): string { | ||
const templateFile = fs.readFileSync(inputPath, 'utf8'); | ||
return cdk_from_cfn.transmute(templateFile, language, stackName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.