From 7d4b41dc7e85b4f5f249c61db11ae27d53144c9c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 25 Apr 2018 16:46:18 +0200 Subject: [rebootcontrol] Nicer status list; list of all running jobs --- .../rebootcontrol/inc/rebootcontrol.inc.php | 65 ++++++++++++++++++++-- 1 file changed, 60 insertions(+), 5 deletions(-) (limited to 'modules-available/rebootcontrol/inc/rebootcontrol.inc.php') diff --git a/modules-available/rebootcontrol/inc/rebootcontrol.inc.php b/modules-available/rebootcontrol/inc/rebootcontrol.inc.php index 789552cd..1024b036 100644 --- a/modules-available/rebootcontrol/inc/rebootcontrol.inc.php +++ b/modules-available/rebootcontrol/inc/rebootcontrol.inc.php @@ -3,28 +3,83 @@ class RebootControl { + const KEY_TASKLIST = 'rebootcontrol.tasklist'; + + const REBOOT = 'REBOOT'; + const KEXEC_REBOOT = 'KEXEC_REBOOT'; + const SHUTDOWN = 'SHUTDOWN'; + /** * @param string[] $uuids List of machineuuids to reboot + * @param bool $kexec whether to trigger kexec-reboot instead of full BIOS cycle * @return false|array task struct for the reboot job */ - public static function reboot($uuids) + public static function reboot($uuids, $kexec = false) { $list = RebootQueries::getMachinesByUuid($uuids); if (empty($list)) return false; - return self::execute($list, false, 0, 0); + return self::execute($list, $kexec ? RebootControl::KEXEC_REBOOT : RebootControl::REBOOT, 0, 0); } - public static function execute($list, $shutdown, $minutes, $locationId) + /** + * @param array $list list of clients containing each keys 'machineuuid' and 'clientip' + * @param string $mode reboot mode: RebootControl::REBOOT ::KEXEC_REBOOT or ::SHUTDOWN + * @param int $minutes delay in minutes for action + * @param int $locationId meta data only: locationId of clients + * @return array|false the task, or false if it could not be started + */ + public static function execute($list, $mode, $minutes, $locationId) { - return Taskmanager::submit("RemoteReboot", array( + $task = Taskmanager::submit("RemoteReboot", array( "clients" => $list, - "shutdown" => $shutdown, + "mode" => $mode, "minutes" => $minutes, "locationId" => $locationId, "sshkey" => SSHKey::getPrivateKey(), "port" => 9922, // Hard-coded, must match mgmt-sshd module )); + if (!Taskmanager::isFailed($task)) { + Property::addToList(RebootControl::KEY_TASKLIST, $locationId . '/' . $task["id"], 60 * 24); + } + return $task; + } + + /** + * @param int[]|null $locations filter by these locations + * @return array list of active tasks for reboots/shutdowns. + */ + public static function getActiveTasks($locations = null) + { + if (is_array($locations) && in_array(0,$locations)) { + $locations = null; + } + $list = Property::getList(RebootControl::KEY_TASKLIST); + $return = []; + foreach ($list as $entry) { + $p = explode('/', $entry, 2); + if (count($p) !== 2) { + Property::removeFromList(RebootControl::KEY_TASKLIST, $entry); + continue; + } + if (is_array($locations) && !in_array($p[0], $locations)) // Ignore + continue; + $id = $p[1]; + $task = Taskmanager::status($id); + if ($task === false) { + Property::removeFromList(RebootControl::KEY_TASKLIST, $entry); + continue; + } + $return[] = [ + 'taskId' => $task['id'], + 'locationId' => $task['data']['locationId'], + 'time' => $task['data']['time'], + 'mode' => $task['data']['mode'], + 'clientCount' => count($task['data']['clients']), + 'status' => $task['statusCode'], + ]; + } + return $return; } } \ No newline at end of file -- cgit v1.2.3-55-g7522