Skip to content

Better mixed runtime & function deploy handling #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 26, 2018
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
29 changes: 24 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,36 @@ class ServerlessPythonRequirements {
}
};

const before = () =>
BbPromise.bind(this)
const before = () => {
if (
arguments[1].functionObj &&
arguments[1].functionObj.runtime &&
!arguments[1].functionObj.runtime.startsWith('python')
)
return;
return BbPromise.bind(this)
.then(pipfileToRequirements)
.then(addVendorHelper)
.then(installAllRequirements)
.then(packRequirements);
};

const after = () =>
BbPromise.bind(this)
const after = () => {
if (
arguments[1].functionObj &&
arguments[1].functionObj.runtime &&
!arguments[1].functionObj.runtime.startsWith('python')
)
return;
return BbPromise.bind(this)
.then(removeVendorHelper)
.then(injectAllRequirements);
.then(() =>
injectAllRequirements.bind(this)(
arguments[1].functionObj &&
arguments[1].functionObj.package.artifact
)
);
};

const invalidateCaches = () => {
if (this.options.invalidateCaches) {
Expand Down
11 changes: 7 additions & 4 deletions lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function moveModuleUp(source, target, module) {
* Inject requirements into packaged application.
* @return {Promise} the combined promise for requirements injection.
*/
function injectAllRequirements() {
function injectAllRequirements(funcArtifact) {
this.serverless.cli.log('Injecting required Python packages to package...');

if (this.options.zip) {
Expand All @@ -94,8 +94,11 @@ function injectAllRequirements() {
})
.map(func => {
if (func.module !== '.') {
const artifact = func.package.artifact;
const newArtifact = path.join('.serverless', `${func.module}.zip`);
const artifact = func.package ? func.package.artifact : funcArtifact;
const newArtifact = path.join(
'.serverless',
`${func.module}-${func.name}.zip`
);
func.package.artifact = newArtifact;
return moveModuleUp(artifact, newArtifact, func.module).then(
() => func
Expand All @@ -114,7 +117,7 @@ function injectAllRequirements() {
} else {
return injectRequirements(
path.join('.serverless', 'requirements'),
this.serverless.service.package.artifact,
this.serverless.service.package.artifact || funcArtifact,
this.options
);
}
Expand Down
46 changes: 26 additions & 20 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,35 @@ function installAllRequirements() {
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
if (this.serverless.service.package.individually) {
let doneModules = [];
values(this.serverless.service.functions).forEach(f => {
if (!get(f, 'module')) {
set(f, ['module'], '.');
}
if (!doneModules.includes(f.module)) {
installRequirements(
path.join(f.module, this.options.fileName),
path.join('.serverless', f.module),
this.serverless,
this.servicePath,
this.options
);
if (f.vendor) {
// copy vendor libraries to requirements folder
copyVendors(
f.vendor,
values(this.serverless.service.functions)
.filter(func =>
(func.runtime || this.serverless.service.provider.runtime).match(
/^python.*/
)
)
.map(f => {
if (!get(f, 'module')) {
set(f, ['module'], '.');
}
if (!doneModules.includes(f.module)) {
installRequirements(
path.join(f.module, this.options.fileName),
path.join('.serverless', f.module),
this.serverless
this.serverless,
this.servicePath,
this.options
);
if (f.vendor) {
// copy vendor libraries to requirements folder
copyVendors(
f.vendor,
path.join('.serverless', f.module),
this.serverless
);
}
doneModules.push(f.module);
}
doneModules.push(f.module);
}
});
});
} else {
installRequirements(
this.options.fileName,
Expand Down
4 changes: 2 additions & 2 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ teardown() {
cd tests/individually
npm i $(npm pack ../..)
sls package
unzip .serverless/module1.zip -d puck
unzip .serverless/module2.zip -d puck2
unzip .serverless/module1-sls-py-req-test-indiv-dev-hello1.zip -d puck
unzip .serverless/module2-sls-py-req-test-indiv-dev-hello2.zip -d puck2
ls puck/handler1.py
ls puck2/handler2.py
ls puck/pyaml
Expand Down
2 changes: 1 addition & 1 deletion tests/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"serverless-python-requirements": "file:serverless-python-requirements-4.0.0.tgz"
"serverless-python-requirements": "file:serverless-python-requirements-4.0.2.tgz"
}
}