-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (130 loc) · 4.31 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
const express = require('express');
const bodyParser = require('body-parser');
const userConfig = require('./config.json') || {};
class GridComposer {
constructor(config) {
const process = require('child_process');
this.exec = process.exec;
this.config = config || {};
// set defaults.
this.config.maxNodes = this.config.maxNodes || 20;
this.config.port = this.config.port || 8080;
this.config.token = this.config.token || false;
// command shortcuts.
this.baseCmd = 'docker-compose';
// router init.
this.router = undefined;
this.initExpressRouter();
}
initExpressRouter() {
const e = express();
e.use(bodyParser.urlencoded({ extended: true }));
e.use(bodyParser.json());
this.router = express.Router();
this.router.get('/status', async (req, res) => {
const token = await this.getTokenFromReq(req);
const confirmed = await this.confirmToken(token);
if (!confirmed) return this.handleRejectedToken(res);
const command = this.exec(`${this.baseCmd} ps`);
try {
return command.stdout.on('data', (data) => {
res.end(data);
});
} catch (err) {
return res.status(500).end(err);
}
});
this.router.post('/start', async (req, res) => {
const token = await this.getTokenFromReq(req);
const confirmed = await this.confirmToken(token);
if (!confirmed) return this.handleRejectedToken(res);
try {
return this.exec(`${this.baseCmd} up -d`, () => res.end());
} catch (err) {
return res.status(500).end(err);
}
});
this.router.post('/stop', async (req, res) => {
const token = await this.getTokenFromReq(req);
const confirmed = await this.confirmToken(token);
if (!confirmed) return this.handleRejectedToken(res);
try {
return this.exec(`${this.baseCmd} down`, () => res.end());
} catch (err) {
return res.status(500).end(err);
}
});
this.router.post('/restart', async (req, res) => {
const token = await this.getTokenFromReq(req);
const confirmed = await this.confirmToken(token);
if (!confirmed) return this.handleRejectedToken(res);
try {
return this.exec(`${this.baseCmd} restart`, () => res.end());
} catch (err) {
return res.status(500).end(err);
}
});
this.router.put('/scale/:browser/:number', async (req, res) => {
const token = await this.getTokenFromReq(req);
const confirmed = await this.confirmToken(token);
if (!confirmed) return this.handleRejectedToken(res);
const { browser, number } = req.params;
if (!this.confirmScaleParams(browser, number)) {
return res.status(500).end('Error request parameters.');
}
try {
return this.exec(`${this.baseCmd} scale ${browser}=${number}`, () =>
res.end());
} catch (err) {
return res.status(500).end(err);
}
});
e.use('/grid', this.router);
if (process.env.NODE_ENV === 'dev') {
return undefined;
}
return e.listen(this.config.port);
}
/**
* @method confirmScaleParams
* @param {string} browser - Browser to scale
* @param {number} number - Amount of nodes for specific browser
* @return {boolean}
*/
confirmScaleParams(browser, number) {
// make sure they exist.
if (!browser || !number) return false;
// make sure number is lower than maxNodes in config.
if (number > this.config.maxNodes) return false;
// make sure number is actually a number.
return !isNaN(number);
}
handleRejectedToken(res) {
return res.status(401).send('Token rejected');
}
async getTokenFromReq(req) {
try {
return req.query.token;
} catch (err) {
return false;
}
}
/**
* @method confirmToken
* @param {?} reqToken - token from request.
* @return {boolean}
*/
async confirmToken(reqToken) {
// make sure user has set token in config. If not, accept all reqTokens
// and return true.
if (this.config.token === false) return true;
// check it against user config token. If they match, return true.
else if (this.config.token === reqToken) return true;
return false;
}
}
module.exports = GridComposer;
if (process.env.NODE_ENV !== 'dev') {
const init = () => new GridComposer(userConfig);
init();
}