Skip to content

Commit 03b1544

Browse files
Merge pull request #43 from noxasaxon/abort-on-packaging-error
Abort on packaging errors
2 parents 4c1d45e + d92de08 commit 03b1544

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The plugin configurations are simple:
133133
| useDocker | Boolean indicating whether to package pip dependencies using Docker. Set this to true if your project uses platform-specific compiled libraries like numpy. Requires a [Docker installation](https://www.docker.com/get-docker). | Yes. Defaults to `false` |
134134
| dockerImage | The Docker image to use to compile functions if `useDocker` is set to `true`. Must be specified as `repository:tag`. If the image doesn't exist on the system, it will be downloaded. The initial download may take some time. | Yes. Defaults to `lambci/lambda:build-${provider.runtime}` |
135135
| containerName | The desired name for the Docker container. | Yes. Defaults to `serverless-package-python-functions` |
136+
| abortOnPackagingErrors | Boolean indicating whether you want to stop deployment when packaging errors are detected. Examples of scenarios that will cause packaging errors include: `useDocker` is enabled but the Docker service is not running, pip finds dependency mismatches, virtual environment errrors, etc.. When an error is detected, this will prompt via commandline to continue or abort deploy. | Yes. Defaults to `false` |
136137

137138
At the function level, you:
138139
- Specify `name` to give your function a name. The plugin uses the function's name as the name of the zip artifact

index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Path = require('path');
77
const ChildProcess = require('child_process');
88
const zipper = require('zip-local');
99
const upath = require('upath');
10+
const readlineSync = require('readline-sync');
1011

1112
BbPromise.promisifyAll(Fse);
1213

@@ -32,6 +33,7 @@ class PkgPyFuncs {
3233
this.dockerImage = config.dockerImage || `lambci/lambda:build-${this.serverless.service.provider.runtime}`
3334
this.containerName = config.containerName || 'serverless-package-python-functions'
3435
this.mountSSH = config.mountSSH || false
36+
this.abortOnPackagingErrors = config.abortOnPackagingErrors || false
3537
this.dockerServicePath = '/var/task'
3638
}
3739

@@ -89,7 +91,7 @@ class PkgPyFuncs {
8991
}
9092

9193
let cmd = 'pip'
92-
let args = ['install','--upgrade','-t', upath.normalize(buildPath), '-r']
94+
let args = ['install', '--upgrade', '-t', upath.normalize(buildPath), '-r']
9395
if ( this.useDocker === true ){
9496
cmd = 'docker'
9597
args = ['exec', this.containerName, 'pip', ...args]
@@ -111,14 +113,48 @@ class PkgPyFuncs {
111113
throw new this.serverless.classes.Error(`[serverless-package-python-functions] ${ret.error.message}`)
112114
}
113115

116+
const out = ret.stdout.toString()
117+
114118
if (ret.stderr.length != 0){
115-
this.log(ret.stderr.toString())
119+
const errorText = ret.stderr.toString().trim()
120+
this.log(errorText) // prints stderr
121+
122+
if (this.abortOnPackagingErrors){
123+
const countErrorNewLines = errorText.split('\n').length
124+
125+
if(countErrorNewLines < 2 && errorText.toLowerCase().includes('git clone')){
126+
// Ignore false positive due to pip git clone printing to stderr
127+
} else if(errorText.toLowerCase().includes('docker')){
128+
console.log('stdout:', out)
129+
this.error("Docker Error Detected")
130+
131+
} else {
132+
// Error is not false positive,
133+
console.log('___ERROR DETECTED, BEGIN STDOUT____\n', out)
134+
this.requestUserConfirmation()
135+
}
136+
}
137+
116138
}
117139

118-
const out = ret.stdout.toString()
119140
return out
120141
}
121142

143+
requestUserConfirmation(prompt="\n\n??? Do you wish to continue deployment with the stated errors? \n",
144+
yesText="Continuing Deployment!",
145+
noText='ABORTING DEPLOYMENT'
146+
){
147+
const response = readlineSync.question(prompt);
148+
if(response.toLowerCase().includes('y')) {
149+
console.log(yesText);
150+
return
151+
} else {
152+
console.log(noText)
153+
this.error('Aborting')
154+
return
155+
}
156+
}
157+
122158
setupContainer(){
123159
let out = this.runProcess('docker',['ps', '-a', '--filter',`name=${this.containerName}`,'--format','{{.Names}}'])
124160
out = out.replace(/^\s+|\s+$/g, '')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"fs-extra": "^3.0.0",
1212
"lodash": "^4.17.11",
1313
"upath": "^1.1.0",
14+
"readline-sync": "^1.4.10",
1415
"zip-local": "^0.3.4"
1516
},
1617
"repository": {

0 commit comments

Comments
 (0)