Files
Varaverk/Plugin/usr/local/emhttp/plugins/varaverk/include/config.php
T
Gmer4Lfe 4ab3ddbc47 Add Varaverk plugin scaffold (PHP, unRAID native)
Plugin lives in Plugin/ — invisible to User Script Plugin.
dev_install.sh symlinks into /usr/local/emhttp/plugins/varaverk/ for development.

Pages: Monitor (5s poll), Scheduler (orchs + Advanced children, toggle + cron),
Config (raw editor, host-aware file access), Docs (markdown + live $VAR substitution).

API endpoints: monitor.php (JSON), scheduler.php (writes schedule.json + cron.d),
config.php (writes conf files with host permission check).

Includes: config.php (parser, host detection, var map), scheduler.php (job tree,
cron.d rebuild), monitor.php (docker, GPU, resources, fallback, transcode),
docs.php (file tree, var substitution, Parsedown renderer).
2026-05-23 16:19:12 -04:00

73 lines
2.4 KiB
PHP

<?php
// Config file parser and writer.
// Reads master.conf and the appropriate host*.conf based on running host.
define('SCRIPTS_DIR', '/mnt/user/appdata/unraid_scripts');
define('CONF_DIR', SCRIPTS_DIR . '/Configurations');
function vv_get_hostname(): string {
return trim(shell_exec('hostname -s') ?: '');
}
function vv_detect_host(): string {
// Reads master.conf to find HOST1/HOST2 hostnames, returns 'host1', 'host2', or 'unknown'
$master = vv_read_conf_raw('master.conf');
preg_match('/^\s*HOST1_NAME\s*=\s*["\']?(\S+?)["\']?\s*$/m', $master, $m1);
preg_match('/^\s*HOST2_NAME\s*=\s*["\']?(\S+?)["\']?\s*$/m', $master, $m2);
$hostname = vv_get_hostname();
if (!empty($m1[1]) && strcasecmp($hostname, $m1[1]) === 0) return 'host1';
if (!empty($m2[1]) && strcasecmp($hostname, $m2[1]) === 0) return 'host2';
return 'unknown';
}
function vv_is_owner(): bool {
return vv_detect_host() === 'host1';
}
function vv_read_conf_raw(string $filename): string {
$path = CONF_DIR . '/' . $filename;
return file_exists($path) ? file_get_contents($path) : '';
}
function vv_write_conf_raw(string $filename, string $content): bool {
$path = CONF_DIR . '/' . $filename;
return file_put_contents($path, $content) !== false;
}
function vv_get_conf_files(): array {
// Returns conf files this host is allowed to view/edit
$host = vv_detect_host();
$files = [];
if ($host === 'host1') {
$files[] = 'master.conf';
$files[] = 'host1.conf';
} elseif ($host === 'host2') {
$files[] = 'host2.conf';
} else {
// Unknown host — show all for dev/debug
foreach (glob(CONF_DIR . '/*.conf') as $f) {
$files[] = basename($f);
}
}
return $files;
}
// Parse conf into key=>value map for $VAR substitution in docs
function vv_conf_vars(): array {
$vars = [];
$files = ['master.conf'];
$host = vv_detect_host();
if ($host === 'host1') $files[] = 'host1.conf';
if ($host === 'host2') $files[] = 'host2.conf';
foreach ($files as $f) {
$raw = vv_read_conf_raw($f);
// Match: VAR_NAME="value" or VAR_NAME=value (no quotes)
preg_match_all('/^\s*([A-Z0-9_]+)\s*=\s*["\']?([^"\'#\n]*?)["\']?\s*(?:#.*)?$/m', $raw, $m);
foreach ($m[1] as $i => $key) {
$vars[$key] = trim($m[2][$i]);
}
}
return $vars;
}