1
- ' use strict' ;
1
+ " use strict" ;
2
2
3
3
// https://serverless.com/blog/writing-serverless-plugins/
4
4
// https://serverless.com/framework/docs/providers/aws/guide/plugins/
5
5
// https://github.com/softprops/lambda-rust/
6
6
7
- const { spawnSync } = require ( ' child_process' ) ;
8
- const path = require ( ' path' ) ;
7
+ const { spawnSync } = require ( " child_process" ) ;
8
+ const path = require ( " path" ) ;
9
9
10
- const DEFAULT_DOCKER_TAG = ' 0.2.0-rust-1.31.0' ;
11
- const RUST_RUNTIME = ' rust' ;
12
- const BASE_RUNTIME = ' provided' ;
13
- const NO_OUTPUT_CAPTURE = { stdio : [ ' ignore' , process . stdout , process . stderr ] } ;
10
+ const DEFAULT_DOCKER_TAG = " 0.2.0-rust-1.32.0" ;
11
+ const RUST_RUNTIME = " rust" ;
12
+ const BASE_RUNTIME = " provided" ;
13
+ const NO_OUTPUT_CAPTURE = { stdio : [ " ignore" , process . stdout , process . stderr ] } ;
14
14
15
15
/** assumes docker is on the host's execution path */
16
16
class RustPlugin {
17
17
constructor ( serverless , options ) {
18
-
19
18
this . serverless = serverless ;
20
19
this . options = options ;
21
- this . servicePath = this . serverless . config . servicePath || '' ;
20
+ this . servicePath = this . serverless . config . servicePath || "" ;
22
21
this . hooks = {
23
- ' before:package:createDeploymentArtifacts' : this . build . bind ( this ) ,
24
- ' before:deploy:function:packageFunction' : this . build . bind ( this ) ,
22
+ " before:package:createDeploymentArtifacts" : this . build . bind ( this ) ,
23
+ " before:deploy:function:packageFunction" : this . build . bind ( this )
25
24
} ;
26
25
this . custom = Object . assign (
27
26
{
28
27
cargoFlags : "" ,
29
28
dockerTag : DEFAULT_DOCKER_TAG
30
29
} ,
31
- this . serverless . service . custom && this . serverless . service . custom . rust || { }
30
+ ( this . serverless . service . custom && this . serverless . service . custom . rust ) ||
31
+ { }
32
32
) ;
33
33
34
34
// By default, Serverless examines node_modules to figure out which
@@ -41,52 +41,51 @@ class RustPlugin {
41
41
42
42
runDocker ( funcArgs , cargoPackage ) {
43
43
const defaultArgs = [
44
- 'run' ,
45
- '--rm' ,
46
- '-t' ,
47
- `-v` , `${ this . servicePath } :/code` ,
48
- `-v` , `${ process . env [ 'HOME' ] } /.cargo/registry:/root/.cargo/registry` ,
49
- `-v` , `${ process . env [ 'HOME' ] } /.cargo/git:/root/.cargo/git` ,
44
+ "run" ,
45
+ "--rm" ,
46
+ "-t" ,
47
+ `-v` ,
48
+ `${ this . servicePath } :/code` ,
49
+ `-v` ,
50
+ `${ process . env [ "HOME" ] } /.cargo/registry:/root/.cargo/registry` ,
51
+ `-v` ,
52
+ `${ process . env [ "HOME" ] } /.cargo/git:/root/.cargo/git`
50
53
] ;
51
54
const customArgs = [ ] ;
52
55
let cargoFlags = ( funcArgs || { } ) . cargoFlags || this . custom . cargoFlags ;
53
56
if ( cargoPackage != undefined ) {
54
57
if ( cargoFlags ) {
55
- cargoFlags = `${ cargoFlags } -p ${ cargoPackage } `
58
+ cargoFlags = `${ cargoFlags } -p ${ cargoPackage } ` ;
56
59
} else {
57
60
cargoFlags = ` -p ${ cargoPackage } ` ;
58
61
}
59
62
}
60
63
if ( cargoFlags ) {
61
64
// --features awesome-feature, ect
62
- customArgs . push ( '-e' , `CARGO_FLAGS=${ cargoFlags } ` ) ;
63
- } ;
65
+ customArgs . push ( "-e" , `CARGO_FLAGS=${ cargoFlags } ` ) ;
66
+ }
64
67
const dockerTag = ( funcArgs || { } ) . dockerTag || this . custom . dockerTag ;
65
68
return spawnSync (
66
- 'docker' ,
67
- [
68
- ...defaultArgs ,
69
- ...customArgs ,
70
- `softprops/lambda-rust:${ dockerTag } `
71
- ] ,
69
+ "docker" ,
70
+ [ ...defaultArgs , ...customArgs , `softprops/lambda-rust:${ dockerTag } ` ] ,
72
71
NO_OUTPUT_CAPTURE
73
72
) ;
74
73
}
75
74
76
75
functions ( ) {
77
76
if ( this . options . function ) {
78
- return [ this . options . function ] ;
77
+ return [ this . options . function ] ;
79
78
} else {
80
- return this . serverless . service . getAllFunctions ( ) ;
79
+ return this . serverless . service . getAllFunctions ( ) ;
81
80
}
82
81
}
83
82
84
83
build ( ) {
85
84
const service = this . serverless . service ;
86
- if ( service . provider . name != ' aws' ) {
85
+ if ( service . provider . name != " aws" ) {
87
86
return ;
88
87
}
89
- let rustFunctionsFound = false
88
+ let rustFunctionsFound = false ;
90
89
this . functions ( ) . forEach ( funcName => {
91
90
const func = service . getFunction ( funcName ) ;
92
91
const runtime = func . runtime || service . provider . runtime ;
@@ -95,14 +94,18 @@ class RustPlugin {
95
94
return ;
96
95
}
97
96
rustFunctionsFound = true ;
98
- let [ cargoPackage , binary ] = func . handler . split ( '.' ) ;
97
+ let [ cargoPackage , binary ] = func . handler . split ( "." ) ;
99
98
if ( binary == undefined ) {
100
99
binary = cargoPackage ;
101
100
}
102
101
this . serverless . cli . log ( `Building native Rust ${ func . handler } func...` ) ;
103
102
const res = this . runDocker ( func . rust , cargoPackage ) ;
104
103
if ( res . error || res . status > 0 ) {
105
- this . serverless . cli . log ( `Dockerized Rust build encountered an error: ${ res . error } ${ res . status } .` ) ;
104
+ this . serverless . cli . log (
105
+ `Dockerized Rust build encountered an error: ${ res . error } ${
106
+ res . status
107
+ } .`
108
+ ) ;
106
109
throw new Error ( res . error ) ;
107
110
}
108
111
// If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
@@ -114,26 +117,26 @@ class RustPlugin {
114
117
// we leverage the ability to declare a package artifact directly
115
118
// see https://serverless.com/framework/docs/providers/aws/guide/packaging/
116
119
// for more information
117
- const artifactPath = path . join ( ' target/lambda/release' , binary + ".zip" )
120
+ const artifactPath = path . join ( " target/lambda/release" , binary + ".zip" ) ;
118
121
func . package = func . package || { } ;
119
122
func . package . artifact = artifactPath ;
120
123
121
124
// Ensure the runtime is set to a sane value for other plugins
122
125
if ( func . runtime == RUST_RUNTIME ) {
123
- func . runtime = BASE_RUNTIME
126
+ func . runtime = BASE_RUNTIME ;
124
127
}
125
- } )
128
+ } ) ;
126
129
if ( service . provider . runtime === RUST_RUNTIME ) {
127
130
service . provider . runtime = BASE_RUNTIME ;
128
131
}
129
132
if ( ! rustFunctionsFound ) {
130
133
throw new Error (
131
134
`Error: no Rust functions found. ` +
132
- `Use 'runtime: ${ RUST_RUNTIME } ' in global or ` +
133
- `function configuration to use this plugin.`
135
+ `Use 'runtime: ${ RUST_RUNTIME } ' in global or ` +
136
+ `function configuration to use this plugin.`
134
137
) ;
135
138
}
136
139
}
137
140
}
138
141
139
- module . exports = RustPlugin ;
142
+ module . exports = RustPlugin ;
0 commit comments