refactor(cli): centralise loggers into one singleton

This commit is contained in:
Nicolas Meienberger 2023-08-16 22:51:42 +02:00
parent e036532b55
commit 6d170ce78b
7 changed files with 22 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import { appInfoSchema, createLogger } from '@runtipi/shared';
import { appInfoSchema } from '@runtipi/shared';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
@ -7,6 +7,7 @@ import { getEnv } from '@/utils/environment/environment';
import { pathExists } from '@/utils/fs-helpers';
import { compose } from '@/utils/docker-helpers';
import { copyDataDir, generateEnvFile } from './app.helpers';
import { fileLogger } from '@/utils/logger/file-logger';
const execAsync = promisify(exec);
@ -24,7 +25,7 @@ export class AppExecutors {
this.rootFolderHost = rootFolderHost;
this.storagePath = storagePath;
this.appsRepoId = appsRepoId;
this.logger = createLogger('app-executors', path.join(rootFolderHost, 'logs'));
this.logger = fileLogger;
}
private handleAppError = (err: unknown) => {

View file

@ -1,10 +1,10 @@
import { getEnv } from 'src/utils/environment/environment';
import { createLogger } from '@runtipi/shared';
import path from 'path';
import { promisify } from 'util';
import { exec } from 'child_process';
import { pathExists } from '@/utils/fs-helpers';
import { getRepoHash } from './repo.helpers';
import { fileLogger } from '@/utils/logger/file-logger';
const execAsync = promisify(exec);
@ -16,7 +16,7 @@ export class RepoExecutors {
constructor() {
const { rootFolderHost } = getEnv();
this.rootFolderHost = rootFolderHost;
this.logger = createLogger('repo-executors', path.join(rootFolderHost, 'logs'));
this.logger = fileLogger;
}
/**

View file

@ -7,15 +7,13 @@ import path from 'path';
import { promisify } from 'util';
import { exec, spawn } from 'child_process';
import si from 'systeminformation';
import { createLogger } from '@runtipi/shared';
import { Stream } from 'stream';
import { AppExecutors } from '../app/app.executors';
import { copySystemFiles, generateSystemEnvFile, generateTlsCertificates } from './system.helpers';
import { TerminalSpinner } from '@/utils/logger/terminal-spinner';
import { pathExists } from '@/utils/fs-helpers';
import { getEnv } from '@/utils/environment/environment';
const logger = createLogger('system-executors', path.join(process.cwd(), 'logs'));
import { fileLogger } from '@/utils/logger/file-logger';
const execAsync = promisify(exec);
@ -32,7 +30,7 @@ export class SystemExecutors {
private handleSystemError = (err: unknown) => {
if (err instanceof Error) {
logger.error(`An error occurred: ${err.message}`);
fileLogger.error(`An error occurred: ${err.message}`);
return { success: false, message: err.message };
}
@ -293,7 +291,7 @@ export class SystemExecutors {
return { success: true, message: 'Tipi updated' };
} catch (e) {
spinner.fail('Tipi update failed, see logs for details');
logger.error(e);
fileLogger.error(e);
return this.handleSystemError(e);
}
};

View file

@ -8,6 +8,7 @@ import { promisify } from 'util';
import chalk from 'chalk';
import { pathExists } from '@/utils/fs-helpers';
import { getRepoHash } from '../repo/repo.helpers';
import { fileLogger } from '@/utils/logger/file-logger';
type EnvKeys =
| 'APPS_REPO_ID'
@ -257,6 +258,7 @@ export const generateTlsCertificates = async (data: { domain?: string }) => {
await execAsync(`openssl req -x509 -newkey rsa:4096 -keyout traefik/tls/key.pem -out traefik/tls/cert.pem -days 365 -subj "${subject}" -addext "${subjectAltName}" -nodes`);
await fs.promises.writeFile(path.join(process.cwd(), 'traefik', 'tls', `${data.domain}.txt`), '');
} catch (error) {
fileLogger.error(error);
console.error(chalk.red('✗'), 'Failed to generate TLS certificates');
}
};

View file

@ -1,19 +1,17 @@
import path from 'path';
import { promisify } from 'util';
import { exec } from 'child_process';
import { createLogger } from '@runtipi/shared';
import { getEnv } from '../environment/environment';
import { pathExists } from '../fs-helpers/fs-helpers';
import { fileLogger } from '../logger/file-logger';
const execAsync = promisify(exec);
const logger = createLogger('docker-helpers', path.join(process.cwd(), 'logs'));
const composeUp = async (args: string[]) => {
const { stdout, stderr } = await execAsync(`docker compose ${args.join(' ')}`);
logger.info('stdout', stdout);
logger.info('stderr', stderr);
fileLogger.info('stdout', stdout);
fileLogger.info('stderr', stderr);
return { stdout, stderr };
};

View file

@ -0,0 +1,4 @@
import { createLogger } from '@runtipi/shared';
import path from 'path';
export const fileLogger = createLogger('cli', path.join(process.cwd(), 'logs'));

View file

@ -154,6 +154,9 @@ fi
URL="https://github.com/meienberger/runtipi/releases/download/$LATEST_VERSION/$LATEST_ASSET"
curl --location "$URL" -o runtipi-cli
mkdir -p ./runtipi
sudo ./runtipi-cli start
curl --location "$URL" -o ./runtipi/runtipi-cli
sudo chmod +x ./runtipi/runtipi-cli
sudo ./runtipi/runtipi-cli start