Skip to content

Commit

Permalink
chore(meshwideupgrade): implement remote abort
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed May 12, 2024
1 parent 5bfa044 commit d6b3263
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down
7 changes: 7 additions & 0 deletions plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 29 additions & 11 deletions plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeQueries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";

Expand Down Expand Up @@ -56,13 +56,6 @@ export function useStartFirmwareUpgradeTransaction(params) {
});
}

export function useAbort(params) {
return useMutation({
mutationFn: setAbort,
...params,
});
}

// Parallel queries/mutations

export type UseScheduleMeshSafeUpgradeType = ReturnType<
Expand All @@ -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(),
Expand All @@ -87,11 +83,33 @@ 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,
ips,
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,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -20,4 +21,5 @@ export const meshUpgradeQueryKeys = {
MeshUpgradeQueryKeys.remoteScheduleUpgrade,
remoteConfirmUpgrade: (): QueryKey =>
MeshUpgradeQueryKeys.remoteConfirmUpgrade,
remoteAbort: (): QueryKey => MeshUpgradeQueryKeys.remoteConfirmUpgrade,
};
13 changes: 7 additions & 6 deletions plugins/lime-plugin-mesh-wide-upgrade/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
MeshUpgradeApiErrorTypes,
MeshWideUpgradeInfo,
UpgradeStatusType,
NodeMeshUpgradeInfo,
} from "plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeTypes";

import { RemoteNodeCallError } from "utils/meshWideSyncCall";
Expand All @@ -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)
Expand All @@ -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
};
Expand Down

0 comments on commit d6b3263

Please sign in to comment.