Skip to content

Commit

Permalink
Fix: Upgrade dockerode and use its new logs method to correctly handl…
Browse files Browse the repository at this point in the history
…e logs
  • Loading branch information
jsalonen committed Jun 20, 2017
1 parent c0c8bcb commit 2e21b2a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 50 deletions.
1 change: 0 additions & 1 deletion client/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import AppBar from "material-ui/AppBar";
import AppMain from "./components/AppMain";
import "./app.css";
import Dashboard from "./components/Dashboard";
require("react-ansi-style/inject-css");

const App = observer(
class App extends Component {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
},
"license": "ISC",
"dependencies": {
"dockerode": "^2.3.1",
"dockerode": "^2.5.0",
"dockerode-options": "^0.2.1",
"express": "^4.14.0"
},
"devDependencies": {
"foreman": "^2.0.0",
"husky": "^0.13.3",
"lint-staged": "^3.4.0",
"lint-staged": "^4.0.0",
"nodemon": "^1.11.0",
"prettier": "^1.2.2"
}
Expand Down
80 changes: 33 additions & 47 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,60 +69,46 @@ app.get("/api/services/:id/logs", (req, res) => {
if (!id) {
return res.status(400);
} else {
let service = docker.getService(id);

function getServiceLogs(service, callback) {
var self = service;

function demux(buffer) {
const items = [];
let i = 0;

while (i < buffer.length) {
const type = buffer.readUInt8(i);
i += 4;
const payloadSize = buffer.readUInt32BE(i);
i += 4;
const payload = buffer.slice(i, i + payloadSize).toString();
i += payloadSize;

items.push([type === 2 ? "stderr" : "stdout", payload]);
}

return items;
const service = docker.getService(id);
const opts = {
stdout: 1,
stderr: 1,
follow: 1
//tail: 25
//timestamps: 1
//since: [UNIX timestamp]
};

function demux(buffer) {
const items = [];
let i = 0;

while (i < buffer.length) {
const type = buffer.readUInt8(i);
i += 4;
const payloadSize = buffer.readUInt32BE(i);
i += 4;
const payload = buffer.slice(i, i + payloadSize).toString();
i += payloadSize;

items.push([type === 2 ? "stderr" : "stdout", payload]);
}

var optsf = {
path: "/services/" + service.id + "/logs?",
method: "GET",
statusCodes: {
200: true,
404: "no such service",
500: "server error"
},
options: {
stdout: 1,
stderr: 1,
follow: 0
//tail: 25
//timestamps: 1
//since: [UNIX timestamp]
}
};

service.modem.dial(optsf, function(err, data) {
const buffer = new Buffer(data);
const items = demux(buffer);

callback(err, items);
});
return items;
}

getServiceLogs(service, (err, data) => {
service.logs(opts, (err, stream) => {
if (err) {
return res.status(500).send(err);
} else {
return res.status(200).send(data);
var chunks = [];
stream.on("data", function(chunk) {
chunks.push(chunk);
});
stream.on("end", function() {
var buffer = Buffer.concat(chunks);
return res.status(200).send(demux(buffer));
});
}
});
}
Expand Down

0 comments on commit 2e21b2a

Please sign in to comment.