- Rename varaverk.page → Varaverk.page: unRAID nginx only routes URLs starting with a capital letter (~^/[A-Z].*), lowercase caused 404 - Add require_once config.php to include/scheduler.php and include/docs.php: both used SCRIPTS_DIR constant without including the file that defines it - Replace hardcoded SCRIPTS_DIR with cfg-file-backed setting: reads from /boot/config/plugins/varaverk/varaverk.cfg, falls back to default on first run - Add Plugin Settings card to Config tab with scripts path field - Add api/settings.php to write the cfg file (validates directory exists)
101 lines
3.6 KiB
PHP
101 lines
3.6 KiB
PHP
<?php
|
|
// Scheduler — manages schedule.json and /etc/cron.d/varaverk.
|
|
// schedule.json is per-host, never synced.
|
|
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
define('SCHEDULE_FILE', '/boot/config/plugins/varaverk/schedule.json');
|
|
define('CRON_FILE', '/etc/cron.d/varaverk');
|
|
define('CRON_USER', 'root');
|
|
|
|
function vv_schedule_load(): array {
|
|
if (!file_exists(SCHEDULE_FILE)) return [];
|
|
$data = json_decode(file_get_contents(SCHEDULE_FILE), true);
|
|
return is_array($data) ? $data : [];
|
|
}
|
|
|
|
function vv_schedule_save(array $schedule): bool {
|
|
$dir = dirname(SCHEDULE_FILE);
|
|
if (!is_dir($dir)) mkdir($dir, 0755, true);
|
|
return file_put_contents(SCHEDULE_FILE, json_encode($schedule, JSON_PRETTY_PRINT)) !== false;
|
|
}
|
|
|
|
function vv_schedule_update(string $id, bool $enabled, string $cron): bool {
|
|
$schedule = vv_schedule_load();
|
|
$schedule[$id] = [
|
|
'id' => $id,
|
|
'enabled' => $enabled,
|
|
'cron' => $cron,
|
|
'updated' => date('c'),
|
|
];
|
|
if (!vv_schedule_save($schedule)) return false;
|
|
return vv_cron_rebuild($schedule);
|
|
}
|
|
|
|
function vv_cron_rebuild(array $schedule): bool {
|
|
$lines = ["# Varaverk — managed by plugin, do not edit manually"];
|
|
$lines[] = "# Regenerated: " . date('Y-m-d H:i:s');
|
|
$lines[] = "";
|
|
|
|
$scriptsDir = SCRIPTS_DIR;
|
|
foreach ($schedule as $entry) {
|
|
if (empty($entry['enabled']) || empty($entry['cron']) || empty($entry['id'])) continue;
|
|
$script = "$scriptsDir/{$entry['id']}";
|
|
$cron = $entry['cron'];
|
|
$lines[] = "$cron " . CRON_USER . " bash \"$script\" >> /var/log/varaverk/jobs.log 2>&1";
|
|
}
|
|
$lines[] = "";
|
|
|
|
return file_put_contents(CRON_FILE, implode("\n", $lines)) !== false;
|
|
}
|
|
|
|
// Walk the scripts repo and return the job tree:
|
|
// orchestrators as top-level, individual scripts as children
|
|
function vv_job_tree(): array {
|
|
$scriptsDir = SCRIPTS_DIR;
|
|
$schedule = vv_schedule_load();
|
|
|
|
// Orchestrators are in Orchestrators/ and their children are all scripts they call
|
|
// For now: walk top-level folders, treat *_management.sh or *_maintenance.sh as orchs
|
|
$orchPattern = "$scriptsDir/Orchestrators/*.sh";
|
|
$orchs = [];
|
|
|
|
foreach (glob($orchPattern) ?: [] as $path) {
|
|
$id = 'Orchestrators/' . basename($path);
|
|
$entry = $schedule[$id] ?? ['enabled' => false, 'cron' => ''];
|
|
$orchs[] = [
|
|
'id' => $id,
|
|
'label' => basename($path, '.sh'),
|
|
'type' => 'orchestrator',
|
|
'enabled' => (bool)($entry['enabled'] ?? false),
|
|
'cron' => $entry['cron'] ?? '',
|
|
'children' => vv_script_children($path, $schedule),
|
|
];
|
|
}
|
|
return $orchs;
|
|
}
|
|
|
|
// Parse an orchestrator script to find which child scripts it calls
|
|
function vv_script_children(string $orchPath, array $schedule): array {
|
|
$scriptsDir = SCRIPTS_DIR;
|
|
$content = file_get_contents($orchPath) ?: '';
|
|
$children = [];
|
|
|
|
// Match: bash "path/to/script.sh" or source path/to/script.sh
|
|
preg_match_all('/(?:bash|source|\.)\s+"?([^"\s]+\.sh)"?/m', $content, $m);
|
|
foreach ($m[1] as $rel) {
|
|
// Normalise relative paths
|
|
$rel = ltrim(str_replace($scriptsDir . '/', '', $rel), './');
|
|
$id = $rel;
|
|
$entry = $schedule[$id] ?? ['enabled' => false, 'cron' => ''];
|
|
$children[] = [
|
|
'id' => $id,
|
|
'label' => basename($rel, '.sh'),
|
|
'type' => 'script',
|
|
'enabled' => (bool)($entry['enabled'] ?? false),
|
|
'cron' => $entry['cron'] ?? '',
|
|
];
|
|
}
|
|
return $children;
|
|
}
|