2018-11-15 19:59:08 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-01-19 18:36:28 -08:00
|
|
|
getAll,
|
|
|
|
|
get,
|
|
|
|
|
configure,
|
|
|
|
|
getLogs,
|
|
|
|
|
getLogStream,
|
2021-01-21 12:53:38 -08:00
|
|
|
restart,
|
|
|
|
|
rebuild
|
2018-11-15 19:59:08 +01:00
|
|
|
};
|
|
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
const assert = require('assert'),
|
2019-10-23 15:57:01 -07:00
|
|
|
BoxError = require('../boxerror.js'),
|
2018-11-15 23:10:05 +01:00
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
2021-01-21 11:31:35 -08:00
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
|
|
|
|
services = require('../services.js');
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2020-10-28 16:16:10 +01:00
|
|
|
function getAll(req, res, next) {
|
2021-01-21 12:38:12 -08:00
|
|
|
services.getServiceIds(function (error, result) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-16 17:53:22 +01:00
|
|
|
|
2018-12-02 18:05:19 -08:00
|
|
|
next(new HttpSuccess(200, { services: result }));
|
2018-11-15 19:59:08 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 16:16:10 +01:00
|
|
|
function get(req, res, next) {
|
2018-12-02 17:45:19 -08:00
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2021-02-16 11:13:33 -08:00
|
|
|
req.clearTimeout();
|
|
|
|
|
|
2021-01-21 12:40:41 -08:00
|
|
|
services.getServiceStatus(req.params.service, function (error, result) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2018-12-02 18:05:19 -08:00
|
|
|
next(new HttpSuccess(200, { service: result }));
|
2018-11-15 19:59:08 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 16:16:10 +01:00
|
|
|
function configure(req, res, next) {
|
2018-12-02 17:45:19 -08:00
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
2018-11-21 15:47:34 +01:00
|
|
|
|
2021-01-19 19:05:35 -08:00
|
|
|
if (typeof req.body.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit must be a number'));
|
2018-11-21 15:47:34 +01:00
|
|
|
|
|
|
|
|
const data = {
|
2021-01-19 19:58:44 -08:00
|
|
|
memoryLimit: req.body.memoryLimit
|
2018-11-21 15:47:34 +01:00
|
|
|
};
|
|
|
|
|
|
2020-12-04 00:22:21 -08:00
|
|
|
if (req.params.service === 'sftp') {
|
2020-10-21 22:31:59 -07:00
|
|
|
if (typeof req.body.requireAdmin !== 'boolean') return next(new HttpError(400, 'requireAdmin must be a boolean'));
|
|
|
|
|
data.requireAdmin = req.body.requireAdmin;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
services.configureService(req.params.service, data, function (error) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-21 15:47:34 +01:00
|
|
|
|
|
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:59:08 +01:00
|
|
|
function getLogs(req, res, next) {
|
2018-12-02 17:45:19 -08:00
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2019-01-08 12:10:53 -08:00
|
|
|
var lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
|
2018-11-15 19:59:08 +01:00
|
|
|
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number'));
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
lines: lines,
|
|
|
|
|
follow: false,
|
2019-01-08 12:10:53 -08:00
|
|
|
format: req.query.format || 'json'
|
2018-11-15 19:59:08 +01:00
|
|
|
};
|
|
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
services.getServiceLogs(req.params.service, options, function (error, logStream) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Type': 'application/x-logs',
|
2020-11-09 10:58:43 +01:00
|
|
|
'Content-Disposition': `attachment; filename="${req.params.service}.log"`,
|
2018-11-15 19:59:08 +01:00
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
'X-Accel-Buffering': 'no' // disable nginx buffering
|
|
|
|
|
});
|
|
|
|
|
logStream.pipe(res);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this route is for streaming logs
|
|
|
|
|
function getLogStream(req, res, next) {
|
2018-12-02 17:45:19 -08:00
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2019-01-08 12:10:53 -08:00
|
|
|
var lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
|
2018-11-15 19:59:08 +01:00
|
|
|
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a valid number'));
|
|
|
|
|
|
|
|
|
|
function sse(id, data) { return 'id: ' + id + '\ndata: ' + data + '\n\n'; }
|
|
|
|
|
|
|
|
|
|
if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream'));
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
lines: lines,
|
2019-01-08 12:10:53 -08:00
|
|
|
follow: true,
|
|
|
|
|
format: 'json'
|
2018-11-15 19:59:08 +01:00
|
|
|
};
|
|
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
services.getServiceLogs(req.params.service, options, function (error, logStream) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
'Connection': 'keep-alive',
|
|
|
|
|
'X-Accel-Buffering': 'no', // disable nginx buffering
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
|
|
|
|
res.write('retry: 3000\n');
|
|
|
|
|
res.on('close', logStream.close);
|
|
|
|
|
logStream.on('data', function (data) {
|
|
|
|
|
var obj = JSON.parse(data);
|
|
|
|
|
res.write(sse(obj.monotonicTimestamp, JSON.stringify(obj))); // send timestamp as id
|
|
|
|
|
});
|
|
|
|
|
logStream.on('end', res.end.bind(res));
|
|
|
|
|
logStream.on('error', res.end.bind(res, null));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 16:16:10 +01:00
|
|
|
function restart(req, res, next) {
|
2018-12-02 17:45:19 -08:00
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
services.restartService(req.params.service, function (error) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
|
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-01-21 12:53:38 -08:00
|
|
|
|
|
|
|
|
function rebuild(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
|
|
|
|
|
|
|
|
|
services.rebuildService(req.params.service, function (error) {
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|