Skip to content

Commit 5a29d40

Browse files
coco-supertanhe123
authored andcommitted
fix log display problems when pulling and building images (#506)
1 parent bdce991 commit 5a29d40

1 file changed

Lines changed: 43 additions & 76 deletions

File tree

lib/docker.js

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,38 @@ function generateDockerCmd(functionProps, httpMode, invokeInitializer = true, ev
254254
return cmd;
255255
}
256256

257+
258+
function followProgress(stream, onFinished) {
259+
260+
const barLines = {};
261+
262+
const onProgress = (event) => {
263+
let status = event.status;
264+
265+
if (event.progress) {
266+
status = `${event.status} ${event.progress}`;
267+
}
268+
269+
if (event.id) {
270+
const id = event.id;
271+
272+
if (!barLines[id]) {
273+
barLines[id] = console.draft();
274+
}
275+
barLines[id](id + ': ' + status);
276+
} else {
277+
if (_.has(event, 'aux.ID')) {
278+
event.stream = event.aux.ID + '\n';
279+
}
280+
// If there is no id, the line should be wrapped manually.
281+
const out = event.status ? event.status + '\n' : event.stream;
282+
process.stdout.write(out);
283+
}
284+
};
285+
286+
docker.modem.followProgress(stream, onFinished, onProgress);
287+
}
288+
257289
async function pullImage(imageName) {
258290

259291
const resolveImageName = await dockerOpts.resolveImageNameForPull(imageName);
@@ -274,7 +306,7 @@ async function pullImage(imageName) {
274306
const registry = await dockerOpts.resolveDockerRegistry();
275307

276308
return new Promise((resolve, reject) => {
277-
309+
278310
console.log(`begin pulling image ${resolveImageName}, you can also use ` + yellow(`'docker pull ${resolveImageName}'`) + ' to pull image by yourself.');
279311

280312
const onFinished = async (err) => {
@@ -332,37 +364,10 @@ async function pullImage(imageName) {
332364
break;
333365
}
334366
}
335-
336367
resolve(resolveImageName);
337368
};
338-
339-
const statuses = {};
340-
const barLines = {};
341-
const onProgress = (event) => {
342-
let status = event.status;
343-
if (event.progress) {
344-
status = `${event.status} ${event.progress}`;
345-
}
346-
347-
if (event.id) {
348-
if (!statuses[event.id]) {
349-
barLines[event.id] = console.draft();
350-
}
351-
statuses[event.id] = status;
352-
}
353-
// Print
354-
const keys = Object.keys(statuses);
355-
for (const key of keys) {
356-
barLines[key](key + ': ' + statuses[key]);
357-
}
358-
if (!event.id) {
359-
if (!barLines['result']) {
360-
barLines['result'] = console.draft();
361-
}
362-
barLines['result'](event.status);
363-
}
364-
};
365-
docker.modem.followProgress(stream, onFinished, onProgress);
369+
// pull image progress
370+
followProgress(stream, onFinished);
366371
});
367372
}
368373

@@ -562,7 +567,7 @@ function resolveDockerUser(nasConfig) {
562567
async function createContainer(opts) {
563568
const isWin = process.platform === 'win32';
564569
const isMac = process.platform === 'darwin';
565-
570+
566571
if (opts && isMac) {
567572
if (opts.HostConfig) {
568573
const pathsOutofSharedPaths = await findPathsOutofSharedPaths(opts.HostConfig.Mounts);
@@ -918,39 +923,6 @@ async function copyFromImage(imageName, from, to) {
918923
await container.remove();
919924
}
920925

921-
const { Transform } = require('stream');
922-
923-
class BuildTransform extends Transform {
924-
constructor(options) {
925-
super(options);
926-
}
927-
928-
_transform(chunk, encoding, done) {
929-
930-
chunk = chunk.toString().trim();
931-
const lines = chunk.split('\n');
932-
933-
for (let line of lines) {
934-
try {
935-
const data = JSON.parse(line);
936-
if (data.error) {
937-
done(data.error);
938-
} else if (data.stream) {
939-
this.push(data.stream);
940-
} else if (_.has(data, 'aux.ID')) {
941-
this.push(data.aux.ID + '\n');
942-
} else {
943-
this.push(line);
944-
}
945-
} catch (e) {
946-
this.push(line);
947-
}
948-
}
949-
950-
done();
951-
}
952-
}
953-
954926
function buildImage(dockerBuildDir, dockerfilePath, imageTag) {
955927

956928
return new Promise((resolve, reject) => {
@@ -960,25 +932,20 @@ function buildImage(dockerBuildDir, dockerfilePath, imageTag) {
960932
dockerfile: path.relative(dockerBuildDir, dockerfilePath),
961933
t: imageTag
962934
}, (error, stream) => {
963-
964-
containers.add(stream);
935+
containers.add(stream);
965936

966937
if (error) { reject(error); }
967938
else {
968-
stream.pipe(new BuildTransform(), {
969-
end: true
970-
})
971-
.on('error', (e) => {
972-
containers.delete(stream);
973-
reject(e);
974-
})
975-
.pipe(process.stdout);
976-
977-
stream.on('end', function() {
939+
stream.on('error', (e) => {
940+
containers.delete(stream);
941+
reject(e);
942+
});
943+
stream.on('end', function () {
978944
containers.delete(stream);
979945
resolve(imageTag);
980946
});
981947
}
948+
followProgress(stream, (err, res) => err ? reject(err) : resolve(res));
982949
});
983950
});
984951
}

0 commit comments

Comments
 (0)