2020-11-07 22:28:14 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-04-09 21:41:30 +00:00
|
|
|
* Copyright 2018-2021 Balena Ltd.
|
2020-11-07 22:28:14 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-10-16 10:25:37 +00:00
|
|
|
import ColorHash = require('color-hash');
|
|
|
|
import * as _ from 'lodash';
|
2020-02-27 12:38:50 +00:00
|
|
|
import type { Readable } from 'stream';
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2020-02-28 15:25:12 +00:00
|
|
|
import { getChalk } from '../lazy';
|
2018-10-16 10:25:37 +00:00
|
|
|
import Logger = require('../logger');
|
2020-12-03 20:50:00 +00:00
|
|
|
import { ExpectedError, SIGINTError } from '../../errors';
|
2020-11-07 22:28:14 +00:00
|
|
|
|
|
|
|
class DeviceConnectionLostError extends ExpectedError {
|
|
|
|
public static defaultMsg = 'Connection to device lost';
|
|
|
|
constructor(msg?: string) {
|
|
|
|
super(msg || DeviceConnectionLostError.defaultMsg);
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 10:25:37 +00:00
|
|
|
|
|
|
|
interface Log {
|
|
|
|
message: string;
|
|
|
|
timestamp?: number;
|
|
|
|
serviceName?: string;
|
|
|
|
|
|
|
|
// There's also a serviceId and imageId, but they're
|
|
|
|
// meaningless in local mode
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BuildLog {
|
|
|
|
serviceName: string;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display logs from a device logging stream. This function will return
|
|
|
|
* when the log stream ends.
|
|
|
|
*
|
2019-04-24 13:59:07 +00:00
|
|
|
* @param logs A stream which produces newline seperated log
|
|
|
|
* objects
|
|
|
|
* @param logger A Logger instance which the logs will be
|
|
|
|
* displayed through
|
2019-04-24 16:30:18 +00:00
|
|
|
* @param system Only show system (and potentially the
|
|
|
|
* filterService) logs
|
2019-04-24 13:59:07 +00:00
|
|
|
* @param filterService Filter the logs so that only logs
|
|
|
|
* from a single service will be displayed
|
2018-10-16 10:25:37 +00:00
|
|
|
*/
|
2020-11-07 22:28:14 +00:00
|
|
|
async function displayDeviceLogs(
|
2018-10-16 10:25:37 +00:00
|
|
|
logs: Readable,
|
|
|
|
logger: Logger,
|
2019-04-24 16:30:18 +00:00
|
|
|
system: boolean,
|
2019-05-28 15:56:39 +00:00
|
|
|
filterServices?: string[],
|
2020-06-30 20:24:35 +00:00
|
|
|
): Promise<void> {
|
2020-12-03 20:50:00 +00:00
|
|
|
const { addSIGINTHandler } = await import('../helpers');
|
2021-04-09 21:41:30 +00:00
|
|
|
const { parse: ndjsonParse } = await import('ndjson');
|
2020-11-07 22:28:14 +00:00
|
|
|
let gotSignal = false;
|
|
|
|
const handleSignal = () => {
|
|
|
|
gotSignal = true;
|
|
|
|
logs.emit('close');
|
|
|
|
};
|
2020-12-03 20:50:00 +00:00
|
|
|
addSIGINTHandler(handleSignal);
|
2020-11-07 22:28:14 +00:00
|
|
|
process.once('SIGTERM', handleSignal);
|
|
|
|
try {
|
2020-12-03 20:50:00 +00:00
|
|
|
await new Promise((_resolve, reject) => {
|
2021-04-09 21:41:30 +00:00
|
|
|
const jsonStream = ndjsonParse();
|
|
|
|
jsonStream.on('data', (log) => {
|
|
|
|
displayLogObject(log, logger, system, filterServices);
|
|
|
|
});
|
|
|
|
jsonStream.on('error', (e) => {
|
|
|
|
logger.logWarn(`Error parsing NDJSON log chunk: ${e}`);
|
2020-11-07 22:28:14 +00:00
|
|
|
});
|
|
|
|
logs.once('error', reject);
|
|
|
|
logs.once('end', () => {
|
|
|
|
logger.logWarn(DeviceConnectionLostError.defaultMsg);
|
|
|
|
if (gotSignal) {
|
2020-12-03 20:50:00 +00:00
|
|
|
reject(new SIGINTError('Log streaming aborted on SIGINT signal'));
|
2020-11-07 22:28:14 +00:00
|
|
|
} else {
|
|
|
|
reject(new DeviceConnectionLostError());
|
|
|
|
}
|
|
|
|
});
|
2021-04-09 21:41:30 +00:00
|
|
|
logs.pipe(jsonStream);
|
2018-10-16 10:25:37 +00:00
|
|
|
});
|
2020-11-07 22:28:14 +00:00
|
|
|
} finally {
|
|
|
|
process.removeListener('SIGINT', handleSignal);
|
|
|
|
process.removeListener('SIGTERM', handleSignal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:50:00 +00:00
|
|
|
/**
|
|
|
|
* Open a TCP connection to the device's supervisor (TCP port 48484) and tail
|
|
|
|
* (display) device logs. Retry (reconnect) up to maxAttempts times if the
|
|
|
|
* TCP connection drops. Don't retry on SIGINT (CTRL-C).
|
|
|
|
* See function `displayDeviceLogs` for parameter documentation.
|
|
|
|
*/
|
2020-11-07 22:28:14 +00:00
|
|
|
export async function connectAndDisplayDeviceLogs({
|
|
|
|
deviceApi,
|
|
|
|
logger,
|
|
|
|
system,
|
|
|
|
filterServices,
|
|
|
|
maxAttempts = 3,
|
|
|
|
}: {
|
|
|
|
deviceApi: import('./api').DeviceAPI;
|
|
|
|
logger: Logger;
|
|
|
|
system: boolean;
|
|
|
|
filterServices?: string[];
|
|
|
|
maxAttempts?: number;
|
|
|
|
}) {
|
|
|
|
async function connectAndDisplay() {
|
|
|
|
// Open a new connection to the device's supervisor, TCP port 48484
|
|
|
|
const logStream = await deviceApi.getLogStream();
|
|
|
|
return displayDeviceLogs(logStream, logger, system, filterServices);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { retry } = await import('../../utils/helpers');
|
|
|
|
try {
|
|
|
|
await retry({
|
|
|
|
func: connectAndDisplay,
|
|
|
|
maxAttempts,
|
|
|
|
label: 'Streaming logs',
|
2019-05-13 10:51:41 +00:00
|
|
|
});
|
2020-11-07 22:28:14 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof DeviceConnectionLostError) {
|
|
|
|
err.message = `Max retry count (${
|
|
|
|
maxAttempts - 1
|
|
|
|
}) exceeded while attempting to reconnect to the device`;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2018-10-16 10:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function displayBuildLog(log: BuildLog, logger: Logger): void {
|
|
|
|
const toPrint = `${getServiceColourFn(log.serviceName)(
|
|
|
|
`[${log.serviceName}]`,
|
|
|
|
)} ${log.message}`;
|
|
|
|
logger.logBuild(toPrint);
|
|
|
|
}
|
|
|
|
|
2019-04-24 13:59:07 +00:00
|
|
|
export function displayLogObject<T extends Log>(
|
|
|
|
obj: T,
|
|
|
|
logger: Logger,
|
2019-04-24 16:30:18 +00:00
|
|
|
system: boolean,
|
2019-05-28 15:56:39 +00:00
|
|
|
filterServices?: string[],
|
2019-04-24 13:59:07 +00:00
|
|
|
): void {
|
2019-04-24 10:31:45 +00:00
|
|
|
let toPrint: string;
|
|
|
|
if (obj.timestamp != null) {
|
|
|
|
toPrint = `[${new Date(obj.timestamp).toLocaleString()}]`;
|
|
|
|
} else {
|
|
|
|
toPrint = `[${new Date().toLocaleString()}]`;
|
|
|
|
}
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2019-04-24 10:31:45 +00:00
|
|
|
if (obj.serviceName != null) {
|
2019-05-28 15:56:39 +00:00
|
|
|
if (filterServices) {
|
|
|
|
if (!_.includes(filterServices, obj.serviceName)) {
|
2019-04-24 16:30:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (system) {
|
2019-04-24 13:59:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-24 16:30:18 +00:00
|
|
|
|
2019-04-24 10:31:45 +00:00
|
|
|
const colourFn = getServiceColourFn(obj.serviceName);
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2019-04-24 10:31:45 +00:00
|
|
|
toPrint += ` ${colourFn(`[${obj.serviceName}]`)}`;
|
2019-05-28 15:56:39 +00:00
|
|
|
} else if (filterServices != null && !system) {
|
2019-04-24 13:59:07 +00:00
|
|
|
// We have a system log here but we are filtering based
|
|
|
|
// on a service, so drop this too
|
|
|
|
return;
|
2019-04-24 10:31:45 +00:00
|
|
|
}
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2019-04-24 10:31:45 +00:00
|
|
|
toPrint += ` ${obj.message}`;
|
2018-10-16 10:25:37 +00:00
|
|
|
|
2019-04-24 10:31:45 +00:00
|
|
|
logger.logLogs(toPrint);
|
2018-10-16 10:25:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:55:58 +00:00
|
|
|
export const getServiceColourFn = _.memoize(_getServiceColourFn);
|
2018-10-16 10:25:37 +00:00
|
|
|
|
|
|
|
const colorHash = new ColorHash();
|
|
|
|
function _getServiceColourFn(serviceName: string): (msg: string) => string {
|
|
|
|
const [r, g, b] = colorHash.rgb(serviceName);
|
|
|
|
|
2020-02-28 15:25:12 +00:00
|
|
|
return getChalk().rgb(r, g, b);
|
2018-10-16 10:25:37 +00:00
|
|
|
}
|