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

View File

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

View File

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