-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.ts
446 lines (380 loc) · 15.1 KB
/
index.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
'use strict';
import { spawnSync, SpawnSyncOptions, SpawnSyncReturns } from 'child_process';
import { chmodSync, copySync, removeSync, writeFileSync } from 'fs-extra';
import * as path from 'path';
import Serverless, { FunctionDefinition, FunctionDefinitionHandler } from 'serverless';
import Service from 'serverless/classes/Service';
import * as AWSEnvironment from './AWSEnvironment';
import * as config from './config';
import * as ld from './ld';
import * as version from './version';
const PACKAGE_NAME = 'serverless-haskell';
const ADDITIONAL_EXCLUDE = [
'**/.stack-work/**',
'node_modules/**',
];
// Dependent libraries not to suggest adding
const IGNORE_LIBRARIES = [
'linux-vdso.so.1',
'/lib64/ld-linux-x86-64.so.2',
].concat(AWSEnvironment.libraries);
const BOOTSTRAP = '#!/bin/sh\nexec ${_HANDLER}';
const NO_OUTPUT_CAPTURE: SpawnSyncOptions = {stdio: ['ignore', process.stdout, process.stderr]};
const OUTPUT_CAPTURE: SpawnSyncOptions = {maxBuffer: 1024 * 1024 * 100};
type Custom = {
stackBuildArgs: string[];
docker: boolean;
buildAll: boolean;
};
type Schema = {
type: 'object';
properties: {
[key: string]: Schema;
};
} | {
type: 'array';
items: Schema;
} | {
type: 'string';
} | {
type: 'boolean';
};
const customSchema: Schema = {
type: 'object',
properties: {
haskell: {
type: 'object',
properties: {
stackBuildArgs: {
type: 'array',
items: {
type: 'string',
},
},
docker: {
type: 'boolean',
},
buildAll: {
type: 'boolean',
},
},
},
},
};
// FIXME: Service is missing 'package' property in @types/serverless
type ServiceEx = Service & {
package: {
exclude: string[];
excludeDevDependencies?: boolean;
};
}
// FIXME: Schema is not implemented in @types/serverless
type ConfigSchemaHandler = {
schema: {
definitions: {
[key: string]: {
enum?: string[];
};
};
};
defineCustomProperties(properties: Schema): void;
}
type ServerlessEx = Serverless & {
configSchemaHandler: ConfigSchemaHandler;
}
function isHandler(func: FunctionDefinition): func is FunctionDefinitionHandler {
return !!Object.prototype.hasOwnProperty.call(func, 'handler');
}
type Options = {
function?: string;
};
class ProcessError extends Error {
result: SpawnSyncReturns<Buffer>;
constructor(message: string, result: SpawnSyncReturns<Buffer>) {
super(message);
this.result = result;
Object.setPrototypeOf(this, new.target.prototype);
}
}
class ServerlessPlugin {
serverless: ServerlessEx;
service: ServiceEx;
options: Options;
hooks: { [hook: string]: (options: {}) => void };
servicePath: string;
additionalFiles: string[];
constructor(serverless: ServerlessEx, options: Options) {
this.serverless = serverless;
this.service = serverless.service as ServiceEx;
this.options = options;
if (this.serverless.service.provider.name !== "aws") {
throw new Error("Only AWS provider is supported.");
}
this.hooks = {
'before:package:createDeploymentArtifacts': this.buildHandlers.bind(this),
'after:package:createDeploymentArtifacts': this.cleanupHandlers.bind(this),
// deploy function
'before:deploy:function:packageFunction': this.buildHandlers.bind(this),
'after:deploy:function:packageFunction': this.cleanupHandlers.bind(this),
// invoke local
'before:invoke:local:invoke': this.buildHandlers.bind(this),
'after:invoke:local:invoke': this.cleanupHandlers.bind(this),
// serverless-offline
'before:offline:start:init': this.buildHandlers.bind(this),
'after:offline:start:end': this.cleanupHandlers.bind(this),
};
this.servicePath = this.serverless.config.servicePath || '';
// By default, Serverless examines node_modules to figure out which
// packages there are from dependencies versus devDependencies of a
// package. While there will always be a node_modules due to Serverless
// and this plugin being installed, it will be excluded anyway.
// Therefore, the filtering can be disabled to speed up the process.
this.service.package.excludeDevDependencies = false;
// Customize Serverless schema
const configSchemaHandler = this.serverless.configSchemaHandler;
// Add new possible runtime to the schema
configSchemaHandler.schema.definitions.awsLambdaRuntime.enum?.push("haskell");
// Add plugin options
configSchemaHandler.defineCustomProperties(customSchema);
this.additionalFiles = [];
}
custom(): Custom {
return Object.assign(
{
stackBuildArgs: [],
docker: true,
buildAll: true,
},
this.serverless.service.custom &&
this.serverless.service.custom.haskell ||
{}
);
}
runStack(directory: string, args: string[], options: {captureOutput?: boolean} = {}): SpawnSyncReturns<Buffer> {
options = options || {};
const envArgs = [];
if (this.custom().docker) {
envArgs.push('--docker');
envArgs.push('--docker-image', config.BUILD_DOCKER_IMAGE);
envArgs.push('--no-nix');
}
if (directory) {
envArgs.push('--stack-yaml', `${directory}stack.yaml`);
}
const stackArgs = [
...envArgs,
...this.custom().stackBuildArgs,
...args,
];
const result = spawnSync(
'stack',
stackArgs,
options.captureOutput ? OUTPUT_CAPTURE : NO_OUTPUT_CAPTURE
);
if (result.error || result.status) {
const reasons = [];
if (result.error) {
reasons.push(result.error);
}
if (result.status) {
reasons.push(`exit code: ${result.status}`);
}
const stderr = result.stderr?.toString().trim();
if (stderr) {
reasons.push(stderr);
}
const message = `Error when running Stack: ${reasons.join('; ')}\n` +
`Stack command: stack ${stackArgs.join(" ")}`;
throw new ProcessError(message, result);
}
return result;
}
runStackOutput(directory: string, args: string[]): string {
const result = this.runStack(directory, args, {captureOutput: true});
return result.stdout.toString().trim();
}
dependentLibraries(directory: string, executablePath: string): ld.Paths {
try {
const lddOutput = this.runStackOutput(
directory,
[
'exec',
'ldd',
executablePath,
]
);
return ld.parseLdOutput(lddOutput);
}
catch (error) {
if (error.result &&
error.result.stdout &&
error.result.stdout.includes("not a dynamic executable")) {
// Static executables have no dependencies
return {};
} else if (process.platform === 'darwin' && !this.custom().docker) {
// Even if ldd was available on macOS, the dependencies won't
// translate
return {};
} else {
throw error;
}
}
}
glibcVersion(directory: string, executablePath: string): version.Version | null {
const objdumpOutput = this.runStackOutput(
directory,
[
'exec',
'objdump',
'--',
'-T',
executablePath,
]
);
return ld.parseObjdumpOutput(objdumpOutput);
}
assertServerlessPackageVersionsMatch(directory: string): void {
// Check that the Haskell package version corresponds to our own
const stackDependencies = this.runStackOutput(
directory,
[
'ls',
'dependencies',
]
).split("\n");
const haskellPackageVersions = stackDependencies.filter(dep => dep.startsWith(`${PACKAGE_NAME} `));
if (haskellPackageVersions.length === 0) {
this.serverless.cli.log(`Could not find ${PACKAGE_NAME} in stack's dependencies. Make sure ${PACKAGE_NAME} you are using LTS 12 (or newer), or add it as an extra-dep in your stack.yaml, and reference it in package.yaml or the Cabal file.`);
throw new Error("Package not found.");
}
const haskellPackageVersion = haskellPackageVersions[0].split(' ')[1];
const javascriptPackageVersion = JSON.parse(spawnSync(
'npm',
[
'list',
PACKAGE_NAME,
'--json',
]
).stdout).dependencies[PACKAGE_NAME].version;
if (haskellPackageVersion !== javascriptPackageVersion) {
this.serverless.cli.log(`Package version mismatch: serverless-haskell installed from NPM: ${javascriptPackageVersion}, installed from Stack: ${haskellPackageVersion}. Versions must be in sync to work correctly. Please install matching versions of serverless-haskell from NPM and Stack by either pinning your NPM version to match stack, or adding an extra-dep in your stack.yaml to match the NPM version.`);
throw new Error("Package version mismatch.");
}
}
writeBootstrap(): void {
const bootstrapPath = path.resolve(this.servicePath, 'bootstrap');
writeFileSync(bootstrapPath, BOOTSTRAP);
chmodSync(bootstrapPath, 0o755);
this.additionalFiles.push(bootstrapPath);
}
// Which functions are being deployed now - all (default) or only one of
// them ('deploy function')
deployedFunctions(): string[] {
if (this.options.function) {
return [this.options.function];
} else {
return this.serverless.service.getAllFunctions();
}
}
buildHandlers(): void {
const service = this.service;
if (!this.custom().docker) {
// Warn when Docker is disabled
this.serverless.cli.log(
"Warning: not using Docker to build. " +
"The resulting binary might not match the AWS environment.");
}
// Exclude Haskell artifacts from uploading
service.package.exclude = service.package.exclude || [];
service.package.exclude = [
...service.package.exclude,
...ADDITIONAL_EXCLUDE,
];
// Keep track of which extra libraries were copied
const libraries: { [name: string]: boolean } = {};
let haskellFunctionsFound = false;
this.deployedFunctions().forEach(funcName => {
const func = service.getFunction(funcName);
// Only process Haskell functions
if (!isHandler(func)) {
return;
}
const runtime = func.runtime || service.provider.runtime;
if (runtime !== config.HASKELL_RUNTIME) {
return;
}
haskellFunctionsFound = true;
func.runtime = config.BASE_RUNTIME;
const handlerPattern = /(.*\/)?([^./]*)\.(.*)/;
const matches = handlerPattern.exec(func.handler);
if (!matches) {
throw new Error(`handler ${func.handler} was not of the form 'packageName.executableName' or 'dir1/dir2/packageName.executableName'.`);
}
const [, directory, packageName, executableName] = matches;
// Ensure package versions match
this.assertServerlessPackageVersionsMatch(directory);
// Ensure the executable is built
this.serverless.cli.log(`Building handler ${funcName} with Stack...`);
const buildCommand = this.custom().buildAll ?
['build'] :
['build', `${packageName}:exe:${executableName}`];
this.runStack(directory, buildCommand);
// Copy the executable to the destination directory
const stackInstallRoot = this.runStackOutput(
directory,
[
'path',
'--local-install-root',
]
);
const targetDirectory = directory ? directory : "./";
const executablePath = path.resolve(stackInstallRoot, 'bin', executableName);
const targetPath = path.resolve(this.servicePath, targetDirectory, executableName);
copySync(executablePath, targetPath);
this.additionalFiles.push(targetPath);
func.handler = targetDirectory + executableName;
// Check glibc version
const glibcVersion = this.glibcVersion(directory, executablePath);
if (glibcVersion && version.greater(glibcVersion, AWSEnvironment.glibcVersion)) {
this.serverless.cli.log(
"Warning: glibc version required by the executable (" + version.format(glibcVersion) + ") is " +
"higher than the one in AWS environment (" + version.format(AWSEnvironment.glibcVersion) + ").");
throw new Error("glibc version mismatch.");
}
// Copy libraries not present on AWS Lambda environment
const executableLibraries = this.dependentLibraries(directory, executablePath);
for (const name in executableLibraries) {
if (!libraries[name] && !IGNORE_LIBRARIES.includes(name)) {
const libPath = executableLibraries[name];
const libTargetPath = path.resolve(this.servicePath, name);
this.runStack(
directory,
[
'exec',
'cp',
libPath,
libTargetPath,
]);
this.additionalFiles.push(libTargetPath);
libraries[name] = true;
}
}
});
if (!this.options.function && !haskellFunctionsFound) {
throw new Error(
`Error: no Haskell functions found. ` +
`Use 'runtime: ${config.HASKELL_RUNTIME}' in global or ` +
`function configuration to use this plugin.`
);
}
this.writeBootstrap();
// Ensure the runtime is set to a sane value for other plugins
if (service.provider.runtime === config.HASKELL_RUNTIME) {
service.provider.runtime = config.BASE_RUNTIME;
}
}
cleanupHandlers(): void {
this.additionalFiles.forEach(fileName => removeSync(fileName));
}
}
module.exports = ServerlessPlugin;