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 ++++++++++++++++++++++ modules-available/rebootcontrol/page.inc.php | 29 ++------- modules-available/rebootcontrol/pages/exec.inc.php | 57 ++++++++++++++++ modules-available/rebootcontrol/pages/task.inc.php | 4 +- .../templates/exec-enter-command.html | 41 ++++++++++++ .../rebootcontrol/templates/status-exec.html | 55 ++++++++++++++++ .../rebootcontrol/templates/status-wol.html | 2 +- modules-available/statistics/page.inc.php | 4 ++ modules-available/statistics/pages/list.inc.php | 2 + .../statistics/templates/clientlist.html | 6 ++ 12 files changed, 262 insertions(+), 58 deletions(-) delete mode 100644 modules-available/rebootcontrol/inc/rebootqueries.inc.php create mode 100644 modules-available/rebootcontrol/inc/rebootutils.inc.php create mode 100644 modules-available/rebootcontrol/pages/exec.inc.php create mode 100644 modules-available/rebootcontrol/templates/exec-enter-command.html create mode 100644 modules-available/rebootcontrol/templates/status-exec.html 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 diff --git a/modules-available/rebootcontrol/page.inc.php b/modules-available/rebootcontrol/page.inc.php index 764a3d7b..cf87a3b3 100644 --- a/modules-available/rebootcontrol/page.inc.php +++ b/modules-available/rebootcontrol/page.inc.php @@ -71,31 +71,10 @@ class Page_RebootControl extends Page return; } - $actualClients = RebootQueries::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('action.' . $action, $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'); + $actualClients = RebootUtils::getFilteredMachineList($requestedClients, 'action.' . $action); + if ($actualClients === false) return; - } - usort($actualClients, 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; - }); + RebootUtils::sortRunningFirst($actualClients); if ($action === 'shutdown') { $mode = 'SHUTDOWN'; $minutes = Request::post('s-minutes', 0, 'int'); @@ -144,7 +123,7 @@ class Page_RebootControl extends Page $clients = Request::post('clients'); if (is_array($clients)) { // XXX No permission check here, should we consider this as leaking sensitive information? - $machines = RebootQueries::getMachinesByUuid(array_values($clients), false, ['machineuuid', 'state']); + $machines = RebootUtils::getMachinesByUuid(array_values($clients), false, ['machineuuid', 'state']); $ret = []; foreach ($machines as $machine) { switch ($machine['state']) { diff --git a/modules-available/rebootcontrol/pages/exec.inc.php b/modules-available/rebootcontrol/pages/exec.inc.php new file mode 100644 index 00000000..58053072 --- /dev/null +++ b/modules-available/rebootcontrol/pages/exec.inc.php @@ -0,0 +1,57 @@ + $machines, 'id' => $id]); + } + + public static function doAjax() + { + + } + +} \ No newline at end of file diff --git a/modules-available/rebootcontrol/pages/task.inc.php b/modules-available/rebootcontrol/pages/task.inc.php index e52eb981..691fd9e2 100644 --- a/modules-available/rebootcontrol/pages/task.inc.php +++ b/modules-available/rebootcontrol/pages/task.inc.php @@ -82,9 +82,9 @@ class SubPage // Output if ($type === RebootControl::TASK_REBOOTCTL) { - $job['clients'] = RebootQueries::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid')); + $job['clients'] = RebootUtils::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid')); } elseif ($type === RebootControl::TASK_EXEC) { - $details = RebootQueries::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid'), true); + $details = RebootUtils::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid'), true); foreach ($job['clients'] as &$client) { if (isset($client['machineuuid']) && isset($details[$client['machineuuid']])) { $client += $details[$client['machineuuid']]; diff --git a/modules-available/rebootcontrol/templates/exec-enter-command.html b/modules-available/rebootcontrol/templates/exec-enter-command.html new file mode 100644 index 00000000..5916e2a8 --- /dev/null +++ b/modules-available/rebootcontrol/templates/exec-enter-command.html @@ -0,0 +1,41 @@ +

{{lang_execRemoteCommand}}

+ + + + + + + + + + + + {{#clients}} + + + + + + {{/clients}} + +
{{lang_client}}{{lang_ip}} + {{lang_status}} +
{{hostname}}{{^hostname}}{{machineuuid}}{{/hostname}}{{clientip}}{{state}}
+ +

{{lang_enterCommand}}

+ +
+ + + +
+ + +
+
+ +
+
\ No newline at end of file diff --git a/modules-available/rebootcontrol/templates/status-exec.html b/modules-available/rebootcontrol/templates/status-exec.html new file mode 100644 index 00000000..140de02b --- /dev/null +++ b/modules-available/rebootcontrol/templates/status-exec.html @@ -0,0 +1,55 @@ + +
{{lang_executingRemotely}}
+ +
+ +
+
{{lang_host}}
+
{{lang_status}}
+
{{lang_exitCode}}
+
+ +{{#clients}} +
+
+
{{hostname}}{{^hostname}}{{clientip}}{{/hostname}}
+
+
+
+ {{lang_stdout}} +

+	{{lang_stderr}}
+	

+
+
+{{/clients}} + + \ No newline at end of file diff --git a/modules-available/rebootcontrol/templates/status-wol.html b/modules-available/rebootcontrol/templates/status-wol.html index da19b57d..3e83126c 100644 --- a/modules-available/rebootcontrol/templates/status-wol.html +++ b/modules-available/rebootcontrol/templates/status-wol.html @@ -4,7 +4,7 @@
{{#tasks}} -
{{lang_aWolJob}}
+
{{lang_aWolJob}}
{{/tasks}} {{^tasks}}
diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index ff5a59cd..533a9bf9 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -74,6 +74,10 @@ class Page_Statistics extends Page $this->rebootControl(false); } elseif ($action === 'wol') { $this->wol(); + } elseif ($action === 'prepare-exec') { + if (Module::isAvailable('rebootcontrol')) { + RebootControl::prepareExec(); + } } // Make sure we don't render any content for POST requests - should be handled above and then diff --git a/modules-available/statistics/pages/list.inc.php b/modules-available/statistics/pages/list.inc.php index f223dfb2..e2e7ff09 100644 --- a/modules-available/statistics/pages/list.inc.php +++ b/modules-available/statistics/pages/list.inc.php @@ -60,6 +60,7 @@ class SubPage $rebootAllowedLocations = User::getAllowedLocations('.rebootcontrol.action.reboot'); $shutdownAllowedLocations = User::getAllowedLocations('.rebootcontrol.action.reboot'); $wolAllowedLocations = User::getAllowedLocations('.rebootcontrol.action.wol'); + $execAllowedLocations = User::getAllowedLocations('.rebootcontrol.action.exec'); // Only make client clickable if user is allowed to view details page $detailsAllowedLocations = User::getAllowedLocations("machine.view-details"); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { @@ -125,6 +126,7 @@ class SubPage 'canShutdown' => !empty($shutdownAllowedLocations), 'canDelete' => !empty($deleteAllowedLocations), 'canWol' => !empty($wolAllowedLocations), + 'canExec' => !empty($execAllowedLocations), ); Render::addTemplate('clientlist', $data); } diff --git a/modules-available/statistics/templates/clientlist.html b/modules-available/statistics/templates/clientlist.html index 6ff9bac7..47be4dd0 100644 --- a/modules-available/statistics/templates/clientlist.html +++ b/modules-available/statistics/templates/clientlist.html @@ -178,6 +178,12 @@ {{lang_wakeOnLan}} {{/canWol}} + {{#canExec}} + + {{/canExec}} {{/rebootcontrol}} {{#canDelete}}