commit
105ac06c5e
92
lxc.js
92
lxc.js
@ -1,6 +1,12 @@
|
|||||||
module.exports = function(config){
|
module.exports = function(config){
|
||||||
|
|
||||||
var obj = {};
|
var obj = {};
|
||||||
|
var cmd = require('node-cmd');
|
||||||
|
|
||||||
|
var sysExec = function(command, callback){
|
||||||
|
// console.log('sysExec: ', command, '||| callback:', callback)
|
||||||
|
cmd.get('unset XDG_SESSION_ID XDG_RUNTIME_DIR; cgm movepid all virt $$; '+command, callback)
|
||||||
|
}
|
||||||
|
/* var obj = {};
|
||||||
var child = require('child'),
|
var child = require('child'),
|
||||||
sshBind = config.sshBind || false;
|
sshBind = config.sshBind || false;
|
||||||
|
|
||||||
@ -21,7 +27,7 @@ module.exports = function(config){
|
|||||||
var runCommand = sshBind.slice();
|
var runCommand = sshBind.slice();
|
||||||
runCommand.push(command);
|
runCommand.push(command);
|
||||||
} else {
|
} else {
|
||||||
var runCommand = textToArgs(command);
|
var runCommand = textToArgs('unset XDG_SESSION_ID XDG_RUNTIME_DIR; cgm movepid all virt $$; '+command);
|
||||||
}
|
}
|
||||||
|
|
||||||
var errors = '';
|
var errors = '';
|
||||||
@ -34,40 +40,42 @@ module.exports = function(config){
|
|||||||
cbClose: function(exitCode){ onClose(exitCode == 0 ? null:exitCode, errors) }
|
cbClose: function(exitCode){ onClose(exitCode == 0 ? null:exitCode, errors) }
|
||||||
}).start();
|
}).start();
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
obj.create = function(name, template, config, cbComplete){
|
||||||
obj.create = function(name, template, config, cbComplete, cbData){
|
sysExec('lxc-create -n '+name+' -t '+template, cbComplete);
|
||||||
sysExec('lxc-create -n '+name+' -t '+template, cbComplete, cbData);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
obj.clone = function(name, base_name, cbComplete, cbData){
|
obj.clone = function(name, base_name, cbComplete, cbData){
|
||||||
sysExec('lxc-clone -o '+base_name+ ' -n '+name +' -B overlayfs -s', cbComplete, cbData);
|
sysExec('lxc-clone -o '+base_name+ ' -n '+name +' -B overlayfs -s', cbComplete, cbData);
|
||||||
};
|
};
|
||||||
|
|
||||||
obj.destroy = function(name, cbComplete, cbData){
|
obj.destroy = function(name, callback){
|
||||||
sysExec('lxc-destroy -n '+ name, cbComplete, cbData);
|
sysExec('lxc-destroy -n '+ name, function(data){
|
||||||
};
|
callback(!data.match(/Destroyed container/))
|
||||||
|
|
||||||
|
|
||||||
obj.start = function(name, cbComplete, cbData){
|
|
||||||
var cmd = 'lxc-start --name '+name+' --daemon';
|
|
||||||
console.log('start cmd\n', cmd, '\n');
|
|
||||||
sysExec(cmd, cbComplete, cbData);
|
|
||||||
};
|
|
||||||
|
|
||||||
obj.startEphemeral = function(name, base_name, cbComplete, cbData){
|
|
||||||
|
|
||||||
var output = '';
|
|
||||||
sysExec('lxc-start-ephemeral -o '+base_name+ ' -n '+name +' --union-type overlayfs -d', function(data){output+=data}, function(error){
|
|
||||||
if(output.match("doesn't exist.")) return cbData({status: 500, error: "doesn't exist."});
|
|
||||||
if(output.match("already exists.")) return cbData({status: 500, error: "already exists"});
|
|
||||||
if(output.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)) return cbData({status: 200, state:'RUNNING', ip: output.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)[0]});
|
|
||||||
cbData({'?': '?', data: output, name: name, base_name: base_name});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
obj.stop = function(name, cbComplete, cbData){
|
obj.start = function(name, callback){
|
||||||
sysExec('lxc-stop -n '+ name, cbComplete, cbData);
|
var cmd = 'lxc-start --name '+name+' --daemon';
|
||||||
|
console.log('start cmd\n', cmd, '\n');
|
||||||
|
sysExec(cmd, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
obj.startEphemeral = function(name, base_name, callback){
|
||||||
|
|
||||||
|
var output = '';
|
||||||
|
sysExec('lxc-start-ephemeral -o '+base_name+ ' -n '+name +' --union-type overlayfs -d', function(data){
|
||||||
|
if(data.match("doesn't exist.")) return callback({status: 500, error: "doesn't exist."});
|
||||||
|
if(data.match("already exists.")) return callback({status: 500, error: "already exists"});
|
||||||
|
if(data.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)) return callback({status: 200, state:'RUNNING', ip: data.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)[0]});
|
||||||
|
callback({'?': '?', data: data, name: name, base_name: base_name});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
obj.stop = function(name, callback){
|
||||||
|
console.log('stop', name);
|
||||||
|
sysExec('lxc-stop -n '+ name, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -79,33 +87,31 @@ module.exports = function(config){
|
|||||||
sysExec('lxc-unfreeze -n '+name, cbComplete, cbData);
|
sysExec('lxc-unfreeze -n '+name, cbComplete, cbData);
|
||||||
};
|
};
|
||||||
|
|
||||||
obj.info = function(name, cbComplete, cbData){
|
obj.info = function(name, callback){
|
||||||
|
|
||||||
var output = '';
|
var output = '';
|
||||||
sysExec('lxc-info -n'+name, function(data){output+=data}, function(error){
|
sysExec('lxc-info -n '+name, function(data){
|
||||||
if(output.match("doesn't exist")) return cbData({state: 'NULL'});
|
console.log('info', name, data)
|
||||||
|
if(data.match("doesn't exist")) return callback({state: 'NULL'});
|
||||||
var info = {};
|
var info = {};
|
||||||
output = output.replace(/\suse/ig, '').replace(/\sbytes/ig, '').split("\n").slice(0,-1);
|
data = data.replace(/\suse/ig, '').replace(/\sbytes/ig, '').split("\n").slice(0,-1);
|
||||||
for(var i in output){
|
for(var i in data){
|
||||||
var temp = output[i].split(/\:\s+/);
|
var temp = data[i].split(/\:\s+/);
|
||||||
info[temp[0].toLowerCase().trim()] = temp[1].trim();
|
info[temp[0].toLowerCase().trim()] = temp[1].trim();
|
||||||
}
|
}
|
||||||
cbData(info);
|
callback(info);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
obj.list = function(cbData){
|
obj.list = function(callback){
|
||||||
|
sysExec('lxc-ls --fancy', function(data){
|
||||||
var output = '';
|
var output = data.split("\n");
|
||||||
sysExec('lxc-ls --fancy', function(data){output+=data}, function(error){
|
|
||||||
|
|
||||||
output = output.split("\n");
|
|
||||||
var keys = output.splice(0,1)[0].split(/\s+/).slice(0,-1);
|
var keys = output.splice(0,1)[0].split(/\s+/).slice(0,-1);
|
||||||
|
var info = [];
|
||||||
|
|
||||||
keys = keys.map(function(v){return v.toLowerCase()});
|
keys = keys.map(function(v){return v.toLowerCase()});
|
||||||
output = output.slice(0).splice(1).slice(0,-1);
|
output = output.slice(0).splice(1).slice(0,-1);
|
||||||
|
|
||||||
var info = [];
|
|
||||||
|
|
||||||
for (var i in output)
|
for (var i in output)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -115,11 +121,9 @@ module.exports = function(config){
|
|||||||
info.push(mapOut);
|
info.push(mapOut);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
callback(info);
|
||||||
cbData(info);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
@ -12,6 +12,12 @@
|
|||||||
"ejs": "~2.3.3",
|
"ejs": "~2.3.3",
|
||||||
"express": "~4.13.1",
|
"express": "~4.13.1",
|
||||||
"morgan": "~1.6.1",
|
"morgan": "~1.6.1",
|
||||||
"serve-favicon": "~2.3.0"
|
"serve-favicon": "~2.3.0",
|
||||||
|
"node.extend": "~1.1.5",
|
||||||
|
"redis": "~2.4.2",
|
||||||
|
"child": "0.0.3",
|
||||||
|
"colors": "~1.1.2",
|
||||||
|
"async": "~1.5.2",
|
||||||
|
"node-cmd": "~1.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,11 +3,11 @@ var router = express.Router();
|
|||||||
var extend = require('node.extend');
|
var extend = require('node.extend');
|
||||||
var redis = require("redis");
|
var redis = require("redis");
|
||||||
var client = redis.createClient();
|
var client = redis.createClient();
|
||||||
var lxc = require('../lxc')({sshBind: ['/usr/bin/ssh', 'virt@127.0.0.1']});
|
var lxc = require('../lxc')({sshBind: false/*['/usr/bin/ssh', 'virt@127.0.0.1']*/});
|
||||||
//lxc.startEphemeral('ubuntu_template', 'ue0', function(){console.log('cb1', arguments)}, function(){console.log('cb2', arguments)})
|
//lxc.startEphemeral('ubuntu_template', 'ue0', function(){console.log('cb1', arguments)}, function(){console.log('cb2', arguments)})
|
||||||
|
|
||||||
router.get('/start/:name', function(req, res, next){
|
router.get('/start/:name', function(req, res, next){
|
||||||
lxc.start(req.params.name, null, function(status, message){
|
lxc.start(req.params.name, function(status, message){
|
||||||
if(status){
|
if(status){
|
||||||
res.json({status: 500, name: req.params.name, message: message});
|
res.json({status: 500, name: req.params.name, message: message});
|
||||||
}else{
|
}else{
|
||||||
@ -30,13 +30,13 @@ router.get('/start/:name', function(req, res, next){
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/live/:template/:name', function(req, res, next){
|
router.get('/live/:template/:name', function(req, res, next){
|
||||||
lxc.startEphemeral(req.params.name, req.params.template, null, function (data) {
|
lxc.startEphemeral(req.params.name, req.params.template, function (data) {
|
||||||
res.json(data);
|
res.json(data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/stop/:name', function(req, res, next){
|
router.get('/stop/:name', function(req, res, next){
|
||||||
lxc.stop(req.params.name, null, function(data, message){
|
lxc.stop(req.params.name, function(data, message){
|
||||||
if(data){
|
if(data){
|
||||||
res.json({status: 500, name: req.params.name, message: message});
|
res.json({status: 500, name: req.params.name, message: message});
|
||||||
}else{
|
}else{
|
||||||
@ -56,9 +56,9 @@ router.get('/clone/:template/:name', function(req, res, next){
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/destroy/:name', function(req, res, next){
|
router.get('/destroy/:name', function(req, res, next){
|
||||||
lxc.destroy(req.params.name, null, function(data, message){
|
lxc.destroy(req.params.name, function(data){
|
||||||
if(data){
|
if(data){
|
||||||
res.json({status: 500, message: message});
|
res.json({status: 500, message: data});
|
||||||
}else{
|
}else{
|
||||||
res.json({status: 200});
|
res.json({status: 200});
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ router.get('/destroy/:name', function(req, res, next){
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/info/:name', function(req, res, next){
|
router.get('/info/:name', function(req, res, next){
|
||||||
lxc.info(req.params.name, null, function(data){
|
lxc.info(req.params.name, function(data){
|
||||||
res.json(data);
|
res.json(data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
if(data.state === 'RUNNING'){
|
if(data.state === 'RUNNING'){
|
||||||
var url = '//'+data.name+'.vm42.us';
|
var url = '//'+data.name+'.vm42.us';
|
||||||
data.url = '<a href="'+url+'" target="_blank">'+url+'</a>';
|
data.url = '<a href="'+url+'" target="_blank">'+url+'</a>';
|
||||||
}else if( data.state !== "RUNNING" && data.name.match(/_template/) ){
|
}else if( data.state !== "RUNNING" && data.name.match(/\w\w/) ){
|
||||||
data.clone = $('#cloneFormTemplate').html();
|
data.clone = $('#cloneFormTemplate').html();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user