diff --git a/api/justfile b/api/justfile index 2ce5bf561..7dd7b87a5 100644 --- a/api/justfile +++ b/api/justfile @@ -5,12 +5,12 @@ default: @just list-commands setup: - npm install - npm run container:build + pnpm install + pnpm run container:build # builds js files that can run on an unraid server @build: - npm run build + pnpm run build # deploys to an unraid server @deploy: @@ -18,4 +18,3 @@ setup: # build & deploy bd: build deploy - diff --git a/api/src/unraid-api/unraid-file-modifier/file-modification.ts b/api/src/unraid-api/unraid-file-modifier/file-modification.ts index 97dde355a..5c483dd5d 100644 --- a/api/src/unraid-api/unraid-file-modifier/file-modification.ts +++ b/api/src/unraid-api/unraid-file-modifier/file-modification.ts @@ -27,7 +27,7 @@ export abstract class FileModification { * Get the path to the applied patch file for the target filePath, saved after applying the patch * @param targetFile - The path to the file that was patched */ - private getPathToAppliedPatch(targetFile: string): string { + protected getPathToAppliedPatch(targetFile = this.filePath): string { const dir = dirname(targetFile); const filename = `${basename(targetFile)}.patch`; return join(dir, filename); diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/.login.php.last-download-time b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/.login.php.last-download-time index e93c4431c..c41576b02 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/.login.php.last-download-time +++ b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/.login.php.last-download-time @@ -1 +1 @@ -1739303188175 \ No newline at end of file +1739911366509 \ No newline at end of file diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/DefaultPageLayout.php.last-download-time b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/DefaultPageLayout.php.last-download-time index d52739fcf..d77a47264 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/DefaultPageLayout.php.last-download-time +++ b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/DefaultPageLayout.php.last-download-time @@ -1 +1 @@ -1739303187535 \ No newline at end of file +1739911365729 \ No newline at end of file diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/Notifications.page.last-download-time b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/Notifications.page.last-download-time index 5c553585e..72a5b106f 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/Notifications.page.last-download-time +++ b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/Notifications.page.last-download-time @@ -1 +1 @@ -1739303187849 \ No newline at end of file +1739911366308 \ No newline at end of file diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/auth-request.php.last-download-time b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/auth-request.php.last-download-time index ffb618811..6d6404683 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/auth-request.php.last-download-time +++ b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/__fixtures__/downloaded/auth-request.php.last-download-time @@ -1 +1 @@ -1739303188587 \ No newline at end of file +1739911366763 \ No newline at end of file diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/generic-modification.spec.ts b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/generic-modification.spec.ts index 90be0aa13..b538a4f90 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/generic-modification.spec.ts +++ b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/generic-modification.spec.ts @@ -19,7 +19,9 @@ interface ModificationTestCase { } const getPathToFixture = (fileName: string) => resolve(__dirname, `__fixtures__/downloaded/${fileName}`); -const testCases: ModificationTestCase[] = [ + +/** Modifications that patch the content of an existing file in one or more places. */ +const patchTestCases: ModificationTestCase[] = [ { ModificationClass: DefaultPageLayoutModification, fileUrl: @@ -43,6 +45,10 @@ const testCases: ModificationTestCase[] = [ fileUrl: 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/auth-request.php', fileName: 'auth-request.php', }, +]; + +/** Modifications that simply add a new file & remove it on rollback. */ +const simpleTestCases: ModificationTestCase[] = [ { ModificationClass: LogRotateModification, fileUrl: 'logrotate.conf', @@ -131,16 +137,24 @@ async function testInvalidModification(testCase: ModificationTestCase, patcher: await patcher.rollback(); } +const allTestCases = [...patchTestCases, ...simpleTestCases]; + describe('File modifications', () => { let patcher: FileModification; - test.each(testCases)(`$fileName modifier correctly applies to fresh install`, async (testCase) => { - await testModification(testCase, patcher); - }); + test.each(allTestCases)( + `$fileName modifier correctly applies to fresh install`, + async (testCase) => { + await testModification(testCase, patcher); + } + ); - test.each(testCases)(`$fileName modifier correctly handles invalid content`, async (testCase) => { - await testInvalidModification(testCase, patcher); - }); + test.each(patchTestCases)( + `$fileName modifier correctly handles invalid content`, + async (testCase) => { + await testInvalidModification(testCase, patcher); + } + ); afterEach(async () => { await patcher?.rollback(); diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/snapshots/logrotate.conf.modified.snapshot.php b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/snapshots/logrotate.conf.modified.snapshot.php index ba2fcc7d5..3f2396b82 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/__test__/snapshots/logrotate.conf.modified.snapshot.php +++ b/api/src/unraid-api/unraid-file-modifier/modifications/__test__/snapshots/logrotate.conf.modified.snapshot.php @@ -1,4 +1,3 @@ - /var/log/unraid-api/*.log { rotate 1 missingok @@ -9,4 +8,13 @@ copytruncate create 0640 root root } - \ No newline at end of file +/var/log/graphql-api.log { + rotate 1 + missingok + size 1M + su root root + compress + delaycompress + copytruncate + create 0640 root root +} diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/log-rotate.modification.ts b/api/src/unraid-api/unraid-file-modifier/modifications/log-rotate.modification.ts index dab033df2..0d40703ac 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/log-rotate.modification.ts +++ b/api/src/unraid-api/unraid-file-modifier/modifications/log-rotate.modification.ts @@ -1,5 +1,5 @@ import { Logger } from '@nestjs/common'; -import { readFile } from 'node:fs/promises'; +import { readFile, rm, writeFile } from 'node:fs/promises'; import { fileExists } from '@app/core/utils/files/file-exists'; import { @@ -21,7 +21,17 @@ export class LogRotateModification extends FileModification { copytruncate create 0640 root root } - `; +/var/log/graphql-api.log { + rotate 1 + missingok + size 1M + su root root + compress + delaycompress + copytruncate + create 0640 root root +} +`.trimStart(); constructor(logger: Logger) { super(logger); @@ -46,4 +56,15 @@ export class LogRotateModification extends FileModification { } return { shouldApply: true, reason: 'No LogRotate config for the API configured yet' }; } + + async apply(): Promise { + await this.rollback(); + await writeFile(this.filePath, this.logRotateConfig, { mode: 0o644 }); + return this.logRotateConfig; + } + + async rollback(): Promise { + await rm(this.getPathToAppliedPatch(), { force: true }); + await rm(this.filePath, { force: true }); + } } diff --git a/api/src/unraid-api/unraid-file-modifier/modifications/patches/log-rotate.patch b/api/src/unraid-api/unraid-file-modifier/modifications/patches/log-rotate.patch index 9a7e8c949..c958961f7 100644 --- a/api/src/unraid-api/unraid-file-modifier/modifications/patches/log-rotate.patch +++ b/api/src/unraid-api/unraid-file-modifier/modifications/patches/log-rotate.patch @@ -2,8 +2,7 @@ Index: /etc/logrotate.d/unraid-api =================================================================== --- /etc/logrotate.d/unraid-api original +++ /etc/logrotate.d/unraid-api modified -@@ -0,0 +1,12 @@ -+ +@@ -0,0 +1,20 @@ +/var/log/unraid-api/*.log { + rotate 1 + missingok @@ -14,5 +13,13 @@ Index: /etc/logrotate.d/unraid-api + copytruncate + create 0640 root root +} -+ -\ No newline at end of file ++/var/log/graphql-api.log { ++ rotate 1 ++ missingok ++ size 1M ++ su root root ++ compress ++ delaycompress ++ copytruncate ++ create 0640 root root ++}