You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.1 KiB
110 lines
3.1 KiB
module.exports = function() { |
|
|
|
var module={}; |
|
|
|
module.connection = function(socket) { |
|
|
|
//var sys = require('sys'); |
|
var proc = require('child_process'); |
|
|
|
console.log("new client"); |
|
|
|
function emit(signal, data, info) { |
|
if (typeof data == 'string' && !data.match("\n")) { |
|
console.log("<- signal: "+signal+"("+data+")"); |
|
} else { |
|
console.log("<- signal: "+signal); |
|
} |
|
if (info) console.log(info); |
|
socket.emit(signal, data); |
|
} |
|
|
|
function broadcast(signal, data) { |
|
console.log("<= signal: "+signal); |
|
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) { |
|
if (error || stderr) |
|
return fail("inspect docker containers failed", { |
|
error: error, stderr: stderr, stdout: stdout |
|
}); |
|
emit("containers", stdout); |
|
} |
|
|
|
function containerlist(error, stdout, stderr) { |
|
if (error || stderr) |
|
return fail("list docker containers failed", { |
|
error: error, stderr: stderr, stdout: stdout |
|
}); |
|
exec("docker inspect "+stdout.trim().replace(/\n/g, " "), containerinspect); |
|
} |
|
|
|
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"); |
|
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; |
|
}
|
|
|