-
Notifications
You must be signed in to change notification settings - Fork 28
/
airfield.js
181 lines (157 loc) · 4.51 KB
/
airfield.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
/**
* Module dependencies.
*/
var settings = require(process.env.NODE_CONFIG || './settings').settings;
var express = require('express')
, RedisStore = require('connect-redis')(express.session)
, routes = require('./routes')
, http = require('http')
, path = require('path');
var async = require('async');
var redis = require('redis');
var db = redis.createClient(process.env.REDIS_PORT || settings.REDIS_PORT, process.env.REDIS_HOST || settings.REDIS_HOST);
var request = require('request');
var cons = require('consolidate');
var swig = require('swig');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || settings.port);
app.set('views', __dirname + '/views');
console.log(__dirname);
app.engine('.html', cons.swig);
app.set('view engine', 'html');
swig.init({
root: __dirname + '/views',
allowError: true
});
// app.set('view options', layout: false);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(settings.cookieSecret));
app.use(express.session({
secret: settings.sessionSecret,
store: new RedisStore
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
//app.use(express.basicAuth('username', 'password'));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
// Authentication mechanism
function checkAuth(req, res, next){
next();
}
app.get('/login', function(req, res){
res.render('login.html', {title:'Login'});
});
app.post('/login', function(req, res){
var post = req.body;
if(post.user == settings.username && post.password == settings.password){
res.redirect('/routes');
}else{
res.send("Your login credientials are invalid!");
}
});
app.get('/logout', function(req, res){
delete req.session.user_id;
res.redirect("/login");
});
//Render few variables of routes
app.get('/routes', checkAuth, function(req, res){
res.render("routes.html", {title: 'Routes'});
});
// Get all routes from redis [ajax]
app.get('/ajax/routes', checkAuth, function(req, res){
db.keys('frontend:*', function(err, routes){
var routelist = [];
async.forEach(routes, function(item, cb){
db.lrange(item, 0, -1, function(err, items){
if(err){
cb(err);
}
var route = {name: item, items: items};
routelist.push(route);
cb();
});
}, function(err){
if(err){
console.log(err);
}
//console.log(routelist);
res.send(routelist);
});
});
});
// Saves route [ajax]
app.post('/ajax/save', checkAuth, function(req, res){
//console.log(req.body.data);
var data = req.body.data;
db.del(data.route, function(err){
db.rpush(data.route, data.name, function(err){
if(err){
console.log(err);
}
for(i in data.hosts){
db.rpush(data.route, data.hosts[i]);
}
});
});
res.send('{"response":"OK"}');
});
// Removes route [ajax]
app.post('/ajax/remove', checkAuth, function(req, res){
var key = req.body.key;
db.del(key, function(err){
if(err){
console.log(err);
}else{
res.send('{"response":"OK"}');
}
});
});
app.get('/ajax/getInstances', checkAuth, function(req, res){
if(!settings.enable_openstack){
res.send(400, {error: 'Openstack support is not enabled'});
return;
}
var params = {
auth:{
passwordCredentials: {
username: settings.os_api_username,
password: settings.os_api_password
},
tenantId: settings.os_api_tenant
}
};
request({url: settings.os_keystone_url+'/v2.0/tokens', method:'POST', body: params, json:true}, function(err, response, body){
//console.log(body);
var token = body.access.token.id;
//res.send(body);
var url = settings.os_nova_url+'/v2/'+settings.os_api_tenant+'/servers/detail?all_tenants=1';
//console.log(url);
request({url: url, method:'GET', headers:{'X-Auth-Token':token}, json:true}, function(err, response, body){
//console.log(body);
var instances = [];
async.forEach(body.servers, function(instance, cb){
var addresses = [];
for (var i = 0; i < instance.addresses.private.length; i++) {
addresses.push(instance.addresses.private[i].addr);
};
var tolist = {id:instance.id, name:instance.name, status:instance.status, addresses:addresses}
instances.push(tolist);
cb();
}, function(err){
res.send(instances);
});
});
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});