From b497d4a7200591e0b7b91948120cbfa395f5c92f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 10 Dec 2019 18:18:28 +0100 Subject: [statistics/rebootcontrol] Add remote exec UI --- .../rebootcontrol/inc/rebootcontrol.inc.php | 16 ++++- .../rebootcontrol/inc/rebootqueries.inc.php | 29 --------- .../rebootcontrol/inc/rebootutils.inc.php | 75 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 30 deletions(-) delete mode 100644 modules-available/rebootcontrol/inc/rebootqueries.inc.php create mode 100644 modules-available/rebootcontrol/inc/rebootutils.inc.php (limited to 'modules-available/rebootcontrol/inc') diff --git a/modules-available/rebootcontrol/inc/rebootcontrol.inc.php b/modules-available/rebootcontrol/inc/rebootcontrol.inc.php index 489b0252..667c8bbd 100644 --- a/modules-available/rebootcontrol/inc/rebootcontrol.inc.php +++ b/modules-available/rebootcontrol/inc/rebootcontrol.inc.php @@ -21,7 +21,7 @@ class RebootControl */ public static function reboot($uuids, $kexec = false) { - $list = RebootQueries::getMachinesByUuid($uuids); + $list = RebootUtils::getMachinesByUuid($uuids); if (empty($list)) return false; return self::execute($list, $kexec ? RebootControl::KEXEC_REBOOT : RebootControl::REBOOT, 0); @@ -501,4 +501,18 @@ class RebootControl $subnet['iclients'] = array_slice($subnet['iclients'], 0, 3); } + public static function prepareExec() + { + User::assertPermission('action.exec'); + $uuids = array_values(Request::post('uuid', Request::REQUIRED, 'array')); + $machines = RebootUtils::getFilteredMachineList($uuids, 'action.exec'); + if ($machines === false) + return; + RebootUtils::sortRunningFirst($machines); + $id = mt_rand(); + Session::set('exec-' . $id, $machines, 60); + Session::save(); + Util::redirect('?do=rebootcontrol&show=exec&what=prepare&id=' . $id); + } + } diff --git a/modules-available/rebootcontrol/inc/rebootqueries.inc.php b/modules-available/rebootcontrol/inc/rebootqueries.inc.php deleted file mode 100644 index c0c479bd..00000000 --- a/modules-available/rebootcontrol/inc/rebootqueries.inc.php +++ /dev/null @@ -1,29 +0,0 @@ -fetchAll(PDO::FETCH_ASSOC); - $ret = []; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $ret[$row['machineuuid']] = $row; - } - return $ret; - } - -} \ No newline at end of file diff --git a/modules-available/rebootcontrol/inc/rebootutils.inc.php b/modules-available/rebootcontrol/inc/rebootutils.inc.php new file mode 100644 index 00000000..99235e8a --- /dev/null +++ b/modules-available/rebootcontrol/inc/rebootutils.inc.php @@ -0,0 +1,75 @@ +fetchAll(PDO::FETCH_ASSOC); + $ret = []; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $ret[$row['machineuuid']] = $row; + } + return $ret; + } + + /** + * Sort list of clients so that machines that are up and running come first. + * Requires the array elements to have key "state" from machine table. + * @param array $clients list of clients + */ + public static function sortRunningFirst(&$clients) + { + usort($clients, function($a, $b) { + $a = ($a['state'] === 'IDLE' || $a['state'] === 'OCCUPIED'); + $b = ($b['state'] === 'IDLE' || $b['state'] === 'OCCUPIED'); + if ($a === $b) + return 0; + return $a ? -1 : 1; + }); + } + + /** + * Query list of clients (by uuid), taking user context into account, by filtering + * by given $permission. + * @param array $requestedClients list of uuids + * @param string $permission name of location-aware permission to check + * @return array|false List of clients the user has access to. + */ + public static function getFilteredMachineList($requestedClients, $permission) + { + $actualClients = RebootUtils::getMachinesByUuid($requestedClients); + if (count($actualClients) !== count($requestedClients)) { + // We could go ahead an see which ones were not found in DB but this should not happen anyways unless the + // user manipulated the request + Message::addWarning('some-machine-not-found'); + } + // Filter ones with no permission + foreach (array_keys($actualClients) as $idx) { + if (!User::hasPermission($permission, $actualClients[$idx]['locationid'])) { + Message::addWarning('locations.no-permission-location', $actualClients[$idx]['locationid']); + unset($actualClients[$idx]); + } + } + // See if anything is left + if (!is_array($actualClients) || empty($actualClients)) { + Message::addError('no-clients-selected'); + return false; + } + return $actualClients; + } + +} \ No newline at end of file -- cgit v1.2.3-55-g7522