Skip to content

Commit cdb7111

Browse files
committed
refactor: Use ServerlessError in docker
1 parent 9479a90 commit cdb7111

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

lib/docker.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const path = require('path');
88
* @param {string[]} options
99
* @return {Object}
1010
*/
11-
async function dockerCommand(options) {
11+
async function dockerCommand(options, pluginInstance) {
1212
const cmd = 'docker';
1313
try {
1414
return await spawn(cmd, options, { encoding: 'utf-8' });
@@ -17,7 +17,10 @@ async function dockerCommand(options) {
1717
e.stderrBuffer &&
1818
e.stderrBuffer.toString().includes('command not found')
1919
) {
20-
throw new Error('docker not found! Please install it.');
20+
throw new pluginInstance.serverless.classes.Error(
21+
'docker not found! Please install it.',
22+
'PYTHON_REQUIREMENTS_DOCKER_NOT_FOUND'
23+
);
2124
}
2225
throw e;
2326
}
@@ -29,19 +32,22 @@ async function dockerCommand(options) {
2932
* @param {string[]} extraArgs
3033
* @return {string} The name of the built docker image.
3134
*/
32-
async function buildImage(dockerFile, extraArgs) {
35+
async function buildImage(dockerFile, extraArgs, pluginInstance) {
3336
const imageName = 'sls-py-reqs-custom';
3437
const options = ['build', '-f', dockerFile, '-t', imageName];
3538

3639
if (Array.isArray(extraArgs)) {
3740
options.push(...extraArgs);
3841
} else {
39-
throw new Error('dockerRunCmdExtraArgs option must be an array');
42+
throw new pluginInstance.serverless.classes.Error(
43+
'dockerRunCmdExtraArgs option must be an array',
44+
'PYTHON_REQUIREMENTS_INVALID_DOCKER_EXTRA_ARGS'
45+
);
4046
}
4147

4248
options.push('.');
4349

44-
await dockerCommand(options);
50+
await dockerCommand(options, pluginInstance);
4551
return imageName;
4652
}
4753

@@ -50,7 +56,7 @@ async function buildImage(dockerFile, extraArgs) {
5056
* @param {string} servicePath
5157
* @return {string} file name
5258
*/
53-
function findTestFile(servicePath) {
59+
function findTestFile(servicePath, pluginInstance) {
5460
if (fse.pathExistsSync(path.join(servicePath, 'serverless.yml'))) {
5561
return 'serverless.yml';
5662
}
@@ -63,8 +69,9 @@ function findTestFile(servicePath) {
6369
if (fse.pathExistsSync(path.join(servicePath, 'requirements.txt'))) {
6470
return 'requirements.txt';
6571
}
66-
throw new Error(
67-
'Unable to find serverless.{yml|yaml|json} or requirements.txt for getBindPath()'
72+
throw new pluginInstance.serverless.classes.Error(
73+
'Unable to find serverless.{yml|yaml|json} or requirements.txt for getBindPath()',
74+
'PYTHON_REQUIREMENTS_MISSING_GET_BIND_PATH_FILE'
6875
);
6976
}
7077

@@ -73,7 +80,8 @@ function findTestFile(servicePath) {
7380
* @param {string} bindPath
7481
* @return {boolean}
7582
*/
76-
async function tryBindPath(bindPath, testFile, { serverless, log }) {
83+
async function tryBindPath(bindPath, testFile, pluginInstance) {
84+
const { serverless, log } = pluginInstance;
7785
const debug = process.env.SLS_DEBUG;
7886
const options = [
7987
'run',
@@ -92,7 +100,7 @@ async function tryBindPath(bindPath, testFile, { serverless, log }) {
92100
serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
93101
}
94102
}
95-
const ps = await dockerCommand(options);
103+
const ps = await dockerCommand(options, pluginInstance);
96104
if (debug) {
97105
if (log) {
98106
log.debug(ps.stdoutBuffer.trim());
@@ -126,7 +134,7 @@ async function getBindPath(servicePath, pluginInstance) {
126134
}
127135

128136
// test docker is available
129-
await dockerCommand(['version']);
137+
await dockerCommand(['version'], pluginInstance);
130138

131139
// find good bind path for Windows
132140
let bindPaths = [];
@@ -159,7 +167,7 @@ async function getBindPath(servicePath, pluginInstance) {
159167
bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
160168
bindPaths.push(`${drive.toUpperCase()}:/${path}`);
161169

162-
const testFile = findTestFile(servicePath);
170+
const testFile = findTestFile(servicePath, pluginInstance);
163171

164172
for (let i = 0; i < bindPaths.length; i++) {
165173
const bindPath = bindPaths[i];
@@ -176,7 +184,7 @@ async function getBindPath(servicePath, pluginInstance) {
176184
* @param {string} bindPath
177185
* @return {boolean}
178186
*/
179-
async function getDockerUid(bindPath) {
187+
async function getDockerUid(bindPath, pluginInstance) {
180188
const options = [
181189
'run',
182190
'--rm',
@@ -188,7 +196,7 @@ async function getDockerUid(bindPath) {
188196
'%u',
189197
'/bin/sh',
190198
];
191-
const ps = await dockerCommand(options);
199+
const ps = await dockerCommand(options, pluginInstance);
192200
return ps.stdoutBuffer.trim();
193201
}
194202

lib/pip.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ async function installRequirements(targetFolder, pluginInstance) {
251251
try {
252252
dockerImage = await buildImage(
253253
options.dockerFile,
254-
options.dockerBuildCmdExtraArgs
254+
options.dockerBuildCmdExtraArgs,
255+
pluginInstance
255256
);
256257
} finally {
257258
buildDockerImageProgress && buildDockerImageProgress.remove();
@@ -335,7 +336,7 @@ async function installRequirements(targetFolder, pluginInstance) {
335336
]);
336337
} else {
337338
// Use same user so --cache-dir works
338-
dockerCmd.push('-u', await getDockerUid(bindPath));
339+
dockerCmd.push('-u', await getDockerUid(bindPath, pluginInstance));
339340
}
340341

341342
for (let path of options.dockerExtraFiles) {

0 commit comments

Comments
 (0)