2018-11-15 19:59:08 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-09-20 09:15:28 -07:00
|
|
|
list,
|
2021-01-19 18:36:28 -08:00
|
|
|
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'),
|
2021-09-30 09:50:30 -07:00
|
|
|
AuditSource = require('../auditsource.js'),
|
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,
|
2021-08-20 09:19:44 -07:00
|
|
|
safe = require('safetydance'),
|
2021-01-21 11:31:35 -08:00
|
|
|
services = require('../services.js');
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2021-09-20 09:15:28 -07:00
|
|
|
async function list(req, res, next) {
|
|
|
|
|
const [error, result] = await safe(services.listServices());
|
2021-08-20 09:19:44 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-16 17:53:22 +01:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
next(new HttpSuccess(200, { services: result }));
|
2018-11-15 19:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async 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-08-25 19:41:46 -07:00
|
|
|
const [error, result] = await safe(services.getServiceStatus(req.params.service));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
next(new HttpSuccess(200, { service: result }));
|
2018-11-15 19:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async 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'));
|
2021-10-01 12:09:13 -07:00
|
|
|
if ('recoveryMode' in req.body && typeof req.body.recoveryMode !== 'boolean') return next(new HttpError(400, 'recoveryMode must be boolean'));
|
2018-11-21 15:47:34 +01:00
|
|
|
|
|
|
|
|
const data = {
|
2021-10-01 12:09:13 -07:00
|
|
|
memoryLimit: req.body.memoryLimit,
|
|
|
|
|
recoveryMode: req.body.recoveryMode || false
|
2018-11-21 15:47:34 +01:00
|
|
|
};
|
|
|
|
|
|
2021-09-30 09:50:30 -07:00
|
|
|
const [error] = await safe(services.configureService(req.params.service, data, AuditSource.fromRequest(req)));
|
2021-08-25 19:41:46 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-21 15:47:34 +01:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
next(new HttpSuccess(202, {}));
|
2018-11-21 15:47:34 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async 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
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const 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'));
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const options = {
|
2018-11-15 19:59:08 +01:00
|
|
|
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-08-25 19:41:46 -07:00
|
|
|
const [error, logStream] = await safe(services.getServiceLogs(req.params.service, options));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Type': 'application/x-logs',
|
|
|
|
|
'Content-Disposition': `attachment; filename="${req.params.service}.log"`,
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
'X-Accel-Buffering': 'no' // disable nginx buffering
|
2018-11-15 19:59:08 +01:00
|
|
|
});
|
2021-08-25 19:41:46 -07:00
|
|
|
logStream.pipe(res);
|
2018-11-15 19:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this route is for streaming logs
|
2021-08-25 19:41:46 -07:00
|
|
|
async 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
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const 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'));
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const options = {
|
2018-11-15 19:59:08 +01:00
|
|
|
lines: lines,
|
2019-01-08 12:10:53 -08:00
|
|
|
follow: true,
|
|
|
|
|
format: 'json'
|
2018-11-15 19:59:08 +01:00
|
|
|
};
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const [error, logStream] = await safe(services.getServiceLogs(req.params.service, options));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
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
|
2018-11-15 19:59:08 +01:00
|
|
|
});
|
2021-08-25 19:41:46 -07:00
|
|
|
logStream.on('end', res.end.bind(res));
|
|
|
|
|
logStream.on('error', res.end.bind(res, null));
|
2018-11-15 19:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async 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-09-30 09:50:30 -07:00
|
|
|
const [error] = await safe(services.restartService(req.params.service, AuditSource.fromRequest(req)));
|
2021-08-25 19:41:46 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-11-15 19:59:08 +01:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
next(new HttpSuccess(202, {}));
|
2018-11-15 19:59:08 +01:00
|
|
|
}
|
2021-01-21 12:53:38 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function rebuild(req, res, next) {
|
2021-01-21 12:53:38 -08:00
|
|
|
assert.strictEqual(typeof req.params.service, 'string');
|
|
|
|
|
|
2021-09-30 09:50:30 -07:00
|
|
|
const [error] = await safe(services.rebuildService(req.params.service, AuditSource.fromRequest(req)));
|
2021-08-25 19:41:46 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2021-01-21 12:53:38 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
next(new HttpSuccess(202, {}));
|
2021-01-21 12:53:38 -08:00
|
|
|
}
|