Skip to content

Commit cb67085

Browse files
committed
fix: invalid state for unraid plugin
1 parent 6bf3f77 commit cb67085

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
3+
import { writeFile } from 'fs/promises';
4+
5+
import { ConnectionMetadata, ConfigType } from './connect.config.js';
6+
7+
@Injectable()
8+
export class MinigraphStatusWriterService implements OnModuleInit {
9+
constructor(private readonly configService: ConfigService<ConfigType, true>) {}
10+
11+
private logger = new Logger(MinigraphStatusWriterService.name);
12+
13+
get statusFilePath() {
14+
// Write to /var/local/emhttp/minigraph-status.json so PHP can read it
15+
return '/var/local/emhttp/minigraph-status.json';
16+
}
17+
18+
async onModuleInit() {
19+
this.logger.verbose(`Status file path: ${this.statusFilePath}`);
20+
21+
// Write initial status
22+
await this.writeStatus();
23+
24+
// Listen for changes to minigraph status
25+
this.configService.changes$.subscribe({
26+
next: async (changes) => {
27+
const minigraphChanged = Array.isArray(changes) && changes.some((change: any) =>
28+
change.path && change.path.startsWith('connect.mothership')
29+
);
30+
if (minigraphChanged) {
31+
await this.writeStatus();
32+
}
33+
},
34+
error: (err) => {
35+
this.logger.error('Error receiving config changes:', err);
36+
},
37+
});
38+
}
39+
40+
private async writeStatus() {
41+
try {
42+
const connectionMetadata = this.configService.get<ConnectionMetadata>('connect.mothership');
43+
44+
// Try to get allowed origins from the store
45+
let allowedOrigins = '';
46+
try {
47+
// We can't import from @app here, so we'll skip allowed origins for now
48+
// This can be added later if needed
49+
allowedOrigins = '';
50+
} catch (error) {
51+
this.logger.debug('Could not get allowed origins:', error);
52+
}
53+
54+
const statusData = {
55+
minigraph: connectionMetadata?.status || 'PRE_INIT',
56+
error: connectionMetadata?.error || null,
57+
lastPing: connectionMetadata?.lastPing || null,
58+
allowedOrigins: allowedOrigins,
59+
timestamp: Date.now()
60+
};
61+
62+
const data = JSON.stringify(statusData, null, 2);
63+
this.logger.verbose(`Writing minigraph status: ${data}`);
64+
65+
await writeFile(this.statusFilePath, data);
66+
this.logger.verbose(`Status written to ${this.statusFilePath}`);
67+
} catch (error) {
68+
this.logger.error(error, `Error writing status to '${this.statusFilePath}'`);
69+
}
70+
}
71+
}

packages/unraid-api-plugin-connect/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
33

44
import { ConnectConfigPersister } from './config/config.persistence.js';
55
import { configFeature } from './config/connect.config.js';
6+
import { MinigraphStatusWriterService } from './config/minigraph-status-writer.service.js';
67
import { MothershipModule } from './mothership-proxy/mothership.module.js';
78
import { ConnectModule } from './unraid-connect/connect.module.js';
89

910
export const adapter = 'nestjs';
1011

1112
@Module({
1213
imports: [ConfigModule.forFeature(configFeature), ConnectModule, MothershipModule],
13-
providers: [ConnectConfigPersister],
14+
providers: [ConnectConfigPersister, MinigraphStatusWriterService],
1415
exports: [],
1516
})
1617
class ConnectPluginModule {

plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/UpdateFlashBackup.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@
2323
$myservers = file_exists($myservers_flash_cfg_path) ? @parse_ini_file($myservers_flash_cfg_path,true) : [];
2424
$isRegistered = !empty($myservers['remote']['username']);
2525

26-
$myservers_memory_cfg_path ='/var/local/emhttp/myservers.cfg';
27-
$mystatus = (file_exists($myservers_memory_cfg_path)) ? @parse_ini_file($myservers_memory_cfg_path) : [];
28-
$isConnected = (($mystatus['minigraph']??'')==='CONNECTED') ? true : false;
26+
// Read minigraph status from the new API status file
27+
$statusFilePath = '/var/local/emhttp/minigraph-status.json';
28+
$minigraphStatus = '';
29+
30+
if (file_exists($statusFilePath)) {
31+
$statusData = @json_decode(file_get_contents($statusFilePath), true);
32+
$minigraphStatus = $statusData['minigraph'] ?? '';
33+
}
34+
35+
$isConnected = ($minigraphStatus === 'CONNECTED') ? true : false;
2936

3037
$flashbackup_ini = '/var/local/emhttp/flashbackup.ini';
3138

plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/state.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ private function getFlashBackupStatus()
168168
private function getMyServersCfgValues()
169169
{
170170
/**
171-
* @todo can we read this from somewhere other than the flash? Connect page uses this path and /boot/config/plugins/dynamix.my.servers/myservers.cfg…
172-
* - $myservers_memory_cfg_path ='/var/local/emhttp/myservers.cfg';
173-
* - $mystatus = (file_exists($myservers_memory_cfg_path)) ? @parse_ini_file($myservers_memory_cfg_path) : [];
171+
* Memory config is now written by the new API to /usr/local/emhttp/state/myservers.cfg
172+
* This contains runtime state including connection status.
174173
*/
175174
$flashCfgPath = '/boot/config/plugins/dynamix.my.servers/myservers.cfg';
176175
$this->myServersFlashCfg = file_exists($flashCfgPath) ? @parse_ini_file($flashCfgPath, true) : [];
@@ -212,11 +211,19 @@ private function getConnectKnownOrigins()
212211
* Include localhost in the test, but only display HTTP(S) URLs that do not include localhost.
213212
*/
214213
$this->host = $_SERVER['HTTP_HOST'] ?? "unknown";
215-
$memoryCfgPath = '/var/local/emhttp/myservers.cfg';
216-
$this->myServersMemoryCfg = (file_exists($memoryCfgPath)) ? @parse_ini_file($memoryCfgPath) : [];
217-
$this->myServersMiniGraphConnected = (($this->myServersMemoryCfg['minigraph'] ?? '') === 'CONNECTED');
214+
// Read minigraph status and allowed origins from the new API status file
215+
$statusFilePath = '/var/local/emhttp/minigraph-status.json';
216+
$minigraphStatus = '';
217+
$allowedOrigins = '';
218+
219+
if (file_exists($statusFilePath)) {
220+
$statusData = @json_decode(file_get_contents($statusFilePath), true);
221+
$minigraphStatus = $statusData['minigraph'] ?? '';
222+
$allowedOrigins = $statusData['allowedOrigins'] ?? '';
223+
}
224+
225+
$this->myServersMiniGraphConnected = ($minigraphStatus === 'CONNECTED');
218226

219-
$allowedOrigins = $this->myServersMemoryCfg['allowedOrigins'] ?? "";
220227
$extraOrigins = $this->myServersFlashCfg['api']['extraOrigins'] ?? "";
221228
$combinedOrigins = $allowedOrigins . "," . $extraOrigins; // combine the two strings for easier searching
222229
$combinedOrigins = str_replace(" ", "", $combinedOrigins); // replace any spaces with nothing

0 commit comments

Comments
 (0)