Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ The plugin configurations are simple:
| 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` |
| 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}` |
| containerName | The desired name for the Docker container. | Yes. Defaults to `serverless-package-python-functions` |
| 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` |

At the function level, you:
- Specify `name` to give your function a name. The plugin uses the function's name as the name of the zip artifact
Expand Down
42 changes: 39 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Path = require('path');
const ChildProcess = require('child_process');
const zipper = require('zip-local');
const upath = require('upath');
const readlineSync = require('readline-sync');

BbPromise.promisifyAll(Fse);

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

Expand Down Expand Up @@ -89,7 +91,7 @@ class PkgPyFuncs {
}

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

const out = ret.stdout.toString()

if (ret.stderr.length != 0){
this.log(ret.stderr.toString())
const errorText = ret.stderr.toString().trim()
this.log(errorText) // prints stderr

if (this.abortOnPackagingErrors){
const countErrorNewLines = errorText.split('\n').length

if(countErrorNewLines < 2 && errorText.toLowerCase().includes('git clone')){
// Ignore false positive due to pip git clone printing to stderr
} else if(errorText.toLowerCase().includes('docker')){
console.log('stdout:', out)
this.error("Docker Error Detected")

} else {
// Error is not false positive,
console.log('___ERROR DETECTED, BEGIN STDOUT____\n', out)
this.requestUserConfirmation()
}
}

}

const out = ret.stdout.toString()
return out
}

requestUserConfirmation(prompt="\n\n??? Do you wish to continue deployment with the stated errors? \n",
yesText="Continuing Deployment!",
noText='ABORTING DEPLOYMENT'
){
const response = readlineSync.question(prompt);
if(response.toLowerCase().includes('y')) {
console.log(yesText);
return
} else {
console.log(noText)
this.error('Aborting')
return
}
}

setupContainer(){
let out = this.runProcess('docker',['ps', '-a', '--filter',`name=${this.containerName}`,'--format','{{.Names}}'])
out = out.replace(/^\s+|\s+$/g, '')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"fs-extra": "^3.0.0",
"lodash": "^4.17.11",
"upath": "^1.1.0",
"readline-sync": "^1.4.10",
"zip-local": "^0.3.4"
},
"repository": {
Expand Down