From dc516c24685518b41bcce0751caf286dc65e471f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 17 Mar 2021 15:39:55 +0100 Subject: [locations/rebootcontrol] Change ENUM constants; display next event --- install.php | 11 +++++++++++ .../locations/lang/de/template-tags.json | 1 + .../locations/lang/en/template-tags.json | 1 + modules-available/locations/pages/details.inc.php | 22 +++++++++++++++++----- .../locations/permissions/permissions.json | 3 +++ .../locations/templates/location-subnets.html | 13 +++++++------ modules-available/rebootcontrol/hooks/cron.inc.php | 8 +++++--- .../rebootcontrol/inc/scheduler.inc.php | 16 ++++++++++------ modules-available/rebootcontrol/install.inc.php | 15 +++++++++++---- 9 files changed, 66 insertions(+), 24 deletions(-) diff --git a/install.php b/install.php index fa1258e0..f0bb1c72 100644 --- a/install.php +++ b/install.php @@ -43,6 +43,17 @@ define('UPDATE_NOOP', 'UPDATE_NOOP'); // Nothing had to be done, everything is u define('UPDATE_RETRY', 'UPDATE_RETRY'); // Install/update process failed, but should be retried later. define('UPDATE_FAILED', 'UPDATE_FAILED'); // Fatal error occurred, retry will not resolve the issue. +/** + * Take the return value of a Database::exec() call and emit failure + * if it's false. + */ +function handleUpdateResult($res) +{ + if ($res !== false) + return; + finalResponse(UPDATE_FAILED, Database::lastError()); +} + /* * Helper functions for dealing with the database */ diff --git a/modules-available/locations/lang/de/template-tags.json b/modules-available/locations/lang/de/template-tags.json index b8534e32..79bdbac6 100644 --- a/modules-available/locations/lang/de/template-tags.json +++ b/modules-available/locations/lang/de/template-tags.json @@ -36,6 +36,7 @@ "lang_moveMachines": "In durch Subnet zugeordneten Raum verschieben", "lang_moveable": "Verschiebbar", "lang_name": "Name", + "lang_nextEvent": "N\u00e4chstes geplantes Ereignis", "lang_numMachinesWithOverrides": "Anzahl Rechner, bei denen mindestens eine Konfigurationsvariable \u00fcberschrieben wird", "lang_offsetEarly": "Min. vorher", "lang_offsetLate": "Min. danach", diff --git a/modules-available/locations/lang/en/template-tags.json b/modules-available/locations/lang/en/template-tags.json index 467af8e1..5790902b 100644 --- a/modules-available/locations/lang/en/template-tags.json +++ b/modules-available/locations/lang/en/template-tags.json @@ -36,6 +36,7 @@ "lang_moveMachines": "Move to room designated by IP address", "lang_moveable": "Moveable", "lang_name": "Name", + "lang_nextEvent": "Next scheduled event", "lang_numMachinesWithOverrides": "Number of clients where at least one variable is overridden", "lang_offsetEarly": "min. before", "lang_offsetLate": "min. after", diff --git a/modules-available/locations/pages/details.inc.php b/modules-available/locations/pages/details.inc.php index e400d492..19a89c88 100644 --- a/modules-available/locations/pages/details.inc.php +++ b/modules-available/locations/pages/details.inc.php @@ -42,7 +42,7 @@ class SubPage $sd = Request::post('sd', false, 'bool'); $sdoffset = Request::post('sd-offset', 0, 'int'); - User::assertPermission('location.edit.*', $locationid); // TODO: Introduce permission + User::assertPermission('location.edit.openingtimes', $locationid); // Construct opening-times for database if ($openingTimes !== '') { @@ -405,19 +405,30 @@ class SubPage $data['used_percent'] = $count === 0 ? 0 : round(($used / $count) * 100); - Permission::addGlobalTags($data['perms'], $locationId, ['location.edit.name', 'location.edit.subnets', 'location.delete', '.roomplanner.edit'], 'save_button'); + Permission::addGlobalTags($data['perms'], $locationId, + ['location.edit.name', 'location.edit.subnets', 'location.delete', 'location.edit.openingtimes', '.roomplanner.edit'], + 'save_button'); if (empty($allowedLocs)) { $data['perms']['location']['edit']['parent']['disabled'] = 'disabled'; } else { unset($data['perms']['save_button']); } + if (Module::get('rebootcontrol') !== false) { + $res = Database::queryFirst("SELECT action, nextexecution FROM `reboot_scheduler` + WHERE locationid = :id", ['id' => $locationId]); + if ($res !== false) { + $data['next_action'] = $res['action']; + $data['next_time'] = Util::prettyTime($res['nextexecution']); + } + } + echo Render::parse('location-subnets', $data); } private static function ajaxOpeningTimes($id) { - User::assertPermission('location.edit', $id); + User::assertPermission('location.edit.openingtimes', $id); $openTimes = Database::queryFirst("SELECT openingtime FROM `location` WHERE locationid = :id", array('id' => $id)); if ($openTimes !== false) { $openingTimes = json_decode($openTimes['openingtime'], true); @@ -432,9 +443,10 @@ class SubPage $rebootcontrol = Module::isAvailable('rebootcontrol'); $data['rebootcontrol'] = $rebootcontrol; if ($rebootcontrol) { - $res = Database::queryFirst("SELECT * FROM `reboot_scheduler` WHERE locationid = :id", array('id' => $id)); + $res = Database::queryFirst("SELECT action, nextexecution, options FROM `reboot_scheduler` + WHERE locationid = :id", ['id' => $id]); if ($res !== false) { - $data['scheduler-options'] = json_decode($res['options']); + $data['scheduler-options'] = json_decode($res['options'], true); } } diff --git a/modules-available/locations/permissions/permissions.json b/modules-available/locations/permissions/permissions.json index 18b24a73..108bf8e0 100644 --- a/modules-available/locations/permissions/permissions.json +++ b/modules-available/locations/permissions/permissions.json @@ -14,6 +14,9 @@ "location.edit.parent": { "location-aware": true }, + "location.edit.openingtimes": { + "location-aware": true + }, "location.view": { "location-aware": true }, diff --git a/modules-available/locations/templates/location-subnets.html b/modules-available/locations/templates/location-subnets.html index e954bf10..8a9b1c99 100644 --- a/modules-available/locations/templates/location-subnets.html +++ b/modules-available/locations/templates/location-subnets.html @@ -62,7 +62,7 @@
{{lang_locationInfo}}
-
+
{{#haveDozmod}}
{{lang_referencingLectures}}: {{lectures}} @@ -80,14 +80,17 @@ {{/statsLink}}
{{/haveStatistics}} + {{#next_action}} +
+ {{lang_nextEvent}}: {{next_action}} – {{next_time}} +
+ {{/next_action}}
-
+
-
- -
diff --git a/modules-available/rebootcontrol/hooks/cron.inc.php b/modules-available/rebootcontrol/hooks/cron.inc.php index 56446c49..c1136c98 100644 --- a/modules-available/rebootcontrol/hooks/cron.inc.php +++ b/modules-available/rebootcontrol/hooks/cron.inc.php @@ -27,12 +27,14 @@ while ($row = $res->fetch(PDO::FETCH_ASSOC)) { $machines = Database::queryAll("SELECT machineuuid, clientip, macaddr, locationid FROM machine WHERE locationid = :locid", ['locid' => $row['locationid']]); - if ($row['action'] === 'sd') { + if ($row['action'] === Scheduler::SHUTDOWN) { RebootControl::execute($machines, RebootControl::SHUTDOWN, 0); - } elseif ($row['action'] === 'wol') { + } elseif ($row['action'] === Scheduler::WOL) { RebootControl::wakeMachines($machines); - } elseif ($row['action'] === 'rb') { + } elseif ($row['action'] === Scheduler::REBOOT) { RebootControl::execute($machines, RebootControl::REBOOT, 0); + } else { + EventLog::warning("Invalid action '{$row['action']}' in schedule for location " . $row['locationid']); } } diff --git a/modules-available/rebootcontrol/inc/scheduler.inc.php b/modules-available/rebootcontrol/inc/scheduler.inc.php index 613fbbee..45aedcc1 100644 --- a/modules-available/rebootcontrol/inc/scheduler.inc.php +++ b/modules-available/rebootcontrol/inc/scheduler.inc.php @@ -3,6 +3,10 @@ class Scheduler { + const SHUTDOWN = 'SHUTDOWN'; + const REBOOT = 'REBOOT'; + const WOL = 'WOL'; + public static function updateSchedule($locationid, $options, $openingTimes) { if (empty($openingTimes)) { @@ -57,11 +61,11 @@ class Scheduler foreach ($openingTimes as $row) { foreach ($row['days'] as $day) { if ($options['wol']) { - $events[] = ['action' => 'wol', + $events[] = ['action' => self::WOL, 'time' => self::calculateTimestamp($now, $day, $row['openingtime'])]; } if ($options['sd']) { - $events[] = ['action' => 'sd', + $events[] = ['action' => self::SHUTDOWN, 'time' => self::calculateTimestamp($now, $day, $row['closingtime'])]; } } @@ -75,9 +79,9 @@ class Scheduler $prev = PHP_INT_MAX; for ($i = count($events) - 1; $i >= 0; --$i) { $event =& $events[$i]; - if ($event['action'] === 'wol') { + if ($event['action'] === self::WOL) { $event['time'] -= $wolOffset; - } elseif ($event['action'] === 'sd') { + } elseif ($event['action'] === self::SHUTDOWN) { $event['time'] += $sdOffset; } else { error_log('BUG Unhandled event type ' . $event['action']); @@ -102,9 +106,9 @@ class Scheduler // If difference to next event is < 5 min, ignore. continue; } - if ($diff < 900 && $event['action'] === 'sd' && $events[$i + 1]['action'] === 'wol') { + if ($diff < 900 && $event['action'] === self::SHUTDOWN && $events[$i + 1]['action'] === self::WOL) { // If difference to next WOL is < 15 min and this is a shutdown, reboot instead. - $res['action'] = 'rb'; + $res['action'] = self::REBOOT; $res['time'] = $event['time']; } else { // Use first event. diff --git a/modules-available/rebootcontrol/install.inc.php b/modules-available/rebootcontrol/install.inc.php index f400129e..d45a2443 100644 --- a/modules-available/rebootcontrol/install.inc.php +++ b/modules-available/rebootcontrol/install.inc.php @@ -39,7 +39,7 @@ $output[] = tableCreate('reboot_subnet_x_subnet', " $output[] = tableCreate('reboot_scheduler', " `locationid` INT(11) NOT NULL, - `action` ENUM('wol', 'sd', 'rb'), + `action` ENUM('WOL', 'SHUTDOWN', 'REBOOT'), `nextexecution` INT(10) UNSIGNED NOT NULL DEFAULT 0, `options` BLOB, PRIMARY KEY (`locationid`)"); @@ -60,9 +60,16 @@ if (tableColumnKeyType('reboot_scheduler', 'action') === 'PRI') { $res = Database::exec("ALTER TABLE `reboot_scheduler` DROP PRIMARY KEY, ADD PRIMARY KEY (`locationid`)"); $output[] = $res !== false ? UPDATE_DONE : UPDATE_FAILED; } -if (strpos(tableColumnType('reboot_scheduler', 'action'), 'rb') === false) { - $res = Database::exec("ALTER TABLE `reboot_scheduler` MODIFY COLUMN `action` ENUM('wol', 'sd', 'rb')"); - $output[] = $res !== false ? UPDATE_DONE : UPDATE_FAILED; +if (strpos(tableColumnType('reboot_scheduler', 'action'), 'REBOOT') === false) { + // Fiddle with column to rename ENUM values + $res = Database::exec("ALTER TABLE `reboot_scheduler` MODIFY COLUMN `action` ENUM('sd', 'rb', 'WOL', 'SHUTDOWN', 'REBOOT')"); + handleUpdateResult($res); + $res = Database::exec("UPDATE reboot_scheduler SET action = + CASE WHEN action = 'sd' THEN 'SHUTDOWN' WHEN action = 'rb' THEN 'REBOOT' ELSE 'WOL' END"); + handleUpdateResult($res); + $res = Database::exec("ALTER TABLE `reboot_scheduler` MODIFY COLUMN `action` ENUM('WOL', 'SHUTDOWN', 'REBOOT')"); + handleUpdateResult($res); + $output[] = UPDATE_DONE; } -- cgit v1.2.3-55-g7522