mirror of https://github.com/jetkvm/kvm.git
feat: implement post-reboot action handling for better reboot handling
This commit is contained in:
parent
cb56007eba
commit
6be9a10ddc
28
network.go
28
network.go
|
|
@ -27,6 +27,11 @@ func (s *RpcNetworkSettings) ToNetworkConfig() *types.NetworkConfig {
|
|||
return &s.NetworkConfig
|
||||
}
|
||||
|
||||
type PostRebootAction struct {
|
||||
HealthCheck string `json:"healthCheck"`
|
||||
RedirectUrl string `json:"redirectUrl"`
|
||||
}
|
||||
|
||||
func toRpcNetworkSettings(config *types.NetworkConfig) *RpcNetworkSettings {
|
||||
return &RpcNetworkSettings{
|
||||
NetworkConfig: *config,
|
||||
|
|
@ -171,9 +176,9 @@ func setHostname(nm *nmlite.NetworkManager, hostname, domain string) error {
|
|||
return nm.SetHostname(hostname, domain)
|
||||
}
|
||||
|
||||
func shouldRebootForNetworkChange(oldConfig, newConfig *types.NetworkConfig) (bool, *string) {
|
||||
func shouldRebootForNetworkChange(oldConfig, newConfig *types.NetworkConfig) (bool, *PostRebootAction) {
|
||||
var rebootRequired bool
|
||||
var suggestedIp *string
|
||||
var postRebootAction *PostRebootAction
|
||||
|
||||
oldDhcpClient := oldConfig.DHCPClient.String
|
||||
|
||||
|
|
@ -183,8 +188,8 @@ func shouldRebootForNetworkChange(oldConfig, newConfig *types.NetworkConfig) (bo
|
|||
networkLogger.Info().Str("old", oldDhcpClient).Str("new", newConfig.DHCPClient.String).Msg("DHCP client changed, reboot required")
|
||||
}
|
||||
|
||||
// IPv4 mode change requires reboot when using udhcpc
|
||||
if newConfig.IPv4Mode.String != oldConfig.IPv4Mode.String && oldDhcpClient == "udhcpc" {
|
||||
// IPv4 mode change requires reboot
|
||||
if newConfig.IPv4Mode.String != oldConfig.IPv4Mode.String {
|
||||
rebootRequired = true
|
||||
networkLogger.Info().Str("old", oldConfig.IPv4Mode.String).Str("new", newConfig.IPv4Mode.String).Msg("IPv4 mode changed with udhcpc, reboot required")
|
||||
}
|
||||
|
|
@ -193,8 +198,13 @@ func shouldRebootForNetworkChange(oldConfig, newConfig *types.NetworkConfig) (bo
|
|||
if newConfig.IPv4Static != nil && oldConfig.IPv4Static != nil {
|
||||
if newConfig.IPv4Static.Address.String != oldConfig.IPv4Static.Address.String {
|
||||
rebootRequired = true
|
||||
suggestedIp = &newConfig.IPv4Static.Address.String
|
||||
networkLogger.Info().Str("old", oldConfig.IPv4Static.Address.String).Str("new", newConfig.IPv4Static.Address.String).Msg("IPv4 address changed, reboot required")
|
||||
newIP := newConfig.IPv4Static.Address.String
|
||||
postRebootAction = &PostRebootAction{
|
||||
// The user can be using self-signed certificates, so we use don't specify the protocol
|
||||
HealthCheck: fmt.Sprintf("//%s/device/status", newIP),
|
||||
RedirectUrl: fmt.Sprintf("//%s", newIP),
|
||||
}
|
||||
networkLogger.Info().Str("old", oldConfig.IPv4Static.Address.String).Str("new", newIP).Msg("IPv4 address changed, reboot required")
|
||||
}
|
||||
if newConfig.IPv4Static.Netmask.String != oldConfig.IPv4Static.Netmask.String {
|
||||
rebootRequired = true
|
||||
|
|
@ -216,7 +226,7 @@ func shouldRebootForNetworkChange(oldConfig, newConfig *types.NetworkConfig) (bo
|
|||
networkLogger.Info().Str("old", oldConfig.IPv6Mode.String).Str("new", newConfig.IPv6Mode.String).Msg("IPv6 mode changed with udhcpc, reboot required")
|
||||
}
|
||||
|
||||
return rebootRequired, suggestedIp
|
||||
return rebootRequired, postRebootAction
|
||||
}
|
||||
|
||||
func rpcGetNetworkState() *types.RpcInterfaceState {
|
||||
|
|
@ -239,12 +249,12 @@ func rpcSetNetworkSettings(settings RpcNetworkSettings) (*RpcNetworkSettings, er
|
|||
l.Debug().Msg("setting new config")
|
||||
|
||||
// Check if reboot is needed
|
||||
rebootRequired, suggestedIp := shouldRebootForNetworkChange(config.NetworkConfig, netConfig)
|
||||
rebootRequired, postRebootAction := shouldRebootForNetworkChange(config.NetworkConfig, netConfig)
|
||||
|
||||
// If reboot required, send willReboot event before applying network config
|
||||
if rebootRequired {
|
||||
l.Info().Msg("Sending willReboot event before applying network config")
|
||||
writeJSONRPCEvent("willReboot", suggestedIp, currentSession)
|
||||
writeJSONRPCEvent("willReboot", postRebootAction, currentSession)
|
||||
}
|
||||
|
||||
_ = setHostname(networkManager, netConfig.Hostname.String, netConfig.Domain.String)
|
||||
|
|
|
|||
9
ota.go
9
ota.go
|
|
@ -488,6 +488,15 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
|
|||
|
||||
if rebootNeeded {
|
||||
scopedLogger.Info().Msg("System Rebooting in 10s")
|
||||
|
||||
// TODO: Future enhancement - send postRebootAction to redirect to release notes
|
||||
// Example:
|
||||
// postRebootAction := &PostRebootAction{
|
||||
// HealthCheck: "[..]/device/status",
|
||||
// RedirectUrl: "[..]/settings/general/update?version=X.Y.Z",
|
||||
// }
|
||||
// writeJSONRPCEvent("willReboot", postRebootAction, currentSession)
|
||||
|
||||
time.Sleep(10 * time.Second)
|
||||
cmd := exec.Command("reboot")
|
||||
err := cmd.Start()
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import { BsMouseFill } from "react-icons/bs";
|
|||
import { Button, LinkButton } from "@components/Button";
|
||||
import LoadingSpinner from "@components/LoadingSpinner";
|
||||
import Card, { GridCard } from "@components/Card";
|
||||
import { useRTCStore } from "@/hooks/stores";
|
||||
import { useRTCStore, PostRebootAction } from "@/hooks/stores";
|
||||
import LogoBlue from "@/assets/logo-blue.svg";
|
||||
import LogoWhite from "@/assets/logo-white.svg";
|
||||
import { isOnDevice } from "@/main";
|
||||
|
|
@ -400,10 +400,10 @@ export function PointerLockBar({ show }: PointerLockBarProps) {
|
|||
|
||||
interface RebootingOverlayProps {
|
||||
readonly show: boolean;
|
||||
readonly suggestedIp: string | null;
|
||||
readonly postRebootAction: PostRebootAction;
|
||||
}
|
||||
|
||||
export function RebootingOverlay({ show, suggestedIp }: RebootingOverlayProps) {
|
||||
export function RebootingOverlay({ show, postRebootAction }: RebootingOverlayProps) {
|
||||
const { peerConnectionState } = useRTCStore();
|
||||
|
||||
// Check if we've already seen the connection drop (confirms reboot actually started)
|
||||
|
|
@ -447,12 +447,12 @@ export function RebootingOverlay({ show, suggestedIp }: RebootingOverlayProps) {
|
|||
const isFetchingRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
// Only run in device mode with a suggested IP
|
||||
if (!isOnDevice || !suggestedIp || !show || !hasSeenDisconnect) {
|
||||
// Only run in device mode with a postRebootAction
|
||||
if (!isOnDevice || !postRebootAction || !show || !hasSeenDisconnect) {
|
||||
return;
|
||||
}
|
||||
|
||||
const checkSuggestedIp = async () => {
|
||||
const checkPostRebootHealth = async () => {
|
||||
// Don't start a new fetch if one is already in progress
|
||||
if (isFetchingRef.current) {
|
||||
return;
|
||||
|
|
@ -468,26 +468,24 @@ export function RebootingOverlay({ show, suggestedIp }: RebootingOverlayProps) {
|
|||
abortControllerRef.current = abortController;
|
||||
isFetchingRef.current = true;
|
||||
|
||||
console.log('Checking suggested IP:', suggestedIp);
|
||||
console.log('Checking post-reboot health endpoint:', postRebootAction.healthCheck);
|
||||
const timeoutId = window.setTimeout(() => abortController.abort(), 2000);
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${window.location.protocol}//${suggestedIp}/device/status`,
|
||||
{
|
||||
signal: abortController.signal,
|
||||
}
|
||||
postRebootAction.healthCheck,
|
||||
{ signal: abortController.signal, }
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
// Device is available at the new IP, redirect to it
|
||||
console.log('Device is available at the new IP, redirecting to it');
|
||||
window.location.href = `${window.location.protocol}//${suggestedIp}`;
|
||||
// Device is available, redirect to the specified URL
|
||||
console.log('Device is available, redirecting to:', postRebootAction.redirectUrl);
|
||||
window.location.href = postRebootAction.redirectUrl;
|
||||
}
|
||||
} catch (err) {
|
||||
// Ignore errors - they're expected while device is rebooting
|
||||
// Only log if it's not an abort error
|
||||
if (err instanceof Error && err.name !== 'AbortError') {
|
||||
console.debug('Error checking suggested IP:', err);
|
||||
console.debug('Error checking post-reboot health:', err);
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
|
|
@ -496,10 +494,10 @@ export function RebootingOverlay({ show, suggestedIp }: RebootingOverlayProps) {
|
|||
};
|
||||
|
||||
// Start interval (check every 2 seconds)
|
||||
const intervalId = setInterval(checkSuggestedIp, 2000);
|
||||
const intervalId = setInterval(checkPostRebootHealth, 2000);
|
||||
|
||||
// Also check immediately
|
||||
checkSuggestedIp();
|
||||
checkPostRebootHealth();
|
||||
|
||||
// Cleanup on unmount or when dependencies change
|
||||
return () => {
|
||||
|
|
@ -509,7 +507,7 @@ export function RebootingOverlay({ show, suggestedIp }: RebootingOverlayProps) {
|
|||
}
|
||||
isFetchingRef.current = false;
|
||||
};
|
||||
}, [show, suggestedIp, hasTimedOut, hasSeenDisconnect]);
|
||||
}, [show, postRebootAction, hasTimedOut, hasSeenDisconnect]);
|
||||
|
||||
return (
|
||||
<AnimatePresence>
|
||||
|
|
@ -543,18 +541,7 @@ export function RebootingOverlay({ show, suggestedIp }: RebootingOverlayProps) {
|
|||
) : (
|
||||
<>
|
||||
Please wait while the device restarts. This usually takes 20-30 seconds.
|
||||
{suggestedIp && (
|
||||
<>
|
||||
{" "}If reconnection fails, the device may be at{" "}
|
||||
<a
|
||||
href={`${window.location.protocol}//${suggestedIp}`}
|
||||
className="font-medium text-blue-600 hover:underline dark:text-blue-400"
|
||||
>
|
||||
{suggestedIp}
|
||||
</a>
|
||||
.
|
||||
</>
|
||||
)}
|
||||
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
|
|
|
|||
|
|
@ -19,6 +19,11 @@ interface JsonRpcResponse {
|
|||
id: number | string | null;
|
||||
}
|
||||
|
||||
export type PostRebootAction = {
|
||||
healthCheck: string;
|
||||
redirectUrl: string;
|
||||
} | null;
|
||||
|
||||
// Utility function to append stats to a Map
|
||||
const appendStatToMap = <T extends { timestamp: number }>(
|
||||
stat: T,
|
||||
|
|
@ -70,9 +75,9 @@ export interface UIState {
|
|||
terminalType: AvailableTerminalTypes;
|
||||
setTerminalType: (type: UIState["terminalType"]) => void;
|
||||
|
||||
rebootState: { isRebooting: boolean; suggestedIp: string | null } | null;
|
||||
rebootState: { isRebooting: boolean; postRebootAction: PostRebootAction } | null;
|
||||
setRebootState: (
|
||||
state: { isRebooting: boolean; suggestedIp: string | null } | null,
|
||||
state: { isRebooting: boolean; postRebootAction: PostRebootAction } | null,
|
||||
) => void;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import {
|
|||
KeysDownState,
|
||||
NetworkState,
|
||||
OtaState,
|
||||
PostRebootAction,
|
||||
USBStates,
|
||||
useHidStore,
|
||||
useNetworkStateStore,
|
||||
|
|
@ -274,7 +275,7 @@ export default function KvmIdRoute() {
|
|||
// 5. WS tries to reconnect
|
||||
// 6. WS reconnects
|
||||
// 7. This function is called and now we clear the reboot state
|
||||
setRebootState({ isRebooting: false, suggestedIp: null });
|
||||
setRebootState({ isRebooting: false, postRebootAction: null });
|
||||
},
|
||||
|
||||
onMessage: message => {
|
||||
|
|
@ -677,8 +678,8 @@ export default function KvmIdRoute() {
|
|||
}
|
||||
|
||||
if (resp.method === "willReboot") {
|
||||
const suggestedIp = resp.params as unknown as string | null;
|
||||
setRebootState({ isRebooting: true, suggestedIp });
|
||||
const postRebootAction = resp.params as unknown as PostRebootAction;
|
||||
setRebootState({ isRebooting: true, postRebootAction });
|
||||
navigateTo("/");
|
||||
}
|
||||
}
|
||||
|
|
@ -785,7 +786,7 @@ export default function KvmIdRoute() {
|
|||
|
||||
// Rebooting takes priority over connection status
|
||||
if (rebootState?.isRebooting) {
|
||||
return <RebootingOverlay show={true} suggestedIp={rebootState.suggestedIp} />;
|
||||
return <RebootingOverlay show={true} postRebootAction={rebootState.postRebootAction} />;
|
||||
}
|
||||
|
||||
const hasConnectionFailed =
|
||||
|
|
@ -812,7 +813,7 @@ export default function KvmIdRoute() {
|
|||
}
|
||||
|
||||
return null;
|
||||
}, [location.pathname, rebootState?.isRebooting, rebootState?.suggestedIp, connectionFailed, peerConnectionState, peerConnection, setupPeerConnection, loadingMessage]);
|
||||
}, [location.pathname, rebootState?.isRebooting, rebootState?.postRebootAction, connectionFailed, peerConnectionState, peerConnection, setupPeerConnection, loadingMessage]);
|
||||
|
||||
return (
|
||||
<FeatureFlagProvider appVersion={appVersion}>
|
||||
|
|
|
|||
Loading…
Reference in New Issue