2023-08-04 13:41:13 +05:30
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
reboot,
|
2023-12-04 00:46:12 +01:00
|
|
|
getInfo,
|
2023-08-04 13:41:13 +05:30
|
|
|
getMemory,
|
|
|
|
|
getLogs,
|
|
|
|
|
getLogStream,
|
2025-05-21 16:26:36 +02:00
|
|
|
getMetrics,
|
2025-05-21 17:15:04 +02:00
|
|
|
getMetricStream,
|
2023-08-04 13:41:13 +05:30
|
|
|
getBlockDevices,
|
2025-07-16 17:20:15 +02:00
|
|
|
getFilesystems,
|
2025-07-16 23:09:06 +02:00
|
|
|
getFilesystemUsage,
|
2023-12-04 00:31:18 +01:00
|
|
|
getCpus,
|
2023-08-04 13:41:13 +05:30
|
|
|
};
|
|
|
|
|
|
2025-08-14 11:17:38 +05:30
|
|
|
const assert = require('node:assert'),
|
2023-08-04 13:41:13 +05:30
|
|
|
BoxError = require('../boxerror.js'),
|
2025-07-10 11:00:31 +02:00
|
|
|
HttpError = require('@cloudron/connect-lastmile').HttpError,
|
|
|
|
|
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
|
2025-05-21 16:26:36 +02:00
|
|
|
metrics = require('../metrics.js'),
|
2023-08-04 13:41:13 +05:30
|
|
|
safe = require('safetydance'),
|
|
|
|
|
system = require('../system.js');
|
|
|
|
|
|
|
|
|
|
async function reboot(req, res, next) {
|
|
|
|
|
// Finish the request, to let the appstore know we triggered the reboot
|
|
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
|
|
|
|
|
|
|
await safe(system.reboot());
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 00:46:12 +01:00
|
|
|
async function getInfo(req, res, next) {
|
|
|
|
|
const [error, info] = await safe(system.getInfo());
|
2023-08-04 13:41:13 +05:30
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
2023-12-04 00:46:12 +01:00
|
|
|
next(new HttpSuccess(200, { info }));
|
2023-08-04 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getMemory(req, res, next) {
|
|
|
|
|
const [error, result] = await safe(system.getMemory());
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, result));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getLogs(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.unit, 'string');
|
|
|
|
|
|
2025-07-13 13:14:32 +02:00
|
|
|
const lines = typeof req.query.lines === 'string' ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
|
2023-08-04 13:41:13 +05:30
|
|
|
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number'));
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
lines: lines,
|
|
|
|
|
follow: false,
|
2025-07-13 13:14:32 +02:00
|
|
|
format: typeof req.query.format === 'string' ? req.query.format : 'json'
|
2023-08-04 13:41:13 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [error, logStream] = await safe(system.getLogs(req.params.unit, options));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Type': 'application/x-logs',
|
|
|
|
|
'Content-Disposition': `attachment; filename="${req.params.unit}.log"`,
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
'X-Accel-Buffering': 'no' // disable nginx buffering
|
|
|
|
|
});
|
2024-02-24 17:18:38 +01:00
|
|
|
res.on('close', () => logStream.destroy());
|
2023-08-04 13:41:13 +05:30
|
|
|
logStream.pipe(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getLogStream(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.unit, 'string');
|
|
|
|
|
|
2025-07-13 13:14:32 +02:00
|
|
|
const lines = typeof req.query.lines === 'string' ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
|
2023-08-04 13:41:13 +05:30
|
|
|
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'));
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
lines: lines,
|
|
|
|
|
follow: true,
|
2025-07-13 13:14:32 +02:00
|
|
|
format: typeof req.query.format === 'string' ? req.query.format : 'json'
|
2023-08-04 13:41:13 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [error, logStream] = await safe(system.getLogs(req.params.unit, 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');
|
2024-02-24 17:18:38 +01:00
|
|
|
res.on('close', () => logStream.destroy());
|
2023-08-04 13:41:13 +05:30
|
|
|
logStream.on('data', function (data) {
|
|
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
res.write(sse(obj.realtimeTimestamp, JSON.stringify(obj))); // send timestamp as id
|
|
|
|
|
});
|
|
|
|
|
logStream.on('end', res.end.bind(res));
|
|
|
|
|
logStream.on('error', res.end.bind(res, null));
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 16:26:36 +02:00
|
|
|
async function getMetrics(req, res, next) {
|
2025-07-13 13:14:32 +02:00
|
|
|
if (typeof req.query.fromSecs !== 'string' || !parseInt(req.query.fromSecs, 10)) return next(new HttpError(400, 'fromSecs must be a number'));
|
|
|
|
|
if (typeof req.query.intervalSecs !== 'string' || !parseInt(req.query.intervalSecs, 10)) return next(new HttpError(400, 'intervalSecs must be a number'));
|
2023-08-04 13:41:13 +05:30
|
|
|
|
2025-05-22 11:17:31 +02:00
|
|
|
const fromSecs = parseInt(req.query.fromSecs, 10);
|
|
|
|
|
const intervalSecs = parseInt(req.query.intervalSecs, 10);
|
2025-07-13 13:14:32 +02:00
|
|
|
const noNullPoints = typeof req.query.noNullPoints === 'string' ? (req.query.noNullPoints === '1' || req.query.noNullPoints === 'true') : false;
|
2025-07-07 10:09:36 +02:00
|
|
|
const system = req.query.system === 'true';
|
|
|
|
|
const appIds = 'appId' in req.query ? (Array.isArray(req.query.appId) ? req.query.appId : [ req.query.appId ]) : [];
|
|
|
|
|
const serviceIds = 'serviceId' in req.query ? (Array.isArray(req.query.serviceId) ? req.query.serviceId : [ req.query.serviceId ]) : [];
|
|
|
|
|
|
2025-07-07 15:53:09 +02:00
|
|
|
const [error, result] = await safe(metrics.get({ fromSecs, intervalSecs, noNullPoints, system, appIds, serviceIds }));
|
2023-08-04 13:41:13 +05:30
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, result));
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 17:15:04 +02:00
|
|
|
async function getMetricStream(req, res, next) {
|
|
|
|
|
if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream'));
|
|
|
|
|
|
2025-07-07 15:53:09 +02:00
|
|
|
const system = req.query.system === 'true';
|
|
|
|
|
const appIds = 'appId' in req.query ? (Array.isArray(req.query.appId) ? req.query.appId : [ req.query.appId ]) : [];
|
|
|
|
|
const serviceIds = 'serviceId' in req.query ? (Array.isArray(req.query.serviceId) ? req.query.serviceId : [ req.query.serviceId ]) : [];
|
|
|
|
|
|
|
|
|
|
const [error, metricStream] = await safe(metrics.getStream({ system, appIds, serviceIds }));
|
2025-05-21 17:15:04 +02:00
|
|
|
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', () => metricStream.destroy());
|
2025-07-04 22:42:05 +02:00
|
|
|
metricStream.on('data', function (obj) { // objectMode stream
|
2025-05-21 17:15:04 +02:00
|
|
|
const sse = `data: ${JSON.stringify(obj)}\n\n`;
|
|
|
|
|
res.write(sse);
|
|
|
|
|
});
|
|
|
|
|
metricStream.on('end', res.end.bind(res));
|
|
|
|
|
metricStream.on('error', res.end.bind(res, null));
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:41:13 +05:30
|
|
|
async function getBlockDevices(req, res, next) {
|
|
|
|
|
const [error, devices] = await safe(system.getBlockDevices());
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { devices }));
|
|
|
|
|
}
|
2023-12-04 00:23:25 +01:00
|
|
|
|
|
|
|
|
async function getCpus(req, res, next) {
|
|
|
|
|
const [error, cpus] = await safe(system.getCpus());
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { cpus }));
|
|
|
|
|
}
|
2025-07-16 17:20:15 +02:00
|
|
|
|
|
|
|
|
async function getFilesystems(req, res, next) {
|
|
|
|
|
const [error, filesystems] = await safe(system.getFilesystems());
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { filesystems }));
|
|
|
|
|
}
|
2025-07-16 23:09:06 +02:00
|
|
|
|
|
|
|
|
async function getFilesystemUsage(req, res, next) {
|
|
|
|
|
if (typeof req.query.filesystem !== 'string') return next(new HttpError(400, 'getFilesystemUsage'));
|
|
|
|
|
if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream'));
|
|
|
|
|
|
|
|
|
|
const [error, task] = await safe(system.getFilesystemUsage(req.query.filesystem));
|
|
|
|
|
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: 30000\n'); // client should .close() to prevent reconnect within 30s
|
|
|
|
|
res.on('close', () => task.stop());
|
|
|
|
|
task.on('data', function (type, data) {
|
|
|
|
|
const obj = { type, ...data };
|
|
|
|
|
const sse = `data: ${JSON.stringify(obj)}\n\n`;
|
|
|
|
|
res.write(sse);
|
|
|
|
|
});
|
|
|
|
|
task.on('done', function (error) {
|
|
|
|
|
const obj = { type: 'done', ...error };
|
|
|
|
|
const sse = `data: ${JSON.stringify(obj)}\n\n`;
|
|
|
|
|
res.write(sse);
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-10 16:26:37 +02:00
|
|
|
task.start(); // background
|
2025-07-16 23:09:06 +02:00
|
|
|
}
|