forked from serverless/serverless-python-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (193 loc) Β· 6.05 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* jshint ignore:start */
'use strict';
const BbPromise = require('bluebird');
const fse = require('fs-extra');
const values = require('lodash.values');
const {
addVendorHelper,
removeVendorHelper,
packRequirements
} = require('./lib/zip');
const { injectAllRequirements } = require('./lib/inject');
const { layerRequirements } = require('./lib/layer');
const { installAllRequirements } = require('./lib/pip');
const { pipfileToRequirements } = require('./lib/pipenv');
const { pyprojectTomlToRequirements } = require('./lib/poetry');
const { cleanup, cleanupCache } = require('./lib/clean');
BbPromise.promisifyAll(fse);
/**
* Plugin for Serverless 1.x that bundles python requirements!
*/
class ServerlessPythonRequirements {
/**
* get the custom.pythonRequirements contents, with defaults set
* @return {Object}
*/
get options() {
const options = Object.assign(
{
slim: false,
slimPatterns: false,
slimPatternsAppendDefaults: true,
zip: false,
layer: false,
cleanupZipHelper: true,
invalidateCaches: false,
fileName: 'requirements.txt',
usePipenv: true,
usePoetry: true,
pythonBin:
process.platform === 'win32'
? 'python.exe'
: this.serverless.service.provider.runtime || 'python',
dockerizePip: false,
dockerSsh: false,
dockerImage: null,
dockerFile: null,
dockerEnv: false,
dockerBuildCmdExtraArgs: [],
dockerRunCmdExtraArgs: [],
dockerExtraFiles: [],
useStaticCache: true,
useDownloadCache: true,
cacheLocation: false,
staticCacheMaxVersions: 0,
pipCmdExtraArgs: [],
noDeploy: [],
vendor: ''
},
(this.serverless.service.custom &&
this.serverless.service.custom.pythonRequirements) ||
{}
);
if (options.dockerizePip === 'non-linux') {
options.dockerizePip = process.platform !== 'linux';
}
if (options.dockerizePip && process.platform === 'win32') {
options.pythonBin = 'python';
}
if (
!options.dockerizePip &&
(options.dockerSsh || options.dockerImage || options.dockerFile)
) {
if (!this.warningLogged) {
this.serverless.cli.log(
'WARNING: You provided a docker related option but dockerizePip is set to false.'
);
this.warningLogged = true;
}
}
if (options.dockerImage && options.dockerFile) {
throw new Error(
'Python Requirements: you can provide a dockerImage or a dockerFile option, not both.'
);
} else if (!options.dockerFile) {
// If no dockerFile is provided, use default image
const defaultImage = `lambci/lambda:build-${this.serverless.service.provider.runtime}`;
options.dockerImage = options.dockerImage || defaultImage;
}
if (options.layer) {
// If layer was set as a boolean, set it to an empty object to use the layer defaults.
if (options.layer === true) {
options.layer = {};
}
}
return options;
}
get targetFuncs() {
let inputOpt = this.serverless.processedInput.options;
return inputOpt.function
? [inputOpt.functionObj]
: values(this.serverless.service.functions);
}
/**
* The plugin constructor
* @param {Object} serverless
* @param {Object} options
* @return {undefined}
*/
constructor(serverless) {
this.serverless = serverless;
this.servicePath = this.serverless.config.servicePath;
this.warningLogged = false;
this.commands = {
requirements: {
usage: 'Serverless plugin to bundle Python packages',
lifecycleEvents: ['requirements'],
commands: {
clean: {
usage: 'Remove .requirements and requirements.zip',
lifecycleEvents: ['clean']
},
install: {
usage: 'install requirements manually',
lifecycleEvents: ['install']
},
cleanCache: {
usage:
'Removes all items in the pip download/static cache (if present)',
lifecycleEvents: ['cleanCache']
}
}
}
};
const isFunctionRuntimePython = args => {
// If functionObj.runtime is undefined, python.
if (!args[1].functionObj || !args[1].functionObj.runtime) {
return true;
}
return args[1].functionObj.runtime.startsWith('python');
};
const clean = () =>
BbPromise.bind(this)
.then(cleanup)
.then(removeVendorHelper);
const before = () => {
if (!isFunctionRuntimePython(arguments)) {
return;
}
return BbPromise.bind(this)
.then(pipfileToRequirements)
.then(pyprojectTomlToRequirements)
.then(addVendorHelper)
.then(installAllRequirements)
.then(packRequirements);
};
const after = () => {
if (!isFunctionRuntimePython(arguments)) {
return;
}
return BbPromise.bind(this)
.then(removeVendorHelper)
.then(layerRequirements)
.then(() =>
injectAllRequirements.bind(this)(
arguments[1].functionObj &&
arguments[1].functionObj.package.artifact
)
);
};
const invalidateCaches = () => {
if (this.options.invalidateCaches) {
return clean;
}
return BbPromise.resolve();
};
const cleanCache = () => BbPromise.bind(this).then(cleanupCache);
this.hooks = {
'after:package:cleanup': invalidateCaches,
'before:package:createDeploymentArtifacts': before,
'after:package:createDeploymentArtifacts': after,
'before:deploy:function:packageFunction': before,
'after:deploy:function:packageFunction': after,
'requirements:requirements': () => {
this.serverless.cli.generateCommandsHelp(['requirements']);
return BbPromise.resolve();
},
'requirements:install:install': before,
'requirements:clean:clean': clean,
'requirements:cleanCache:cleanCache': cleanCache
};
}
}
module.exports = ServerlessPythonRequirements;