This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
forked from serverless/serverless-python-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
252 lines (236 loc) Β· 7.64 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* 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 { 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,
dockerPrivateKey: null,
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 ||
options.dockerPrivateKey)
) {
if (!this.warningLogged) {
if (this.log) {
this.log.warning(
'You provided a docker related option but dockerizePip is set to false.'
);
} else {
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
? [this.serverless.service.functions[inputOpt.function]]
: values(this.serverless.service.functions).filter((func) => !func.image);
}
/**
* The plugin constructor
* @param {Object} serverless
* @param {Object} options
* @param {Object} v3Utils
* @return {undefined}
*/
constructor(serverless, cliOptions, v3Utils) {
this.serverless = serverless;
this.servicePath = this.serverless.config.servicePath;
this.warningLogged = false;
if (
this.serverless.configSchemaHandler &&
this.serverless.configSchemaHandler.defineFunctionProperties
) {
this.serverless.configSchemaHandler.defineFunctionProperties('aws', {
properties: {
module: {
type: 'string',
},
},
});
}
if (v3Utils) {
this.log = v3Utils.log;
this.progress = v3Utils.progress;
this.writeText = v3Utils.writeText;
}
this.commands = {
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'],
},
},
},
};
if (this.serverless.cli.generateCommandsHelp) {
Object.assign(this.commands.requirements, {
usage: 'Serverless plugin to bundle Python packages',
lifecycleEvents: ['requirements'],
});
} else {
this.commands.requirements.type = 'container';
}
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 setupArtifactPathCapturing = () => {
// Reference:
// https://github.com/serverless/serverless/blob/9591d5a232c641155613d23b0f88ca05ea51b436/lib/plugins/package/lib/packageService.js#L139
// The packageService#packageFunction does set artifact path back to the function config.
// As long as the function config's "package" attribute wasn't undefined, we can still use it
// later to access the artifact path.
for (const functionName in this.serverless.service.functions) {
if (!serverless.service.functions[functionName].package) {
serverless.service.functions[functionName].package = {};
}
}
};
const before = () => {
if (!isFunctionRuntimePython(arguments)) {
return;
}
return BbPromise.bind(this)
.then(pipfileToRequirements)
.then(addVendorHelper)
.then(installAllRequirements)
.then(packRequirements)
.then(setupArtifactPathCapturing);
};
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;