Support since and until in supervisor journalctl wrapper API.

Signed-off-by: Ruben Keulemans ruben.keulemans@protonmail.com
Change-type: minor
Closes: #2083
This commit is contained in:
Ruben Keulemans 2022-12-27 08:30:21 +01:00 committed by rkeulemans
parent 8b7cecfd3e
commit 9a1cde7f44
4 changed files with 27 additions and 7 deletions

View File

@ -1291,5 +1291,15 @@ From an app container:
$ curl -X POST -H "Content-Type: application/json" --data '{"follow":true,"all":true}' "$BALENA_SUPERVISOR_ADDRESS/v2/journal-logs?apikey=$BALENA_SUPERVISOR_API_KEY" > log.journal $ curl -X POST -H "Content-Type: application/json" --data '{"follow":true,"all":true}' "$BALENA_SUPERVISOR_ADDRESS/v2/journal-logs?apikey=$BALENA_SUPERVISOR_API_KEY" > log.journal
``` ```
##### since: string
> **Introduced in supervisor v14.7.0**
Show journal logs since the given `since` timestamp, formats are described here:
[https://www.freedesktop.org/software/systemd/man/journalctl.html#-S](https://www.freedesktop.org/software/systemd/man/journalctl.html#-S)
##### until: string
> **Introduced in supervisor v14.7.0**
Show journal logs until the given `until` timestamp, formats are described here:
[https://www.freedesktop.org/software/systemd/man/journalctl.html#-S](https://www.freedesktop.org/software/systemd/man/journalctl.html#-S)
An example project using this endpoint can be found An example project using this endpoint can be found
[in this repository](https://github.com/balena-io-playground/device-cloud-logging). [in this repository](https://github.com/balena-io-playground/device-cloud-logging).

View File

@ -549,6 +549,8 @@ router.post('/v2/journal-logs', (req, res) => {
const unit = req.body.unit; const unit = req.body.unit;
const format = req.body.format || 'short'; const format = req.body.format || 'short';
const containerId = req.body.containerId; const containerId = req.body.containerId;
const since = req.body.since;
const until = req.body.until;
const journald = spawnJournalctl({ const journald = spawnJournalctl({
all, all,
@ -557,6 +559,8 @@ router.post('/v2/journal-logs', (req, res) => {
unit, unit,
format, format,
containerId, containerId,
since,
until,
}); });
res.status(200); res.status(200);
// We know stdout will be present // We know stdout will be present

View File

@ -10,7 +10,8 @@ export function spawnJournalctl(opts: {
containerId?: string; containerId?: string;
format: string; format: string;
filterString?: string; filterString?: string;
since?: number; since?: number | string;
until?: number | string;
}): ChildProcess { }): ChildProcess {
const args: string[] = []; const args: string[] = [];
if (opts.all) { if (opts.all) {
@ -33,12 +34,11 @@ export function spawnJournalctl(opts: {
} }
if (opts.since != null) { if (opts.since != null) {
args.push('-S'); args.push('-S');
args.push( args.push(opts.since.toString());
new Date(opts.since) }
.toISOString() if (opts.until != null) {
.replace(/T/, ' ') // replace T with a space args.push('-U');
.replace(/\..+/, ''), // delete the dot and everything after args.push(opts.until.toString());
);
} }
args.push('-o'); args.push('-o');
args.push(opts.format); args.push(opts.format);

View File

@ -26,6 +26,8 @@ describe('lib/journald', () => {
unit: 'nginx.service', unit: 'nginx.service',
containerId: 'abc123', containerId: 'abc123',
format: 'json-pretty', format: 'json-pretty',
since: '2014-03-25 03:59:56.654563',
until: '2014-03-25 03:59:59.654563',
}); });
const expectedCommand = `journalctl`; const expectedCommand = `journalctl`;
@ -40,6 +42,10 @@ describe('lib/journald', () => {
'10', '10',
'-o', '-o',
'json-pretty', 'json-pretty',
'-S',
'2014-03-25 03:59:56.654563',
'-U',
'2014-03-25 03:59:59.654563',
]; ];
const actualCommand = spawn.firstCall.args[0]; const actualCommand = spawn.firstCall.args[0];