$id, 'enabled' => $enabled, 'cron' => $cron, 'updated' => date('c'), ]; if (!vv_schedule_save($schedule)) return false; return vv_cron_rebuild($schedule); } define('LOG_DIR', '/var/log/varaverk'); function vv_job_log_path(string $id): string { return LOG_DIR . '/' . preg_replace('/\.sh$/', '.log', $id); } function vv_cron_rebuild(array $schedule): bool { if (!is_dir(LOG_DIR)) mkdir(LOG_DIR, 0755, true); $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']}"; $logFile = vv_job_log_path($entry['id']); $logDir = dirname($logFile); if (!is_dir($logDir)) mkdir($logDir, 0755, true); $lines[] = "{$entry['cron']} " . CRON_USER . " bash \"$script\" >> \"$logFile\" 2>&1"; } $lines[] = ""; return file_put_contents(CRON_FILE, implode("\n", $lines)) !== false; } // Extract a one-line description from a bash script header. // Supports two patterns: // 1. # PURPOSE (or # DESCRIPTION) block — returns first non-separator line after it // 2. First meaningful comment line after the banner function vv_script_description(string $path): string { if (!file_exists($path)) return ''; $lines = array_slice(file($path) ?: [], 0, 50); $purposeNext = false; $first = ''; foreach ($lines as $raw) { $raw = rtrim($raw); if (str_starts_with($raw, '#!')) continue; // shebang if (!str_starts_with($raw, '#')) continue; // non-comment $inner = ltrim(substr($raw, 1)); // strip leading # if ($inner === '') continue; // blank if (preg_match('/^[\s=\-─━\*]+$/', $inner)) continue; // pure separator if (preg_match('/^={3,}/', $inner)) continue; // banner (=== Title ===) if (preg_match('/^\s*(PURPOSE|DESCRIPTION|Description)\s*$/i', $inner)) { $purposeNext = true; continue; } if ($purposeNext) { return mb_substr(trim($inner), 0, 200); } if (!$first) $first = mb_substr(trim($inner), 0, 200); } return $first; } // 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'), 'desc' => vv_script_description($path), 'type' => 'orchestrator', 'enabled' => (bool)($entry['enabled'] ?? false), 'cron' => $entry['cron'] ?? '', 'children' => vv_script_children($path, $schedule), ]; } return $orchs; } // Parse a bash array from master.conf content and return its script paths. // Handles entries with inline args ("script.sh --flag") and skips commented lines (#"..."). function vv_parse_conf_array(string $conf, string $varName): array { if (!preg_match('/^\s*' . preg_quote($varName, '/') . '\s*=\s*\((.*?)^\s*\)/ms', $conf, $m)) { return []; } $scripts = []; preg_match_all('/^\s*(?!#)"([^"]+)"/m', $m[1], $entries); foreach ($entries[1] as $entry) { $parts = preg_split('/\s+/', trim($entry)); $path = $parts[0] ?? ''; if (substr($path, -3) === '.sh') $scripts[] = $path; } return $scripts; } // Parse an orchestrator script to find which child scripts it calls. // Two strategies, merged and deduped: // 1. Static paths: $SCRIPT_DIR/../Category/script.sh or $SCRIPTS_ROOT/Category/script.sh // 2. master.conf arrays: detects ${VARNAME[@]} iteration and reads the array from master.conf // Root-level files (load_config.sh etc.) excluded — must be in a subdirectory. // Each candidate validated against the filesystem. function vv_script_children(string $orchPath, array $schedule): array { $scriptsDir = SCRIPTS_DIR; $content = file_get_contents($orchPath) ?: ''; $children = []; $seen = []; $addChild = function(string $rel) use ($scriptsDir, $schedule, &$children, &$seen) { if (isset($seen[$rel]) || !file_exists("$scriptsDir/$rel")) return; $seen[$rel] = true; $entry = $schedule[$rel] ?? ['enabled' => false, 'cron' => '']; $children[] = [ 'id' => $rel, 'label' => basename($rel, '.sh'), 'desc' => vv_script_description("$scriptsDir/$rel"), 'type' => 'script', 'enabled' => (bool)($entry['enabled'] ?? false), 'cron' => $entry['cron'] ?? '', ]; }; // Strategy 1: static variable paths ($VAR/../Category/script.sh or $VAR/Category/script.sh) preg_match_all( '/\$[A-Z_]+\/(?:\.\.\/)?([A-Za-z][A-Za-z0-9_.\-]*\/[A-Za-z0-9_.\-]+\.sh)/', $content, $m ); foreach ($m[1] as $rel) $addChild($rel); // Strategy 2: master.conf arrays — find every ${VARNAME_SCRIPTS[@]} the orch iterates preg_match_all('/\$\{([A-Z_]+_SCRIPTS)\[@\]\}/', $content, $refs); if (!empty($refs[1])) { $confRaw = file_get_contents(CONF_DIR . '/master.conf') ?: ''; foreach (array_unique($refs[1]) as $varName) { foreach (vv_parse_conf_array($confRaw, $varName) as $rel) $addChild($rel); } } return $children; }