Merge pull request #1070 from balena-io/update-journald-handling-and-docs

Update documentation and explicitly unpipe journald stream
This commit is contained in:
CameronDiver 2019-08-22 09:36:40 +01:00 committed by GitHub
commit 8fe18ed74a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

@ -1185,7 +1185,7 @@ Unsuccessful response:
Added in supervisor version v10.2.0 Added in supervisor version v10.2.0
Retrieve a stream to the journald logs on device. This is Retrieve a stream to the journald logs on device. This is
equivalent to running `journalctl -o export`. Options equivalent to running `journalctl --no-pager`. Options
supported are: supported are:
##### all: boolean ##### all: boolean
@ -1203,14 +1203,19 @@ Show the most recent `count` events, equivalent to
Show journal logs from `unit` only, equivalent to Show journal logs from `unit` only, equivalent to
`journalctl --unit=<unit>`. `journalctl --unit=<unit>`.
##### format
Added in supervisor version v10.3.0
The format which will be streamed from journalctl, formats
are described here:
https://www.freedesktop.org/software/systemd/man/journalctl.html#-o
Fields should be provided via POST body in JSON format. Fields should be provided via POST body in JSON format.
From an application container (with systemd installed): From an application container:
``` ```
$ curl -X POST --data '{"follow":true,"all":true}' "$BALENA_SUPERVISOR_ADDRESS/v2/journal-logs?apikey=$BALENA_SUPERVISOR_API_KEY" | systemd-journal-remote - -o 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
``` ```
The `log.journal` file can then be viewed with An example project using this endpoint can be found
``` [in this repository](https://github.com/balena-io-playground/device-cloud-logging).
journalctl --file log.journal -f
```

View File

@ -539,14 +539,16 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
const follow = checkTruthy(req.body.follow) || false; const follow = checkTruthy(req.body.follow) || false;
const count = checkInt(req.body.count, { positive: true }) || undefined; const count = checkInt(req.body.count, { positive: true }) || undefined;
const unit = req.body.unit; const unit = req.body.unit;
const format = req.body.format || 'short';
const journald = spawnJournalctl({ all, follow, count, unit }); const journald = spawnJournalctl({ all, follow, count, unit, format });
res.status(200); res.status(200);
journald.stdout.pipe(res); journald.stdout.pipe(res);
res.on('close', () => { res.on('close', () => {
journald.kill('SIGKILL'); journald.kill('SIGKILL');
}); });
journald.on('exit', () => { journald.on('exit', () => {
journald.stdout.unpipe();
res.end(); res.end();
}); });
} catch (e) { } catch (e) {

View File

@ -8,13 +8,12 @@ export function spawnJournalctl(opts: {
follow: boolean; follow: boolean;
count?: number; count?: number;
unit?: string; unit?: string;
format: string;
}): ChildProcess { }): ChildProcess {
const args = [ const args = [
// The directory we want to run the chroot from // The directory we want to run the chroot from
constants.rootMountPoint, constants.rootMountPoint,
'journalctl', 'journalctl',
'-o',
'export',
]; ];
if (opts.all) { if (opts.all) {
args.push('-a'); args.push('-a');
@ -30,6 +29,8 @@ export function spawnJournalctl(opts: {
args.push('-n'); args.push('-n');
args.push(opts.count.toString()); args.push(opts.count.toString());
} }
args.push('-o');
args.push(opts.format);
log.debug('Spawning journald with: chroot ', args.join(' ')); log.debug('Spawning journald with: chroot ', args.join(' '));