refactor tail invokation into logtail.sh

This commit is contained in:
Girish Ramakrishnan
2023-03-27 10:38:09 +02:00
parent 456da972e9
commit 603f92251e
7 changed files with 80 additions and 85 deletions

View File

@@ -45,7 +45,7 @@ const addonConfigs = require('./addonconfigs.js'),
hat = require('./hat.js'),
http = require('http'),
infra = require('./infra_version.js'),
LogStream = require('./log-stream.js'),
logs = require('./logs.js'),
mail = require('./mail.js'),
os = require('os'),
path = require('path'),
@@ -57,7 +57,6 @@ const addonConfigs = require('./addonconfigs.js'),
settings = require('./settings.js'),
sftp = require('./sftp.js'),
shell = require('./shell.js'),
spawn = require('child_process').spawn,
superagent = require('superagent'),
system = require('./system.js');
@@ -425,10 +424,6 @@ async function getServiceLogs(id, options) {
assert.strictEqual(typeof id, 'string');
assert(options && typeof options === 'object');
assert.strictEqual(typeof options.lines, 'number');
assert.strictEqual(typeof options.format, 'string');
assert.strictEqual(typeof options.follow, 'boolean');
const [name, instance ] = id.split(':');
if (instance) {
@@ -439,41 +434,18 @@ async function getServiceLogs(id, options) {
debug(`Getting logs for ${name}`);
const lines = options.lines,
format = options.format || 'json',
follow = options.follow;
let cp;
let cmd, args = [];
// docker and unbound use journald
if (name === 'docker' || name === 'unbound') {
cmd = 'journalctl';
args.push('--lines=' + (lines === -1 ? 'all' : lines));
args.push(`--unit=${name}`);
args.push('--no-pager');
args.push('--output=short-iso');
if (follow) args.push('--follow');
cp = logs.journalctl(name, options);
} else if (name === 'nginx') {
cmd = '/usr/bin/tail';
args.push('--lines=' + (lines === -1 ? '+1' : lines));
if (follow) args.push('--follow', '--retry', '--quiet'); // same as -F. to make it work if file doesn't exist, --quiet to not output file headers, which are no logs
args.push('/var/log/nginx/access.log');
args.push('/var/log/nginx/error.log');
cp = logs.tail(['/var/log/nginx/access.log', '/var/log/nginx/error.log'], { lines: options.lines, follow: options.follow });
} else {
cmd = '/usr/bin/tail';
args.push('--lines=' + (lines === -1 ? '+1' : lines));
if (follow) args.push('--follow', '--retry', '--quiet'); // same as -F. to make it work if file doesn't exist, --quiet to not output file headers, which are no logs
const containerName = APP_SERVICES[name] ? `${name}-${instance}` : name;
args.push(path.join(paths.LOG_DIR, containerName, 'app.log'));
cp = logs.tail([path.join(paths.LOG_DIR, containerName, 'app.log')], { lines: options.lines, follow: options.follow });
}
const cp = spawn(cmd, args);
const logStream = new LogStream({ format, source: name });
const logStream = new logs.LogStream({ format: options.format || 'json', source: name });
logStream.close = cp.kill.bind(cp, 'SIGKILL'); // closing stream kills the child process
cp.stdout.pipe(logStream);