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

155 lines
5.2 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,
restart,
2022-10-13 20:32:36 +02:00
rebuild,
2023-08-12 21:53:28 +05:30
getGraphs,
getPlatformStatus
2018-11-15 19:59:08 +01:00
};
const assert = require('assert'),
AuditSource = require('../auditsource.js'),
2019-10-23 15:57:01 -07:00
BoxError = require('../boxerror.js'),
2022-10-13 20:32:36 +02:00
graphs = require('../graphs.js'),
2018-11-15 23:10:05 +01:00
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
2023-08-12 21:53:28 +05:30
platform = require('../platform.js'),
2021-08-20 09:19:44 -07:00
safe = require('safetydance'),
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) {
assert.strictEqual(typeof req.params.service, 'string');
2018-11-15 19:59:08 +01: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) {
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
};
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) {
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,
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) {
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,
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);
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) {
assert.strictEqual(typeof req.params.service, 'string');
2018-11-15 19:59:08 +01: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-08-25 19:41:46 -07:00
async function rebuild(req, res, next) {
assert.strictEqual(typeof req.params.service, 'string');
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-08-25 19:41:46 -07:00
next(new HttpSuccess(202, {}));
}
2022-10-13 20:32:36 +02:00
async function getGraphs(req, res, next) {
assert.strictEqual(typeof req.params.service, 'string');
if (!req.query.fromMinutes || !parseInt(req.query.fromMinutes)) return next(new HttpError(400, 'fromMinutes must be a number'));
const fromMinutes = parseInt(req.query.fromMinutes);
const noNullPoints = !!req.query.noNullPoints;
const [error, result] = await safe(graphs.getContainerStats(req.params.service, fromMinutes, noNullPoints));
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()));
}