This commit is contained in:
William Mantly 2016-02-02 16:44:29 -05:00
parent 1d8fa99ddc
commit 48b4dd5453

View File

@ -8,13 +8,14 @@ var client = redis.createClient();
var request = require('request'); var request = require('request');
var lxc = require('../lxc'); var lxc = require('../lxc');
router.get('/start/:name', function(req, res, next){ var runner = function(res, req, ip){
lxc.start(req.params.name, function(data){ return request.post({url:'http://'+ip, form: req.body}, function(error, response, body){
console.log('start', arguments); body = JSON.parse(body);
if(!data){ body['ip'] = ip.replace('10.0.', '')
res.json({status: 500, name: req.params.name, message: data}); return res.json(body)
}else{ });
setTimeout(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';
domain = req.params.name+'.'+domain; domain = req.params.name+'.'+domain;
@ -24,10 +25,17 @@ router.get('/start/:name', function(req, res, next){
client.HSET(domain, "ip", ip, redis.print); client.HSET(domain, "ip", ip, redis.print);
client.HSET(domain, "updated", (new Date).getTime(), redis.print); client.HSET(domain, "updated", (new Date).getTime(), redis.print);
client.hset(domain, "include", "proxy.include"); client.hset(domain, "include", "proxy.include");
res.json({status: 200, info: data}); return res.json({status: 200, info: data});
}); });
}, 5000); };
router.get('/start/:name', function(req, res, next){
return lxc.start(req.params.name, function(data){
console.log('start', arguments);
if(!data){
return res.json({status: 500, name: req.params.name, message: data});
}else{
res.json({})
} }
}); });
}); });
@ -85,29 +93,15 @@ router.get('/list', function(req, res, next) {
}); });
router.post('/run/:ip?', 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){ if(req.params.ip){
var ip = '10.0.'+ req.params.ip; var ip = '10.0.'+ req.params.ip;
return runner(res, req, ip); return runner(res, req, ip);
}else{ }else{
var name = 'u1-'+(Math.random()*100).toString().replace('.',''); var name = 'u1-'+(Math.random()*100).toString().replace('.','');
console.log('new VM', name);
return lxc.startEphemeral(name, 'u1', function(data){ return lxc.startEphemeral(name, 'u1', function(data){
return runner(res, req, data.ip); return runner(res, req, data.ip);
}); });
} }
}); });
module.exports = router; module.exports = router;