commit
b7ae900557
38
lxc.js
38
lxc.js
@ -13,6 +13,29 @@ function sysExec(command, callback){
|
|||||||
})(callback));
|
})(callback));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var Container = function(config){
|
||||||
|
this.name = config.name;
|
||||||
|
this.state = config.state;
|
||||||
|
this.ip = config.ip || (config.ipv4 || '').replace('-', '') || null ;
|
||||||
|
}
|
||||||
|
|
||||||
|
Container.prototype.autoShutDown = function(time) {
|
||||||
|
time = time || 600000;
|
||||||
|
|
||||||
|
// this.__shutDownTimeout = setTimeout(function(){}, this.autoShutDown):
|
||||||
|
};
|
||||||
|
|
||||||
|
var lxcORM = function(){
|
||||||
|
var orm = {}
|
||||||
|
lxc.list(function(data){
|
||||||
|
for(var idx = data.length; idx--;){
|
||||||
|
orm[data[idx].name] = new Container(data[idx]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return orm
|
||||||
|
};
|
||||||
|
|
||||||
var lxc = {
|
var lxc = {
|
||||||
create: function(name, template, config, callback){
|
create: function(name, template, config, callback){
|
||||||
return sysExec('lxc-create -n '+name+' -t '+template, callback);
|
return sysExec('lxc-create -n '+name+' -t '+template, callback);
|
||||||
@ -27,13 +50,12 @@ var lxc = {
|
|||||||
var info = data.match(/Destroyed container/);
|
var info = data.match(/Destroyed container/);
|
||||||
console.log('destroy info:', info);
|
console.log('destroy info:', info);
|
||||||
var args = [true].concat(Array.prototype.slice.call(arguments, 1));
|
var args = [true].concat(Array.prototype.slice.call(arguments, 1));
|
||||||
callback.apply(this, args);
|
return callback.apply(this, args);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
start: function(name, callback){
|
start: function(name, callback){
|
||||||
var cmd = 'lxc-start --name '+name+' --daemon';
|
return sysExec('lxc-start --name '+name+' --daemon', callback);
|
||||||
return sysExec(cmd, callback);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
startEphemeral: function(name, base_name, callback){
|
startEphemeral: function(name, base_name, callback){
|
||||||
@ -43,8 +65,8 @@ var lxc = {
|
|||||||
if(data.match("doesn't exist.")){
|
if(data.match("doesn't exist.")){
|
||||||
return callback({status: 500, error: "doesn't exist."});
|
return callback({status: 500, error: "doesn't exist."});
|
||||||
}
|
}
|
||||||
if(data.match("already exists.")){
|
if(data.match('already exists.')){
|
||||||
return callback({status: 500, error: "already exists"});
|
return callback({status: 500, error: 'already exists'});
|
||||||
}
|
}
|
||||||
if(data.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)){
|
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]});
|
return callback({status: 200, state:'RUNNING', ip: data.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)[0]});
|
||||||
@ -94,7 +116,8 @@ var lxc = {
|
|||||||
output = output.slice(0).slice(0,-1);
|
output = output.slice(0).slice(0,-1);
|
||||||
|
|
||||||
for(var i in output){
|
for(var i in output){
|
||||||
if(output[i].match(/^-/)) continue;
|
if(output[i].match(/^-/)) continue; // compatibility with 1.x and 2.x output
|
||||||
|
|
||||||
var aIn = output[i].split(/\s+/).slice(0,-1);
|
var aIn = output[i].split(/\s+/).slice(0,-1);
|
||||||
var mapOut = {};
|
var mapOut = {};
|
||||||
aIn.map(function(value,idx){
|
aIn.map(function(value,idx){
|
||||||
@ -110,3 +133,6 @@ var lxc = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = lxc;
|
module.exports = lxc;
|
||||||
|
|
||||||
|
var orm = lxcORM()
|
||||||
|
setTimeout(function(){console.log(orm)}, 5000)
|
||||||
|
@ -15,6 +15,7 @@ var runner = function(req, res, ip){
|
|||||||
return res.json(body);
|
return res.json(body);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var addToRedis = function(){
|
var addToRedis = function(){
|
||||||
lxc.info(req.params.name, null, function(data){
|
lxc.info(req.params.name, null, function(data){
|
||||||
var domain = req.query.domain || 'vm42.us';
|
var domain = req.query.domain || 'vm42.us';
|
||||||
@ -35,7 +36,7 @@ router.get('/start/:name', function(req, res, next){
|
|||||||
if(!data){
|
if(!data){
|
||||||
return res.json({status: 500, name: req.params.name, message: data});
|
return res.json({status: 500, name: req.params.name, message: data});
|
||||||
}else{
|
}else{
|
||||||
res.json({})
|
res.json({});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user