Skip to content

Commit

Permalink
fix log display problems when pulling and building images (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
coco-super authored and tanhe123 committed Oct 11, 2019
1 parent bdce991 commit 5a29d40
Showing 1 changed file with 43 additions and 76 deletions.
119 changes: 43 additions & 76 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,38 @@ function generateDockerCmd(functionProps, httpMode, invokeInitializer = true, ev
return cmd;
}


function followProgress(stream, onFinished) {

const barLines = {};

const onProgress = (event) => {
let status = event.status;

if (event.progress) {
status = `${event.status} ${event.progress}`;
}

if (event.id) {
const id = event.id;

if (!barLines[id]) {
barLines[id] = console.draft();
}
barLines[id](id + ': ' + status);
} else {
if (_.has(event, 'aux.ID')) {
event.stream = event.aux.ID + '\n';
}
// If there is no id, the line should be wrapped manually.
const out = event.status ? event.status + '\n' : event.stream;
process.stdout.write(out);
}
};

docker.modem.followProgress(stream, onFinished, onProgress);
}

async function pullImage(imageName) {

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

return new Promise((resolve, reject) => {

console.log(`begin pulling image ${resolveImageName}, you can also use ` + yellow(`'docker pull ${resolveImageName}'`) + ' to pull image by yourself.');

const onFinished = async (err) => {
Expand Down Expand Up @@ -332,37 +364,10 @@ async function pullImage(imageName) {
break;
}
}

resolve(resolveImageName);
};

const statuses = {};
const barLines = {};
const onProgress = (event) => {
let status = event.status;
if (event.progress) {
status = `${event.status} ${event.progress}`;
}

if (event.id) {
if (!statuses[event.id]) {
barLines[event.id] = console.draft();
}
statuses[event.id] = status;
}
// Print
const keys = Object.keys(statuses);
for (const key of keys) {
barLines[key](key + ': ' + statuses[key]);
}
if (!event.id) {
if (!barLines['result']) {
barLines['result'] = console.draft();
}
barLines['result'](event.status);
}
};
docker.modem.followProgress(stream, onFinished, onProgress);
// pull image progress
followProgress(stream, onFinished);
});
}

Expand Down Expand Up @@ -562,7 +567,7 @@ function resolveDockerUser(nasConfig) {
async function createContainer(opts) {
const isWin = process.platform === 'win32';
const isMac = process.platform === 'darwin';

if (opts && isMac) {
if (opts.HostConfig) {
const pathsOutofSharedPaths = await findPathsOutofSharedPaths(opts.HostConfig.Mounts);
Expand Down Expand Up @@ -918,39 +923,6 @@ async function copyFromImage(imageName, from, to) {
await container.remove();
}

const { Transform } = require('stream');

class BuildTransform extends Transform {
constructor(options) {
super(options);
}

_transform(chunk, encoding, done) {

chunk = chunk.toString().trim();
const lines = chunk.split('\n');

for (let line of lines) {
try {
const data = JSON.parse(line);
if (data.error) {
done(data.error);
} else if (data.stream) {
this.push(data.stream);
} else if (_.has(data, 'aux.ID')) {
this.push(data.aux.ID + '\n');
} else {
this.push(line);
}
} catch (e) {
this.push(line);
}
}

done();
}
}

function buildImage(dockerBuildDir, dockerfilePath, imageTag) {

return new Promise((resolve, reject) => {
Expand All @@ -960,25 +932,20 @@ function buildImage(dockerBuildDir, dockerfilePath, imageTag) {
dockerfile: path.relative(dockerBuildDir, dockerfilePath),
t: imageTag
}, (error, stream) => {

containers.add(stream);
containers.add(stream);

if (error) { reject(error); }
else {
stream.pipe(new BuildTransform(), {
end: true
})
.on('error', (e) => {
containers.delete(stream);
reject(e);
})
.pipe(process.stdout);

stream.on('end', function() {
stream.on('error', (e) => {
containers.delete(stream);
reject(e);
});
stream.on('end', function () {
containers.delete(stream);
resolve(imageTag);
});
}
followProgress(stream, (err, res) => err ? reject(err) : resolve(res));
});
});
}
Expand Down

0 comments on commit 5a29d40

Please sign in to comment.