with popup menu
This commit is contained in:
@@ -4,13 +4,13 @@ module.exports = function() {
|
||||
|
||||
module.connection = function(socket) {
|
||||
|
||||
var sys = require('sys');
|
||||
var exec = require('child_process').exec;
|
||||
//var sys = require('sys');
|
||||
var proc = require('child_process');
|
||||
|
||||
console.log("new client");
|
||||
|
||||
function emit(signal, data, info) {
|
||||
if (typeof data == 'string') {
|
||||
if (typeof data == 'string' && !data.match("\n")) {
|
||||
console.log("<- signal: "+signal+"("+data+")");
|
||||
} else {
|
||||
console.log("<- signal: "+signal);
|
||||
@@ -24,31 +24,86 @@ module.exports = function() {
|
||||
socket.broadcast.emit(signal, data);
|
||||
}
|
||||
|
||||
function exec(cmd, callback) {
|
||||
console.log("== "+cmd);
|
||||
proc.exec(cmd, callback);
|
||||
}
|
||||
|
||||
function fail(txt, data) {
|
||||
console.log("** "+txt, data);
|
||||
emit("fail", txt);
|
||||
}
|
||||
|
||||
function containerinspect(error, stdout, stderr) {
|
||||
console.log(error);
|
||||
if (!error && !stderr) {
|
||||
// var res = {};
|
||||
// JSON.parse(stdout).forEach(function(c) {
|
||||
// res[c.Id] = c;
|
||||
// });
|
||||
emit("containers", stdout);
|
||||
}
|
||||
if (error || stderr)
|
||||
return fail("inspect docker containers failed", {
|
||||
error: error, stderr: stderr, stdout: stdout
|
||||
});
|
||||
emit("containers", stdout);
|
||||
}
|
||||
|
||||
function containerlist(error, stdout, stderr) {
|
||||
console.log(error);
|
||||
console.log("docker inspect "+stdout.trim().replace(/\n/g, " "));
|
||||
if (!error && !stderr)
|
||||
exec("docker inspect "+stdout.trim().replace(/\n/g, " "),
|
||||
containerinspect);
|
||||
if (error || stderr)
|
||||
return fail("list docker containers failed", {
|
||||
error: error, stderr: stderr, stdout: stdout
|
||||
});
|
||||
exec("docker inspect "+stdout.trim().replace(/\n/g, " "), containerinspect);
|
||||
}
|
||||
|
||||
socket.on("containers", function() {
|
||||
function updatecontainers(error, stdout, stderr) {
|
||||
if (error || stderr)
|
||||
return fail("update docker container failed", {
|
||||
error: error, stderr: stderr, stdout: stdout
|
||||
});
|
||||
exec("docker ps -aq", containerlist);
|
||||
}
|
||||
|
||||
function modify(cmd, name) {
|
||||
if (!name.match(/^[a-z0-9][-_:.+a-z0-9]*$/i))
|
||||
return fail("illegal instance name", {
|
||||
error: error, stderr: stderr, stdout: stdout
|
||||
});
|
||||
exec("docker "+cmd+" "+name, updatecontainers);
|
||||
}
|
||||
|
||||
function containers() {
|
||||
console.log("-> containers");
|
||||
exec("docker ps -aq",
|
||||
containerlist);
|
||||
});
|
||||
updatecontainers();
|
||||
}
|
||||
|
||||
function start(name) {
|
||||
console.log("-> start("+name+")");
|
||||
modify("start", name);
|
||||
}
|
||||
|
||||
function stop(name) {
|
||||
console.log("-> stop("+name+")");
|
||||
modify("stop", name);
|
||||
}
|
||||
|
||||
function pause(name) {
|
||||
console.log("-> pause("+name+")");
|
||||
modify("pause", name);
|
||||
}
|
||||
|
||||
function unpause(name) {
|
||||
console.log("-> unpause("+name+")");
|
||||
modify("unpause", name);
|
||||
}
|
||||
|
||||
function remove(name) {
|
||||
console.log("-> remove("+name+")");
|
||||
modify("rm", name);
|
||||
}
|
||||
|
||||
socket
|
||||
.on("containers", containers)
|
||||
.on("start", start)
|
||||
.on("stop", stop)
|
||||
.on("pause", pause)
|
||||
.on("unpause", unpause)
|
||||
.on("remove", remove);
|
||||
|
||||
}
|
||||
|
||||
return module;
|
||||
|
Reference in New Issue
Block a user