mirror of
https://github.com/unraid/api.git
synced 2026-03-13 13:38:51 -05:00
refactor: state.php with RebootDetails class for type and version
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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'] ?? '',
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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<Props>(), {
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
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);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -19,6 +19,7 @@ import { storeToRefs } from 'pinia';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
import { WEBGUI_TOOLS_UPDATE } from '~/helpers/urls';
|
||||
import { useServerStore } from '~/store/server';
|
||||
import { useUpdateOsActionsStore } from '~/store/updateOsActions';
|
||||
|
||||
import 'tailwindcss/tailwind.css';
|
||||
@@ -26,6 +27,14 @@ import '~/assets/main.css';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
export interface Props {
|
||||
rebootVersion?: string;
|
||||
}
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
rebootVersion: '',
|
||||
});
|
||||
|
||||
const serverStore = useServerStore();
|
||||
const updateOsActionsStore = useUpdateOsActionsStore();
|
||||
const { rebootType } = storeToRefs(updateOsActionsStore);
|
||||
|
||||
@@ -43,6 +52,7 @@ onBeforeMount(() => {
|
||||
if (showLoader.value) {
|
||||
updateOsActionsStore.executeUpdateOsCallback(true);
|
||||
}
|
||||
serverStore.setRebootVersion(props.rebootVersion);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -901,6 +901,10 @@ export const useServerStore = defineStore('server', () => {
|
||||
});
|
||||
};
|
||||
|
||||
const setRebootVersion = (version: string) => {
|
||||
rebootVersion.value = version;
|
||||
};
|
||||
|
||||
watchEffect(() => {
|
||||
if (rebootVersion.value) {
|
||||
console.debug('[server.rebootVersion]', rebootVersion.value);
|
||||
@@ -968,5 +972,6 @@ export const useServerStore = defineStore('server', () => {
|
||||
fetchServerFromApi,
|
||||
refreshServerState,
|
||||
filteredKeyActions,
|
||||
setRebootVersion,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user