Skip to content

Commit 5209df2

Browse files
authored
fix(api): logrotate modification & permissions (#1145)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Chores** - Updated deployment and build commands to use a more efficient package manager. - **Refactor** - Improved the internal file modification structure for enhanced flexibility and maintainability. - **New Features** - Enhanced log management by adding functionality for proper permission handling and cleanup after operations. - Introduced a new log rotation configuration for managing log files effectively. - Updated timestamps for various components to reflect the latest download times. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 874d132 commit 5209df2

File tree

10 files changed

+73
-24
lines changed

10 files changed

+73
-24
lines changed

api/justfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ default:
55
@just list-commands
66

77
setup:
8-
npm install
9-
npm run container:build
8+
pnpm install
9+
pnpm run container:build
1010

1111
# builds js files that can run on an unraid server
1212
@build:
13-
npm run build
13+
pnpm run build
1414

1515
# deploys to an unraid server
1616
@deploy:
1717
./scripts/deploy-dev.sh
1818

1919
# build & deploy
2020
bd: build deploy
21-

api/src/unraid-api/unraid-file-modifier/file-modification.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export abstract class FileModification {
2727
* Get the path to the applied patch file for the target filePath, saved after applying the patch
2828
* @param targetFile - The path to the file that was patched
2929
*/
30-
private getPathToAppliedPatch(targetFile: string): string {
30+
protected getPathToAppliedPatch(targetFile = this.filePath): string {
3131
const dir = dirname(targetFile);
3232
const filename = `${basename(targetFile)}.patch`;
3333
return join(dir, filename);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1739303188175
1+
1739911366509
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1739303187535
1+
1739911365729
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1739303187849
1+
1739911366308
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1739303188587
1+
1739911366763

api/src/unraid-api/unraid-file-modifier/modifications/__test__/generic-modification.spec.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ interface ModificationTestCase {
1919
}
2020

2121
const getPathToFixture = (fileName: string) => resolve(__dirname, `__fixtures__/downloaded/${fileName}`);
22-
const testCases: ModificationTestCase[] = [
22+
23+
/** Modifications that patch the content of an existing file in one or more places. */
24+
const patchTestCases: ModificationTestCase[] = [
2325
{
2426
ModificationClass: DefaultPageLayoutModification,
2527
fileUrl:
@@ -43,6 +45,10 @@ const testCases: ModificationTestCase[] = [
4345
fileUrl: 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/auth-request.php',
4446
fileName: 'auth-request.php',
4547
},
48+
];
49+
50+
/** Modifications that simply add a new file & remove it on rollback. */
51+
const simpleTestCases: ModificationTestCase[] = [
4652
{
4753
ModificationClass: LogRotateModification,
4854
fileUrl: 'logrotate.conf',
@@ -131,16 +137,24 @@ async function testInvalidModification(testCase: ModificationTestCase, patcher:
131137
await patcher.rollback();
132138
}
133139

140+
const allTestCases = [...patchTestCases, ...simpleTestCases];
141+
134142
describe('File modifications', () => {
135143
let patcher: FileModification;
136144

137-
test.each(testCases)(`$fileName modifier correctly applies to fresh install`, async (testCase) => {
138-
await testModification(testCase, patcher);
139-
});
145+
test.each(allTestCases)(
146+
`$fileName modifier correctly applies to fresh install`,
147+
async (testCase) => {
148+
await testModification(testCase, patcher);
149+
}
150+
);
140151

141-
test.each(testCases)(`$fileName modifier correctly handles invalid content`, async (testCase) => {
142-
await testInvalidModification(testCase, patcher);
143-
});
152+
test.each(patchTestCases)(
153+
`$fileName modifier correctly handles invalid content`,
154+
async (testCase) => {
155+
await testInvalidModification(testCase, patcher);
156+
}
157+
);
144158

145159
afterEach(async () => {
146160
await patcher?.rollback();

api/src/unraid-api/unraid-file-modifier/modifications/__test__/snapshots/logrotate.conf.modified.snapshot.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/var/log/unraid-api/*.log {
32
rotate 1
43
missingok
@@ -9,4 +8,13 @@
98
copytruncate
109
create 0640 root root
1110
}
12-
11+
/var/log/graphql-api.log {
12+
rotate 1
13+
missingok
14+
size 1M
15+
su root root
16+
compress
17+
delaycompress
18+
copytruncate
19+
create 0640 root root
20+
}

api/src/unraid-api/unraid-file-modifier/modifications/log-rotate.modification.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Logger } from '@nestjs/common';
2-
import { readFile } from 'node:fs/promises';
2+
import { readFile, rm, writeFile } from 'node:fs/promises';
33

44
import { fileExists } from '@app/core/utils/files/file-exists';
55
import {
@@ -21,7 +21,17 @@ export class LogRotateModification extends FileModification {
2121
copytruncate
2222
create 0640 root root
2323
}
24-
`;
24+
/var/log/graphql-api.log {
25+
rotate 1
26+
missingok
27+
size 1M
28+
su root root
29+
compress
30+
delaycompress
31+
copytruncate
32+
create 0640 root root
33+
}
34+
`.trimStart();
2535

2636
constructor(logger: Logger) {
2737
super(logger);
@@ -46,4 +56,15 @@ export class LogRotateModification extends FileModification {
4656
}
4757
return { shouldApply: true, reason: 'No LogRotate config for the API configured yet' };
4858
}
59+
60+
async apply(): Promise<string> {
61+
await this.rollback();
62+
await writeFile(this.filePath, this.logRotateConfig, { mode: 0o644 });
63+
return this.logRotateConfig;
64+
}
65+
66+
async rollback(): Promise<void> {
67+
await rm(this.getPathToAppliedPatch(), { force: true });
68+
await rm(this.filePath, { force: true });
69+
}
4970
}

api/src/unraid-api/unraid-file-modifier/modifications/patches/log-rotate.patch

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ Index: /etc/logrotate.d/unraid-api
22
===================================================================
33
--- /etc/logrotate.d/unraid-api original
44
+++ /etc/logrotate.d/unraid-api modified
5-
@@ -0,0 +1,12 @@
6-
+
5+
@@ -0,0 +1,20 @@
76
+/var/log/unraid-api/*.log {
87
+ rotate 1
98
+ missingok
@@ -14,5 +13,13 @@ Index: /etc/logrotate.d/unraid-api
1413
+ copytruncate
1514
+ create 0640 root root
1615
+}
17-
+
18-
\ No newline at end of file
16+
+/var/log/graphql-api.log {
17+
+ rotate 1
18+
+ missingok
19+
+ size 1M
20+
+ su root root
21+
+ compress
22+
+ delaycompress
23+
+ copytruncate
24+
+ create 0640 root root
25+
+}

0 commit comments

Comments
 (0)