-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
325 lines (261 loc) · 8.59 KB
/
index.js
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
import util from "util";
import cp from "child_process";
import prettyHrtime from "pretty-hrtime";
import chalk from "chalk";
import path from "path";
import plimit from "p-limit";
import AdmZip from "adm-zip";
import { readFileSync } from "fs";
// SupportedRuntimes Runtimes that are allowed to run Golang lambdas.
const supportedRuntimes = ["provided.al2"];
const logPrefix = "GoPlugin";
export default class Go {
constructor(serverless, options) {
this.readFileSync = readFileSync;
this.zip = new AdmZip();
this.exec = util.promisify(cp.exec);
this.serverless = serverless;
this.options = options || {};
this.defaultConfig = {
baseDir: ".",
binDir: ".bin",
env: {
CGO_ENABLED: "0",
GOOS: "linux",
},
cmd: 'go build -ldflags="-s -w"',
monorepo: false,
concurrency: 5,
};
this.config = this.getConfig();
this.hooks = {
"before:deploy:function:packageFunction": this.compileFunction.bind(this),
"before:package:createDeploymentArtifacts":
this.compileFunctions.bind(this),
// Because of https://github.com/serverless/serverless/blob/master/lib/plugins/aws/invokeLocal/index.js#L361
// plugin needs to compile a function and then ignore packaging.
"before:invoke:local:invoke": this.compileToInvoke.bind(this),
"go:build:build": this.compileFunctions.bind(this),
};
this.commands = {
go: {
usage: "Manage Go functions",
lifecycleEvents: ["go"],
commands: {
build: {
usage: "Build all Go functions",
lifecycleEvents: ["build"],
},
},
},
};
}
/**
* Execute single function compilation and package.
*/
async compileFunction() {
const name = this.options.function;
const func = this.serverless.service.functions[this.options.function];
this.logInfo(`Starting compilation...`);
const timeStart = process.hrtime();
await this.compile(name, func);
const timeEnd = process.hrtime(timeStart);
this.logInfo(`Compilation time (${name}): ${prettyHrtime(timeEnd)}`);
}
/**
* Load all configured functions and execute compilation according it's configuration.
*/
async compileFunctions() {
let names = Object.keys(this.serverless.service.functions);
this.logInfo(`Starting compilation...`);
const timeStart = process.hrtime();
this.logDebug(`using concurrency limit ${this.config.concurrency}...`);
const limit = plimit(this.config.concurrency);
let compiles = names.map((funcName) => {
const func = this.serverless.service.functions[funcName];
return limit(() => this.compile(funcName, func));
});
await Promise.all(compiles);
const timeEnd = process.hrtime(timeStart);
this.logInfo(`Compilation time: ${prettyHrtime(timeEnd)}`);
}
/**
* Execute single function compilation and do not create the final artifact.
* This is used when `invoke` command is performed.
*/
async compileToInvoke() {
const name = this.options.function;
const func = this.serverless.service.functions[this.options.function];
this.logInfo(`Starting compilation...`);
const timeStart = process.hrtime();
await this.compile(name, func, false);
const timeEnd = process.hrtime(timeStart);
this.logInfo(`Compilation time (${name}): ${prettyHrtime(timeEnd)}`);
}
/**
* Collect configuration to build binaries and create the final artifact if needed
*
* @param {string} name name of the node function
* @param {Object} func function configuration object
* @param {bool} artifact flag to create or not the final artifact
*
* @throws {Error} if compilation command fails
*/
async compile(name, func, artifact = true) {
const config = this.config;
const arch = this.getFunctionArch(name);
const runtime = this.getFunctionRuntime(name);
if (!supportedRuntimes.includes(runtime)) {
return;
}
if (arch == "arm64") {
config.env["GOARCH"] = "arm64";
}
const absHandler = path.resolve(config.baseDir);
const absBin = path.resolve(config.binDir);
let compileBinPath = path.join(path.relative(absHandler, absBin), name); // binPath is based on cwd no baseDir
let cwd = config.baseDir;
let handler = func.handler;
if (config.monorepo) {
cwd = path.relative(absHandler, func.handler);
handler = ".";
if (func.handler.endsWith(".go")) {
cwd = path.relative(absHandler, path.dirname(func.handler));
handler = path.basename(func.handler);
}
compileBinPath = path.relative(cwd, compileBinPath);
}
this.logDebug(`[${name}] env: ${JSON.stringify(config.env)}`);
this.logDebug(`[${name}] cwd: ${cwd}`);
this.logDebug(`[${name}] arch: ${arch}`);
await this.execCompilation(
config.cmd,
compileBinPath,
handler,
cwd,
config.env,
arch,
);
this.logInfo(`Compiled function: ${chalk.magenta(name)}`);
let binPath = path.join(config.binDir, name);
if (process.platform === "win32") {
binPath = binPath.replace(/\\/g, "/");
}
this.serverless.service.functions[name].handler = binPath;
if (artifact) {
this.packageBootstrap(name, binPath);
}
}
/**
* Execute compilation command from collected configuration.
*
* @param {string} cmd Compilation command
* @param {string} out Binary out path
* @param {string} main Path to the main package or file
* @param {string} cwd Working directory
* @param {Object} env Set of environment variables to execute compilation
* @param {string} arch Compilation architecture
*/
async execCompilation(cmd, out, main, cwd, env, arch) {
let command = `${cmd} -o ${out} ${main}`;
let execOpts = { cwd: cwd, env: { ...process.env, ...env } };
try {
await this.exec(command, execOpts);
} catch (e) {
this.logError(`Error compiling function (cwd: ${cwd}): ${e.message}`);
throw new Error(`error compiling function (cwd: ${cwd})`);
}
}
/**
* Create package to deploy lambda
*
* @param {string} name Name of the function config
* @param {Object} baseConfig Configuration object
* @param {string} binPath Path to generated binary
*/
packageBootstrap(name, binPath) {
this.zip.addFile("bootstrap", this.readFileSync(binPath), "", 0o755);
const zipPath = binPath + ".zip";
this.zip.writeZip(zipPath);
this.serverless.service.functions[name].package = {
individually: true,
artifact: zipPath,
};
}
/**
* Merge default con
* @returns {Object}
*/
getConfig() {
let config = { ...this.defaultConfig };
const service = this.serverless.service;
if (service.custom && service.custom.go) {
config = { ...config, ...service.custom.go };
}
for (let env of Object.keys(config.env)) {
config.env[env] = `${config.env[env]}`;
}
config.concurrency =
parseInt(process.env.SP_GO_CONCURRENCY) || config.concurrency;
return config;
}
/**
* Read provider and function configuration and return the compilation architecture
*
* @param {string} funcName Function name to get compilation architecture.
*
* @returns Architecture name
*/
getFunctionArch(funcName) {
const funcConf = this.serverless.service.functions[funcName];
const provider = this.serverless.service.provider;
let arch = provider.architecture || "x86_64";
if (funcConf.architecture) {
arch = funcConf.architecture;
}
return arch;
}
/**
* Check provider and function architecture and determine the compilation arch
*
* @param {string} funcName Function name to collect architecture
*
* @returns architecture name
*/
getFunctionRuntime(funcName) {
const funcConf = this.serverless.service.functions[funcName];
const provider = this.serverless.service.provider;
let runtime = provider.runtime;
if (funcConf.runtime) {
runtime = funcConf.runtime;
}
return runtime;
}
/**
* Print a plugin debug log message
*
* @param {string} message Log message
*/
logDebug(message) {
if (!this.options.verbose) {
return;
}
this.serverless.cli.consoleLog(`${chalk.yellow(logPrefix)}: ${message}`);
}
/**
* Print a plugin info log message
*
* @param {string} message Log message
*/
logInfo(message) {
this.serverless.cli.consoleLog(`${chalk.cyan(logPrefix)}: ${message}`);
}
/**
* Print a plugin error log message
*
* @param {string} message Log message
*/
logError(message) {
this.serverless.cli.consoleLog(`${chalk.red(logPrefix)}: ${message}`);
}
}