Skip to content

Commit 4b9c1a0

Browse files
committed
refactor: Adapt docker for modern logs
1 parent 5067583 commit 4b9c1a0

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class ServerlessPythonRequirements {
137137
this.log = v3Utils.log;
138138
this.progress = v3Utils.progress;
139139
this.writeText = v3Utils.writeText;
140-
};
140+
}
141141

142142
this.commands = {
143143
requirements: {

lib/docker.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function findTestFile(servicePath) {
7373
* @param {string} bindPath
7474
* @return {boolean}
7575
*/
76-
async function tryBindPath(serverless, bindPath, testFile) {
76+
async function tryBindPath(bindPath, testFile, { serverless, log }) {
7777
const debug = process.env.SLS_DEBUG;
7878
const options = [
7979
'run',
@@ -85,12 +85,30 @@ async function tryBindPath(serverless, bindPath, testFile) {
8585
`/test/${testFile}`,
8686
];
8787
try {
88-
if (debug) serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
88+
if (debug) {
89+
if (log) {
90+
log.debug(`Trying bindPath ${bindPath} (${options})`);
91+
} else {
92+
serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
93+
}
94+
}
8995
const ps = await dockerCommand(options);
90-
if (debug) serverless.cli.log(ps.stdout.trim());
91-
return ps.stdout.trim() === `/test/${testFile}`;
96+
if (debug) {
97+
if (log) {
98+
log.debug(ps.stdoutBuffer.trim());
99+
} else {
100+
serverless.cli.log(ps.stdoutBuffer.trim());
101+
}
102+
}
103+
return ps.stdoutBuffer.trim() === `/test/${testFile}`;
92104
} catch (err) {
93-
if (debug) serverless.cli.log(`Finding bindPath failed with ${err}`);
105+
if (debug) {
106+
if (log) {
107+
log.debug(`Finding bindPath failed with ${err}`);
108+
} else {
109+
serverless.cli.log(`Finding bindPath failed with ${err}`);
110+
}
111+
}
94112
return false;
95113
}
96114
}
@@ -101,7 +119,7 @@ async function tryBindPath(serverless, bindPath, testFile) {
101119
* @param {string} servicePath
102120
* @return {string} The bind path.
103121
*/
104-
async function getBindPath(serverless, servicePath) {
122+
async function getBindPath(servicePath, pluginInstance) {
105123
// Determine bind path
106124
if (process.platform !== 'win32' && !isWsl) {
107125
return servicePath;
@@ -145,7 +163,7 @@ async function getBindPath(serverless, servicePath) {
145163

146164
for (let i = 0; i < bindPaths.length; i++) {
147165
const bindPath = bindPaths[i];
148-
if (await tryBindPath(serverless, bindPath, testFile)) {
166+
if (await tryBindPath(bindPath, testFile, pluginInstance)) {
149167
return bindPath;
150168
}
151169
}
@@ -171,7 +189,7 @@ async function getDockerUid(bindPath) {
171189
'/bin/sh',
172190
];
173191
const ps = await dockerCommand(options);
174-
return ps.stdout.trim();
192+
return ps.stdoutBuffer.trim();
175193
}
176194

177195
module.exports = { buildImage, getBindPath, getDockerUid };

lib/inject.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,15 @@ function injectAllRequirements(funcArtifact) {
148148
);
149149
}
150150

151-
return returnPromise
152-
.then(() => injectProgress && injectProgress.remove())
153-
.catch((e) => {
154-
injectProgress && injectProgress.remove();
155-
throw e;
156-
});
151+
return (
152+
returnPromise &&
153+
returnPromise
154+
.then(() => injectProgress && injectProgress.remove())
155+
.catch((e) => {
156+
injectProgress && injectProgress.remove();
157+
throw e;
158+
})
159+
);
157160
}
158161

159162
module.exports = { injectAllRequirements };

lib/pip.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,15 @@ async function pipAcceptsSystem(pythonBin) {
133133
* @param {Object} options
134134
* @return {undefined}
135135
*/
136-
async function installRequirements(
137-
targetFolder,
138-
{ options, serverless, log, progress }
139-
) {
136+
async function installRequirements(targetFolder, pluginInstance) {
137+
const { options, serverless, log, progress } = pluginInstance;
140138
const targetRequirementsTxt = path.join(targetFolder, 'requirements.txt');
141139

142140
let installProgress;
143141
if (progress) {
142+
log.info(`Installing requirements from "${targetRequirementsTxt}"`);
144143
installProgress = progress.get('python-install');
145-
installProgress.update(
146-
`Installing requirements from "${targetRequirementsTxt}"`,
147-
{ isMainEvent: true }
148-
);
144+
installProgress.update('Installing requirements');
149145
} else {
150146
serverless.cli.log(
151147
`Installing requirements from ${targetRequirementsTxt} ...`
@@ -226,7 +222,7 @@ async function installRequirements(
226222
pipCmd.push('--cache-dir', downloadCacheDir);
227223
}
228224

229-
if (pipAcceptsSystem(options.pythonBin)) {
225+
if (await pipAcceptsSystem(options.pythonBin)) {
230226
pipCmd.push('--system');
231227
}
232228
}
@@ -253,7 +249,7 @@ async function installRequirements(
253249
);
254250
}
255251
try {
256-
dockerImage = buildImage(
252+
dockerImage = await buildImage(
257253
options.dockerFile,
258254
options.dockerBuildCmdExtraArgs
259255
);
@@ -270,7 +266,9 @@ async function installRequirements(
270266
}
271267

272268
// Prepare bind path depending on os platform
273-
const bindPath = dockerPathForWin(getBindPath(serverless, targetFolder));
269+
const bindPath = dockerPathForWin(
270+
await getBindPath(targetFolder, pluginInstance)
271+
);
274272

275273
dockerCmd.push('docker', 'run', '--rm', '-v', `${bindPath}:/var/task:z`);
276274
if (options.dockerSsh) {
@@ -308,7 +306,7 @@ async function installRequirements(
308306
fse.closeSync(
309307
fse.openSync(path.join(downloadCacheDir, 'requirements.txt'), 'w')
310308
);
311-
const windowsized = getBindPath(serverless, downloadCacheDir);
309+
const windowsized = await getBindPath(downloadCacheDir, pluginInstance);
312310
// And now push it to a volume mount and to pip...
313311
dockerCmd.push('-v', `${windowsized}:${dockerDownloadCacheDir}:z`);
314312
pipCmd.push('--cache-dir', dockerDownloadCacheDir);
@@ -337,7 +335,7 @@ async function installRequirements(
337335
]);
338336
} else {
339337
// Use same user so --cache-dir works
340-
dockerCmd.push('-u', getDockerUid(bindPath));
338+
dockerCmd.push('-u', await getDockerUid(bindPath));
341339
}
342340

343341
for (let path of options.dockerExtraFiles) {
@@ -716,9 +714,7 @@ async function installAllRequirements() {
716714
// If we didn't already process a module (functions can re-use modules)
717715
if (!doneModules.includes(f.module)) {
718716
const reqsInstalledAt = await installRequirementsIfNeeded(
719-
this.servicePath,
720717
f.module,
721-
this.options,
722718
f,
723719
this.serverless
724720
);

0 commit comments

Comments
 (0)