File tree 6 files changed +70
-0
lines changed 6 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,16 @@ custom:
113
113
pythonBin: /opt/python3.6/bin/python
114
114
` ` `
115
115
116
+ # ## Run Custom extra packaging scripts
117
+ As a convenience method for if you need to install system dependencies for your python package you
118
+ can specify a command to do so. It honors the `dockerizePip` command so you can `yum install`
119
+ anything available in Amazon Linux if using that option. Example config :
120
+ ` ` ` yaml
121
+ custom:
122
+ pythonRequirements:
123
+ prereqCmd: bash -c "yum install mysql-devel && cp /usr/lib64/mysql/libmysqlclient.so.18.0.0 ./libmysqlclient.so.18"
124
+ ` ` `
125
+
116
126
# # Manual invocations
117
127
118
128
The `.requirements` and `requirements.zip`(if using zip support) files are left
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+ echo foobar > foobar
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ custom:
11
11
zip : false
12
12
cleanupZipHelper : true
13
13
dockerizePip : false
14
+ prereqCmd : null
14
15
15
16
package :
16
17
exclude :
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ const BbPromise = require('bluebird');
5
5
const fse = require ( 'fs-extra' ) ;
6
6
const { addVendorHelper, removeVendorHelper, packRequirements} = require ( './lib/zip' ) ;
7
7
const { installRequirements} = require ( './lib/pip' ) ;
8
+ const { runPrereqCmd} = require ( './lib/prereqCmd' ) ;
8
9
const { pipfileToRequirements} = require ( './lib/pipenv' ) ;
9
10
const { linkRequirements, unlinkRequirements} = require ( './lib/link' ) ;
10
11
const { cleanup} = require ( './lib/clean' ) ;
@@ -30,6 +31,7 @@ class ServerlessPythonRequirements {
30
31
dockerizePip : false ,
31
32
dockerImage : `lambci/lambda:build-${ this . serverless . service . provider . runtime } ` ,
32
33
pipCmdExtraArgs : [ ] ,
34
+ prereqCmd : null ,
33
35
noDeploy : [
34
36
'boto3' ,
35
37
'botocore' ,
@@ -80,6 +82,7 @@ class ServerlessPythonRequirements {
80
82
. then ( pipfileToRequirements )
81
83
. then ( addVendorHelper )
82
84
. then ( installRequirements )
85
+ . then ( runPrereqCmd )
83
86
. then ( packRequirements )
84
87
. then ( linkRequirements ) ;
85
88
@@ -105,6 +108,7 @@ class ServerlessPythonRequirements {
105
108
'requirements:install:install' : ( ) => BbPromise . bind ( this )
106
109
. then ( pipfileToRequirements )
107
110
. then ( addVendorHelper )
111
+ . then ( runPrereqCmd )
108
112
. then ( installRequirements )
109
113
. then ( packRequirements ) ,
110
114
'requirements:clean:clean' : ( ) => BbPromise . bind ( this )
Original file line number Diff line number Diff line change
1
+ const fse = require ( 'fs-extra' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const { spawnSync} = require ( 'child_process' ) ;
4
+
5
+ /**
6
+ * run a prerequisite script if provided
7
+ * @return {Promise }
8
+ */
9
+ function runPrereqCmd ( ) {
10
+ const { prereqCmd} = this . options ;
11
+ this . serverless . cli . log ( `Running prerequisite script: ${ prereqCmd } ` ) ;
12
+ if ( ! prereqCmd )
13
+ return ;
14
+ this . serverless . cli . log ( `Running prerequisite script: ${ prereqCmd } ` ) ;
15
+ let options = [ ] ;
16
+ if ( this . options . dockerizePip ) {
17
+ cmd = 'docker' ;
18
+ options = [
19
+ 'run' , '--rm' ,
20
+ '-v' , `${ this . servicePath } :/var/task:z` ,
21
+ prereqCmd ,
22
+ ] ;
23
+ if ( process . platform === 'linux' )
24
+ options . push ( '-u' , `${ process . getuid ( ) } :${ process . getgid ( ) } ` ) ;
25
+ options . push ( this . options . dockerImage ) ;
26
+ options . push ( ...pipCmd ) ;
27
+ } else {
28
+ cmd = prereqCmd ;
29
+ }
30
+ const res = spawnSync ( cmd , options , { cwd : this . servicePath } ) ;
31
+ if ( res . error ) {
32
+ if ( res . error . code === 'ENOENT' ) {
33
+ if ( this . options . dockerizePip ) {
34
+ throw new Error ( 'docker not found! Please install it.' ) ;
35
+ } else {
36
+ throw new Error ( `${ prereqCmd } not found!` ) ;
37
+ }
38
+ }
39
+ throw new Error ( res . error ) ;
40
+ }
41
+ if ( res . status != 0 )
42
+ throw new Error ( res . stderr ) ;
43
+ } ;
44
+
45
+ module . exports = { runPrereqCmd} ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ setup() {
14
14
teardown () {
15
15
sls requirements clean
16
16
rm -rf puck node_modules
17
+ rm -f foobar
17
18
if [ -f serverless.yml.bak ]; then mv serverless.yml.bak serverless.yml; fi
18
19
}
19
20
@@ -148,3 +149,10 @@ teardown() {
148
149
unzip .serverless/sls-py-req-test.zip -d puck
149
150
ls puck/.requirements.zip puck/unzip_requirements.py
150
151
}
152
+
153
+ @test " py3.6 runs prereq script" {
154
+ sed -i' .bak' -e ' s;prereqCmd: *null;prereqCmd: ./prereq;' serverless.yml
155
+ sls package
156
+ unzip .serverless/sls-py-req-test.zip -d puck
157
+ ls foobar
158
+ }
You can’t perform that action at this time.
0 commit comments