-
Notifications
You must be signed in to change notification settings - Fork 274
/
.projenrc.ts
486 lines (428 loc) · 16.1 KB
/
.projenrc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env node
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { readFileSync } from "node:fs";
import { Project, YamlFile } from "projen";
import { AwsCdkTypeScriptApp } from "projen/lib/awscdk";
import {
Jest,
JestOptions,
JestReporter,
NodePackageManager,
Transform,
TypescriptConfigOptions,
UpdateSnapshot,
} from "projen/lib/javascript";
import { PythonProject } from "projen/lib/python";
function main() {
new InstanceScheduler({ version: "3.0.6", cdkVersion: "2.164.1" }).synth();
}
interface InstanceSchedulerProps {
readonly version: string;
readonly cdkVersion: string;
}
class InstanceScheduler extends AwsCdkTypeScriptApp {
private static readonly solutionId: string = "SO0030";
private static readonly solutionName: string = "instance-scheduler-on-aws";
private static readonly cdkContext: { [key: string]: any } = {
solutionId: this.solutionId,
solutionName: this.solutionName,
appRegApplicationName: "AWS-Solutions",
appRegSolutionName: this.solutionName,
"instance-scheduler-on-aws-pipeline-source": "codecommit",
};
private static readonly tsconfig: TypescriptConfigOptions = {
include: ["deployment/cdk-solution-helper/**/*.ts"],
compilerOptions: {
forceConsistentCasingInFileNames: true,
lib: ["es2022", "dom"],
noPropertyAccessFromIndexSignature: false,
noUncheckedIndexedAccess: false,
target: "ES2022",
outDir: "build/cdk.ts.dist",
rootDir: ".",
},
};
private static readonly prTemplate: string[] = readFileSync("projenrc/PULL_REQUEST_TEMPLATE.md")
.toString()
.split("\n");
private static readonly deps: string[] = [
"@aws-sdk/client-auto-scaling",
"@aws-sdk/client-cloudformation",
"@aws-sdk/client-docdb",
"@aws-sdk/client-dynamodb",
"@aws-sdk/util-dynamodb",
"@aws-sdk/client-ec2",
"@aws-sdk/client-lambda",
"@aws-sdk/client-neptune",
"@aws-sdk/client-rds",
"@aws-sdk/client-ssm",
"cdk-nag",
"source-map-support",
"uuid",
];
private static readonly devDeps: string[] = [
"@cdklabs/cdk-ssm-documents",
"@types/uuid",
"@typescript-eslint/eslint-plugin",
"eslint",
"eslint-config-prettier",
"eslint-plugin-header",
"eslint-plugin-import",
"eslint-plugin-prettier",
"jest-extended",
"jest-junit",
"ts-jest",
];
private static readonly testReportDir: string = "deployment/test-reports";
private static readonly coverageReportDir: string = "deployment/coverage-reports";
private static readonly gitignore: string[] = [
".idea/",
".vscode/",
".venv/",
"*.DS_Store",
"deployment/open-source/",
"deployment/global-s3-assets",
"deployment/regional-s3-assets",
"__pycache__/",
this.testReportDir,
this.coverageReportDir,
];
private static readonly jestConfigFile: string = "jest.config.json";
private static readonly testdir: string = "source/instance-scheduler/tests";
private static readonly jestOptions: JestOptions = {
junitReporting: false, // we will override
updateSnapshot: UpdateSnapshot.NEVER,
configFilePath: this.jestConfigFile,
jestConfig: {
reporters: [
new JestReporter("jest-junit", {
outputDirectory: this.testReportDir,
outputName: "cdk-test-report.xml",
}),
],
roots: [`<rootDir>/${this.testdir}`],
transform: { "^.+\\.tsx?$": new Transform("ts-jest") },
setupFilesAfterEnv: ["jest-extended/all"],
},
};
constructor(props: InstanceSchedulerProps) {
const authorName = "Amazon Web Services";
const license = "Apache-2.0";
super({
appEntrypoint: "instance-scheduler.ts",
cdkVersion: props.cdkVersion,
cdkVersionPinning: true,
context: InstanceScheduler.cdkContext,
cdkout: "build/cdk.out",
srcdir: "source",
testdir: InstanceScheduler.testdir,
eslint: false,
tsconfig: InstanceScheduler.tsconfig,
typescriptVersion: "~5.2.x", //@typescript-eslint/typescript-estree doesn't support 5.3.x yet
disableTsconfigDev: true,
projenrcTs: true,
defaultReleaseBranch: "main",
npmignoreEnabled: false,
pullRequestTemplateContents: InstanceScheduler.prTemplate,
gitignore: InstanceScheduler.gitignore,
jestOptions: InstanceScheduler.jestOptions,
githubOptions: { mergify: false, workflows: false },
name: InstanceScheduler.solutionName,
description: `Instance Scheduler on AWS (${InstanceScheduler.solutionId})`,
deps: InstanceScheduler.deps,
devDeps: InstanceScheduler.devDeps,
packageManager: NodePackageManager.NPM,
authorName,
authorUrl: "https://aws.amazon.com/solutions",
authorOrganization: true,
minNodeVersion: "18.0.0",
license,
});
// manage project versioning manually
this.overrideVersion(props.version);
// cdk deps should lock to the same version as cdk itself, so they must be specified separately
this.addDeps(...this.getCdkDeps(props.cdkVersion));
this.addTestTasks();
this.addTypescriptFiles("global.d.ts");
// adding to project props doesn't seem to work as expected
this.addJestMatch("**/*.test.ts");
// use default snapshot resolution
this.removeCustomSnapshotResolver();
this.addScripts({
"update-deps": "chmod +x ./update-all-dependencies.sh && exec ./update-all-dependencies.sh"
})
new YamlFile(this, "solution-manifest.yaml", {
obj: {
id: InstanceScheduler.solutionId,
name: InstanceScheduler.solutionName,
version: props.version,
cloudformation_templates: [
{ template: "instance-scheduler-on-aws.template", main_template: true },
{ template: "instance-scheduler-on-aws-remote.template" },
],
build_environment: { build_image: "aws/codebuild/standard:7.0" },
},
});
const homepage = "https://aws.amazon.com/solutions/implementations/instance-scheduler-on-aws/";
const commonPythonDevDeps = [
"black@^24.3.0",
"flake8@^6.1.0",
"isort@^5.12.0",
"mypy@^1.7.1",
"pytest@^7.4.3",
"pytest-cov@^4.1.0",
"tox@^4.11.4",
];
const commonPythonProjectOptions: CommonPythonProjectOptions = {
authorName,
version: props.version,
parent: this,
license,
homepage,
devDeps: commonPythonDevDeps,
};
new InstanceSchedulerLambdaFunction(commonPythonProjectOptions);
new InstanceSchedulerCli(commonPythonProjectOptions);
}
private overrideVersion(version: string): void {
const packageFile = this.tryFindObjectFile("package.json");
if (!packageFile) {
throw new Error("Error overriding package version");
}
packageFile.addOverride("version", version);
}
private getCdkDeps(cdkVersion: string): string[] {
return [
`@aws-cdk/aws-lambda-python-alpha@${cdkVersion}-alpha.0`,
`@aws-cdk/aws-servicecatalogappregistry-alpha@${cdkVersion}-alpha.0`,
`@aws-cdk/aws-neptune-alpha@${cdkVersion}-alpha.0`,
];
}
private addTestTasks(): void {
this.addE2ETestTask();
const prettierTask = this.addTask("test:prettier", { exec: "npx prettier --check ./**/*.ts" });
const eslintTask = this.addTask("test:eslint", { exec: "npx eslint --max-warnings=0 ." });
const updateSnapshotsTask = this.addTask("test:update-snapshots", {
exec: "jest --updateSnapshot --passWithNoTests --coverageProvider=v8 --ci",
});
const updateTask = this.tasks.tryFind("test:update");
if (!updateTask) {
throw new Error("Error adding subtasks to update task");
}
updateTask.reset();
updateTask.spawn(prettierTask);
updateTask.spawn(eslintTask);
updateTask.spawn(updateSnapshotsTask);
const baseJestCommand = "jest --coverageProvider=v8 --ci";
const cdkTestTask = this.addTask("test:cdk-tests", { exec: baseJestCommand });
const cdkTask = this.addTask("test:cdk");
cdkTask.spawn(prettierTask);
cdkTask.spawn(eslintTask);
cdkTask.spawn(cdkTestTask);
const baseToxCommand = "python -m tox --parallel --exit-and-dump-after 1200";
const appDir = "source/app";
const cliDir = "source/cli";
const appTestTask = this.addTask("test:app", {
cwd: appDir,
env: { TOX_PARALLEL_NO_SPINNER: "true" },
exec: baseToxCommand,
});
const cliTestTask = this.addTask("test:cli", {
cwd: cliDir,
env: { TOX_PARALLEL_NO_SPINNER: "true" },
exec: baseToxCommand,
});
const testTask = this.tasks.tryFind("test");
if (!testTask) {
throw new Error("Error adding subtasks to test task");
}
testTask.reset();
testTask.spawn(cdkTask);
testTask.spawn(appTestTask);
testTask.spawn(cliTestTask);
const testCiTask = this.addTask("test:ci");
const jestCoverageOptions = `--coverage --coverageDirectory ${InstanceScheduler.coverageReportDir}/cdk-coverage`;
const cdkTestCiTask = this.addTask("test:cdk-tests:ci", { exec: `${baseJestCommand} ${jestCoverageOptions}` });
const cdkCiTask = this.addTask("test:cdk:ci");
cdkCiTask.spawn(prettierTask);
cdkCiTask.spawn(eslintTask);
cdkCiTask.spawn(cdkTestCiTask);
const ciToxOptions = "--skip-missing-interpreters false";
const appPytestOptions = `--junitxml=../../${InstanceScheduler.testReportDir}/lambda-test-report.xml --cov --cov-report "xml:../../${InstanceScheduler.coverageReportDir}/lambda-coverage.xml"`;
const appReportFixupCommand = `sed -i -e "s|<source>.*</source>|<source>source/app/instance_scheduler</source>|g" ../../${InstanceScheduler.coverageReportDir}/lambda-coverage.xml`;
const appTestCiTask = this.addTask("test:app:ci", {
cwd: appDir,
env: { TOX_PARALLEL_NO_SPINNER: "true" },
exec: `${baseToxCommand} ${ciToxOptions} -- ${appPytestOptions} && ${appReportFixupCommand}`,
});
const cliPytestOptions = `--junitxml=../../${InstanceScheduler.testReportDir}/cli-test-report.xml --cov --cov-report "xml:../../${InstanceScheduler.coverageReportDir}/cli-coverage.xml"`;
const cliReportFixupCommand = `sed -i -e "s|<source>.*</source>|<source>source/cli/instance_scheduler_cli</source>|g" ../../${InstanceScheduler.coverageReportDir}/cli-coverage.xml`;
const cliTestCiTask = this.addTask("test:cli:ci", {
cwd: cliDir,
env: { TOX_PARALLEL_NO_SPINNER: "true" },
exec: `${baseToxCommand} ${ciToxOptions} -- ${cliPytestOptions} && ${cliReportFixupCommand}`,
});
testCiTask.spawn(cdkCiTask);
testCiTask.spawn(appTestCiTask);
testCiTask.spawn(cliTestCiTask);
}
private addE2ETestTask(): void {
const e2eConfigFile = "source/pipeline/jest.config.json";
new Jest(this, {
junitReporting: false, // we will override
updateSnapshot: UpdateSnapshot.NEVER,
configFilePath: e2eConfigFile,
jestConfig: {
reporters: [
new JestReporter("jest-junit", {
outputDirectory: InstanceScheduler.testReportDir,
outputName: "e2e-test-report.xml",
}),
],
roots: [`<rootDir>/e2e-tests`],
setupFilesAfterEnv: ["jest-extended/all"],
transform: { "^.+\\.tsx?$": new Transform("ts-jest") },
globalSetup: "./setup.ts",
},
});
this.addTask("e2e-tests", { exec: `jest --config ${e2eConfigFile}`, receiveArgs: true });
}
private addTypescriptFiles(...files: string[]): void {
const tsconfig = this.tryFindObjectFile("tsconfig.json");
if (!tsconfig) {
throw new Error("Error overriding tsconfig");
}
tsconfig.addOverride("files", files);
}
private addJestMatch(pattern: string): void {
if (!this.jest) {
throw new Error("Error overriding jest matcher");
}
this.jest.addTestMatch(pattern);
}
private removeCustomSnapshotResolver(): void {
const jestConfig = this.tryFindObjectFile(InstanceScheduler.jestConfigFile);
if (!jestConfig) {
throw new Error("Error overriding jest config");
}
jestConfig.addOverride("snapshotResolver", undefined);
}
}
interface CommonPythonProjectOptions {
readonly authorName: string;
readonly license: string;
readonly version: string;
readonly parent: Project;
readonly homepage: string;
readonly devDeps: string[];
}
class InstanceSchedulerLambdaFunction extends PythonProject {
constructor(options: CommonPythonProjectOptions) {
super({
authorEmail: "",
moduleName: "instance_scheduler",
name: "instance_scheduler",
outdir: "./source/app",
poetry: true,
description: "Instance Scheduler on AWS",
deps: ["python@^3.11"],
pytest: false,
...options,
});
const boto3StubsExtras = [
"autoscaling",
"cloudwatch",
"dynamodb",
"ec2",
"ecs",
"lambda",
"logs",
"rds",
"resourcegroupstaggingapi",
"sns",
"ssm",
"sts",
];
const motoExtras = ["autoscaling", "dynamodb", "ec2", "logs", "rds", "resourcegroupstaggingapi", "ssm"];
const boto3Version = "^1.34.1";
const jmespathVersion = "1.0.1";
const pythonDateutilVersion = "2.8.2";
const urllib3Version = "1.26.15";
[
`boto3@${boto3Version}`,
`boto3-stubs-lite@{version = "${boto3Version}", extras = ${JSON.stringify(boto3StubsExtras)}}`,
`botocore@${boto3Version}`,
"botocore-stubs@^1.31.66",
"freezegun@^1.3.1",
`jmespath@${jmespathVersion}`,
"pytest-mock@^3.12.0",
"pytest-runner@^6.0.1",
"pytest-xdist@^3.5.0",
`python-dateutil@${pythonDateutilVersion}`,
`moto@{version = "^5.0.2", extras = ${JSON.stringify(motoExtras)}}`,
"types-freezegun@^1.1.10",
`types-jmespath@${jmespathVersion}`,
`types-python-dateutil@${pythonDateutilVersion}`,
"[email protected]", // held back, need to support urllib3@^1
`types-urllib3@^${urllib3Version}`,
"tzdata@^2023.3",
`urllib3@^${urllib3Version}`,
].forEach((spec: string) => this.addDevDependency(spec));
[
"aws-lambda-powertools@^2.26.0",
"packaging@^24.0",
].forEach((spec: string) => this.addDependency(spec));
const pyproject = this.tryFindObjectFile("pyproject.toml");
if (!pyproject) {
throw new Error("Could not override pyproject.toml");
}
pyproject.addOverride("tool.poetry.authors", [options.authorName]);
const installTask = this.tasks.tryFind("install");
if (!installTask) {
throw new Error("Could not override install task");
}
installTask.reset();
installTask.exec("poetry lock --no-update && poetry install");
}
}
class InstanceSchedulerCli extends PythonProject {
constructor(options: CommonPythonProjectOptions) {
const boto3Version = "^1.34.1";
const jmespathVersion = "1.0.1";
super({
authorEmail: "",
moduleName: "instance_scheduler_cli",
name: "instance_scheduler_cli",
outdir: "./source/cli",
poetry: true,
description: "Instance Scheduler on AWS CLI",
deps: ["python@^3.8.1", `boto3@${boto3Version}`, `jmespath@^${jmespathVersion}`],
pytest: false,
...options,
});
const boto3StubsExtras = ["cloudformation", "lambda"];
const motoExtras = ["cloudformation", "lambda"];
[
`boto3-stubs-lite@{version = "${boto3Version}", extras = ${JSON.stringify(boto3StubsExtras)}}`,
"jsonschema@~4.17.3", // held back, 4.18.0 is a breaking change
`moto@{version = "^5.0.2", extras = ${JSON.stringify(motoExtras)}}`,
`types-jmespath@^${jmespathVersion}`,
"types-PyYAML@^6.0.12.12",
"[email protected]", // held back, need to support urllib3@^1
].forEach((spec: string) => this.addDevDependency(spec));
const pyproject = this.tryFindObjectFile("pyproject.toml");
if (!pyproject) {
throw new Error("Could not override pyproject.toml");
}
pyproject.addOverride("tool.poetry.authors", [options.authorName]);
pyproject.addOverride("tool.poetry.scripts.scheduler-cli", "instance_scheduler_cli:__main__");
const installTask = this.tasks.tryFind("install");
if (!installTask) {
throw new Error("Could not override install task");
}
installTask.reset();
installTask.exec("poetry lock --no-update && poetry install");
}
}
main();