Files
cloudron-box/src/routes/services.js
T

158 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-11-15 19:59:08 +01:00
'use strict';
exports = module.exports = {
2021-09-20 09:15:28 -07:00
list,
get,
configure,
getLogs,
getLogStream,
2021-01-21 12:53:38 -08:00
restart,
2022-10-13 20:32:36 +02:00
rebuild,
2025-05-21 16:26:36 +02:00
getMetrics,
2023-08-12 21:53:28 +05:30
getPlatformStatus
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,
2025-05-21 16:26:36 +02:00
metrics = require('../metrics.js'),
2023-08-12 21:53:28 +05:30
platform = require('../platform.js'),
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
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
});
res.on('close', () => logStream.destroy());
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'));
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.destroy());
2021-08-25 19:41:46 -07:00
logStream.on('data', function (data) {
2022-04-14 17:41:41 -05:00
const obj = JSON.parse(data);
2024-02-22 18:08:32 +01:00
const sse = `data: ${JSON.stringify(obj)}\n\n`;
res.write(sse);
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
}
2022-10-13 20:32:36 +02:00
2025-05-21 16:26:36 +02:00
async function getMetrics(req, res, next) {
2022-10-13 20:32:36 +02:00
assert.strictEqual(typeof req.params.service, 'string');
2025-05-21 16:26:36 +02:00
if (!req.query.fromSecs || !parseInt(req.query.fromSecs)) return next(new HttpError(400, 'fromSecs must be a number'));
if (!req.query.intervalSecs || !parseInt(req.query.intervalSecs)) return next(new HttpError(400, 'intervalSecs must be a number'));
2022-10-13 20:32:36 +02:00
2025-05-21 16:26:36 +02:00
const fromSecs = parseInt(req.query.fromSecs);
const intervalSecs = parseInt(req.query.intervalSecs);
2022-10-13 20:32:36 +02:00
const noNullPoints = !!req.query.noNullPoints;
const [error, result] = await safe(metrics.get(req.params.service, { fromSecs, intervalSecs, noNullPoints, serviceIds: [req.params.service] }));
2022-10-13 20:32:36 +02:00
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, result));
}
2023-08-12 21:53:28 +05:30
async function getPlatformStatus(req, res, next) {
next(new HttpSuccess(200, platform.getStatus()));
}