-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (150 loc) · 4.75 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
var Ansible = require('node-ansible');
var createNewDeployment = require('./createNewDeployment');
var destroyDeployment = require('./destroyDeployment');
var getExternalIp = require('./getExternalIp');
const app = express();
const path = require('path');
var getDb = require('./db');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
var database = null;
getDb.waitConnection(function(db){
database = db;
})
const testUser = {
username: 'superAdmin',
password: 'sugarizer'
}
app.use(express.static('public'));
app.get('/', (req, res)=>{
res.redirect('/schoolconsole');
})
// =================
// School console routes
app.get('/schoolconsole', (req, res)=>{
res.redirect('/schoolconsole/request');
});
app.get('/schoolconsole/request', (req, res)=>{
res.render('request-new-deployment');
});
app.post('/schoolconsole/request', (req, res)=>{
database.collection('requests').find({schoolId: req.body.schoolId}).toArray(function(err, result){
if (err){
console.log(err);
} else if (result.length !== 0){
res.send('<h1>Every school most have a unique school id</h1>'); // replace this by error flashing
}
database.collection('requests').insertOne({
schoolName: req.body.schoolName,
schoolId: req.body.schoolId.toLowerCase(),
other: req.body.other,
status: -1
}, function(err, r){
if (err){
res.send('<h1>Error while requesting deployment!, Please try again</h1>'); // replace this with flash
} else {
res.send('<h1>Deployment successfully requested!</h1>'); // replace this with an templated and pass r.ops to it
}
});
})
});
// End of School Console routes
// =================
// =================
// Super Admin Console routes
app.get('/superadminconsole', (req, res)=>{
res.redirect('/superadminconsole/requests');
});
app.get('/superadminconsole/login', (req, res)=>{
res.render('login', {});
});
app.get('/superadminconsole/deployments', (req, res)=>{
database.collection('requests').find().toArray(function(err, result){
if (err){
console.log(err);
}
console.log(result);
res.render('deployments', {
module: 'Deployments',
results: result,
})
})
})
app.get('/superadminconsole/requests', (req, res)=>{
database.collection('requests').find().toArray(function(err, result){
if (err){
console.log(err);
}
console.log(result);
res.render('requests', {
results: result,
module: 'Requests'
});
})
})
app.post('/superadminconsole/requests', (req, res)=>{
if (req.body.approve){
var school_Id = req.body.approve;
database.collection('requests').updateOne({schoolId: req.body.approve}, {$set: {status: 0}}, function(err, result){
if (err){
console.log(err);
}
createNewDeployment(
{name: 'test-auth-cluster'},
{Id: school_Id},
{
project_Id: 'auth-testing-272315',
zone: 'asia-northeast1-a'
},
database,
school_Id
);
});
}
if (req.body.decline){
database.collection('requests').findOneAndDelete({schoolId: req.body.decline}, function(err, r){
if (err){
console.log(err);
}
})
}
res.redirect('/superadminconsole/requests');
});
app.post('/superadminconsole/destroyDeployment', function(req, res){
var school_Id = req.body.destroy;
destroyDeployment(
{name: 'test-auth-cluster'},
{Id: school_Id},
{
project_Id: 'auth-testing-272315',
zone: 'asia-northeast1-a'
},
database,
school_Id
);
res.redirect('/superadminconsole/deployments');
})
// End of Super Admin Console routes
// ================
// app.get('/domything', function(req, res){
// database.collection('requests').deleteOne({schoolName: 'tes'},
// function(err, result){
// if (err){
// console.log(err);
// }});
// res.send('done!');
// })
app.get('/deleteFromDb/:id', function(req, res){
database.collection('requests').findOneAndDelete({schoolId: req.params.id}, function(err, r){
if (err){
console.log(err);
}
res.send(`${req.params.id} deleted from database!`);
})
})
app.listen(8080, ()=>{
console.log("connected to db, app listening on port 3000");
})