Skip to content

Commit 9ffdf6f

Browse files
author
cgrimal
committed
Move some docker logic to a docker.js module
1 parent 8de19f5 commit 9ffdf6f

File tree

2 files changed

+67
-50
lines changed

2 files changed

+67
-50
lines changed

lib/docker.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const {spawnSync} = require('child_process');
2+
const isWsl = require('is-wsl');
3+
4+
5+
function dockerCommand(options) {
6+
const cmd = 'docker';
7+
const ps = spawnSync(cmd, options, {'timeout': 10000, 'encoding': 'utf-8'});
8+
if (ps.error) {
9+
if (ps.error.code === 'ENOENT') {
10+
throw new Error('docker not found! Please install it.');
11+
}
12+
throw new Error(ps.error);
13+
} else if (ps.status !== 0) {
14+
throw new Error(ps.stderr);
15+
}
16+
return ps;
17+
}
18+
19+
/**
20+
* Build the custom Docker image
21+
*/
22+
function buildImage(dockerFile) {
23+
const imageName = 'sls-py-reqs-custom';
24+
const options = [
25+
'build', '-f', dockerFile, '-t', imageName, '.'
26+
];
27+
const ps = dockerCommand(options);
28+
return imageName;
29+
};
30+
31+
/**
32+
* Get bind path depending on os platform
33+
*/
34+
function getBindPath(servicePath) {
35+
// Determine os platform of docker CLI from 'docker version'
36+
const options = ['version', '--format', '{{with .Client}}{{.Os}}{{end}}'];
37+
const ps = dockerCommand(options);
38+
const cliPlatform = ps.stdout.trim();
39+
40+
// Determine bind path
41+
let bindPath;
42+
if (process.platform === 'win32') {
43+
bindPath = servicePath.replace(/\\([^\s])/g, '/$1');
44+
if (cliPlatform === 'windows') {
45+
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
46+
}
47+
} else if (isWsl) {
48+
bindPath = servicePath.replace(/^\/mnt\//, '/');
49+
if (cliPlatform === 'windows') {
50+
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
51+
}
52+
} else {
53+
bindPath = servicePath;
54+
}
55+
56+
return bindPath;
57+
};
58+
59+
module.exports = {buildImage, getBindPath};

lib/pip.js

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const fse = require('fs-extra');
22
const path = require('path');
33
const {spawnSync} = require('child_process');
4-
const isWsl = require('is-wsl');
54
const {quote} = require('shell-quote');
5+
const {buildImage, getBindPath} = require('./docker');
66

77
/**
88
* pip install the requirements to the .serverless/requirements directory
@@ -53,60 +53,18 @@ function installRequirements() {
5353
if (this.options.dockerizePip) {
5454
cmd = 'docker';
5555

56-
let dockerImage = this.options.dockerImage;
56+
// Build docker image if required
57+
let dockerImage;
5758
if (this.options.dockerFile) {
5859
this.serverless.cli.log(`Building custom docker image from ${this.options.dockerFile}...`);
59-
// Build docker image from provided Dockerfile
60-
const imageName = 'sls-py-reqs-custom';
61-
options = [
62-
'build', '-f', this.options.dockerFile, '-t', imageName, '.'
63-
];
64-
const ps = spawnSync(cmd, options, {
65-
'timeout': 10000,
66-
'encoding': 'utf-8'
67-
});
68-
if (ps.error) {
69-
if (ps.error.code === 'ENOENT') {
70-
throw new Error('docker not found! Please install it.');
71-
}
72-
throw new Error(ps.error);
73-
} else if (ps.status !== 0) {
74-
throw new Error(ps.stderr);
75-
}
76-
77-
// Set dockerImage option value
78-
dockerImage = imageName;
60+
dockerImage = buildImage(this.options.dockerFile);
61+
} else {
62+
dockerImage = this.options.dockerImage;
7963
}
80-
8164
this.serverless.cli.log(`Docker Image: ${dockerImage}`);
8265

83-
// Determine os platform of docker CLI from 'docker version'
84-
options = ['version', '--format', '{{with .Client}}{{.Os}}{{end}}'];
85-
const ps = spawnSync(cmd, options, {'timeout': 10000, 'encoding': 'utf-8'});
86-
if (ps.error) {
87-
if (ps.error.code === 'ENOENT') {
88-
throw new Error('docker not found! Please install it.');
89-
}
90-
throw new Error(ps.error);
91-
} else if (ps.status !== 0) {
92-
throw new Error(ps.stderr);
93-
}
94-
95-
let bindPath;
96-
const cliPlatform = ps.stdout.trim();
97-
if (process.platform === 'win32') {
98-
bindPath = this.servicePath.replace(/\\([^\s])/g, '/$1');
99-
if (cliPlatform === 'windows') {
100-
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
101-
}
102-
} else if (isWsl) {
103-
bindPath = this.servicePath.replace(/^\/mnt\//, '/');
104-
if (cliPlatform === 'windows') {
105-
bindPath = bindPath.replace(/^\/(\w)\//i, '$1:/');
106-
}
107-
} else {
108-
bindPath = this.servicePath;
109-
}
66+
// Prepare bind path depending on os platform
67+
const bindPath = getBindPath(this.servicePath);
11068

11169
options = [
11270
'run', '--rm',

0 commit comments

Comments
 (0)