@@ -13,7 +13,10 @@ function runCommand(cmd,args,options,cli) {
13
13
if ( ps . stderr ) {
14
14
cli . log ( ps . stderr . toString ( ) )
15
15
}
16
- if ( ps . error && ! ps . error . code == 'ENOENT' ) {
16
+ if ( ps . error && ps . error . code == 'ENOENT' ) {
17
+ return ps ;
18
+ }
19
+ if ( ps . error ) {
17
20
throw new Error ( ps . error ) ;
18
21
} else if ( ps . status !== 0 ) {
19
22
throw new Error ( ps . stderr ) ;
@@ -27,29 +30,39 @@ function docker(args, options,cli){
27
30
28
31
function cleanBuild ( ) {
29
32
this . cli = this . serverless . cli
30
- this . cli . log ( "Cleaning ruby layer build" )
33
+ this . cli . log ( "Clearing previous build ruby layer build" )
31
34
this . ruby_layer = path . join ( this . servicePath , '.serverless' , 'ruby_layer' )
32
35
if ( fs . pathExistsSync ( this . ruby_layer ) ) {
33
36
fs . removeSync ( this . ruby_layer )
34
37
}
35
38
}
39
+
36
40
function bundleInstall ( ) {
37
- this . cli . log ( this . ruby_layer )
38
- const gem_path = path . join ( this . ruby_layer , 'Gemfile' )
39
- fs . copySync ( path . join ( this . servicePath , 'Gemfile' ) , gem_path )
41
+ this . debug = process . env . SLS_DEBUG ;
42
+ const gem_file_path = path . join ( this . servicePath , 'Gemfile' )
43
+ if ( ! fs . pathExistsSync ( gem_file_path ) ) {
44
+ throw new Error ( "No Gemfile found in the path " + gem_file_path + "\n Please add a Gemfile in the path" ) ;
45
+ }
46
+
47
+ fs . copySync ( path . join ( this . servicePath , 'Gemfile' ) , path . join ( this . ruby_layer , 'Gemfile' ) )
40
48
const bundle_args = [ 'bundle' , 'install' , '--path=build' , '--without' , 'test' , 'development' ]
41
- const options = { cwd : this . ruby_layer , encoding : 'utf8' }
42
49
this . build_path = path . join ( this . ruby_layer , 'build' )
43
50
fs . mkdirSync ( this . build_path )
51
+ const options = { cwd : this . ruby_layer , encoding : 'utf8' }
44
52
if ( this . options . use_docker ) {
45
53
docker_name = 'lambci/lambda:build-' + this . serverless . service . provider . runtime
46
54
ps = docker ( [ 'version' ] , options , this . cli )
47
55
if ( ps . error && ps . error . code === 'ENOENT' ) {
48
- throw new Error ( 'docker command not found' ) ;
56
+ throw new Error ( 'docker command not found. Please install docker https://www.docker.com/products/docker-desktop ' ) ;
49
57
}
50
58
let buildDocker = false ;
51
59
if ( this . options . docker_file ) {
52
- fs . copySync ( path . join ( this . servicePath , this . options . docker_file ) ,
60
+ const docker_file_path = path . join ( this . servicePath , this . options . docker_file )
61
+ if ( ! fs . pathExistsSync ( docker_file_path ) ) {
62
+ throw new Error ( "No Dockerfile found in the path " + docker_file_path ) ;
63
+ }
64
+
65
+ fs . copySync ( docker_file_path ,
53
66
path . join ( this . ruby_layer , 'Dockerfile' ) )
54
67
buildDocker = true
55
68
} else if ( this . options . docker_yums ) {
@@ -63,10 +76,13 @@ function bundleInstall(){
63
76
buildDocker = true
64
77
}
65
78
if ( buildDocker ) {
79
+ this . cli . log ( "Building docker for bundle install" )
66
80
docker_name = 'ruby-layer:docker'
67
81
docker ( [ 'build' , '-t' , docker_name , '-f' , 'Dockerfile' , '.' ] , options , this . cli )
68
82
}
83
+
69
84
if ( this . options . native_libs ) {
85
+ this . cli . log ( "Packing the native libraries from the specified path" )
70
86
ps = docker ( [ 'run' , '-d' , docker_name , 'false' ] , options , this . cli )
71
87
container_id = ps . stdout . toString ( ) . trim ( )
72
88
const lib_path = path . join ( this . build_path , 'lib' )
@@ -75,15 +91,27 @@ function bundleInstall(){
75
91
ps = docker ( [ 'cp' , '-L' , container_id + ':' + lib_to_copy , lib_path ] , options , this . cli )
76
92
} )
77
93
}
94
+
95
+ this . cli . log ( "Installing gem using docker bundler" )
78
96
args = [ 'run' , '--rm' , '-i' , '-v' , `${ this . ruby_layer } :/var/gem_build` , '-w' , '/var/gem_build' ]
79
97
args . push ( docker_name )
98
+
80
99
docker ( args . concat ( bundle_args ) , options , this . cli )
81
100
} else {
82
- ps = runCommand ( "bundle" , [ '-v' ] , options , this . cli )
101
+ ps = runCommand ( "bundle" , [ '-v' ] , options , this . cli )
83
102
if ( ps . error && ps . error . code === 'ENOENT' ) {
84
- throw new Error ( 'bundle command not found in local' ) ;
103
+ throw new Error ( 'bundle command not found in local. Please install ruby. https://www.ruby-lang.org/en/downloads/' ) ;
104
+ }
105
+ this . cli . log ( "Installing gem using local bundler" )
106
+ if ( this . debug ) {
107
+ this . cli . log ( "Ruby layer Path: \n " + this . ruby_layer )
108
+ ps = runCommand ( "ruby" , [ '--version' ] , options , this . cli )
109
+ this . cli . log ( "Ruby version: " + ps . stdout . toString ( ) . trim ( ) )
110
+ ps = runCommand ( "bundle" , [ '-v' ] , options , this . cli )
111
+ this . cli . log ( "Bundler version: " + ps . stdout . toString ( ) . trim ( ) )
112
+ this . cli . log ( bundle_args . join ( " " ) )
85
113
}
86
- this . cli . log ( bundle_args . slice ( 1 , bundle_args . length ) )
114
+
87
115
runCommand ( bundle_args [ 0 ] , bundle_args . slice ( 1 , bundle_args . length ) , options , this . cli )
88
116
}
89
117
}
@@ -125,8 +153,13 @@ function zipBundleFolder() {
125
153
this . gem_folder = fs . readdirSync ( path . join ( this . build_path , 'ruby' ) ) [ 0 ]
126
154
fs . removeSync ( path . join ( this . build_path , 'ruby' , this . gem_folder , 'cache' ) )
127
155
const platform = process . platform == 'win32' ? 'DOS' : 'UNIX'
128
- return zipDir ( this . build_path ,
129
- path . join ( this . ruby_layer , 'gemLayer.zip' ) ,
156
+ zipping_message = "Zipping the gemfiles"
157
+ if ( this . options . native_libs ) {
158
+ zipping_message += " and native libs"
159
+ }
160
+ this . gemLayer_zip_path = path . join ( this . ruby_layer , 'gemLayer.zip' )
161
+ this . cli . log ( zipping_message + ' to ' + this . gemLayer_zip_path )
162
+ return zipDir ( this . build_path , this . gemLayer_zip_path ,
130
163
{ platform : platform , compression : 'DEFLATE' ,
131
164
compressionOptions : { level : 9 } } ) ;
132
165
}
@@ -143,12 +176,17 @@ function excludePackage(){
143
176
}
144
177
145
178
function configureLayer ( ) {
179
+ this . cli . log ( "Configuring Layer and GEM_PATH to the functions" )
180
+ if ( this . debug ) {
181
+ this . cli . log ( "GEM_PATH:" + "/opt/ruby/" + this . gem_folder )
182
+ this . cli . log ( "Zip Path:" + this . gemLayer_zip_path )
183
+ }
146
184
if ( ! this . serverless . service . layers ) {
147
185
this . serverless . service . layers = { } ;
148
186
}
149
187
this . serverless . service . layers [ 'gemLayer' ] = Object . assign (
150
188
{
151
- package : { artifact : path . join ( this . ruby_layer , 'gemLayer.zip' ) } ,
189
+ package : { artifact : this . gemLayer_zip_path } ,
152
190
name : `${
153
191
this . serverless . service . service
154
192
} -${ this . serverless . providers . aws . getStage ( ) } -ruby-bundle`,
@@ -160,6 +198,9 @@ function configureLayer() {
160
198
) ;
161
199
162
200
Object . keys ( this . serverless . service . functions ) . forEach ( funcName => {
201
+ if ( this . debug ) {
202
+ this . cli . log ( "Configuring Layer for function: " + funcName )
203
+ }
163
204
const function_ = this . serverless . service . getFunction ( funcName )
164
205
function_ . environment = { }
165
206
function_ . environment [ "GEM_PATH" ] = "/opt/ruby/" + this . gem_folder
0 commit comments