From 9c33ef8248e90df3e13a7ce6dd6ab9e17e2e421c Mon Sep 17 00:00:00 2001 From: Zack Spear Date: Tue, 7 Nov 2023 14:49:41 -0800 Subject: [PATCH] refactor: state.php with RebootDetails class for type and version --- .../include/reboot-details.php | 97 +++++++++++++++++++ .../dynamix.my.servers/include/state.php | 59 ++--------- .../dynamix.plugin.manager/Downgrade.page | 8 +- .../dynamix.plugin.manager/Update.page | 9 +- web/components/DowngradeOs.ce.vue | 11 ++- web/components/UpdateOs.ce.vue | 10 ++ web/store/server.ts | 5 + 7 files changed, 143 insertions(+), 56 deletions(-) create mode 100644 plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/reboot-details.php diff --git a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/reboot-details.php b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/reboot-details.php new file mode 100644 index 000000000..29c91e48f --- /dev/null +++ b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/reboot-details.php @@ -0,0 +1,97 @@ +getRebootType(); + * ``` + */ +class RebootDetails +{ + /** + * @var string $rebootType Stores the type of reboot required, which can be 'update', 'downgrade', or 'thirdPartyDriversDownloading'. + */ + private $rebootType = ''; + + /** + * Constructs a new RebootDetails object and automatically detects the reboot type during initialization. + */ + public function __construct() + { + $this->detectRebootType(); + } + + /** + * Detects the type of reboot required based on the contents of the unRAID server's README.md file. + * Sets the $rebootType property accordingly. + */ + private function detectRebootType() + { + $docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp'; + + $rebootReadme = @file_get_contents("$docroot/plugins/unRAIDServer/README.md", false, null, 0, 20) ?: ''; + $rebootDetected = preg_match("/^\*\*(REBOOT REQUIRED|DOWNGRADE)/", $rebootReadme); + + $rebootForDowngrade = $rebootDetected && strpos($rebootReadme, 'DOWNGRADE') !== false; + $rebootForUpdate = $rebootDetected && strpos($rebootReadme, 'REBOOT REQUIRED') !== false; + + $this->rebootType = $rebootForDowngrade ? 'downgrade' : ($rebootForUpdate ? 'update' : ''); + + // Detect if third-party drivers were part of the update process + $processWaitingThirdParthDrivers = "inotifywait -q /boot/changes.txt -e move_self,delete_self"; + // Run the ps command to list processes and check if the process is running + $ps_command = "ps aux | grep -E \"$processWaitingThirdParthDrivers\" | grep -v \"grep -E\""; + $output = shell_exec($ps_command) ?? ''; + if (strpos($output, $processWaitingThirdParthDrivers) !== false) { + $this->rebootType = 'thirdPartyDriversDownloading'; + } + } + + /** + * Gets the type of reboot required, which can be 'update', 'downgrade', or 'thirdPartyDriversDownloading'. + * + * @return string The type of reboot required. + */ + public function getRebootType() + { + return $this->rebootType; + } + + /** + * Detects and retrieves the version information related to the system reboot based on the contents of the '/boot/changes.txt' file. + * + * @return string The system version information or 'Not found' if not found, or 'File not found' if the file is not present. + */ + public function getRebootVersion() + { + $file_path = '/boot/changes.txt'; + + // Check if the file exists + if (file_exists($file_path)) { + // Open the file for reading + $file = fopen($file_path, 'r'); + + // Read the file line by line until we find a line that starts with '# Version' + while (($line = fgets($file)) !== false) { + if (strpos($line, '# Version') === 0) { + // Use a regular expression to extract the full version string + if (preg_match('/# Version\s+(\S+)/', $line, $matches)) { + $fullVersion = $matches[1]; + return $fullVersion; + } else { + return 'Not found'; + } + break; + } + } + + // Close the file + fclose($file); + } else { + return 'File not found'; + } + } +} diff --git a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/state.php b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/state.php index d8e834ab8..c83821088 100644 --- a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/state.php +++ b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/state.php @@ -1,4 +1,7 @@ getRebootType(); $serverState = [ "apiKey" => $myservers['upc']['apikey'] ?? '', @@ -122,7 +78,6 @@ $serverState = [ "osVersionBranch" => $osVersionBranch, "protocol" => $_SERVER['REQUEST_SCHEME'], "rebootType" => $rebootType, - "rebootVersion" => $rebootVersion, "regDev" => @(int)$var['regDev'] ?? 0, "regGen" => @(int)$var['regGen'], "regGuid" => @$var['regGUID'] ?? '', diff --git a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Downgrade.page b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Downgrade.page index ff77bf336..f61c473c4 100644 --- a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Downgrade.page +++ b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Downgrade.page @@ -4,6 +4,11 @@ Icon="icon-update" Tag="upload" --- getRebootVersion(); /** * @note icon-update is rotated via CSS in myservers1.php * @@ -11,8 +16,6 @@ Tag="upload" * Parse the text file /boot/previous/changes.txt to get the version number of the previous version. * Then we move some files around and reboot. */ -$check = $notify['unraidos'] ? 0 : 1; - $restoreVersion = $restoreBranch = $restoreVersionReleaseDate = 'unknown'; $restoreExists = file_exists('/boot/previous/bzroot'); $restoreChangelogPath = '/boot/previous/changes.txt'; @@ -158,6 +161,7 @@ function confirmDowngrade() { \ No newline at end of file diff --git a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Update.page b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Update.page index f2d471dcf..2bcd2ec9b 100644 --- a/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Update.page +++ b/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.plugin.manager/Update.page @@ -3,6 +3,13 @@ Title="Update OS" Icon="icon-update" Tag="upload" --- +getRebootVersion(); +?> - + \ No newline at end of file diff --git a/web/components/DowngradeOs.ce.vue b/web/components/DowngradeOs.ce.vue index b14907c28..cc07c1a2d 100644 --- a/web/components/DowngradeOs.ce.vue +++ b/web/components/DowngradeOs.ce.vue @@ -16,8 +16,10 @@ else fi */ import { storeToRefs } from 'pinia'; +import { onBeforeMount } from 'vue'; import { useI18n } from 'vue-i18n'; +import { useServerStore } from '~/store/server'; import { useUpdateOsActionsStore } from '~/store/updateOsActions'; import 'tailwindcss/tailwind.css'; @@ -26,14 +28,17 @@ import '~/assets/main.css'; const { t } = useI18n(); export interface Props { + rebootVersion?: string; restoreReleaseDate?: string; restoreVersion?: string; } -withDefaults(defineProps(), { +const props = withDefaults(defineProps(), { + rebootVersion: '', restoreReleaseDate: '', restoreVersion: '', }); +const serverStore = useServerStore(); const updateOsActionsStore = useUpdateOsActionsStore(); const { rebootType } = storeToRefs(updateOsActionsStore); @@ -44,6 +49,10 @@ const subtitle = computed(() => { } return ''; }); + +onBeforeMount(() => { + serverStore.setRebootVersion(props.rebootVersion); +});