diff options
Diffstat (limited to 'inc/trigger.inc.php')
-rw-r--r-- | inc/trigger.inc.php | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php index 134bab53..d0d5d365 100644 --- a/inc/trigger.inc.php +++ b/inc/trigger.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * This is one giant class containing various functions that will generate * required config files, daemon instances and more, mostly through the Taskmanager. @@ -14,20 +16,21 @@ class Trigger /** * Compile iPXE pxelinux menu. Needs to be done whenever the server's IP * address changes. - * - * @param boolean $force force recompilation even if it seems up to date - * @return boolean|string false if launching task failed, task-id otherwise + * + * @return ?string null if launching task failed, task-id otherwise */ - public static function ipxe() + public static function ipxe(string $taskId = null): ?string { + static $lastResult = null; + if ($lastResult !== null) + return $lastResult; $hooks = Hook::load('ipxe-update'); - static $taskId = false; foreach ($hooks as $hook) { $ret = function($taskId) use ($hook) { $ret = include_once($hook->file); if (is_string($ret)) return $ret; - return isset($taskId) ? $taskId : false; + return $taskId; }; $ret = $ret($taskId); if (is_string($ret)) { @@ -36,7 +39,7 @@ class Trigger $taskId = $ret['id']; } } - Property::set('ipxe-task-id', $taskId, 15); + $lastResult = $taskId; return $taskId; } @@ -48,7 +51,7 @@ class Trigger * @return boolean true if current configured IP address is still valid, or if a new address could * successfully be determined, false otherwise */ - public static function autoUpdateServerIp() + public static function autoUpdateServerIp(): bool { for ($i = 0; $i < 5; ++$i) { $task = Taskmanager::submit('LocalAddressesList'); @@ -59,7 +62,7 @@ class Trigger if ($task === false) return false; $task = Taskmanager::waitComplete($task, 10000); - if (!isset($task['data']['addresses']) || empty($task['data']['addresses'])) + if (empty($task['data']['addresses'])) return false; $serverIp = Property::getServerIp(); @@ -96,22 +99,18 @@ class Trigger /** * Mount the VM store into the server. * - * @param array $vmstore VM Store configuration to use. If false, read from properties + * @param array|false $vmstore VM Store configuration to use. If false, read from properties * @param bool $ifLocalOnly Only execute task if the storage type is local (used for DNBD3) - * @return array|false task status of mount procedure, or false on error + * @return ?string task id of mount procedure, or false on error */ - public static function mount($vmstore = false, $ifLocalOnly = false) + public static function mount($vmstore = false, bool $ifLocalOnly = false): ?string { if ($vmstore === false) { $vmstore = Property::getVmStoreConfig(); } if (!is_array($vmstore)) - return false; - if (isset($vmstore['storetype'])) { - $storetype = $vmstore['storetype']; - } else { - $storetype = 'unknown'; - } + return null; + $storetype = $vmstore['storetype'] ?? 'unknown'; if ($storetype === 'nfs') { $addr = $vmstore['nfsaddr']; $opts = 'nfsopts'; @@ -124,13 +123,9 @@ class Trigger } // Bail out if storage is not local, and we only want to run it in that case if ($ifLocalOnly && $addr !== 'null') - return false; - if (isset($vmstore[$opts])) { - $opts = $vmstore[$opts]; - }else { - $opts = null; - } - return Taskmanager::submit('MountVmStore', array( + return null; + $opts = $vmstore[$opts] ?? null; + $status = Taskmanager::submit('MountVmStore', array( 'address' => $addr, 'type' => 'images', 'opts' => $opts, @@ -138,6 +133,12 @@ class Trigger 'username' => $vmstore['cifsuser'], 'password' => $vmstore['cifspasswd'] )); + if (!Taskmanager::isFailed($status)) { + // In case we have a concurrent active task, this should be enough + // for the taskmanager to give us the existing id + $status = Taskmanager::waitComplete($status, 100); + } + return $status['data']['existingTask'] ?? $status['id'] ?? null; } /** @@ -145,7 +146,7 @@ class Trigger * * @return boolean Whether there are still callbacks pending */ - public static function checkCallbacks() + public static function checkCallbacks(): bool { $tasksLeft = false; $callbackList = TaskmanagerCallback::getPendingCallbacks(); @@ -156,15 +157,16 @@ class Trigger foreach ($callbacks as $callback) { TaskmanagerCallback::handleCallback($callback, $status); } - if (Taskmanager::isFailed($status) || Taskmanager::isFinished($status)) + if (Taskmanager::isFailed($status) || Taskmanager::isFinished($status)) { Taskmanager::release($status); - else + } else { $tasksLeft = true; + } } return $tasksLeft; } - private static function triggerDaemons($action, $parent, &$taskids) + private static function triggerDaemons(string $action, ?string $parent, array &$taskids): ?string { $task = Taskmanager::submit('Systemctl', array( 'operation' => $action, @@ -192,7 +194,7 @@ class Trigger /** * Stop any daemons that might be sitting on the VMstore, or database. */ - public static function stopDaemons($parent, &$taskids) + public static function stopDaemons(?string $parent, array &$taskids): ?string { $parent = self::triggerDaemons('stop', $parent, $taskids); $task = Taskmanager::submit('LdadpLauncher', array( |