Skip to content

Commit cca4627

Browse files
committed
#19 Added validation and user informative logs & errors
1 parent 8cb99bd commit cca4627

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

lib/bundle.js

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function runCommand(cmd,args,options,cli) {
1313
if (ps.stderr){
1414
cli.log(ps.stderr.toString())
1515
}
16-
if (ps.error && !ps.error.code == 'ENOENT') {
16+
if (ps.error && ps.error.code == 'ENOENT'){
17+
return ps;
18+
}
19+
if (ps.error) {
1720
throw new Error(ps.error);
1821
} else if (ps.status !== 0) {
1922
throw new Error(ps.stderr);
@@ -27,29 +30,39 @@ function docker(args, options,cli){
2730

2831
function cleanBuild(){
2932
this.cli = this.serverless.cli
30-
this.cli.log("Cleaning ruby layer build")
33+
this.cli.log("Clearing previous build ruby layer build")
3134
this.ruby_layer = path.join(this.servicePath,'.serverless','ruby_layer')
3235
if (fs.pathExistsSync(this.ruby_layer)){
3336
fs.removeSync(this.ruby_layer)
3437
}
3538
}
39+
3640
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') )
4048
const bundle_args = ['bundle', 'install', '--path=build','--without', 'test', 'development']
41-
const options={cwd : this.ruby_layer, encoding : 'utf8'}
4249
this.build_path = path.join(this.ruby_layer, 'build')
4350
fs.mkdirSync(this.build_path)
51+
const options = {cwd : this.ruby_layer, encoding : 'utf8'}
4452
if (this.options.use_docker) {
4553
docker_name = 'lambci/lambda:build-'+this.serverless.service.provider.runtime
4654
ps=docker(['version'], options,this.cli)
4755
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');
4957
}
5058
let buildDocker = false;
5159
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,
5366
path.join(this.ruby_layer,'Dockerfile'))
5467
buildDocker = true
5568
}else if (this.options.docker_yums) {
@@ -63,10 +76,13 @@ function bundleInstall(){
6376
buildDocker = true
6477
}
6578
if (buildDocker) {
79+
this.cli.log("Building docker for bundle install")
6680
docker_name ='ruby-layer:docker'
6781
docker(['build', '-t', docker_name, '-f', 'Dockerfile', '.'], options,this.cli)
6882
}
83+
6984
if (this.options.native_libs) {
85+
this.cli.log("Packing the native libraries from the specified path")
7086
ps=docker(['run', '-d', docker_name, 'false'], options,this.cli)
7187
container_id = ps.stdout.toString().trim()
7288
const lib_path = path.join(this.build_path,'lib')
@@ -75,15 +91,27 @@ function bundleInstall(){
7591
ps=docker(['cp','-L', container_id+':'+lib_to_copy, lib_path],options,this.cli)
7692
})
7793
}
94+
95+
this.cli.log("Installing gem using docker bundler")
7896
args=['run','--rm', '-i','-v', `${this.ruby_layer}:/var/gem_build`, '-w', '/var/gem_build']
7997
args.push(docker_name)
98+
8099
docker(args.concat(bundle_args), options,this.cli)
81100
} else {
82-
ps = runCommand("bundle",['-v'],options,this.cli)
101+
ps = runCommand("bundle",['-v'], options, this.cli)
83102
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(" "))
85113
}
86-
this.cli.log(bundle_args.slice(1,bundle_args.length))
114+
87115
runCommand(bundle_args[0],bundle_args.slice(1,bundle_args.length),options,this.cli)
88116
}
89117
}
@@ -125,8 +153,13 @@ function zipBundleFolder() {
125153
this.gem_folder= fs.readdirSync(path.join(this.build_path,'ruby'))[0]
126154
fs.removeSync(path.join(this.build_path,'ruby',this.gem_folder,'cache'))
127155
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,
130163
{ platform: platform, compression: 'DEFLATE',
131164
compressionOptions: { level: 9 }});
132165
}
@@ -143,12 +176,17 @@ function excludePackage(){
143176
}
144177

145178
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+
}
146184
if (!this.serverless.service.layers) {
147185
this.serverless.service.layers = {};
148186
}
149187
this.serverless.service.layers['gemLayer'] = Object.assign(
150188
{
151-
package: {artifact: path.join(this.ruby_layer, 'gemLayer.zip')},
189+
package: {artifact: this.gemLayer_zip_path },
152190
name: `${
153191
this.serverless.service.service
154192
}-${this.serverless.providers.aws.getStage()}-ruby-bundle`,
@@ -160,6 +198,9 @@ function configureLayer() {
160198
);
161199

162200
Object.keys(this.serverless.service.functions).forEach(funcName => {
201+
if(this.debug){
202+
this.cli.log("Configuring Layer for function: " + funcName)
203+
}
163204
const function_ = this.serverless.service.getFunction(funcName)
164205
function_.environment={}
165206
function_.environment["GEM_PATH"]="/opt/ruby/"+this.gem_folder

test/helper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const path = require('path');
66

77
function runCommand(cmd,args,options) {
88
const ps = spawnSync(cmd, args,options);
9-
console.log(ps.stderr.toString())
10-
console.log(ps.stdout.toString());
9+
if (ps.stdout){
10+
console.log(ps.stdout.toString())
11+
}
12+
if (ps.stderr){
13+
console.log(ps.stderr.toString())
14+
}
1115
if (ps.error) {
1216
throw new Error(ps.error);
1317
} else if (ps.status !== 0) {

0 commit comments

Comments
 (0)