From 48b4dd54536a23ac0c88cccbb2ace9fd59807582 Mon Sep 17 00:00:00 2001 From: william Date: Tue, 2 Feb 2016 16:44:29 -0500 Subject: [PATCH 1/8] runner --- routes/api.js | 54 +++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/routes/api.js b/routes/api.js index 942c4ee..ddb4a66 100644 --- a/routes/api.js +++ b/routes/api.js @@ -8,26 +8,34 @@ var client = redis.createClient(); var request = require('request'); var lxc = require('../lxc'); +var runner = function(res, req, ip){ + return request.post({url:'http://'+ip, form: req.body}, function(error, response, body){ + body = JSON.parse(body); + body['ip'] = ip.replace('10.0.', '') + return res.json(body) + }); +}; +var addToRedis = function(){ + lxc.info(req.params.name, null, function(data){ + var domain = req.query.domain || 'vm42.us'; + domain = req.params.name+'.'+domain; + client.SADD("hosts", domain, function(){}); + + var ip = data.ip + ':5000'; + client.HSET(domain, "ip", ip, redis.print); + client.HSET(domain, "updated", (new Date).getTime(), redis.print); + client.hset(domain, "include", "proxy.include"); + return res.json({status: 200, info: data}); + }); +}; + router.get('/start/:name', function(req, res, next){ - lxc.start(req.params.name, function(data){ + return lxc.start(req.params.name, function(data){ console.log('start', arguments); if(!data){ - res.json({status: 500, name: req.params.name, message: data}); + return res.json({status: 500, name: req.params.name, message: data}); }else{ - setTimeout(function() { - lxc.info(req.params.name, null, function(data){ - var domain = req.query.domain || 'vm42.us'; - domain = req.params.name+'.'+domain; - client.SADD("hosts", domain, function(){}); - - var ip = data.ip + ':5000'; - client.HSET(domain, "ip", ip, redis.print); - client.HSET(domain, "updated", (new Date).getTime(), redis.print); - client.hset(domain, "include", "proxy.include"); - res.json({status: 200, info: data}); - }); - }, 5000); - + res.json({}) } }); }); @@ -85,29 +93,15 @@ router.get('/list', function(req, res, next) { }); router.post('/run/:ip?', function(req, res, next){ - - var runner = function(res, req, ip){ - console.log('runner on', ip,'with body:\n', JSON.stringify(req.body)); - return request.post({url:'http://'+ip, form: req.body}, function(error, response, body){ - console.log('request args:', arguments) - body = JSON.parse(body); - body['ip'] = ip.replace('10.0.', '') - return res.json(body) - }); - }; - - if(req.params.ip){ var ip = '10.0.'+ req.params.ip; return runner(res, req, ip); }else{ var name = 'u1-'+(Math.random()*100).toString().replace('.',''); - console.log('new VM', name); return lxc.startEphemeral(name, 'u1', function(data){ return runner(res, req, data.ip); }); } - }); module.exports = router; From d136e97fa5192db0a18aa725fd4d4866b3c994f1 Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 13:21:39 -0500 Subject: [PATCH 2/8] runner --- routes/api.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/routes/api.js b/routes/api.js index ddb4a66..c93a91e 100644 --- a/routes/api.js +++ b/routes/api.js @@ -92,9 +92,19 @@ router.get('/list', function(req, res, next) { }); }); -router.post('/run/:ip?', function(req, res, next){ - if(req.params.ip){ +router.post('/run/:ip?', function doRun(req, res, next){ + // check if server is + + lxc.list(function(data){ var ip = '10.0.'+ req.params.ip; + data.forEach(function(idx, element){ + if(element.ipv4 === ip){ + runner(req, res, ip) + } + }); + }); + + if(req.params.ip){ return runner(res, req, ip); }else{ var name = 'u1-'+(Math.random()*100).toString().replace('.',''); From 26eed34abdaa96637fa080c2ec270eb12c98208f Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 13:32:16 -0500 Subject: [PATCH 3/8] runner --- routes/api.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/routes/api.js b/routes/api.js index c93a91e..e3e0f0c 100644 --- a/routes/api.js +++ b/routes/api.js @@ -8,7 +8,7 @@ var client = redis.createClient(); var request = require('request'); var lxc = require('../lxc'); -var runner = function(res, req, ip){ +var runner = function(req, res, ip){ return request.post({url:'http://'+ip, form: req.body}, function(error, response, body){ body = JSON.parse(body); body['ip'] = ip.replace('10.0.', '') @@ -95,23 +95,27 @@ router.get('/list', function(req, res, next) { router.post('/run/:ip?', function doRun(req, res, next){ // check if server is - lxc.list(function(data){ + return lxc.list(function(data){ + if(!req.params.ip) data = []; var ip = '10.0.'+ req.params.ip; - data.forEach(function(idx, element){ - if(element.ipv4 === ip){ - runner(req, res, ip) + var found = false; + + for(var idx=data.length; idx--;){ + if( data[idx]['ipv4'] === ip ){ + found = true; + break; } - }); + } + + if(found){ + return runner(req, res, ip) + }else{ + return lxc.startEphemeral(name, 'u1', function(data){ + return runner(req, res, data.ip); + }); + } }); - if(req.params.ip){ - return runner(res, req, ip); - }else{ - var name = 'u1-'+(Math.random()*100).toString().replace('.',''); - return lxc.startEphemeral(name, 'u1', function(data){ - return runner(res, req, data.ip); - }); - } }); module.exports = router; From 74c73e8c6d436e5fa25d6c8a9c2bd60b932ed6ea Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 13:33:01 -0500 Subject: [PATCH 4/8] runner --- routes/api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/routes/api.js b/routes/api.js index e3e0f0c..358afa9 100644 --- a/routes/api.js +++ b/routes/api.js @@ -110,6 +110,7 @@ router.post('/run/:ip?', function doRun(req, res, next){ if(found){ return runner(req, res, ip) }else{ + var name = 'u1-'+(Math.random()*100).toString().replace('.',''); return lxc.startEphemeral(name, 'u1', function(data){ return runner(req, res, data.ip); }); From 7f24f3f772561f679f1a34dbd45951e53a91bc8c Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 16:59:21 -0500 Subject: [PATCH 5/8] runner --- lxc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lxc.js b/lxc.js index 648f16b..7af3eaa 100644 --- a/lxc.js +++ b/lxc.js @@ -91,6 +91,7 @@ var lxc = { var info = []; keys = keys.map(function(v){return v.toLowerCase()}); + console.log(output) output = output.slice(0).splice(1).slice(0,-1); for(var i in output){ From 1558c54e973c4e0fd07cb776e8361a3d9e0ff1a9 Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 17:03:09 -0500 Subject: [PATCH 6/8] runner --- lxc.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lxc.js b/lxc.js index 7af3eaa..a37b0e8 100644 --- a/lxc.js +++ b/lxc.js @@ -91,11 +91,10 @@ var lxc = { var info = []; keys = keys.map(function(v){return v.toLowerCase()}); - console.log(output) - output = output.slice(0).splice(1).slice(0,-1); + output = output.slice(0).slice(0,-1); for(var i in output){ - + console.log(i, output[i]) var aIn = output[i].split(/\s+/).slice(0,-1); var mapOut = {}; aIn.map(function(value,idx){ From 073c147389133cafdec8ce030c6a2368ed33a31d Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 17:04:50 -0500 Subject: [PATCH 7/8] runner --- lxc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxc.js b/lxc.js index a37b0e8..6f8a66c 100644 --- a/lxc.js +++ b/lxc.js @@ -94,7 +94,7 @@ var lxc = { output = output.slice(0).slice(0,-1); for(var i in output){ - console.log(i, output[i]) + if(output[i].match(/^-/)) continue; var aIn = output[i].split(/\s+/).slice(0,-1); var mapOut = {}; aIn.map(function(value,idx){ From 7794e4b2ca489d406fd7a213283b66ebd79d5902 Mon Sep 17 00:00:00 2001 From: william Date: Wed, 3 Feb 2016 17:44:19 -0500 Subject: [PATCH 8/8] runner --- routes/api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/api.js b/routes/api.js index 358afa9..a7d6887 100644 --- a/routes/api.js +++ b/routes/api.js @@ -11,8 +11,8 @@ var lxc = require('../lxc'); var runner = function(req, res, ip){ return request.post({url:'http://'+ip, form: req.body}, function(error, response, body){ body = JSON.parse(body); - body['ip'] = ip.replace('10.0.', '') - return res.json(body) + body['ip'] = ip.replace('10.0.', ''); + return res.json(body); }); }; var addToRedis = function(){