mirror of
https://github.com/unraid/api.git
synced 2025-12-31 13:39:52 -06:00
feat: add log rotation
This commit is contained in:
@@ -75,6 +75,7 @@ export const fileExists = async (path: string) =>
|
||||
access(path, F_OK)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
|
||||
export const fileExistsSync = (path: string) => {
|
||||
try {
|
||||
accessSync(path, F_OK);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { rm, writeFile } from 'node:fs/promises';
|
||||
|
||||
import { execa } from 'execa';
|
||||
|
||||
import { fileExists } from '@app/core/utils/misc/parse-config';
|
||||
import {
|
||||
FileModification,
|
||||
ShouldApplyWithReason,
|
||||
} from '@app/unraid-api/unraid-file-modifier/unraid-file-modifier.service';
|
||||
|
||||
export class LogRotateModification implements FileModification {
|
||||
id: string = 'log-rotate';
|
||||
filePath: string = '/etc/logrotate.d/unraid-api' as const;
|
||||
logger: Logger;
|
||||
logRotateConfig: string = `
|
||||
/var/log/unraid-api/*.log {
|
||||
rotate 1
|
||||
missingok
|
||||
size 1M
|
||||
su root root
|
||||
compress
|
||||
delaycompress
|
||||
copytruncate
|
||||
create 0640 root root
|
||||
}
|
||||
`;
|
||||
|
||||
constructor(logger: Logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
async apply(): Promise<void> {
|
||||
await writeFile(this.filePath, this.logRotateConfig, { mode: '644' });
|
||||
// Ensure file is owned by root:root
|
||||
await execa('chown', ['root:root', this.filePath]).catch((err) => this.logger.error(err));
|
||||
}
|
||||
|
||||
async rollback(): Promise<void> {
|
||||
await rm(this.filePath);
|
||||
}
|
||||
|
||||
async shouldApply(): Promise<ShouldApplyWithReason> {
|
||||
const alreadyConfigured = await fileExists(this.filePath);
|
||||
if (alreadyConfigured) {
|
||||
return { shouldApply: false, reason: 'LogRotate configuration already exists' };
|
||||
}
|
||||
return { shouldApply: true, reason: 'No LogRotate config for the API configured yet' };
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/commo
|
||||
|
||||
import AuthRequestModification from '@app/unraid-api/unraid-file-modifier/modifications/auth-request.modification';
|
||||
import SSOFileModification from '@app/unraid-api/unraid-file-modifier/modifications/sso.modification';
|
||||
import { LogRotateModification } from '@app/unraid-api/unraid-file-modifier/modifications/log-rotate.modification';
|
||||
|
||||
export interface ShouldApplyWithReason {
|
||||
shouldApply: boolean;
|
||||
@@ -42,6 +43,7 @@ export class UnraidFileModificationService implements OnModuleInit, OnModuleDest
|
||||
async loadModifications(): Promise<FileModification[]> {
|
||||
const modifications: FileModification[] = [];
|
||||
const modificationClasses: Array<new (logger: Logger) => FileModification> = [
|
||||
LogRotateModification,
|
||||
AuthRequestModification,
|
||||
SSOFileModification,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user