@dotcom-tool-kit/serverless 4.0.1 → 4.0.2-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/schema.js +1 -0
- package/lib/schema.js.map +1 -0
- package/lib/tasks/deploy.js +1 -0
- package/lib/tasks/deploy.js.map +1 -0
- package/lib/tasks/provision.d.ts +1 -0
- package/lib/tasks/provision.d.ts.map +1 -1
- package/lib/tasks/provision.js +11 -9
- package/lib/tasks/provision.js.map +1 -0
- package/lib/tasks/run.js +1 -0
- package/lib/tasks/run.js.map +1 -0
- package/lib/tasks/teardown.d.ts.map +1 -1
- package/lib/tasks/teardown.js +7 -5
- package/lib/tasks/teardown.js.map +1 -0
- package/package.json +3 -2
- package/src/schema.ts +40 -0
- package/src/tasks/deploy.ts +36 -0
- package/src/tasks/provision.ts +62 -0
- package/src/tasks/run.ts +87 -0
- package/src/tasks/teardown.ts +50 -0
package/lib/schema.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;AAAA,+CAAwB;AAExB,oDAAgD;AAEhD,MAAM,kBAAkB,GAAG,CACzB,MAAc,EACd,IAAY,EACZ,OAAO,GAAG,MAAM,EAChB,EAAE,CACF;IACE,CAAC,OAAU,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,IAAI,OAAO,CAAC;IACpC;QACE,OAAO,EAAE,cAAc,eAAM,CAAC,IAAI,CAAC,MAAM,CAAC,iBAAiB,eAAM,CAAC,IAAI,CACpE,iBAAiB,eAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,OAAO,EAAE,CAChD,EAAE;KACJ;CACO,CAAA;AAEZ,kBAAe,CAAC;KACb,MAAM,CAAC;IACN,YAAY,EAAE,CAAC;SACZ,MAAM,EAAE;SACR,QAAQ,CACP,gIAAgI,CACjI;IACH,UAAU,EAAE,CAAC,CAAC,MAAM,EA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AAA,qBAAK,EAAC,YAAY,EAAE,IAAI,EAAE;gBACtC,GAAG,EAAE,OAAO,CAAC,GAAG;gBAChB,GAAG;aACJ,CAAC,CAAA;YAEF,IAAA,iBAAQ,EAAC,IAAI,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,CAAA;YAC1C,MAAM,IAAA,mBAAU,EAAC,YAAY,EAAE,KAAK,CAAC,CAAA;QACvC,CAAC;IACH,CAAC;;AA7BH,mCA8BC"}
|
package/lib/tasks/provision.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provision.d.ts","sourceRoot":"","sources":["../../src/tasks/provision.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAG5D,OAAO,KAAK,gBAAgB,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"provision.d.ts","sourceRoot":"","sources":["../../src/tasks/provision.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAG5D,OAAO,KAAK,gBAAgB,MAAM,WAAW,CAAA;AAG7C,MAAM,CAAC,OAAO,OAAO,mBAAoB,SAAQ,IAAI,CAAC;IAAE,MAAM,EAAE,OAAO,gBAAgB,CAAA;CAAE,CAAC;IACxF,MAAM,CAAC,WAAW,SAA2C;IAEvD,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CA8ClD;AAED,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,UAEzC"}
|
package/lib/tasks/provision.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stageName = stageName;
|
|
3
4
|
const error_1 = require("@dotcom-tool-kit/error");
|
|
4
5
|
const logger_1 = require("@dotcom-tool-kit/logger");
|
|
5
6
|
const base_1 = require("@dotcom-tool-kit/base");
|
|
6
7
|
const child_process_1 = require("child_process");
|
|
7
8
|
const state_1 = require("@dotcom-tool-kit/state");
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
8
10
|
class ServerlessProvision extends base_1.Task {
|
|
9
11
|
static description = 'Provision a review serverless function';
|
|
10
12
|
async run({ cwd }) {
|
|
@@ -13,19 +15,18 @@ class ServerlessProvision extends base_1.Task {
|
|
|
13
15
|
if (!ciState) {
|
|
14
16
|
throw new error_1.ToolKitError(`the ${logger_1.styles.task('ServerlessDeploy')} should be run in CI, but no CI state was found. check you have a plugin installed that initialises the CI state.`);
|
|
15
17
|
}
|
|
16
|
-
const
|
|
17
|
-
if (!
|
|
18
|
-
const error = new error_1.ToolKitError(`the ${logger_1.styles.task('ServerlessDeploy')} requires a CI
|
|
19
|
-
error.details = `this is provided by plugins such as ${logger_1.styles.plugin('circleci')}, which populates it from the
|
|
18
|
+
const ciBranch = ciState?.branch;
|
|
19
|
+
if (!ciBranch) {
|
|
20
|
+
const error = new error_1.ToolKitError(`the ${logger_1.styles.task('ServerlessDeploy')} requires a CI branch in the CI state.`);
|
|
21
|
+
error.details = `this is provided by plugins such as ${logger_1.styles.plugin('circleci')}, which populates it from the CIRCLE_BRANCH environment variable.`;
|
|
20
22
|
}
|
|
21
|
-
const stageName = `ci${buildNum}`;
|
|
22
23
|
this.logger.verbose('starting the child serverless process...');
|
|
23
24
|
const args = [
|
|
24
25
|
'deploy',
|
|
25
26
|
'--region',
|
|
26
27
|
regions[0],
|
|
27
28
|
'--stage',
|
|
28
|
-
stageName,
|
|
29
|
+
stageName(ciBranch),
|
|
29
30
|
'--aws-profile',
|
|
30
31
|
`CircleCI-role-${systemCode}`
|
|
31
32
|
];
|
|
@@ -38,9 +39,10 @@ class ServerlessProvision extends base_1.Task {
|
|
|
38
39
|
});
|
|
39
40
|
(0, logger_1.hookFork)(this.logger, 'serverless', child);
|
|
40
41
|
await (0, logger_1.waitOnExit)('serverless', child);
|
|
41
|
-
(0, state_1.writeState)('review', {
|
|
42
|
-
stageName
|
|
43
|
-
});
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
exports.default = ServerlessProvision;
|
|
45
|
+
function stageName(ciBranch) {
|
|
46
|
+
return (0, crypto_1.createHash)('sha256').update(ciBranch).digest('hex').slice(0, 6);
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=provision.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provision.js","sourceRoot":"","sources":["../../src/tasks/provision.ts"],"names":[],"mappings":";;AA2DA,8BAEC;AA7DD,kDAAqD;AACrD,oDAAsE;AACtE,gDAA4D;AAC5D,iDAAqC;AACrC,kDAAkD;AAElD,mCAAmC;AAEnC,MAAqB,mBAAoB,SAAQ,WAAyC;IACxF,MAAM,CAAC,WAAW,GAAG,wCAAwC,CAAA;IAE7D,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,EAAkB;QAC/B,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,aAAa,CAAA;QAC9D,MAAM,OAAO,GAAG,IAAA,iBAAS,EAAC,IAAI,CAAC,CAAA;QAE/B,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,oBAAY,CACpB,OAAO,eAAM,CAAC,IAAI,CAChB,kBAAkB,CACnB,mHAAmH,CACrH,CAAA;QACH,CAAC;QAED,MAAM,QAAQ,GAAG,OAAO,EAAE,MAAM,CAAA;QAEhC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,KAAK,GAAG,IAAI,oBAAY,CAC5B,OAAO,eAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,wCAAwC,CAC/E,CAAA;YAED,KAAK,CAAC,OAAO,GAAG,uCAAuC,eAAM,CAAC,MAAM,CAClE,UAAU,CACX,mEAAmE,CAAA;QACtE,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,0C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AG,UAAU;gBACb,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE;gBACrB,GAAG,OAAO,CAAC,GAAG;aACf;YACD,GAAG;SACJ,CAAC,CAAA;QAEF,IAAA,iBAAQ,EAAC,IAAI,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QAE/C,MAAM,MAAM,GAAG,IAAA,oBAAW,EAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;QACpD,IAAI,CAAC;YACH,MAAM,IAAA,mBAAQ,EAAC;gBACb,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,IAAI;aACX,CAAC,CAAA;QACJ,CAAC;gBAAS,CAAC;YACT,MAAM,EAAE,CAAA;QACV,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YACvE,0EAA0E;YAC1E,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;CACF;AA9DD,gCA8DC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"teardown.d.ts","sourceRoot":"","sources":["../../src/tasks/teardown.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"teardown.d.ts","sourceRoot":"","sources":["../../src/tasks/teardown.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAG5D,OAAO,KAAK,gBAAgB,MAAM,WAAW,CAAA;AAG7C,MAAM,CAAC,OAAO,OAAO,kBAAmB,SAAQ,IAAI,CAAC;IAAE,MAAM,EAAE,OAAO,gBAAgB,CAAA;CAAE,CAAC;IACvF,MAAM,CAAC,WAAW,SAA4C;IAExD,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAuClD"}
|
package/lib/tasks/teardown.js
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const error_1 = require("@dotcom-tool-kit/error");
|
|
4
3
|
const logger_1 = require("@dotcom-tool-kit/logger");
|
|
5
4
|
const base_1 = require("@dotcom-tool-kit/base");
|
|
6
5
|
const state_1 = require("@dotcom-tool-kit/state");
|
|
7
6
|
const child_process_1 = require("child_process");
|
|
7
|
+
const provision_1 = require("./provision");
|
|
8
8
|
class ServerlessTeardown extends base_1.Task {
|
|
9
9
|
static description = 'Tear down existing serverless functions';
|
|
10
10
|
async run({ cwd }) {
|
|
11
11
|
const { configPath, regions, systemCode } = this.pluginOptions;
|
|
12
|
-
const
|
|
13
|
-
if (!
|
|
14
|
-
throw new
|
|
12
|
+
const ciState = (0, state_1.readState)('ci');
|
|
13
|
+
if (!ciState || !ciState.branch) {
|
|
14
|
+
throw new Error(`Couldn't get CI state to generate the hashed branch name. Make sure this task is running in CI and you have a Tool Kit plugin that provides CI state, such as ${logger_1.styles.plugin('@dotcom-tool-kit/circleci')}, installed.`);
|
|
15
15
|
}
|
|
16
|
+
const ciBranch = ciState.branch;
|
|
16
17
|
this.logger.verbose('starting the child serverless process...');
|
|
17
18
|
const args = [
|
|
18
19
|
'remove',
|
|
19
20
|
'--region',
|
|
20
21
|
regions[0],
|
|
21
22
|
'--stage',
|
|
22
|
-
|
|
23
|
+
(0, provision_1.stageName)(ciBranch),
|
|
23
24
|
'--aws-profile',
|
|
24
25
|
`CircleCI-role-${systemCode}`
|
|
25
26
|
];
|
|
@@ -35,3 +36,4 @@ class ServerlessTeardown extends base_1.Task {
|
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
exports.default = ServerlessTeardown;
|
|
39
|
+
//# sourceMappingURL=teardown.js.map
|
|
@@ -0,0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AAM,IAAA,mBAAU,EAAC,YAAY,EAAE,KAAK,CAAC,CAAA;IACvC,CAAC;;AAzCH,qCA0CC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcom-tool-kit/serverless",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2-beta.2",
|
|
4
4
|
"description": "a plugin to manage and deploy apps using AWS Serverless",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"scripts": {
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/serverless",
|
|
19
19
|
"files": [
|
|
20
20
|
"/lib",
|
|
21
|
+
"/src",
|
|
21
22
|
".toolkitrc.yml"
|
|
22
23
|
],
|
|
23
24
|
"peerDependencies": {
|
|
@@ -38,4 +39,4 @@
|
|
|
38
39
|
"engines": {
|
|
39
40
|
"node": ">=20.x"
|
|
40
41
|
}
|
|
41
|
-
}
|
|
42
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as z from 'zod'
|
|
2
|
+
|
|
3
|
+
import { styles } from '@dotcom-tool-kit/logger'
|
|
4
|
+
|
|
5
|
+
const movedPluginOptions = <T extends Record<string, unknown>>(
|
|
6
|
+
option: string,
|
|
7
|
+
task: string,
|
|
8
|
+
newName = option
|
|
9
|
+
) =>
|
|
10
|
+
[
|
|
11
|
+
(options: T) => !(option in options),
|
|
12
|
+
{
|
|
13
|
+
message: `the option ${styles.code(option)} has moved to ${styles.code(
|
|
14
|
+
`options.tasks.${styles.task(task)}.${newName}`
|
|
15
|
+
)}`
|
|
16
|
+
}
|
|
17
|
+
] as const
|
|
18
|
+
|
|
19
|
+
export default z
|
|
20
|
+
.object({
|
|
21
|
+
awsAccountId: z
|
|
22
|
+
.string()
|
|
23
|
+
.describe(
|
|
24
|
+
'the ID of the AWS account you wish to deploy to (account IDs can be found at the [FT login page](https://awslogin.in.ft.com/))'
|
|
25
|
+
),
|
|
26
|
+
systemCode: z.string().describe('the system code for your app'),
|
|
27
|
+
regions: z
|
|
28
|
+
.array(z.string())
|
|
29
|
+
.default(['eu-west-1'])
|
|
30
|
+
.describe('an array of AWS regions you want to deploy to'),
|
|
31
|
+
configPath: z
|
|
32
|
+
.string()
|
|
33
|
+
.optional()
|
|
34
|
+
.describe(
|
|
35
|
+
'path to your serverless config file. If this is not provided, Serverless defaults to `./serverless.yml` but [other config fomats are accepted](https://www.serverless.com/framework/docs/providers/aws/guide/intro#alternative-configuration-format)'
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
.passthrough()
|
|
39
|
+
.refine(...movedPluginOptions('useVault', 'ServerlessRun', 'useDoppler'))
|
|
40
|
+
.refine(...movedPluginOptions('ports', 'ServerlessRun'))
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { hookFork, waitOnExit } from '@dotcom-tool-kit/logger'
|
|
2
|
+
import { Task, TaskRunContext } from '@dotcom-tool-kit/base'
|
|
3
|
+
import { spawn } from 'child_process'
|
|
4
|
+
import type ServerlessSchema from '../schema'
|
|
5
|
+
|
|
6
|
+
export default class ServerlessDeploy extends Task<{ plugin: typeof ServerlessSchema }> {
|
|
7
|
+
static description = 'Deploy a serverless function'
|
|
8
|
+
|
|
9
|
+
async run({ cwd }: TaskRunContext): Promise<void> {
|
|
10
|
+
const { configPath, regions, systemCode } = this.pluginOptions
|
|
11
|
+
|
|
12
|
+
for (const region of regions) {
|
|
13
|
+
this.logger.verbose('starting the child serverless process...')
|
|
14
|
+
const args = [
|
|
15
|
+
'deploy',
|
|
16
|
+
'--region',
|
|
17
|
+
region,
|
|
18
|
+
'--stage',
|
|
19
|
+
'prod',
|
|
20
|
+
'--aws-profile',
|
|
21
|
+
`CircleCI-role-${systemCode}`
|
|
22
|
+
]
|
|
23
|
+
if (configPath) {
|
|
24
|
+
args.push('--config', configPath)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const child = spawn('serverless', args, {
|
|
28
|
+
env: process.env,
|
|
29
|
+
cwd
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
hookFork(this.logger, 'serverless', child)
|
|
33
|
+
await waitOnExit('serverless', child)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ToolKitError } from '@dotcom-tool-kit/error'
|
|
2
|
+
import { hookFork, styles, waitOnExit } from '@dotcom-tool-kit/logger'
|
|
3
|
+
import { Task, TaskRunContext } from '@dotcom-tool-kit/base'
|
|
4
|
+
import { spawn } from 'child_process'
|
|
5
|
+
import { readState } from '@dotcom-tool-kit/state'
|
|
6
|
+
import type ServerlessSchema from '../schema'
|
|
7
|
+
import { createHash } from 'crypto'
|
|
8
|
+
|
|
9
|
+
export default class ServerlessProvision extends Task<{ plugin: typeof ServerlessSchema }> {
|
|
10
|
+
static description = 'Provision a review serverless function'
|
|
11
|
+
|
|
12
|
+
async run({ cwd }: TaskRunContext): Promise<void> {
|
|
13
|
+
const { configPath, systemCode, regions } = this.pluginOptions
|
|
14
|
+
const ciState = readState('ci')
|
|
15
|
+
|
|
16
|
+
if (!ciState) {
|
|
17
|
+
throw new ToolKitError(
|
|
18
|
+
`the ${styles.task(
|
|
19
|
+
'ServerlessDeploy'
|
|
20
|
+
)} should be run in CI, but no CI state was found. check you have a plugin installed that initialises the CI state.`
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const ciBranch = ciState?.branch
|
|
25
|
+
|
|
26
|
+
if (!ciBranch) {
|
|
27
|
+
const error = new ToolKitError(
|
|
28
|
+
`the ${styles.task('ServerlessDeploy')} requires a CI branch in the CI state.`
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
error.details = `this is provided by plugins such as ${styles.plugin(
|
|
32
|
+
'circleci'
|
|
33
|
+
)}, which populates it from the CIRCLE_BRANCH environment variable.`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.logger.verbose('starting the child serverless process...')
|
|
37
|
+
const args = [
|
|
38
|
+
'deploy',
|
|
39
|
+
'--region',
|
|
40
|
+
regions[0],
|
|
41
|
+
'--stage',
|
|
42
|
+
stageName(ciBranch),
|
|
43
|
+
'--aws-profile',
|
|
44
|
+
`CircleCI-role-${systemCode}`
|
|
45
|
+
]
|
|
46
|
+
if (configPath) {
|
|
47
|
+
args.push('--config', './serverless.yml')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const child = spawn('serverless', args, {
|
|
51
|
+
env: process.env,
|
|
52
|
+
cwd
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
hookFork(this.logger, 'serverless', child)
|
|
56
|
+
await waitOnExit('serverless', child)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function stageName(ciBranch: string) {
|
|
61
|
+
return createHash('sha256').update(ciBranch).digest('hex').slice(0, 6)
|
|
62
|
+
}
|
package/src/tasks/run.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Task, TaskRunContext } from '@dotcom-tool-kit/base'
|
|
2
|
+
import type ServerlessSchema from '../schema'
|
|
3
|
+
import { ChildProcess, spawn } from 'child_process'
|
|
4
|
+
import { DopplerEnvVars } from '@dotcom-tool-kit/doppler'
|
|
5
|
+
import { hookConsole, hookFork } from '@dotcom-tool-kit/logger'
|
|
6
|
+
import getPort from 'get-port'
|
|
7
|
+
import waitPort from 'wait-port'
|
|
8
|
+
import * as z from 'zod'
|
|
9
|
+
|
|
10
|
+
const ServerlessRunSchema = z
|
|
11
|
+
.object({
|
|
12
|
+
ports: z
|
|
13
|
+
.number()
|
|
14
|
+
.array()
|
|
15
|
+
.default([3001, 3002, 3003])
|
|
16
|
+
.describe('ports to try to bind to for this application'),
|
|
17
|
+
useDoppler: z
|
|
18
|
+
.boolean()
|
|
19
|
+
.default(true)
|
|
20
|
+
.describe('run the application with environment variables from Doppler')
|
|
21
|
+
})
|
|
22
|
+
.describe('Run serverless functions locally')
|
|
23
|
+
export { ServerlessRunSchema as schema }
|
|
24
|
+
|
|
25
|
+
export default class ServerlessRun extends Task<{
|
|
26
|
+
task: typeof ServerlessRunSchema
|
|
27
|
+
plugin: typeof ServerlessSchema
|
|
28
|
+
}> {
|
|
29
|
+
child?: ChildProcess
|
|
30
|
+
|
|
31
|
+
async run({ cwd, config }: TaskRunContext): Promise<void> {
|
|
32
|
+
const { useDoppler, ports } = this.options
|
|
33
|
+
const { configPath } = this.pluginOptions
|
|
34
|
+
|
|
35
|
+
let dopplerEnv = {}
|
|
36
|
+
|
|
37
|
+
if (useDoppler) {
|
|
38
|
+
const doppler = new DopplerEnvVars(
|
|
39
|
+
this.logger,
|
|
40
|
+
'dev',
|
|
41
|
+
config.pluginOptions['@dotcom-tool-kit/doppler']?.options
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
dopplerEnv = await doppler.get()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const port =
|
|
48
|
+
Number(process.env.PORT) ||
|
|
49
|
+
(await getPort({
|
|
50
|
+
port: ports
|
|
51
|
+
}))
|
|
52
|
+
|
|
53
|
+
this.logger.verbose('starting the child serverless process...')
|
|
54
|
+
const args = ['offline', 'start', '--host', 'local.ft.com', '--httpPort', `${port}`]
|
|
55
|
+
if (configPath) {
|
|
56
|
+
args.push('--config', './serverless.yml')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.child = spawn('serverless', args, {
|
|
60
|
+
env: {
|
|
61
|
+
...dopplerEnv,
|
|
62
|
+
PORT: port.toString(),
|
|
63
|
+
...process.env
|
|
64
|
+
},
|
|
65
|
+
cwd
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
hookFork(this.logger, 'serverless', this.child)
|
|
69
|
+
|
|
70
|
+
const unhook = hookConsole(this.logger, 'wait-port')
|
|
71
|
+
try {
|
|
72
|
+
await waitPort({
|
|
73
|
+
host: 'localhost',
|
|
74
|
+
port: port
|
|
75
|
+
})
|
|
76
|
+
} finally {
|
|
77
|
+
unhook()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async stop() {
|
|
82
|
+
if (this.child && (this.child.exitCode === null || !this.child.killed)) {
|
|
83
|
+
// SIGINT instead of SIGKILL so the process gets chance to exit gracefully
|
|
84
|
+
this.child.kill('SIGINT')
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { hookFork, styles, waitOnExit } from '@dotcom-tool-kit/logger'
|
|
2
|
+
import { Task, TaskRunContext } from '@dotcom-tool-kit/base'
|
|
3
|
+
import { readState } from '@dotcom-tool-kit/state'
|
|
4
|
+
import { spawn } from 'child_process'
|
|
5
|
+
import type ServerlessSchema from '../schema'
|
|
6
|
+
import { stageName } from './provision'
|
|
7
|
+
|
|
8
|
+
export default class ServerlessTeardown extends Task<{ plugin: typeof ServerlessSchema }> {
|
|
9
|
+
static description = 'Tear down existing serverless functions'
|
|
10
|
+
|
|
11
|
+
async run({ cwd }: TaskRunContext): Promise<void> {
|
|
12
|
+
const { configPath, regions, systemCode } = this.pluginOptions
|
|
13
|
+
|
|
14
|
+
const ciState = readState('ci')
|
|
15
|
+
|
|
16
|
+
if (!ciState || !ciState.branch) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`Couldn't get CI state to generate the hashed branch name. Make sure this task is running in CI and you have a Tool Kit plugin that provides CI state, such as ${styles.plugin(
|
|
19
|
+
'@dotcom-tool-kit/circleci'
|
|
20
|
+
)}, installed.`
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const ciBranch = ciState.branch
|
|
25
|
+
|
|
26
|
+
this.logger.verbose('starting the child serverless process...')
|
|
27
|
+
|
|
28
|
+
const args = [
|
|
29
|
+
'remove',
|
|
30
|
+
'--region',
|
|
31
|
+
regions[0],
|
|
32
|
+
'--stage',
|
|
33
|
+
stageName(ciBranch),
|
|
34
|
+
'--aws-profile',
|
|
35
|
+
`CircleCI-role-${systemCode}`
|
|
36
|
+
]
|
|
37
|
+
if (configPath) {
|
|
38
|
+
args.push('--config', './serverless.yml')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const child = spawn('serverless', args, {
|
|
42
|
+
env: process.env,
|
|
43
|
+
cwd
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
hookFork(this.logger, 'serverless', child)
|
|
47
|
+
|
|
48
|
+
await waitOnExit('serverless', child)
|
|
49
|
+
}
|
|
50
|
+
}
|