forked from ThomasLachaux/openshift-backup-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·243 lines (204 loc) · 6.22 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const express = require('express');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
const http = require('http');
const { execSync } = require('child_process');
const passport = require('passport');
const LdapStrategy = require('passport-ldapauth');
const flash = require('connect-flash');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
require('dotenv').config();
const app = express();
const server = http.createServer(app);
const port = 8080;
app.use(cookieParser('secret'));
app.use(
session({
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false,
secret: process.env.SESSION_SECRET,
store: new FileStore({}),
}),
);
app.use(flash());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(passport.initialize());
app.use(passport.session());
app.set('views', '.');
app.set('view engine', 'ejs');
passport.use(
new LdapStrategy({
server: {
url: process.env.LDAP_URL,
bindDN: process.env.LDAP_BIND_DN,
bindCredentials: process.env.LDAP_BIND_CREDENTIALS,
searchBase: process.env.LDAP_SEARCH_BASE,
searchFilter: process.env.LDAP_SEARCH_FILTER,
},
}),
);
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
const oc = (command) => JSON.parse(execSync(`oc ${command}`).toString()).items;
const fetchDatabases = (type, accumulator = []) => {
const result = oc(`get dc -l backup=${type} --all-namespaces -o json`);
return result.reduce((acc, curr) => {
const namespace = curr.metadata.namespace;
const name = curr.metadata.name;
const available = Number(curr.status.availableReplicas) > 0;
const namespaceIndex = acc.findIndex((item) => item.name === namespace);
const item = {
name,
badges: [
{
name: type,
color: 'secondary',
},
{
name: available ? 'Available' : 'Not available',
color: available ? 'success' : 'danger',
},
],
color: 'blank',
};
// If namespace doesn't exists
if (namespaceIndex === -1) {
acc.push({
name: namespace,
items: [item],
});
} else {
acc[namespaceIndex].items.push(item);
}
return acc;
}, accumulator);
};
const reducePvc = (result, backup, accumulator = []) =>
result
.reduce((acc, curr) => {
const namespace = curr.metadata.namespace;
const name = curr.metadata.name;
const size = curr.status.capacity ? curr.status.capacity.storage : '?';
const phase = curr.status.phase;
const namespaceIndex = acc.findIndex((item) => item.name === namespace);
const item = {
name: name,
color: backup ? 'success' : 'danger',
badges: [
{
name: size,
color: 'secondary',
},
{
name: phase,
color: phase === 'Bound' ? 'success' : 'danger',
},
],
};
// If namespace doesn't exists
if (namespaceIndex === -1) {
acc.push({
name: namespace,
items: [item],
});
} else {
acc[namespaceIndex].items.push(item);
}
return acc;
}, accumulator)
.sort((a, b) => (a.name < b.name ? -1 : 1));
// backup: if true, selects ONLY pvc which are backuped. If false, selects ONLY pvc which are NOT backuped
const fetchNfs = (backup, accumulator = []) => {
const selector = backup ? 'backup=nfs' : 'backup!=nfs';
const result = oc(`get pvc --all-namespaces -o json -l ${selector}`);
return reducePvc(
result.filter((item) => item.metadata.annotations['volume.beta.kubernetes.io/storage-class'] === 'nfs-proxmox-vm'),
backup,
accumulator,
);
};
const mergeDcWithCephPvc = (dcs) => {
const result = oc(`get pvc --all-namespaces -o json`);
const pvcs = reducePvc(
result.filter((item) => item.metadata.annotations['volume.beta.kubernetes.io/storage-class'] === 'ceph'),
false,
).map((namespace) => ({
...namespace,
items: namespace.items.map((item) => ({
...item,
badges: [
...item.badges,
(() => {
const dcNamespace = dcs.find((dcNamespace) => dcNamespace.name === namespace.name);
if (!dcNamespace) {
return null;
}
const dcItem = dcNamespace.items.find((dcItem) => dcItem.name === item.name);
if (!dcItem) {
return null;
}
// A bit ugly to get the dc available badge
return dcItem.badges.find((badge) => badge.name === 'Available' || badge.name === 'Not available');
})(),
].filter((item) => item !== null),
color: (() => {
const dcNamespace = dcs.find((dcNamespace) => dcNamespace.name === namespace.name);
if (!dcNamespace) {
return false;
}
return dcNamespace.items.findIndex((dcItem) => dcItem.name === item.name) !== -1;
})()
? 'success'
: 'danger',
})),
}));
return pvcs;
};
const isAuth = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
return res.redirect('/');
};
app.get('//', (req, res) => res.render('index', { title: 'Login', message: req.flash('error'), loginPage: true }));
app.get('/databases', isAuth, (req, res) => {
let items = [];
['mysql', 'postgresql', 'mongodb'].forEach((type) => {
items = fetchDatabases(type, items);
});
items = mergeDcWithCephPvc(items);
return res.render('index', {
title: 'Databases',
alert: 'An item is marked as backup only if the DC name is equal to the PVC name',
items,
logged: true,
});
});
app.get('/nfs', isAuth, (req, res) => {
let items = fetchNfs(false);
items = fetchNfs(true, items);
return res.render('index', { title: 'NFS', items });
});
app.post(
'/login',
passport.authenticate('ldapauth', {
successRedirect: '/databases',
failureRedirect: '/',
failureFlash: true,
}),
);
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});
app.use('*', (req, res) => res.status(404).send('NOT FOUND !'));
server.listen(port, () => {
console.log(`Serveur ready on port ${port}`);
});