commit
6b662f131d
3
.gitignore
vendored
3
.gitignore
vendored
@ -29,3 +29,6 @@ node_modules
|
|||||||
# Debug log from npm
|
# Debug log from npm
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
.c9
|
.c9
|
||||||
|
|
||||||
|
# keys
|
||||||
|
secrets.js
|
119
doapi.js
Normal file
119
doapi.js
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
var request = require('request');
|
||||||
|
|
||||||
|
api = function(key){
|
||||||
|
key = key || require('./secrets.js').doAPI;
|
||||||
|
this.BASEURL = 'https://api.digitalocean.com/v2/';
|
||||||
|
this.headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer '+key
|
||||||
|
}
|
||||||
|
this.calls = 0;
|
||||||
|
|
||||||
|
this.account = function(callback){
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'account',
|
||||||
|
headers: this.headers
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.get(options, function(error, response, body){
|
||||||
|
return callback(body, response, error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.dropletsByTag = function(tag, callback){
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'droplets?tag_name='+tag,
|
||||||
|
headers: this.headers
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.get(options, function(error, response, body){
|
||||||
|
return callback(body, response, error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.dropletSetTag = function(tag, dropletID, callback) {
|
||||||
|
var data = {
|
||||||
|
resources: [
|
||||||
|
{
|
||||||
|
resource_id: dropletID,
|
||||||
|
resource_type: 'droplet'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'tags/'+tag+'/resources',
|
||||||
|
headers: this.headers,
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.post(options, function(error, response, body){
|
||||||
|
return callback(body, response, error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.dropletCreate = function(args, callback){
|
||||||
|
var data = {
|
||||||
|
name: args.name, // || return false,
|
||||||
|
region: args.region || 'nyc3',
|
||||||
|
size: args.size || '512mb',
|
||||||
|
image: args.image || 'ubuntu-14-04-x64',
|
||||||
|
ssh_keys: args.ssh_key || null,
|
||||||
|
backups: args.backup || false,
|
||||||
|
private_networking: args.private_networking || true,
|
||||||
|
user_data: args.user_data || null
|
||||||
|
};
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'droplets',
|
||||||
|
headers: this.headers,
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.post(options, function(error, response, body){
|
||||||
|
return callback(body, response, error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dropletDestroy = function(dropletID, callback){
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'droplets/'+dropletID,
|
||||||
|
headers: this.headers
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.del(options, function(error, response, body){
|
||||||
|
callback(body, response, error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.dropletInfo = function(dropletID, callback){
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'droplets/'+dropletID,
|
||||||
|
headers: this.headers
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.get(options, function(error, response, body){
|
||||||
|
callback(body, response, error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.tagsList = function(callback){
|
||||||
|
var options = {
|
||||||
|
url: this.BASEURL+'tags',
|
||||||
|
headers: this.headers
|
||||||
|
};
|
||||||
|
this.calls++;
|
||||||
|
|
||||||
|
return request.get(options, function(e,r,b){
|
||||||
|
callback(b,r,e);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = api;
|
339
lxc.js
339
lxc.js
@ -1,31 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var exec = require('child_process').exec;
|
var exec = require('child_process').exec;
|
||||||
var extend = require('node.extend');
|
|
||||||
|
|
||||||
|
function sysExec(command, ip, callback){
|
||||||
var parseArgs = function(config){
|
ip = ip || '104.236.77.157';
|
||||||
var all = Object.keys(config.defaults);
|
command = new Buffer(command).toString('base64')
|
||||||
// console.log(all)
|
command = 'ssh -o StrictHostKeyChecking=no virt@'+ ip + ' "echo ' + command + '|base64 --decode|bash"';
|
||||||
for(var i=config.required.length; i--;){
|
// command = 'unset XDG_SESSION_ID XDG_RUNTIME_DIR; cgm movepid all virt $$; ' + command;
|
||||||
if(all.indexOf(config.required[i]) !== -1){
|
|
||||||
config.required.splice(i, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(config.required.length !== 0) return false;
|
|
||||||
|
|
||||||
var out = '';
|
|
||||||
for(var i=0; i< config.takes.length; i++){
|
|
||||||
if(all.indexOf(config.takes[i]) !== -1){
|
|
||||||
out += '--'+config.takes[i]+' '+config.defaults[config.takes[i]]+' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
};
|
|
||||||
|
|
||||||
function sysExec(command, callback){
|
|
||||||
command = 'unset XDG_SESSION_ID XDG_RUNTIME_DIR; cgm movepid all virt $$; ' + command;
|
|
||||||
|
|
||||||
return exec(command, (function(callback){
|
return exec(command, (function(callback){
|
||||||
return function(err,data,stderr){
|
return function(err,data,stderr){
|
||||||
@ -36,237 +16,102 @@ function sysExec(command, callback){
|
|||||||
})(callback));
|
})(callback));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var lxc = {
|
||||||
|
exec: sysExec,
|
||||||
|
|
||||||
var Container = function(config){
|
create: function(name, template, config, callback){
|
||||||
this.name = config.name;
|
return sysExec('lxc-create -n '+name+' -t '+template, callback);
|
||||||
this.state = config.state || 'STOPPED';
|
},
|
||||||
this.ip = config.ip || (config.ipv4 || '').replace('-', '') || null;
|
|
||||||
this.overlayfs = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
Container.prototype.clone = function(callback){
|
clone: function(name, base_name, callback){
|
||||||
var overlayfs = this.overlayfs ? ' -B overlayfs -s ' : '';
|
return sysExec('lxc-clone -o '+base_name+ ' -n '+name +' -B overlayfs -s', callback);
|
||||||
|
},
|
||||||
|
|
||||||
return sysExec('lxc-clone -o '+this.orig+ ' -n '+this.name + overlayfs, callback);
|
destroy: function(name, callback){
|
||||||
};
|
return sysExec('lxc-destroy -n '+ name, function(data){
|
||||||
|
var info = data.match(/Destroyed container/);
|
||||||
|
// console.log('destroy info:', info);
|
||||||
|
var args = [true].concat(Array.prototype.slice.call(arguments, 1));
|
||||||
|
return callback.apply(this, args);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
Container.prototype.start = function(callback){
|
start: function(name, callback){
|
||||||
var args = parseArgs({
|
return sysExec('lxc-start --name '+name+' --daemon', callback);
|
||||||
required: ['name'],
|
},
|
||||||
takes: ['name'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
|
|
||||||
});
|
startEphemeral: function(name, base_name, ip, callback){
|
||||||
|
var command = 'lxc-start-ephemeral -o '+base_name+ ' -n '+name +' --union-type overlayfs -d';
|
||||||
var that = this;
|
return sysExec(command, ip, function(data){
|
||||||
callback = function(callback){
|
// console.log('startEphemeral', arguments);
|
||||||
that.info();
|
if(data.match("doesn't exist.")){
|
||||||
return callback;
|
return callback({status: 500, error: "doesn't exist."});
|
||||||
};
|
}
|
||||||
return sysExec('lxc-start --daemon '+args, callback);
|
if(data.match('already exists.')){
|
||||||
};
|
return callback({status: 500, error: 'already exists'});
|
||||||
|
}
|
||||||
Container.prototype.startEphemeral = function(callback){
|
if(data.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)){
|
||||||
var args = parseArgs({
|
return callback({status: 200, state:'RUNNING', ip: data.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)[0]});
|
||||||
required: ['orig'],
|
|
||||||
takes: ['orig', 'name', 'key', 'union-type', 'keep-data'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
var command = 'lxc-start-ephemeral --daemon '+args;
|
|
||||||
return sysExec(command, function(data){
|
|
||||||
console.log('startEphemeral', arguments);
|
|
||||||
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});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.destroy = function(callback){
|
|
||||||
var args = parseArgs({
|
|
||||||
required: ['name'],
|
|
||||||
takes: ['name', 'force'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
});
|
|
||||||
|
|
||||||
return sysExec('lxc-destroy '+ args, function(data){
|
|
||||||
var info = data.match(/Destroyed container/);
|
|
||||||
console.log('destroy info:', info);
|
|
||||||
var args = [true].concat(Array.prototype.slice.call(arguments, 1));
|
|
||||||
return callback.apply(this, args);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
Container.prototype.stop = function(callback){
|
|
||||||
var args = parseArgs({
|
|
||||||
required: ['name'],
|
|
||||||
takes: ['name', 'reboot', 'nowait', 'timeout', 'kill'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
|
|
||||||
});
|
|
||||||
var that = this;
|
|
||||||
callback = function(callback){
|
|
||||||
that.info();
|
|
||||||
return callback;
|
|
||||||
};
|
|
||||||
return sysExec('lxc-stop '+args, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.freeze = function(callback){
|
|
||||||
var args = parseArgs({
|
|
||||||
required: ['name'],
|
|
||||||
takes: ['name', 'force'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
});
|
|
||||||
return sysExec('lxc-freeze -n '+name, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.unfreeze = function(callback){
|
|
||||||
|
|
||||||
return sysExec('lxc-unfreeze --name '+this.name, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.info = function(callback){
|
|
||||||
var args = parseArgs({
|
|
||||||
required: ['name'],
|
|
||||||
takes: ['name', 'reboot', 'nowait', 'timeout', 'kill'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
|
|
||||||
});
|
|
||||||
return sysExec('lxc-stop '+args, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.freeze = function(callback){
|
|
||||||
var args = parseArgs({
|
|
||||||
required: ['name'],
|
|
||||||
takes: ['name', 'force'],
|
|
||||||
defaults: extend({}, this)
|
|
||||||
});
|
|
||||||
return sysExec('lxc-freeze -n '+name, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.unfreeze = function(callback){
|
|
||||||
|
|
||||||
return sysExec('lxc-unfreeze --name '+this.name, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.info = function(callback){
|
|
||||||
var that = this;
|
|
||||||
callback = callback || function(){}
|
|
||||||
|
|
||||||
return sysExec('lxc-info --name '+this.name, function(data){
|
|
||||||
// console.log('info', arguments);
|
|
||||||
if(data.match("doesn't exist")){
|
|
||||||
return callback({state: 'NULL'});
|
|
||||||
}
|
|
||||||
|
|
||||||
var info = {};
|
|
||||||
data = data.replace(/\suse/ig, '').replace(/\sbytes/ig, '').split("\n").slice(0,-1);
|
|
||||||
for(var i in data){
|
|
||||||
var temp = data[i].split(/\:\s+/);
|
|
||||||
info[temp[0].toLowerCase().trim()] = temp[1].trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
that.updateFromInfo(info);
|
|
||||||
|
|
||||||
var args = [info].concat(Array.prototype.slice.call(arguments, 1));
|
|
||||||
return callback.apply(that, args);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.updateFromInfo = function(data){
|
|
||||||
var keys = ['state', 'ip', 'total', 'rx', 'tx', 'link', 'kmem', 'memory', 'blkio', 'cpu', 'pid'];
|
|
||||||
for(var i=keys.length; i--;){
|
|
||||||
this[keys[i]] = data[keys[i]];
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var lxcORM = function(){
|
|
||||||
this.containers = {};
|
|
||||||
this.isReady = false;
|
|
||||||
this.whenReady = [];
|
|
||||||
var that = this;
|
|
||||||
|
|
||||||
this.list(function(data){
|
|
||||||
for(var idx = data.length; idx--;){
|
|
||||||
that.containers[data[idx].name] = new Container(data[idx]);
|
|
||||||
if(idx===0){
|
|
||||||
// console.log('call ready!')
|
|
||||||
that.callReady();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
};
|
callback({'?': '?', data: data, name: name, base_name: base_name});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
lxcORM.prototype.callReady = function(){
|
stop: function(name, ip, callback){
|
||||||
for(var idx=0; idx<this.whenReady.length; idx--){
|
return sysExec('lxc-stop -n '+ name, ip, callback);
|
||||||
this.whenReady[idx].apply(this);
|
},
|
||||||
}
|
|
||||||
this.isReady = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
lxcORM.prototype.ready = function(callback){
|
freeze: function(name, callback){
|
||||||
if(this.isReady){
|
return sysExec('lxc-freeze -n '+name, callback);
|
||||||
return callback.apply(this);
|
},
|
||||||
}
|
|
||||||
else{
|
unfreeze: function(name, callback){
|
||||||
this.whenReady.push(callback);
|
return sysExec('lxc-unfreeze -n '+name, callback);
|
||||||
|
},
|
||||||
|
|
||||||
|
info: function(name, callback){
|
||||||
|
return sysExec('lxc-info -n '+name, function(data){
|
||||||
|
// console.log('info', arguments);
|
||||||
|
if(data.match("doesn't exist")){
|
||||||
|
return callback({state: 'NULL'});
|
||||||
|
}
|
||||||
|
|
||||||
|
var info = {};
|
||||||
|
data = data.replace(/\suse/ig, '').replace(/\sbytes/ig, '').split("\n").slice(0,-1);
|
||||||
|
for(var i in data){
|
||||||
|
var temp = data[i].split(/\:\s+/);
|
||||||
|
info[temp[0].toLowerCase().trim()] = temp[1].trim();
|
||||||
|
}
|
||||||
|
var args = [info].concat(Array.prototype.slice.call(arguments, 1));
|
||||||
|
callback.apply(this, args);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
list: function(callback){
|
||||||
|
sysExec('lxc-ls --fancy', function(data){
|
||||||
|
var output = data.split("\n");
|
||||||
|
var keys = output.splice(0,1)[0].split(/\s+/).slice(0,-1);
|
||||||
|
var info = [];
|
||||||
|
|
||||||
|
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; // compatibility with 1.x and 2.x output
|
||||||
|
|
||||||
|
var aIn = output[i].split(/\s+/).slice(0,-1);
|
||||||
|
var mapOut = {};
|
||||||
|
aIn.map(function(value,idx){
|
||||||
|
mapOut[keys[idx]] = value;
|
||||||
|
});
|
||||||
|
info.push(mapOut);
|
||||||
|
|
||||||
|
}
|
||||||
|
var args = [info].concat(Array.prototype.slice.call(arguments, 1));
|
||||||
|
callback.apply(this, args);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
lxcORM.prototype.create = function(args, callback){
|
module.exports = lxc;
|
||||||
|
|
||||||
var args = parseArgs({
|
|
||||||
required: ['name', 'template'],
|
|
||||||
takes: ['name', 'template', ' ', 'd', 'r', 'a'],
|
|
||||||
defaults: extend({template:'download', ' ': ' ', d: 'ubuntu', r: 'trusty', a: 'amd64'}, args)
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
return sysExec('lxc-create '+args, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
lxcORM.prototype.list = function(callback){
|
|
||||||
sysExec('lxc-ls --fancy', function(data){
|
|
||||||
var output = data.split("\n");
|
|
||||||
var keys = output.splice(0,1)[0].split(/\s+/).slice(0,-1);
|
|
||||||
var info = [];
|
|
||||||
|
|
||||||
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; // compatibility with 1.x and 2.x output
|
|
||||||
|
|
||||||
var aIn = output[i].split(/\s+/).slice(0,-1);
|
|
||||||
var mapOut = {};
|
|
||||||
aIn.map(function(value,idx){
|
|
||||||
mapOut[keys[idx]] = value;
|
|
||||||
});
|
|
||||||
info.push(mapOut);
|
|
||||||
|
|
||||||
}
|
|
||||||
var args = [info].concat(Array.prototype.slice.call(arguments, 1));
|
|
||||||
callback.apply(this, args);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = new lxcORM();
|
|
||||||
|
410
routes/api.js
410
routes/api.js
@ -2,102 +2,300 @@
|
|||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
var extend = require('node.extend');
|
var util = require('util');
|
||||||
var redis = require("redis");
|
|
||||||
var client = redis.createClient();
|
|
||||||
var request = require('request');
|
var request = require('request');
|
||||||
var lxc = require('../lxc');
|
var lxc = require('../lxc');
|
||||||
var os = require('os');
|
var doapi = require('../doapi')();
|
||||||
var spawn = require('child_process').spawn;
|
|
||||||
|
|
||||||
|
var workerSnapID = 'V1'
|
||||||
|
|
||||||
var totalMem = os.totalmem();
|
// console.log = function(){};
|
||||||
var timeoutEvents = {};
|
|
||||||
var ip2name = {};
|
|
||||||
var availContainers = [];
|
|
||||||
var usedContainers = [];
|
|
||||||
|
|
||||||
var getFreeMem = function(callback){
|
var label2runner = {};
|
||||||
|
var workers = [];
|
||||||
|
var isCheckingWorkers = false;
|
||||||
|
|
||||||
var prc = spawn('free', ['-b']);
|
var workers = (function(){
|
||||||
|
var workers = [];
|
||||||
|
|
||||||
prc.stdout.setEncoding('utf8');
|
workers.currentCreatingMax = 2;
|
||||||
prc.stdout.on('data', function (data) {
|
|
||||||
var str = data.toString()
|
|
||||||
var lines = str.split(/\n/g);
|
|
||||||
for(var i = 0; i < lines.length; i++) {
|
|
||||||
lines[i] = lines[i].split(/\s+/);
|
|
||||||
}
|
|
||||||
var freeMem = Number(lines[2][3]);
|
|
||||||
return callback(freeMem);
|
|
||||||
});
|
|
||||||
|
|
||||||
prc.on('close', function (code) {
|
workers.currentCreating = 0;
|
||||||
});
|
|
||||||
|
workers.checkDroplet = function(id, time){
|
||||||
|
time = time || 10000;
|
||||||
|
|
||||||
|
return doapi.dropletInfo(id, function(data){
|
||||||
|
var worker = JSON.parse(data)['droplet'];
|
||||||
|
if(worker.status == 'active'){
|
||||||
|
console.log('Droplet is now active, starting runners in 20 seconds');
|
||||||
|
|
||||||
|
return setTimeout(function(worker){
|
||||||
|
console.log('Ready to start runners!');
|
||||||
|
workers.startRunners(workers.makeWorkerObj(worker), true);
|
||||||
|
workers.currentCreating--;
|
||||||
|
}, 20000, worker);
|
||||||
|
}else{
|
||||||
|
console.log('Worker not ready, check again in ', time, 'MS');
|
||||||
|
|
||||||
|
return setTimeout(function(){
|
||||||
|
workers.checkDroplet(id);
|
||||||
|
}, time);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
workers.create = function(){
|
||||||
|
if(workers.currentCreating > workers.currentCreatingMax ) return false;
|
||||||
|
return doapi.dropletCreate({
|
||||||
|
name: 'clw'+workerSnapID+'-'+(Math.random()*100).toString().slice(-4),
|
||||||
|
image: '17575764'
|
||||||
|
}, function(data){
|
||||||
|
data = JSON.parse(data);
|
||||||
|
workers.currentCreating++;
|
||||||
|
setTimeout(function(dopletNewID){
|
||||||
|
return workers.checkDroplet(dopletNewID);
|
||||||
|
}, 70000, data.droplet.id);
|
||||||
|
return doapi.dropletSetTag('clworker', data.droplet.id, function(){});
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
workers.destroy = function(worker){
|
||||||
|
var worker = worker || workers.pop();
|
||||||
|
return doapi.dropletDestroy(worker.id, function(){});
|
||||||
|
};
|
||||||
|
|
||||||
|
workers.makeWorkerObj = function(worker){
|
||||||
|
worker.networks.v4.forEach(function(value){
|
||||||
|
worker[value.type+'IP'] = value.ip_address;
|
||||||
|
});
|
||||||
|
worker.availrunners = [];
|
||||||
|
worker.ip = worker.privateIP;
|
||||||
|
worker.usedrunner = 0;
|
||||||
|
worker.index = workers.length,
|
||||||
|
worker.getRunner = function(){
|
||||||
|
if(this.availrunners.length === 0) return false;
|
||||||
|
console.log('getting runner from ', worker.name, ' avail length ', this.availrunners.length);
|
||||||
|
var runner = this.availrunners.pop();
|
||||||
|
this.usedrunner++;
|
||||||
|
label2runner[runner.label] = runner;
|
||||||
|
|
||||||
|
return runner;
|
||||||
|
}
|
||||||
|
|
||||||
|
return worker;
|
||||||
|
};
|
||||||
|
|
||||||
|
workers.destroyOld = function(){
|
||||||
|
doapi.dropletsByTag('clworker', function(data){
|
||||||
|
data = JSON.parse(data);
|
||||||
|
data['droplets'].forEach(function(worker){
|
||||||
|
console.log('found old droplet, killing it');
|
||||||
|
doapi.dropletDestroy(worker.id, function(){});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
workers.startRunners = function(worker, newWorker, stopPercent){
|
||||||
|
// console.log('starting runners on', worker.name, worker.ip)
|
||||||
|
stopPercent = stopPercent || 80;
|
||||||
|
ramPercentUsed(worker.ip, function(usedMemPercent){
|
||||||
|
if(usedMemPercent < stopPercent ){
|
||||||
|
var name = 'crunner-'+(Math.random()*100).toString().slice(-4);
|
||||||
|
return lxc.startEphemeral(name, 'crunner0', worker.ip, function(data){
|
||||||
|
if(!data.ip) return setTimeout(workers.startRunners(worker, newWorker),0);
|
||||||
|
console.log('started runner on', worker.name)
|
||||||
|
if(newWorker) worker = workers[workers.push(worker)-1]
|
||||||
|
|
||||||
|
worker.availrunners.push({
|
||||||
|
ip: data.ip,
|
||||||
|
name: name,
|
||||||
|
worker: worker,
|
||||||
|
label: worker.name + ':' + name
|
||||||
|
});
|
||||||
|
return setTimeout(workers.startRunners(worker, false ,stopPercent), 0);
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
console.log('using', String(usedMemPercent), 'percent memory, stopping runner creation!', worker.availrunners.length, 'created on ', worker.name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
workers.checkBalance = function(){
|
||||||
|
|
||||||
|
var minWorkers = 3;
|
||||||
|
console.log('checking balance');
|
||||||
|
|
||||||
|
if(workers.length < minWorkers){
|
||||||
|
console.log('less then 3 workers, starting a droplet');
|
||||||
|
for(var i=minWorkers-workers.length; i--;) workers.create();
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if(workers[workers.length-3].usedrunner !== 0 && workers[workers.length-2].usedrunner !== 0 && workers[workers.length-1].usedrunner !== 0){
|
||||||
|
console.log('last 3 workers have no free runners, starting droplet');
|
||||||
|
return workers.create();
|
||||||
|
}
|
||||||
|
if(workers.length > minWorkers && workers[workers.length-3].usedrunner === 0 && workers[workers.length-2].usedrunner === 0 && workers[workers.length-1].usedrunner === 0){
|
||||||
|
console.log('Last 2 runners not used, killing last runner', workers.length);
|
||||||
|
return workers.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
for(let worker of workers){
|
||||||
|
if(worker.length <= 3) break;
|
||||||
|
if(worker.availrunners.length === 0 && worker.usedrunner === 0){
|
||||||
|
console.log('found zombie worker, destroying')
|
||||||
|
workers.destroy(worker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('stopping workers balancing check');
|
||||||
|
};
|
||||||
|
|
||||||
|
return workers;
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
||||||
|
var ramPercentUsed = function(ip, callback){
|
||||||
|
|
||||||
|
return lxc.exec(
|
||||||
|
"python3 -c \"a=`head /proc/meminfo|grep MemAvail|grep -Po '\\d+'`;t=`head /proc/meminfo|grep MemTotal|grep -Po '\\d+'`;print(round(((t-a)/t)*100, 2))\"",
|
||||||
|
ip,
|
||||||
|
callback
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var runnerFree = function(runner){
|
||||||
var lxcTimeout = function(ip, time){
|
lxc.stop(runner.name, runner.worker.ip);
|
||||||
var name = ip2name[ip];
|
runner.worker.usedrunner--;
|
||||||
console.log(name)
|
if(runner.hasOwnProperty('timeout')){
|
||||||
time = time || 900000; // 15 minutes
|
clearTimeout(runner.timeout);
|
||||||
var keys = Object.keys(timeoutEvents)
|
|
||||||
if(keys.indexOf(name) !== -1){
|
|
||||||
clearTimeout(timeoutEvents[name])
|
|
||||||
}
|
}
|
||||||
timeoutEvents[name] = setTimeout(function(){
|
delete label2runner[runner.label];
|
||||||
lxc.stop(name);
|
|
||||||
|
workers.startRunners(runner.worker);
|
||||||
|
};
|
||||||
|
|
||||||
|
var lxcTimeout = function(runner, time){
|
||||||
|
time = time || 300000; // 5 minutes
|
||||||
|
|
||||||
|
if(runner.hasOwnProperty('timeout')){
|
||||||
|
clearTimeout(runner.timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
return runner.timeout = setTimeout(function(){
|
||||||
|
runnerFree(runner);
|
||||||
}, time);
|
}, time);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
var getAvailrunner = function(runner){
|
||||||
|
for(let worker of workers){
|
||||||
|
if(worker.availrunners.length === 0) continue;
|
||||||
|
if(runner && runner.worker.index <= worker.index) break;
|
||||||
|
if(runner) runnerFree(runner);
|
||||||
|
return worker.getRunner();
|
||||||
|
}
|
||||||
|
if(runner) return runner;
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
var runner = function(req, res, ip){
|
var run = function(req, res, runner, count){
|
||||||
lxcTimeout(ip);
|
count = count || 0;
|
||||||
|
console.log('run start', count, runner);
|
||||||
|
|
||||||
|
if(!runner){
|
||||||
|
console.log('no runner');
|
||||||
|
res.status(503);
|
||||||
|
return res.json({error: 'No runners, try again soon.'});
|
||||||
|
}
|
||||||
|
|
||||||
var httpOptions = {
|
var httpOptions = {
|
||||||
url:'http://' + ip + ':15000',
|
url: 'http://' + runner.worker.ip,
|
||||||
|
headers: {
|
||||||
|
Host: runner.name
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
code: req.body.code
|
code: req.body.code
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
console.log('run', runner);
|
||||||
|
|
||||||
|
|
||||||
return request.post(httpOptions, function(error, response, body){
|
return request.post(httpOptions, function(error, response, body){
|
||||||
|
// console.log('runner response:', arguments)
|
||||||
|
console.log('in request');
|
||||||
|
if(error || response.statusCode !== 200) return run(req, res, getAvailrunner(), ++count);
|
||||||
body = JSON.parse(body);
|
body = JSON.parse(body);
|
||||||
body['ip'] = ip.replace('10.0.', '');
|
|
||||||
|
body['ip'] = runner.label;
|
||||||
|
lxcTimeout(runner);
|
||||||
return res.json(body);
|
return res.json(body);
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var addToRedis = function(){
|
setTimeout(function(){
|
||||||
lxc.info(req.params.name, null, function(data){
|
console.log('Starting balance checking in 30 seconds')
|
||||||
var domain = req.query.domain || 'vm42.us';
|
setInterval(workers.checkBalance, 15000);
|
||||||
domain = req.params.name+'.'+domain;
|
}, 180000);
|
||||||
client.SADD("hosts", domain, function(){});
|
|
||||||
|
|
||||||
var ip = data.ip + ':5000';
|
workers.destroyOld();
|
||||||
client.HSET(domain, "ip", ip, redis.print);
|
workers.checkBalance();
|
||||||
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){
|
// router.get('/start/:name', function(req, res, next){
|
||||||
return lxc.start(req.params.name, function(data){
|
// return lxc.start(req.params.name, function(data){
|
||||||
if(!data){
|
// if(!data){
|
||||||
return res.json({status: 500, name: req.params.name, message: data});
|
// return res.json({
|
||||||
}else{
|
// status: 500,
|
||||||
res.json({});
|
// name: req.params.name,
|
||||||
}
|
// message: data
|
||||||
});
|
// });
|
||||||
});
|
// }else{
|
||||||
|
// res.json({});
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
router.get('/live/:template/:name', function(req, res, next){
|
// router.get('/live/:template/:name', function(req, res, next){
|
||||||
return lxc.startEphemeral(req.params.name, req.params.template, function (data) {
|
// return lxc.startEphemeral(req.params.name, req.params.template, function (data) {
|
||||||
console.log('live', arguments);
|
// console.log('live', arguments);
|
||||||
return res.json(data);
|
// return res.json(data);
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
|
|
||||||
|
// router.get('/clone/:template/:name', function(req, res, next){
|
||||||
|
// return lxc.clone(req.params.name, req.params.template, function(data){
|
||||||
|
// console.log('clone', arguments);
|
||||||
|
// if( data.match(/Created runner/) ){
|
||||||
|
// return res.json({status: 200});
|
||||||
|
// }else{
|
||||||
|
// return res.json({status: 500, message: data});
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
|
// router.get('/destroy/:name', function(req, res, next){
|
||||||
|
// return lxc.destroy(req.params.name, function(data){
|
||||||
|
// console.log('destroy', arguments);
|
||||||
|
// if(data){
|
||||||
|
// return res.json({status: 500, message: data});
|
||||||
|
// }else{
|
||||||
|
// return res.json({status: 200});
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
|
// router.get('/info/:name', function(req, res, next){
|
||||||
|
// return lxc.info(req.params.name, function(data){
|
||||||
|
// return res.json(data);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
|
// router.get('/list', function(req, res, next) {
|
||||||
|
// return lxc.list(workers.clworker0.ip, function(data){
|
||||||
|
// return res.json(data);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
router.get('/stop/:name', function(req, res, next){
|
router.get('/stop/:name', function(req, res, next){
|
||||||
return lxc.stop(req.params.name, function(data){
|
return lxc.stop(req.params.name, function(data){
|
||||||
@ -110,83 +308,15 @@ router.get('/stop/:name', function(req, res, next){
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/clone/:template/:name', function(req, res, next){
|
router.get('/liststuff', function(req, res, next){
|
||||||
return lxc.clone(req.params.name, req.params.template, function(data){
|
var obj = util.inspect(workers, {depth: 4});
|
||||||
console.log('clone', arguments);
|
res.send("<h1>Workers</h1><pre>"+obj+"</pre><h1>label2runner</h1><pre>"+util.inspect(label2runner)+'</pre><h1>DO calls</h1>'+doapi.calls);
|
||||||
if( data.match(/Created container/) ){
|
|
||||||
return res.json({status: 200});
|
|
||||||
}else{
|
|
||||||
return res.json({status: 500, message: data});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/destroy/:name', function(req, res, next){
|
|
||||||
return lxc.destroy(req.params.name, function(data){
|
|
||||||
console.log('destroy', arguments);
|
|
||||||
if(data){
|
|
||||||
return res.json({status: 500, message: data});
|
|
||||||
}else{
|
|
||||||
return res.json({status: 200});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/info/:name', function(req, res, next){
|
|
||||||
return lxc.info(req.params.name, function(data){
|
|
||||||
return res.json(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/list', function(req, res, next) {
|
|
||||||
return lxc.list(function(data){
|
|
||||||
return res.json(data);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/run/:ip?', function doRun(req, res, next){
|
router.post('/run/:ip?', function doRun(req, res, next){
|
||||||
// check if server is
|
console.log('hit runner route');
|
||||||
|
var runner = getAvailrunner(label2runner[req.params.ip]);
|
||||||
return lxc.list(function(data){
|
return run(req, res, runner);
|
||||||
if(!req.params.ip) data = [];
|
|
||||||
var ip = '10.0.'+ req.params.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 runner(req, res, availContainers.pop());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// freeMem: 97700 totalmem 513818624 usedMem: 0
|
|
||||||
// freeMem: 420,472 totalmem 513,818,624 usedMem: 100
|
|
||||||
var startAll = function(){
|
|
||||||
getFreeMem(function(freeMem){
|
|
||||||
var usedMemPercent = Math.round(( (totalMem-freeMem) /totalMem)*100);
|
|
||||||
console.log('freeMem:', freeMem, 'totalmem', totalMem, 'usedMemPercent:', usedMemPercent);
|
|
||||||
if(usedMemPercent < 81 ){
|
|
||||||
var name = 'crunner-'+(Math.random()*100).toString().replace('.','');
|
|
||||||
return lxc.startEphemeral(name, 'crunner', function(data){
|
|
||||||
ip2name[data.ip] = name;
|
|
||||||
availContainers.push(data.ip);
|
|
||||||
return startAll();
|
|
||||||
});
|
|
||||||
}else{
|
|
||||||
console.log('using', usedMemPercent, 'percent memory, stopping container creation!', availContainers.length, 'created');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
startAll();
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
19
testAPI.py
Normal file
19
testAPI.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import requests as r
|
||||||
|
import time
|
||||||
|
|
||||||
|
def testAPI(times=100, sleep=2):
|
||||||
|
errors = 0
|
||||||
|
|
||||||
|
for i in range(times):
|
||||||
|
try:
|
||||||
|
res = r.post(
|
||||||
|
'http://codeland.bytedev.co:2000/api/run',
|
||||||
|
data={'code': 'pwd'}
|
||||||
|
)
|
||||||
|
if res.status_code != 200: errors += 1
|
||||||
|
print(i, res.status_code, res.content)
|
||||||
|
except:
|
||||||
|
print('caught error')
|
||||||
|
errors += 1
|
||||||
|
time.sleep(sleep)
|
||||||
|
print('errors ', errors, (errors/times)*100)
|
Loading…
x
Reference in New Issue
Block a user