Skip to content

Commit 6558824

Browse files
committed
refactor: Adapt docker for modern logs
1 parent ab3d7a2 commit 6558824

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function findTestFile(servicePath) {
7272
* @param {string} bindPath
7373
* @return {boolean}
7474
*/
75-
function tryBindPath(serverless, bindPath, testFile) {
75+
function tryBindPath(bindPath, testFile, { serverless, log }) {
7676
const debug = process.env.SLS_DEBUG;
7777
const options = [
7878
'run',
@@ -84,12 +84,30 @@ function tryBindPath(serverless, bindPath, testFile) {
8484
`/test/${testFile}`,
8585
];
8686
try {
87-
if (debug) serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
87+
if (debug) {
88+
if (log) {
89+
log.debug(`Trying bindPath ${bindPath} (${options})`);
90+
} else {
91+
serverless.cli.log(`Trying bindPath ${bindPath} (${options})`);
92+
}
93+
}
8894
const ps = dockerCommand(options);
89-
if (debug) serverless.cli.log(ps.stdout.trim());
95+
if (debug) {
96+
if (log) {
97+
log.debug(ps.stdout.trim());
98+
} else {
99+
serverless.cli.log(ps.stdout.trim());
100+
}
101+
}
90102
return ps.stdout.trim() === `/test/${testFile}`;
91103
} catch (err) {
92-
if (debug) serverless.cli.log(`Finding bindPath failed with ${err}`);
104+
if (debug) {
105+
if (log) {
106+
log.debug(`Finding bindPath failed with ${err}`);
107+
} else {
108+
serverless.cli.log(`Finding bindPath failed with ${err}`);
109+
}
110+
}
93111
return false;
94112
}
95113
}
@@ -100,7 +118,7 @@ function tryBindPath(serverless, bindPath, testFile) {
100118
* @param {string} servicePath
101119
* @return {string} The bind path.
102120
*/
103-
function getBindPath(serverless, servicePath) {
121+
function getBindPath(servicePath, pluginInstance) {
104122
// Determine bind path
105123
if (process.platform !== 'win32' && !isWsl) {
106124
return servicePath;
@@ -144,7 +162,7 @@ function getBindPath(serverless, servicePath) {
144162

145163
for (let i = 0; i < bindPaths.length; i++) {
146164
const bindPath = bindPaths[i];
147-
if (tryBindPath(serverless, bindPath, testFile)) {
165+
if (tryBindPath(bindPath, testFile, pluginInstance)) {
148166
return bindPath;
149167
}
150168
}

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: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@ function pipAcceptsSystem(pythonBin) {
126126
* @param {Object} options
127127
* @return {undefined}
128128
*/
129-
function installRequirements(
130-
targetFolder,
131-
{ options, serverless, log, progress }
132-
) {
129+
function installRequirements(targetFolder, pluginInstance) {
130+
const { options, serverless, log, progress } = pluginInstance;
133131
const targetRequirementsTxt = path.join(targetFolder, 'requirements.txt');
134132

135133
let installProgress;
136134
if (progress) {
135+
log.info(
136+
`Installing requirements from "${targetRequirementsTxt}"`,
137+
);
137138
installProgress = progress.get('python-install');
138139
installProgress.update(
139-
`Installing requirements from "${targetRequirementsTxt}"`,
140-
{ isMainEvent: true }
140+
'Installing requirements',
141141
);
142142
} else {
143143
serverless.cli.log(
@@ -262,7 +262,9 @@ function installRequirements(
262262
}
263263

264264
// Prepare bind path depending on os platform
265-
const bindPath = dockerPathForWin(getBindPath(serverless, targetFolder));
265+
const bindPath = dockerPathForWin(
266+
getBindPath(targetFolder, pluginInstance)
267+
);
266268

267269
dockerCmd.push('docker', 'run', '--rm', '-v', `${bindPath}:/var/task:z`);
268270
if (options.dockerSsh) {
@@ -300,7 +302,7 @@ function installRequirements(
300302
fse.closeSync(
301303
fse.openSync(path.join(downloadCacheDir, 'requirements.txt'), 'w')
302304
);
303-
const windowsized = getBindPath(serverless, downloadCacheDir);
305+
const windowsized = getBindPath(downloadCacheDir, pluginInstance);
304306
// And now push it to a volume mount and to pip...
305307
dockerCmd.push('-v', `${windowsized}:${dockerDownloadCacheDir}:z`);
306308
pipCmd.push('--cache-dir', dockerDownloadCacheDir);

0 commit comments

Comments
 (0)