refactor: state.php with RebootDetails class for type and version

This commit is contained in:
Zack Spear
2023-11-07 14:49:41 -08:00
parent 05ce165b83
commit 9c33ef8248
7 changed files with 143 additions and 56 deletions

View File

@@ -0,0 +1,97 @@
<?
/**
* RebootDetails class is responsible for detecting the type and version of a system reboot required in the context of an unRAID server.
*
* Usage:
* ```
* $rebootDetails = new RebootDetails();
* $rebootType = $rebootDetails->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';
}
}
}

View File

@@ -1,4 +1,7 @@
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";
// read flashbackup ini file
$flashbackup_ini = '/var/local/emhttp/flashbackup.ini';
$flashbackup_status = (file_exists($flashbackup_ini)) ? @parse_ini_file($flashbackup_ini) : [];
@@ -34,57 +37,10 @@ $registered = !empty($myservers['remote']['apikey']) && $connectPluginInstalled;
/**
* Reboot detection
*/
$rebootReadme = @file_get_contents("$docroot/plugins/unRAIDServer/README.md",false,null,0,20)?:''; // read first 20 bytes of README.md
$rebootDetected = preg_match("/^\*\*(REBOOT REQUIRED|DOWNGRADE)/", $rebootReadme);
$rebootForDowngrade = $rebootDetected && strpos($rebootReadme, 'DOWNGRADE') !== false;
$rebootForUpdate = $rebootDetected && strpos($rebootReadme, 'REBOOT REQUIRED') !== false;
$rebootType = $rebootForDowngrade ? 'downgrade' : ($rebootForUpdate ? 'update' : '');
$rebootVersion = '';
/**
* 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) {
$rebootType = 'thirdPartyDriversDownloading';
}
function rebootExtractVersion() {
$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';
}
}
if ($rebootType === 'downgrade' || $rebootType === 'update') {
$rebootVersion = rebootExtractVersion();
}
// Create an instance of the RebootDetails class
$rebootDetails = new RebootDetails();
// Access the detected reboot type
$rebootType = $rebootDetails->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'] ?? '',

View File

@@ -4,6 +4,11 @@ Icon="icon-update"
Tag="upload"
---
<?php
require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";
// Create an instance of the RebootDetails class
$rebootDetails = new RebootDetails();
// Access the detected reboot type
$rebootVersion = $rebootDetails->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() {
<unraid-i18n-host>
<unraid-downgrade-os
reboot-version="<?= $rebootVersion ?>"
restore-version="<?= $restoreExists && $restoreVersion != 'unknown' ? $restoreVersion : '' ?>"
restore-release-date="<?= $restoreExists && $restoreVersionReleaseDate != 'unknown' ? $restoreVersionReleaseDate : '' ?>"></unraid-downgrade-os>
</unraid-i18n-host>

View File

@@ -3,6 +3,13 @@ Title="Update OS"
Icon="icon-update"
Tag="upload"
---
<?php
require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";
// Create an instance of the RebootDetails class
$rebootDetails = new RebootDetails();
// Access the detected reboot type
$rebootVersion = $rebootDetails->getRebootVersion();
?>
<script>
function cleanUpFlashBackup(zip) {
if (document.hasFocus()) {
@@ -33,5 +40,5 @@ function flashBackup() {
</script>
<unraid-i18n-host>
<unraid-update-os></unraid-update-os>
<unraid-update-os reboot-version="<?= $rebootVersion ?>"></unraid-update-os>
</unraid-i18n-host>