-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
148 lines (133 loc) · 5.24 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// server.js
// config
global.dirname = __dirname;
global.config = require('./config/serverconfig');
// requires
var express = require('express'),
passport = require('passport'),
basicStrategy = require('passport-http').BasicStrategy,
spawn = require('child_process').spawn,
busboy = require('connect-busboy'),
fs = require('fs'),
express_session = require('express-session'),
SQLiteStore = require('connect-sqlite3')(express_session),
cookieParser = require('cookie-parser'),
BrendaProjects = require('./server/brenda-projects')(fs),
app = express(),
server = app.listen(global.config.port),
io = require('socket.io').listen(server),
procs = require('./server/processes')(spawn, io),
serveIndex = require('serve-index'),
serveStatic = require('serve-static'),
influx = require('influx');
// file handler
app.use(busboy());
// set up authentication and then static files
var sessionStore = new SQLiteStore({'dir': __dirname + '/server'});
var cParser = new cookieParser(global.config.session_secret);
var session = express_session({
key: global.config.session_key,
store: sessionStore,
secret: global.config.session_secret,
resave: true,
saveUninitialized: true
});
require('./server/auth')(app, global.config, passport, basicStrategy, cParser, session);
app.use(express.static(__dirname + '/grafana/dist'));
app.use('/projects', serveIndex(global.config.projects_dir, {'icons': true}));
app.use('/projects', serveStatic(global.config.projects_dir));
if (global.config.influxdb && !global.config.influxdb.readonly) {
// InfluxDB connection
var dbname = global.config.influxdb.database || 'brenda';
var influxclient = influx({
host : global.config.influxdb.host || 'localhost',
port : global.config.influxdb.port || 8086, // optional, default 8086
username : global.config.influxdb.user || 'root',
password : global.config.influxdb.password || 'root',
database : dbname
});
procs.setDatabase(influxclient);
influxclient.getDatabaseNames(function(err, dbnames) {
// Create databases if they don't exist
if (dbnames.indexOf('grafana') == -1) influxclient.createDatabase('grafana');
if (dbnames.indexOf(dbname) == -1) influxclient.createDatabase(dbname);
});
}
// make sure children die
process.on('exit', function() {
procs.killAll();
});
// Start polling for instance and job information
procs.checkInstanceCounts();
procs.checkJobCount();
// socket setup
io.use(function(socket, next) {
// get the session info from the request and assign it to the socket
session(socket.request, {}, next);
}).use(function(socket, next) {
// checks if the socket has a valid session and user, and accepts or denies the
// connection accordingly
var ip_address = socket.request.headers['x-forwarded-for'];
if (socket.request.session && socket.request.session.passport && socket.request.session.passport.user) {
console.log('Accepting socket connection from user:', socket.request.session.passport.user.username, ip_address);
return next();
}
console.log('Denying socket connection from unauthorized user at', ip_address);
return next(new Error("Unauthorized user"), false);
});
// event listeners
io.on('connection', function(client) {
client.emit('connected', client.id);
console.log('client', client.id, 'connected');
client.emit('projectupdate', BrendaProjects.projects);
procs.getRegionConfigs(function(files) {
var regions = [];
for (var i = 0; i < files.length; i++) {
var parts = files[i].split('/');
regions.push(parts[parts.length - 1]);
}
client.emit('regionconfigs', regions);
});
client.on('submitjob', function(data) {
console.log('job submit, data: ', data);
procs.submitJob(client, data);
});
client.on('spawninstance', function(data) {
console.log('instance submit, data: ', data);
procs.spawnInstance(client, data);
});
client.on('checkprice', function(data) {
console.log('price check', data.instancetype);
procs.checkInstancePrice(client, data);
});
client.on('addProject', function(data) {
console.log('adding new project: ', data);
BrendaProjects.addProject(data, function(name) {
console.log('added project:', name);
client.emit('projectadded', name);
client.emit('projectupdate', BrendaProjects.projects);
});
});
client.on('getBlenderFiles', function(data) {
console.log('getting blender files for project', data);
procs.getBlenderFiles(data, function(files) {
client.emit('blenderFileUpdate', files);
});
});
});
// api routes
app.post('/api/upload:client_id', function(req, res) {
var client_id = req.params.client_id;
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename) {
console.log(global.config.jobdata_dir + filename);
var fstream = fs.createWriteStream(global.config.jobdata_dir + filename);
file.pipe(fstream);
fstream.on('close', function () {
// file upload completed (hopefully)
procs.buildJobFile(client_id, filename);
res.redirect('back');
});
});
});
console.log("server listening on", global.config.port);