-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
87 lines (72 loc) · 2.6 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
const path = require('path');
const { readFile } = require('fs.promised');
const getWritableDirectory = require('@vercel/build-utils/fs/get-writable-directory');
const download = require('@vercel/build-utils/fs/download');
const glob = require('@vercel/build-utils/fs/glob');
const { createLambda } = require('@vercel/build-utils/lambda');
const {
log,
pip,
python,
apt,
} = require('./build-utils');
exports.config = {
maxLambdaSize: '15mb',
};
exports.build = async ({ files, entrypoint, config }) => {
log.info(`Files: ${files}`);
log.title('Starting build');
const systemReleaseContents = await readFile(
path.join('/etc', 'system-release'),
'utf8',
);
log.info(`Build AMI version: ${systemReleaseContents.trim()}`);
const runtime = config.runtime || 'python3.8';
python.validateRuntime(runtime);
log.info(`Lambda runtime: ${runtime}`);
const wsgiMod = entrypoint.split('.').shift().replace(/\//g, '.');
const wsgiApplicationName = config.wsgiApplicationName || 'application';
const wsgiApplication = `${wsgiMod}.${wsgiApplicationName}`;
log.info(`WSGI application: ${wsgiApplication}`);
log.heading('Selecting python version');
const pythonBin = await python.findPythonBinary(runtime);
process.env.PYTHONUSERBASE = await getWritableDirectory();
process.env.DISABLE_HANDLER = config.production || 'false' === true ? 'true' : 'false';
log.heading('Installing pip');
const pipPath = await pip.downloadAndInstallPip(pythonBin);
log.heading('Downloading project');
const srcDir = await getWritableDirectory();
// eslint-disable-next-line no-param-reassign
files = await download(files, srcDir);
process.env.srcDir = srcDir;
log.heading('Installing handler');
await pip.install(pipPath, srcDir, __dirname);
log.heading('Running setup script');
let setupPath = apt.findRequirements(entrypoint, files);
if (setupPath) {
await apt.install(setupPath);
}
log.heading('Running pip script');
const requirementsTxtPath = pip.findRequirements(entrypoint, files);
if (requirementsTxtPath) {
await pip.install(pipPath, srcDir, '-r', requirementsTxtPath);
}
log.heading('Running post-setup script');
setupPath = apt.findPostRequirements(entrypoint, files);
if (setupPath) {
await apt.install(setupPath);
}
log.heading('Preparing lambda bundle');
const lambda = await createLambda({
files: await glob('**', srcDir),
handler: 'pyvercel.vercel_handler',
runtime: `${config.runtime || 'python3.8'}`,
environment: {
WSGI_APPLICATION: `${wsgiApplication}`,
},
});
log.title('Done!');
return {
[entrypoint]: lambda,
};
};