From 3e45ec44d22f03ca6642e08f695c6d7274cecfaf Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sat, 7 Dec 2019 13:52:54 +0100 Subject: [statistics/rebootcontrol] Add WOL button to statistics module * Overhauled task display in rebootcontrol module * Can only add subnets by CIDR now instead of start and end --- modules-available/rebootcontrol/pages/task.inc.php | 119 +++++++++++++++------ 1 file changed, 84 insertions(+), 35 deletions(-) (limited to 'modules-available/rebootcontrol/pages/task.inc.php') diff --git a/modules-available/rebootcontrol/pages/task.inc.php b/modules-available/rebootcontrol/pages/task.inc.php index 15449aaf..e52eb981 100644 --- a/modules-available/rebootcontrol/pages/task.inc.php +++ b/modules-available/rebootcontrol/pages/task.inc.php @@ -10,6 +10,12 @@ class SubPage public static function doRender() { + $xxx = Request::get('tasks'); + if (is_array($xxx)) { + $data = array_map(function($item) { return ['id' => $item]; }, $xxx); + Render::addTemplate('status-wol', ['tasks' => $data]); + return; + } $show = Request::get('what', 'tasklist', 'string'); if ($show === 'tasklist') { self::showTaskList(); @@ -21,67 +27,110 @@ class SubPage private static function showTask() { $taskid = Request::get("taskid", Request::REQUIRED, 'string'); - $task = Taskmanager::status($taskid); - - if (!Taskmanager::isTask($task) || !isset($task['data'])) { - Message::addError('no-such-task', $taskid); - return; - } - - $td =& $task['data']; $type = Request::get('type', false, 'string'); - if ($type === false) { - // Try to guess - if (isset($td['locationId']) || isset($td['clients'])) { - $type = 'reboot'; - } elseif (isset($td['result'])) { - $type = 'exec'; + if ($type === 'checkhost') { + // Override + $task = Taskmanager::status($taskid); + if (!Taskmanager::isTask($task) || !isset($task['data'])) { + Message::addError('no-such-task', $taskid); + return; } - } - if ($type === 'reboot') { - $data = [ - 'taskId' => $task['id'], - 'locationId' => $td['locationId'], - 'locationName' => Location::getName($td['locationId']), - ]; - $uuids = array_map(function ($entry) { - return $entry['machineuuid']; - }, $td['clients']); - $data['clients'] = RebootQueries::getMachinesByUuid($uuids); - Render::addTemplate('status-reboot', $data); - } elseif ($type === 'exec') { - $data = [ - 'taskId' => $task['id'], - ]; - Render::addTemplate('status-exec', $data); - } elseif ($type === 'checkhost') { + $td =& $task['data']; $ip = array_key_first($td['result']); $data = [ 'taskId' => $task['id'], 'host' => $ip, ]; Render::addTemplate('status-checkconnection', $data); - } else { + return; + } + if ($type !== false) { Message::addError('unknown-task-type'); } + + $job = RebootControl::getActiveTasks(null, $taskid); + if ($job === false) { + Message::addError('no-such-task', $taskid); + return; + } + if (isset($job['type'])) { + $type = $job['type']; + } + if ($type === RebootControl::TASK_EXEC) { + $template = $perm = 'exec'; + } elseif ($type === RebootControl::TASK_REBOOTCTL) { + $template = 'reboot'; + if ($job['action'] === RebootControl::SHUTDOWN) { + $perm = 'shutdown'; + } else { + $perm = 'reboot'; + } + } elseif ($type == RebootControl::TASK_WOL) { + $template = $perm = 'wol'; + } else { + Message::addError('unknown-task-type', $type); + return; + } + if (!empty($job['locations'])) { + $allowedLocs = User::getAllowedLocations("action.$perm"); + if (!in_array(0, $allowedLocs) && array_diff($job['locations'], $allowedLocs) !== []) { + Message::addError('main.no-permission'); + return; + } + self::expandLocationIds($job['locations']); + } + + // Output + if ($type === RebootControl::TASK_REBOOTCTL) { + $job['clients'] = RebootQueries::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid')); + } elseif ($type === RebootControl::TASK_EXEC) { + $details = RebootQueries::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid'), true); + foreach ($job['clients'] as &$client) { + if (isset($client['machineuuid']) && isset($details[$client['machineuuid']])) { + $client += $details[$client['machineuuid']]; + } + } + } elseif ($type === RebootControl::TASK_WOL) { + // Nothing (yet) + } else { + Util::traceError('oopsie'); + } + Render::addTemplate('status-' . $template, $job); } private static function showTaskList() { + Render::addTemplate('task-header'); // Append list of active reboot/shutdown tasks - $allowedLocs = User::getAllowedLocations("*"); + $allowedLocs = User::getAllowedLocations("action.*"); $active = RebootControl::getActiveTasks($allowedLocs); if (empty($active)) { Message::addInfo('no-current-tasks'); } else { foreach ($active as &$entry) { - $entry['locationName'] = Location::getName($entry['locationId']); + self::expandLocationIds($entry['locations']); + if (isset($entry['clients'])) { + $entry['clients'] = count($entry['clients']); + } } unset($entry); Render::addTemplate('task-list', ['list' => $active]); } } + private static function expandLocationIds(&$lids) + { + foreach ($lids as &$locid) { + if ($locid === 0) { + $name = '-'; + } else { + $name = Location::getName($locid); + } + $locid = ['id' => $locid, 'name' => $name]; + } + $lids = array_values($lids); + } + public static function doAjax() { -- cgit v1.2.3-55-g7522