Skip to content

Commit

Permalink
refactor: Use ServerlessError in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Nov 26, 2021
1 parent 9479a90 commit cdb7111
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
36 changes: 22 additions & 14 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const path = require('path');
* @param {string[]} options
* @return {Object}
*/
async function dockerCommand(options) {
async function dockerCommand(options, pluginInstance) {
const cmd = 'docker';
try {
return await spawn(cmd, options, { encoding: 'utf-8' });
Expand All @@ -17,7 +17,10 @@ async function dockerCommand(options) {
e.stderrBuffer &&
e.stderrBuffer.toString().includes('command not found')
) {
throw new Error('docker not found! Please install it.');
throw new pluginInstance.serverless.classes.Error(
'docker not found! Please install it.',
'PYTHON_REQUIREMENTS_DOCKER_NOT_FOUND'
);
}
throw e;
}
Expand All @@ -29,19 +32,22 @@ async function dockerCommand(options) {
* @param {string[]} extraArgs
* @return {string} The name of the built docker image.
*/
async function buildImage(dockerFile, extraArgs) {
async function buildImage(dockerFile, extraArgs, pluginInstance) {
const imageName = 'sls-py-reqs-custom';
const options = ['build', '-f', dockerFile, '-t', imageName];

if (Array.isArray(extraArgs)) {
options.push(...extraArgs);
} else {
throw new Error('dockerRunCmdExtraArgs option must be an array');
throw new pluginInstance.serverless.classes.Error(
'dockerRunCmdExtraArgs option must be an array',
'PYTHON_REQUIREMENTS_INVALID_DOCKER_EXTRA_ARGS'
);
}

options.push('.');

await dockerCommand(options);
await dockerCommand(options, pluginInstance);
return imageName;
}

Expand All @@ -50,7 +56,7 @@ async function buildImage(dockerFile, extraArgs) {
* @param {string} servicePath
* @return {string} file name
*/
function findTestFile(servicePath) {
function findTestFile(servicePath, pluginInstance) {
if (fse.pathExistsSync(path.join(servicePath, 'serverless.yml'))) {
return 'serverless.yml';
}
Expand All @@ -63,8 +69,9 @@ function findTestFile(servicePath) {
if (fse.pathExistsSync(path.join(servicePath, 'requirements.txt'))) {
return 'requirements.txt';
}
throw new Error(
'Unable to find serverless.{yml|yaml|json} or requirements.txt for getBindPath()'
throw new pluginInstance.serverless.classes.Error(
'Unable to find serverless.{yml|yaml|json} or requirements.txt for getBindPath()',
'PYTHON_REQUIREMENTS_MISSING_GET_BIND_PATH_FILE'
);
}

Expand All @@ -73,7 +80,8 @@ function findTestFile(servicePath) {
* @param {string} bindPath
* @return {boolean}
*/
async function tryBindPath(bindPath, testFile, { serverless, log }) {
async function tryBindPath(bindPath, testFile, pluginInstance) {
const { serverless, log } = pluginInstance;
const debug = process.env.SLS_DEBUG;
const options = [
'run',
Expand All @@ -92,7 +100,7 @@ async function tryBindPath(bindPath, testFile, { serverless, log }) {
serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
}
}
const ps = await dockerCommand(options);
const ps = await dockerCommand(options, pluginInstance);
if (debug) {
if (log) {
log.debug(ps.stdoutBuffer.trim());
Expand Down Expand Up @@ -126,7 +134,7 @@ async function getBindPath(servicePath, pluginInstance) {
}

// test docker is available
await dockerCommand(['version']);
await dockerCommand(['version'], pluginInstance);

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

const testFile = findTestFile(servicePath);
const testFile = findTestFile(servicePath, pluginInstance);

for (let i = 0; i < bindPaths.length; i++) {
const bindPath = bindPaths[i];
Expand All @@ -176,7 +184,7 @@ async function getBindPath(servicePath, pluginInstance) {
* @param {string} bindPath
* @return {boolean}
*/
async function getDockerUid(bindPath) {
async function getDockerUid(bindPath, pluginInstance) {
const options = [
'run',
'--rm',
Expand All @@ -188,7 +196,7 @@ async function getDockerUid(bindPath) {
'%u',
'/bin/sh',
];
const ps = await dockerCommand(options);
const ps = await dockerCommand(options, pluginInstance);
return ps.stdoutBuffer.trim();
}

Expand Down
5 changes: 3 additions & 2 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ async function installRequirements(targetFolder, pluginInstance) {
try {
dockerImage = await buildImage(
options.dockerFile,
options.dockerBuildCmdExtraArgs
options.dockerBuildCmdExtraArgs,
pluginInstance
);
} finally {
buildDockerImageProgress && buildDockerImageProgress.remove();
Expand Down Expand Up @@ -335,7 +336,7 @@ async function installRequirements(targetFolder, pluginInstance) {
]);
} else {
// Use same user so --cache-dir works
dockerCmd.push('-u', await getDockerUid(bindPath));
dockerCmd.push('-u', await getDockerUid(bindPath, pluginInstance));
}

for (let path of options.dockerExtraFiles) {
Expand Down

0 comments on commit cdb7111

Please sign in to comment.