forked from softprops/serverless-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (173 loc) · 6.61 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
"use strict";
// https://serverless.com/blog/writing-serverless-plugins/
// https://serverless.com/framework/docs/providers/aws/guide/plugins/
// https://github.com/softprops/lambda-rust/
const { spawnSync } = require("child_process");
const { homedir } = require("os");
const path = require("path");
const DEFAULT_DOCKER_TAG = "0.2.7-rust-1.43.0";
const DEFAULT_DOCKER_IMAGE = "softprops/lambda-rust";
const RUST_RUNTIME = "rust";
const BASE_RUNTIME = "provided";
const NO_OUTPUT_CAPTURE = { stdio: ["ignore", process.stdout, process.stderr] };
function includeInvokeHook(serverlessVersion) {
let [major, minor] = serverlessVersion.split(".");
let majorVersion = parseInt(major);
let minorVersion = parseInt(minor);
return majorVersion === 1 && minorVersion >= 38 && minorVersion < 40;
}
/** assumes docker is on the host's execution path */
class RustPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.servicePath = this.serverless.config.servicePath || "";
this.hooks = {
"before:package:createDeploymentArtifacts": this.build.bind(this),
"before:deploy:function:packageFunction": this.build.bind(this),
};
if (includeInvokeHook(serverless.version)) {
this.hooks["before:invoke:local:invoke"] = this.build.bind(this);
}
this.custom = Object.assign(
{
cargoFlags: "",
dockerTag: DEFAULT_DOCKER_TAG,
dockerImage: DEFAULT_DOCKER_IMAGE,
},
(this.serverless.service.custom && this.serverless.service.custom.rust) ||
{}
);
console.log("this.custom.dockerPath: " + this.custom.dockerPath);
console.log("this.servicePath: " + this.servicePath);
this.dockerPath = path.resolve(this.custom.dockerPath || this.servicePath);
console.log("this.dockerPath: " + this.dockerPath);
// 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 (~3.2s) the process.
this.serverless.service.package.excludeDevDependencies = false;
}
runDocker(funcArgs, cargoPackage, binary, profile) {
const cargoHome = process.env.CARGO_HOME || path.join(homedir(), ".cargo");
const cargoRegistry = path.join(cargoHome, "registry");
const cargoDownloads = path.join(cargoHome, "git");
const dockerCLI = process.env["SLS_DOCKER_CLI"] || "docker";
const defaultArgs = [
"run",
"--rm",
"-t",
"-e",
`BIN=${binary}`,
`-v`,
`${this.dockerPath}:/code`,
`-v`,
`${cargoRegistry}:/root/.cargo/registry`,
`-v`,
`${cargoDownloads}:/root/.cargo/git`,
];
const customArgs = (process.env["SLS_DOCKER_ARGS"] || "").split(" ") || [];
let cargoFlags = (funcArgs || {}).cargoFlags || this.custom.cargoFlags;
if (profile) {
// release or dev
customArgs.push("-e", `PROFILE=${profile}`);
}
if (cargoPackage != undefined) {
if (cargoFlags) {
cargoFlags = `${cargoFlags} -p ${cargoPackage}`;
} else {
cargoFlags = ` -p ${cargoPackage}`;
}
}
if (cargoFlags) {
// --features awesome-feature, ect
customArgs.push("-e", `CARGO_FLAGS=${cargoFlags}`);
}
const dockerTag = (funcArgs || {}).dockerTag || this.custom.dockerTag;
const dockerImage = (funcArgs || {}).dockerImage || this.custom.dockerImage;
const finalArgs = [
...defaultArgs,
...customArgs,
`${dockerImage}:${dockerTag}`,
].filter((i) => i);
this.serverless.cli.log(
`Running container build with: ${dockerCLI}, args: ${finalArgs}.`
);
return spawnSync(dockerCLI, finalArgs, NO_OUTPUT_CAPTURE);
}
functions() {
if (this.options.function) {
return [this.options.function];
} else {
return this.serverless.service.getAllFunctions();
}
}
build() {
const service = this.serverless.service;
if (service.provider.name != "aws") {
return;
}
let rustFunctionsFound = false;
this.functions().forEach((funcName) => {
const func = service.getFunction(funcName);
const runtime = func.runtime || service.provider.runtime;
if (runtime != RUST_RUNTIME) {
// skip functions which don't apply to rust
return;
}
rustFunctionsFound = true;
let [cargoPackage, binary] = func.handler.split(".");
if (binary == undefined) {
binary = cargoPackage;
}
this.serverless.cli.log(`Building native Rust ${func.handler} func...`);
let profile = (func.rust || {}).profile || this.custom.profile;
const res = this.runDocker(func.rust, cargoPackage, binary, profile);
if (res.error || res.status > 0) {
this.serverless.cli.log(
`Dockerized Rust build encountered an error: ${res.error} ${res.status}.`
);
throw new Error(res.error);
}
// If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
//
// The AWS "provided" lambda runtime requires executables to be named
// "bootstrap" -- https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
//
// To avoid artifact nameing conflicts when we potentially have more than one function
// we leverage the ability to declare a package artifact directly
// see https://serverless.com/framework/docs/providers/aws/guide/packaging/
// for more information
console.log("dockerPath: " + this.dockerPath);
const artifactPath = path.join(
`target/lambda/${"dev" === profile ? "debug" : "release"}`,
binary + ".zip"
);
console.log("artifactPath: " + artifactPath);
console.log("func.package: " + func.package);
func.package = func.package || {};
func.package.artifact = artifactPath;
console.log("func.package: " + func.package);
console.log("func: ");
console.log(func);
// Ensure the runtime is set to a sane value for other plugins
if (func.runtime == RUST_RUNTIME) {
func.runtime = BASE_RUNTIME;
}
});
if (service.provider.runtime === RUST_RUNTIME) {
service.provider.runtime = BASE_RUNTIME;
}
console.log("service: ");
console.log(service);
if (!rustFunctionsFound) {
throw new Error(
`Error: no Rust functions found. ` +
`Use 'runtime: ${RUST_RUNTIME}' in global or ` +
`function configuration to use this plugin.`
);
}
}
}
module.exports = RustPlugin;