-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (132 loc) · 4.76 KB
/
Copy pathindex.js
File metadata and controls
142 lines (132 loc) · 4.76 KB
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
"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 path = require("path");
const DEFAULT_DOCKER_TAG = "0.2.0-rust-1.32.0";
const RUST_RUNTIME = "rust";
const BASE_RUNTIME = "provided";
const NO_OUTPUT_CAPTURE = { stdio: ["ignore", process.stdout, process.stderr] };
/** 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)
};
this.custom = Object.assign(
{
cargoFlags: "",
dockerTag: DEFAULT_DOCKER_TAG
},
(this.serverless.service.custom && this.serverless.service.custom.rust) ||
{}
);
// 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) {
const defaultArgs = [
"run",
"--rm",
"-t",
`-v`,
`${this.servicePath}:/code`,
`-v`,
`${process.env["HOME"]}/.cargo/registry:/root/.cargo/registry`,
`-v`,
`${process.env["HOME"]}/.cargo/git:/root/.cargo/git`
];
const customArgs = [];
let cargoFlags = (funcArgs || {}).cargoFlags || this.custom.cargoFlags;
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;
return spawnSync(
"docker",
[...defaultArgs, ...customArgs, `softprops/lambda-rust:${dockerTag}`],
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
return;
}
rustFunctionsFound = true;
let [cargoPackage, binary] = func.handler.split(".");
if (binary == undefined) {
binary = cargoPackage;
}
this.serverless.cli.log(`Building native Rust ${func.handler} func...`);
const res = this.runDocker(func.rust, cargoPackage);
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
const artifactPath = path.join("target/lambda/release", binary + ".zip");
func.package = func.package || {};
func.package.artifact = artifactPath;
// 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;
}
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;