forked from serverless/serverless-python-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.js
176 lines (159 loc) · 4.63 KB
/
docker.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
const { spawnSync } = require('child_process');
const isWsl = require('is-wsl');
const fse = require('fs-extra');
const path = require('path');
/**
* Helper function to run a docker command
* @param {string[]} options
* @return {Object}
*/
function dockerCommand(options) {
const cmd = 'docker';
const ps = spawnSync(cmd, options, { encoding: 'utf-8' });
if (ps.error) {
if (ps.error.code === 'ENOENT') {
throw new Error('docker not found! Please install it.');
}
throw new Error(ps.error);
} else if (ps.status !== 0) {
throw new Error(ps.stderr);
}
return ps;
}
/**
* Build the custom Docker image
* @param {string} dockerFile
* @param {string[]} extraArgs
* @return {string} The name of the built docker image.
*/
function buildImage(dockerFile, extraArgs) {
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');
}
options.push('.');
dockerCommand(options);
return imageName;
}
/**
* Find a file that exists on all projects so we can test if Docker can see it too
* @param {string} servicePath
* @return {string} file name
*/
function findTestFile(servicePath) {
if (fse.pathExistsSync(path.join(servicePath, 'serverless.yml'))) {
return 'serverless.yml';
}
if (fse.pathExistsSync(path.join(servicePath, 'serverless.yaml'))) {
return 'serverless.yaml';
}
if (fse.pathExistsSync(path.join(servicePath, 'serverless.json'))) {
return 'serverless.json';
}
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()'
);
}
/**
* Test bind path to make sure it's working
* @param {string} bindPath
* @return {boolean}
*/
function tryBindPath(serverless, bindPath, testFile) {
const debug = process.env.SLS_DEBUG;
const options = [
'run',
'--rm',
'-v',
`${bindPath}:/test`,
'alpine',
'ls',
`/test/${testFile}`,
];
try {
if (debug) serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
const ps = dockerCommand(options);
if (debug) serverless.cli.log(ps.stdout.trim());
return ps.stdout.trim() === `/test/${testFile}`;
} catch (err) {
if (debug) serverless.cli.log(`Finding bindPath failed with ${err}`);
return false;
}
}
/**
* Get bind path depending on os platform
* @param {object} serverless
* @param {string} servicePath
* @return {string} The bind path.
*/
function getBindPath(serverless, servicePath) {
// Determine bind path
if (process.platform !== 'win32' && !isWsl) {
return servicePath;
}
// test docker is available
dockerCommand(['version']);
// find good bind path for Windows
let bindPaths = [];
let baseBindPath = servicePath.replace(/\\([^\s])/g, '/$1');
let drive;
let path;
bindPaths.push(baseBindPath);
if (baseBindPath.startsWith('/mnt/')) {
// cygwin "/mnt/C/users/..."
baseBindPath = baseBindPath.replace(/^\/mnt\//, '/');
}
if (baseBindPath[1] == ':') {
// normal windows "c:/users/..."
drive = baseBindPath[0];
path = baseBindPath.substring(3);
} else if (baseBindPath[0] == '/' && baseBindPath[2] == '/') {
// gitbash "/c/users/..."
drive = baseBindPath[1];
path = baseBindPath.substring(3);
} else {
throw new Error(`Unknown path format ${baseBindPath.substr(10)}...`);
}
bindPaths.push(`/${drive.toLowerCase()}/${path}`); // Docker Toolbox (seems like Docker for Windows can support this too)
bindPaths.push(`${drive.toLowerCase()}:/${path}`); // Docker for Windows
// other options just in case
bindPaths.push(`/${drive.toUpperCase()}/${path}`);
bindPaths.push(`/mnt/${drive.toLowerCase()}/${path}`);
bindPaths.push(`/mnt/${drive.toUpperCase()}/${path}`);
bindPaths.push(`${drive.toUpperCase()}:/${path}`);
const testFile = findTestFile(servicePath);
for (let i = 0; i < bindPaths.length; i++) {
const bindPath = bindPaths[i];
if (tryBindPath(serverless, bindPath, testFile)) {
return bindPath;
}
}
throw new Error('Unable to find good bind path format');
}
/**
* Find out what uid the docker machine is using
* @param {string} bindPath
* @return {boolean}
*/
function getDockerUid(bindPath) {
const options = [
'run',
'--rm',
'-v',
`${bindPath}:/test`,
'alpine',
'stat',
'-c',
'%u',
'/bin/sh',
];
const ps = dockerCommand(options);
return ps.stdout.trim();
}
module.exports = { buildImage, getBindPath, getDockerUid };