diff --git a/lxc.js b/lxc.js index 6f8a66c..c392780 100644 --- a/lxc.js +++ b/lxc.js @@ -13,6 +13,29 @@ function sysExec(command, 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); + } + }); + + return orm +}; + var lxc = { create: function(name, template, config, callback){ return sysExec('lxc-create -n '+name+' -t '+template, callback); @@ -27,13 +50,12 @@ var lxc = { var info = data.match(/Destroyed container/); console.log('destroy info:', info); var args = [true].concat(Array.prototype.slice.call(arguments, 1)); - callback.apply(this, args); + return callback.apply(this, args); }); }, start: function(name, callback){ - var cmd = 'lxc-start --name '+name+' --daemon'; - return sysExec(cmd, callback); + return sysExec('lxc-start --name '+name+' --daemon', callback); }, startEphemeral: function(name, base_name, callback){ @@ -43,8 +65,8 @@ var lxc = { 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('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]}); @@ -93,8 +115,9 @@ var lxc = { keys = keys.map(function(v){return v.toLowerCase()}); output = output.slice(0).slice(0,-1); - for(var i in output){ - if(output[i].match(/^-/)) continue; + for(var i in output){ + if(output[i].match(/^-/)) continue; // compatibility with 1.x and 2.x output + var aIn = output[i].split(/\s+/).slice(0,-1); var mapOut = {}; aIn.map(function(value,idx){ diff --git a/routes/api.js b/routes/api.js index a7d6887..4fb20de 100644 --- a/routes/api.js +++ b/routes/api.js @@ -15,6 +15,7 @@ var runner = function(req, res, ip){ return res.json(body); }); }; + var addToRedis = function(){ lxc.info(req.params.name, null, function(data){ var domain = req.query.domain || 'vm42.us'; @@ -35,7 +36,7 @@ router.get('/start/:name', function(req, res, next){ if(!data){ return res.json({status: 500, name: req.params.name, message: data}); }else{ - res.json({}) + res.json({}); } }); });