From d6b32630854413544d455f0ddf3f9a5d04dbf3ee Mon Sep 17 00:00:00 2001 From: selankon Date: Sun, 12 May 2024 11:36:38 +0200 Subject: [PATCH] chore(meshwideupgrade): implement remote abort --- .../src/hooks/meshWideUpgradeProvider.tsx | 5 ++- .../src/meshUpgradeApi.tsx | 7 ++++ .../src/meshUpgradeQueries.tsx | 40 ++++++++++++++----- .../src/meshUpgradeQueriesKeys.tsx | 2 + .../src/utils/api.ts | 13 +++--- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/plugins/lime-plugin-mesh-wide-upgrade/src/hooks/meshWideUpgradeProvider.tsx b/plugins/lime-plugin-mesh-wide-upgrade/src/hooks/meshWideUpgradeProvider.tsx index f61a5e834..9692e98f2 100644 --- a/plugins/lime-plugin-mesh-wide-upgrade/src/hooks/meshWideUpgradeProvider.tsx +++ b/plugins/lime-plugin-mesh-wide-upgrade/src/hooks/meshWideUpgradeProvider.tsx @@ -6,10 +6,10 @@ import { useCallback, useContext } from "react"; import { useNewVersion } from "plugins/lime-plugin-firmware/src/firmwareQueries"; import { getStepperStatus } from "plugins/lime-plugin-mesh-wide-upgrade/src/hooks/useStepper"; import { - useAbort, useBecomeMainNode, useMeshUpgradeNodeStatus, useMeshWideUpgradeInfo, + useParallelAbort, useParallelConfirmUpgrade, useParallelScheduleUpgrade, useStartFirmwareUpgradeTransaction, @@ -97,7 +97,8 @@ export const MeshWideUpgradeProvider = ({ // Inner state to control is aborting callback awaiting until query invalidation const [isAborting, setIsAborting] = useState(false); - const { mutateAsync: abortMutation } = useAbort({}); + // const { mutateAsync: abortMutation } = useAbort({}); + const { callMutations: abortMutation } = useParallelAbort(); const { data: session } = useSession(); const { data: newVersionData } = useNewVersion({ diff --git a/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeApi.tsx b/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeApi.tsx index 4704d3410..ca55bd97f 100644 --- a/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeApi.tsx +++ b/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeApi.tsx @@ -59,6 +59,13 @@ export async function remoteConfirmUpgrade({ ip }: { ip: string }) { }); } +export async function remoteAbort({ ip }: { ip: string }) { + return await callToRemoteNode({ + ip, + apiCall: (customApi) => meshUpgradeApiCall("abort", customApi), + }); +} + const meshUpgradeApiCall = async ( method: string, customApi?: UhttpdService diff --git a/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueries.tsx b/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueries.tsx index 1a0894e6d..d31e86aba 100644 --- a/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueries.tsx +++ b/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueries.tsx @@ -3,9 +3,9 @@ import { useMutation, useQuery } from "@tanstack/react-query"; import { getMeshUpgradeNodeStatus, getMeshWideUpgradeInfo, + remoteAbort, remoteConfirmUpgrade, remoteScheduleUpgrade, - setAbort, setBecomeMainNode, setStartFirmwareUpgradeTransaction, } from "plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeApi"; @@ -14,7 +14,7 @@ import { MeshWideUpgradeInfo, NodeMeshUpgradeInfo, } from "plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeTypes"; -import { getNodeIpsByStatus } from "plugins/lime-plugin-mesh-wide-upgrade/src/utils/api"; +import { getNodeIpsByCondition } from "plugins/lime-plugin-mesh-wide-upgrade/src/utils/api"; import { useMeshWideSyncCall } from "utils/meshWideSyncCall"; @@ -56,13 +56,6 @@ export function useStartFirmwareUpgradeTransaction(params) { }); } -export function useAbort(params) { - return useMutation({ - mutationFn: setAbort, - ...params, - }); -} - // Parallel queries/mutations export type UseScheduleMeshSafeUpgradeType = ReturnType< @@ -71,7 +64,10 @@ export type UseScheduleMeshSafeUpgradeType = ReturnType< export const useParallelScheduleUpgrade = (opts?) => { // State to store the errors const { data: nodes } = useMeshWideUpgradeInfo({}); - const ips = getNodeIpsByStatus(nodes, "READY_FOR_UPGRADE"); + const ips = getNodeIpsByCondition( + nodes, + (node) => node.upgrade_state === "READY_FOR_UPGRADE" + ); // localStorage.setItem("hideReleaseBannerPlease", value); return useMeshWideSyncCall({ mutationKey: meshUpgradeQueryKeys.remoteScheduleUpgrade(), @@ -87,7 +83,10 @@ export type UseConfirmUpgradeType = ReturnType< export const useParallelConfirmUpgrade = (opts?) => { // State to store the errors const { data: nodes } = useMeshWideUpgradeInfo({}); - const ips = getNodeIpsByStatus(nodes, "CONFIRMATION_PENDING"); + const ips = getNodeIpsByCondition( + nodes, + (node) => node.upgrade_state === "CONFIRMATION_PENDING" + ); return useMeshWideSyncCall({ mutationKey: meshUpgradeQueryKeys.remoteConfirmUpgrade(), mutationFn: remoteConfirmUpgrade, @@ -95,3 +94,22 @@ export const useParallelConfirmUpgrade = (opts?) => { options: opts, }); }; + +export const useParallelAbort = (opts?) => { + // State to store the errors + const { data: nodes } = useMeshWideUpgradeInfo({}); + const ips = getNodeIpsByCondition(nodes, (node) => + [ + "READY_FOR_UPGRADE", + "UPGRADE_SCHEDULED", + "CONFIRMATION_PENDING", + "ERROR", + ].includes(node.upgrade_state) + ); + return useMeshWideSyncCall({ + mutationKey: meshUpgradeQueryKeys.remoteAbort(), + mutationFn: remoteAbort, + ips, + options: opts, + }); +}; diff --git a/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueriesKeys.tsx b/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueriesKeys.tsx index dc947d179..fc54018d5 100644 --- a/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueriesKeys.tsx +++ b/plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueriesKeys.tsx @@ -9,6 +9,7 @@ const MeshUpgradeQueryKeys: MeshUpgradeQueryKeysProps = { getMeshUpgradeNodeStatus: ["lime-mesh-upgrade", "get_node_status"], remoteScheduleUpgrade: ["lime-mesh-upgrade", "schedule_upgrade"], remoteConfirmUpgrade: ["lime-mesh-upgrade", "confirm_boot_partition"], + remoteAbort: ["lime-mesh-upgrade", "abort"], }; export const meshUpgradeQueryKeys = { @@ -20,4 +21,5 @@ export const meshUpgradeQueryKeys = { MeshUpgradeQueryKeys.remoteScheduleUpgrade, remoteConfirmUpgrade: (): QueryKey => MeshUpgradeQueryKeys.remoteConfirmUpgrade, + remoteAbort: (): QueryKey => MeshUpgradeQueryKeys.remoteConfirmUpgrade, }; diff --git a/plugins/lime-plugin-mesh-wide-upgrade/src/utils/api.ts b/plugins/lime-plugin-mesh-wide-upgrade/src/utils/api.ts index 64c6e5ab3..76c7d267a 100644 --- a/plugins/lime-plugin-mesh-wide-upgrade/src/utils/api.ts +++ b/plugins/lime-plugin-mesh-wide-upgrade/src/utils/api.ts @@ -1,7 +1,7 @@ import { MeshUpgradeApiErrorTypes, MeshWideUpgradeInfo, - UpgradeStatusType, + NodeMeshUpgradeInfo, } from "plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeTypes"; import { RemoteNodeCallError } from "utils/meshWideSyncCall"; @@ -21,13 +21,14 @@ export class MeshUpgradeApiError extends Error { } /** - * From a MeshWideUpgradeInfo nodes it returns the ips of the nodes that are in certain status provided + * From a MeshWideUpgradeInfo nodes it returns the ips of the nodes that match the condition defined on the function * @param nodes the nodes to check - * @param status the status to check the criteria + * @param condition function that receives a NodeMeshUpgradeInfo and returns a boolean */ -export const getNodeIpsByStatus = ( +export const getNodeIpsByCondition = ( nodes: MeshWideUpgradeInfo, - status: UpgradeStatusType + // status: UpgradeStatusType + condition: (node: NodeMeshUpgradeInfo) => boolean ) => { if (!nodes) return []; return Object.values(nodes) @@ -36,7 +37,7 @@ export const getNodeIpsByStatus = ( node.node_ip !== null && node.node_ip !== undefined && node.node_ip.trim() !== "" && - node.upgrade_state === status + condition(node) ) .map((node) => node.node_ip as string); // 'as string' is safe here due to the filter condition };