-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathlayer.js
110 lines (100 loc) · 3.17 KB
/
layer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const BbPromise = require('bluebird');
const fse = require('fs-extra');
const path = require('path');
const JSZip = require('jszip');
const { writeZip, addTree } = require('./zipTree');
const { sha256Path, getRequirementsLayerPath } = require('./shared');
BbPromise.promisifyAll(fse);
/**
* Zip up requirements to be used as layer package.
* @return {Promise} the JSZip object constructed.
*/
function zipRequirements() {
const src = path.join('.serverless', 'requirements');
const reqChecksum = sha256Path(path.join('.serverless', 'requirements.txt'));
const targetZipPath = path.join('.serverless', 'pythonRequirements.zip');
const zipCachePath = getRequirementsLayerPath(
reqChecksum,
targetZipPath,
this.options,
this.serverless
);
const promises = [];
if (fse.existsSync(zipCachePath)) {
let layerProgress;
if (this.progress && this.log) {
layerProgress = this.progress.get('python-layer-requirements');
layerProgress.update(
'Using cached Python Requirements Lambda Layer file'
);
this.log.info('Found cached Python Requirements Lambda Layer file');
} else {
this.serverless.cli.log(
'Found cached Python Requirements Lambda Layer file'
);
}
} else {
const rootZip = new JSZip();
const runtimepath = 'python';
promises.push(
addTree(rootZip.folder(runtimepath), src).then(() =>
writeZip(rootZip, zipCachePath)
)
);
}
return BbPromise.all(promises).then(() => {
if (zipCachePath !== targetZipPath) {
if (process.platform === 'win32') {
fse.copySync(zipCachePath, targetZipPath);
} else {
fse.symlink(zipCachePath, targetZipPath, 'file');
}
}
});
}
/**
* Creates a layer on the serverless service for the requirements zip.
* @return {Promise} empty promise
*/
function createLayers() {
if (!this.serverless.service.layers) {
this.serverless.service.layers = {};
}
this.serverless.service.layers['pythonRequirements'] = Object.assign(
{
artifact: path.join('.serverless', 'pythonRequirements.zip'),
name: `${
this.serverless.service.service
}-${this.serverless.providers.aws.getStage()}-python-requirements`,
description:
'Python requirements generated by serverless-python-requirements.',
compatibleRuntimes: [this.serverless.service.provider.runtime],
},
this.options.layer
);
return BbPromise.resolve();
}
/**
* Creates a layer from the installed requirements.
* @return {Promise} the combined promise for requirements layer.
*/
function layerRequirements() {
if (!this.options.layer) {
return BbPromise.resolve();
}
let layerProgress;
if (this.progress && this.log) {
layerProgress = this.progress.get('python-layer-requirements');
layerProgress.update('Packaging Python Requirements Lambda Layer');
this.log.info('Packaging Python Requirements Lambda Layer');
} else {
this.serverless.cli.log('Packaging Python Requirements Lambda Layer...');
}
return BbPromise.bind(this)
.then(zipRequirements)
.then(createLayers)
.finally(() => layerProgress && layerProgress.remove());
}
module.exports = {
layerRequirements,
};