@@ -7,7 +7,7 @@ const {spawnSync} = require('child_process');
7
7
const values = require ( 'lodash.values' ) ;
8
8
const { buildImage, getBindPath, getDockerUid} = require ( './docker' ) ;
9
9
const {
10
- checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, md5Path,
10
+ checkForAndDeleteMaxCacheVersions, getRequirementsWorkingPath, getUserCachePath , md5Path,
11
11
} = require ( './shared' ) ;
12
12
13
13
/**
@@ -63,6 +63,14 @@ function installRequirements(targetFolder, serverless, options) {
63
63
pipCmd . push ( '-t' , targetFolder ) ;
64
64
pipCmd . push ( '-r' , targetRequirementsTxt ) ;
65
65
66
+ // If we want a download cache...
67
+ if ( options . useDownloadCache ) {
68
+ const downloadCacheDir = path . join ( getUserCachePath ( options ) , 'downloadCacheslspyc' ) ;
69
+ serverless . cli . log ( `Using download cache directory ${ downloadCacheDir } ` ) ;
70
+ fse . ensureDirSync ( downloadCacheDir ) ;
71
+ pipCmd . push ( '--cache-dir' , downloadCacheDir ) ;
72
+ }
73
+
66
74
// Check if pip has Debian's --system option and set it if so
67
75
const pipTestRes = spawnSync (
68
76
options . pythonBin , [ '-m' , 'pip' , 'help' , 'install' ] ) ;
@@ -82,11 +90,8 @@ function installRequirements(targetFolder, serverless, options) {
82
90
// If we are dockerizing pip
83
91
if ( options . dockerizePip ) {
84
92
cmd = 'docker' ;
85
- // Push docker-specific paths for requirements and target directory
86
- pipCmd . push ( '-t' , '/var/task/' ) ;
87
- pipCmd . push ( '-r' , '/var/task/requirements.txt' ) ;
88
93
89
- // Build docker image if required
94
+ // First, build docker image if required
90
95
let dockerImage ;
91
96
if ( options . dockerFile ) {
92
97
serverless . cli . log ( `Building custom docker image from ${ options . dockerFile } ...` ) ;
@@ -96,6 +101,10 @@ function installRequirements(targetFolder, serverless, options) {
96
101
}
97
102
serverless . cli . log ( `Docker Image: ${ dockerImage } ` ) ;
98
103
104
+ // Push docker-specific paths for requirements and target directory
105
+ pipCmd . push ( '-t' , '/var/task/' ) ;
106
+ pipCmd . push ( '-r' , '/var/task/requirements.txt' ) ;
107
+
99
108
// Prepare bind path depending on os platform
100
109
const bindPath = getBindPath ( targetFolder ) ;
101
110
@@ -110,6 +119,16 @@ function installRequirements(targetFolder, serverless, options) {
110
119
cmdOptions . push ( '-v' , `${ process . env . SSH_AUTH_SOCK } :/tmp/ssh_sock:z` ) ;
111
120
cmdOptions . push ( '-e' , 'SSH_AUTH_SOCK=/tmp/ssh_sock' ) ;
112
121
}
122
+ // If we want a download cache...
123
+ if ( options . useDownloadCache ) {
124
+ const downloadCacheDir = path . join ( getUserCachePath ( options ) , 'downloadCacheslspyc' ) ;
125
+ serverless . cli . log ( `Using download cache directory ${ downloadCacheDir } ` ) ;
126
+ fse . ensureDirSync ( downloadCacheDir ) ;
127
+ // And now push it to a volume mount and to pip...
128
+ cmdOptions . push ( '-v' , `${ downloadCacheDir } :/var/useDownloadCache:z` ) ;
129
+ pipCmd . push ( '--cache-dir' , '/var/useDownloadCache' ) ;
130
+ }
131
+
113
132
if ( process . platform === 'linux' ) {
114
133
// Use same user so requirements folder is not root and so --cache-dir works
115
134
cmdOptions . push ( '-u' , `${ process . getuid ( ) } ` ) ;
@@ -207,10 +226,7 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
207
226
// First, generate the requirements file to our local .serverless folder
208
227
fse . ensureDirSync ( path . join ( servicePath , '.serverless' ) ) ;
209
228
const slsReqsTxt = path . join ( servicePath , '.serverless' , 'requirements.txt' ) ;
210
- // Incase it's laying around from a previous package (when individually/failed packaging)
211
- if ( fse . existsSync ( slsReqsTxt ) ) {
212
- fse . removeSync ( slsReqsTxt ) ;
213
- }
229
+
214
230
installRequirementsFile (
215
231
fileName ,
216
232
slsReqsTxt ,
@@ -242,7 +258,6 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
242
258
serverless . cli . log ( `Using static cache of requirements found at ${ workingReqsFolder } ...` ) ;
243
259
// We'll "touch" the folder, as to bring it to the start of the FIFO cache
244
260
fse . utimesSync ( workingReqsFolder , new Date ( ) , new Date ( ) ) ;
245
- fse . removeSync ( slsReqsTxt ) ;
246
261
return workingReqsFolder ;
247
262
}
248
263
// Remove our old folder if it didn't complete properly, but _just incase_ only remove it if named properly...
@@ -276,7 +291,6 @@ function installRequirementsIfNeeded(servicePath, modulePath, options, serverles
276
291
if ( options . useStaticCache ) {
277
292
fse . closeSync ( fse . openSync ( path . join ( workingReqsFolder , '.completed_requirements' ) , 'w' ) ) ;
278
293
}
279
- fse . removeSync ( slsReqsTxt ) ; // Clean up after ourselves
280
294
return workingReqsFolder ;
281
295
}
282
296
0 commit comments