Skip to content

Commit 757762f

Browse files
author
bweigel
committed
Merge branch 'package_individually_like_base_sls' of https://github.com/bweigel/serverless-python-requirements into package_individually_like_base_sls
2 parents f46066d + ddfc636 commit 757762f

File tree

12 files changed

+492
-56
lines changed

12 files changed

+492
-56
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ It is important that the host of your private repositories has already been adde
7272
`$HOME/.ssh/known_hosts` file, as the install process will fail otherwise due to host authenticity
7373
failure.
7474

75+
You can also pass environment variables to docker by specifying them in `dockerEnv`
76+
option:
77+
```yaml
78+
custom:
79+
pythonRequirements:
80+
dockerEnv:
81+
- https_proxy
82+
```
83+
7584
[:checkered_flag: Windows notes](#checkered_flag-windows-dockerizepip-notes)
7685

7786
## Pipenv support :sparkles::cake::sparkles:

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ServerlessPythonRequirements {
4242
dockerSsh: false,
4343
dockerImage: null,
4444
dockerFile: null,
45+
dockerEnv: false,
4546
useStaticCache: false,
4647
useDownloadCache: false,
4748
cacheLocation: false,

lib/inject.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,15 @@ function injectRequirements(requirementsPath, packagePath, options) {
5050
* @param {string} module module to keep
5151
* @return {Promise} the JSZip object written out.
5252
*/
53-
function moveModuleUp(source, target, module, options) {
53+
function injectSourceCode(source, target, module) {
5454
const targetZip = new JSZip();
5555

5656
return fse
5757
.readFileAsync(source)
5858
.then(buffer => JSZip.loadAsync(buffer))
59-
.then(sourceZip => sourceZip.filter(file => file.startsWith(module + '/')))
59+
.then(sourceZip => sourceZip.filter(() => true))
6060
.map(srcZipObj => {
61-
let targetName;
62-
63-
if (
64-
options.IndividuallyMoveUpModules === true ||
65-
options.IndividuallyMoveUpModules == 'true'
66-
) {
67-
targetName = srcZipObj.name.replace(module + '/', '');
68-
} else {
69-
targetName = srcZipObj.name;
70-
}
71-
61+
let targetName = srcZipObj.name.replace(module + '/', '');
7262
return zipFile(targetZip, targetName, srcZipObj.async('nodebuffer'));
7363
})
7464
.then(() => writeZip(targetZip, target));
@@ -95,19 +85,26 @@ function injectAllRequirements(funcArtifact) {
9585
return func;
9686
})
9787
.map(func => {
98-
if (func.module !== '.') {
88+
if (
89+
this.options.IndividuallyMoveUpModules === true ||
90+
this.options.IndividuallyMoveUpModules === 'true'
91+
) {
9992
const artifact = func.package ? func.package.artifact : funcArtifact;
100-
const newArtifact = path.join(
101-
'.serverless',
102-
`${func.module}-${func.name}.zip`
103-
);
93+
94+
let newArtifact;
95+
if (func.module === '.') {
96+
newArtifact = path.join('.serverless', `root-${func.name}.zip`);
97+
} else {
98+
newArtifact = path.join(
99+
'.serverless',
100+
`${func.module}-${func.name}.zip`
101+
);
102+
}
103+
104104
func.package.artifact = newArtifact;
105-
return moveModuleUp(
106-
artifact,
107-
newArtifact,
108-
func.module,
109-
this.options
110-
).then(() => func);
105+
return injectSourceCode(artifact, newArtifact, func.module).then(
106+
() => func
107+
);
111108
} else {
112109
return func;
113110
}

lib/pip.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ function quote_single(quoteme) {
2727
* @param {Object} options
2828
* @return {undefined}
2929
*/
30-
function installRequirementsFile(
30+
function generateRequirementsFile(
3131
requirementsPath,
3232
targetFile,
3333
serverless,
3434
servicePath,
3535
options
3636
) {
3737
if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) {
38-
generateRequirementsFile(
38+
filterRequirementsFile(
39+
/*
40+
TODO: this is supposed to generate a requirements.txt from Pipfile,
41+
but source and target are basicalle the same
42+
*/
3943
path.join(servicePath, '.serverless/requirements.txt'),
4044
targetFile,
4145
options
@@ -44,7 +48,7 @@ function installRequirementsFile(
4448
`Parsed requirements.txt from Pipfile in ${targetFile}...`
4549
);
4650
} else {
47-
generateRequirementsFile(requirementsPath, targetFile, options);
51+
filterRequirementsFile(requirementsPath, targetFile, options);
4852
serverless.cli.log(
4953
`Generated requirements from ${requirementsPath} in ${targetFile}...`
5054
);
@@ -196,6 +200,13 @@ function installRequirements(targetFolder, serverless, options) {
196200
pipCmd.push('--cache-dir', quote_single(dockerDownloadCacheDir));
197201
}
198202

203+
if (options.dockerEnv) {
204+
// Add environment variables to docker run cmd
205+
options.dockerEnv.forEach(function(item) {
206+
cmdOptions.push('-e', item);
207+
});
208+
}
209+
199210
if (process.platform === 'linux') {
200211
// Use same user so requirements folder is not root and so --cache-dir works
201212
var commands = [];
@@ -293,7 +304,7 @@ function dockerPathForWin(options, path) {
293304
* @param {string} target requirements where results are written
294305
* @param {Object} options
295306
*/
296-
function generateRequirementsFile(source, target, options) {
307+
function filterRequirementsFile(source, target, options) {
297308
const noDeploy = new Set(options.noDeploy || []);
298309
const requirements = fse
299310
.readFileSync(source, { encoding: 'utf-8' })
@@ -374,11 +385,26 @@ function installRequirementsIfNeeded(
374385
// Our source requirements, under our service path, and our module path (if specified)
375386
const fileName = path.join(servicePath, modulePath, options.fileName);
376387

377-
// First, generate the requirements file to our local .serverless folder
378-
fse.ensureDirSync(path.join(servicePath, '.serverless'));
379-
const slsReqsTxt = path.join(servicePath, '.serverless', 'requirements.txt');
388+
// Skip requirements generation, if requirements file doesn't exist
389+
if (!fse.existsSync(fileName)) {
390+
return false;
391+
}
380392

381-
installRequirementsFile(
393+
let requirementsTxtDirectory;
394+
// Copy our requirements to another filename in .serverless (incase of individually packaged)
395+
if (modulePath && modulePath != '.') {
396+
requirementsTxtDirectory = path.join(
397+
servicePath,
398+
'.serverless',
399+
modulePath
400+
);
401+
} else {
402+
requirementsTxtDirectory = path.join(servicePath, '.serverless');
403+
}
404+
fse.ensureDirSync(requirementsTxtDirectory);
405+
const slsReqsTxt = path.join(requirementsTxtDirectory, 'requirements.txt');
406+
407+
generateRequirementsFile(
382408
fileName,
383409
slsReqsTxt,
384410
serverless,
@@ -394,28 +420,13 @@ function installRequirementsIfNeeded(
394420
return false;
395421
}
396422

397-
// Copy our requirements to another filename in .serverless (incase of individually packaged)
398-
if (modulePath && modulePath != '.') {
399-
fse.existsSync(path.join(servicePath, '.serverless', modulePath));
400-
const destinationFile = path.join(
401-
servicePath,
402-
'.serverless',
403-
modulePath,
404-
'requirements.txt'
405-
);
406-
serverless.cli.log(
407-
`Copying from ${slsReqsTxt} into ${destinationFile} ...`
408-
);
409-
fse.copySync(slsReqsTxt, destinationFile);
410-
}
411-
412423
// Then generate our MD5 Sum of this requirements file to determine where it should "go" to and/or pull cache from
413424
const reqChecksum = md5Path(slsReqsTxt);
414425

415426
// Then figure out where this cache should be, if we're caching, if we're in a module, etc
416427
const workingReqsFolder = getRequirementsWorkingPath(
417428
reqChecksum,
418-
servicePath,
429+
requirementsTxtDirectory,
419430
options
420431
);
421432

lib/shared.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ function checkForAndDeleteMaxCacheVersions(options, serverless) {
5757
* @param {Object} options
5858
* @return {string}
5959
*/
60-
function getRequirementsWorkingPath(subfolder, servicePath, options) {
60+
function getRequirementsWorkingPath(
61+
subfolder,
62+
requirementsTxtDirectory,
63+
options
64+
) {
6165
// If we want to use the static cache
6266
if (options && options.useStaticCache) {
6367
if (subfolder) {
@@ -69,7 +73,7 @@ function getRequirementsWorkingPath(subfolder, servicePath, options) {
6973
}
7074

7175
// If we don't want to use the static cache, then fallback to the way things used to work
72-
return path.join(servicePath, '.serverless', 'requirements');
76+
return path.join(requirementsTxtDirectory, 'requirements');
7377
}
7478

7579
/**

0 commit comments

Comments
 (0)