2025-04-28 23:32:41 +00:00
|
|
|
import React, { useCallback, useContext, useState } from "react";
|
|
|
|
|
import { useQuery } from "react-query";
|
|
|
|
|
|
|
|
|
|
import classnames from "classnames";
|
|
|
|
|
|
|
|
|
|
import { NotificationContext } from "context/notification";
|
|
|
|
|
|
|
|
|
|
import { IScript } from "interfaces/script";
|
2025-05-22 23:45:43 +00:00
|
|
|
import { getErrorReason } from "interfaces/errors";
|
2025-04-28 23:32:41 +00:00
|
|
|
|
|
|
|
|
import { DEFAULT_USE_QUERY_OPTIONS } from "utilities/constants";
|
|
|
|
|
|
|
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
|
|
|
|
|
import scriptsAPI, {
|
|
|
|
|
IListScriptsQueryKey,
|
2025-05-22 23:45:43 +00:00
|
|
|
IScriptBatchSupportedFilters,
|
2025-04-28 23:32:41 +00:00
|
|
|
IScriptsResponse,
|
|
|
|
|
} from "services/entities/scripts";
|
|
|
|
|
import ScriptDetailsModal from "pages/hosts/components/ScriptDetailsModal";
|
|
|
|
|
import Spinner from "components/Spinner";
|
|
|
|
|
import EmptyTable from "components/EmptyTable";
|
|
|
|
|
import Button from "components/buttons/Button";
|
|
|
|
|
|
|
|
|
|
import RunScriptBatchPaginatedList from "../RunScriptBatchPaginatedList";
|
|
|
|
|
import { IPaginatedListScript } from "../RunScriptBatchPaginatedList/RunScriptBatchPaginatedList";
|
|
|
|
|
|
|
|
|
|
const baseClass = "run-script-batch-modal";
|
|
|
|
|
|
|
|
|
|
interface IRunScriptBatchModal {
|
2025-05-22 23:45:43 +00:00
|
|
|
runByFilters: boolean; // otherwise, by selectedHostIds
|
|
|
|
|
// since teamId has multiple uses in this component, it's passed in as its own prop and added to
|
|
|
|
|
// `filters` as needed
|
|
|
|
|
filters: Omit<IScriptBatchSupportedFilters, "team_id">;
|
|
|
|
|
teamId: number;
|
|
|
|
|
totalFilteredHostsCount: number;
|
2025-04-28 23:32:41 +00:00
|
|
|
selectedHostIds: number[];
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const RunScriptBatchModal = ({
|
2025-05-22 23:45:43 +00:00
|
|
|
runByFilters = false,
|
|
|
|
|
filters,
|
|
|
|
|
totalFilteredHostsCount,
|
2025-04-28 23:32:41 +00:00
|
|
|
selectedHostIds,
|
|
|
|
|
teamId,
|
2025-05-22 23:45:43 +00:00
|
|
|
onCancel,
|
2025-04-28 23:32:41 +00:00
|
|
|
}: IRunScriptBatchModal) => {
|
|
|
|
|
const { renderFlash } = useContext(NotificationContext);
|
|
|
|
|
|
|
|
|
|
const [isUpdating, setIsUpdating] = useState(false);
|
|
|
|
|
const [scriptForDetails, setScriptForDetails] = useState<
|
|
|
|
|
IPaginatedListScript | undefined
|
|
|
|
|
>(undefined);
|
|
|
|
|
// just used to get total number of scripts, could be optimized by implementing a dedicated scriptsCount endpoint
|
|
|
|
|
const { data: scripts } = useQuery<
|
|
|
|
|
IScriptsResponse,
|
|
|
|
|
Error,
|
|
|
|
|
IScript[],
|
|
|
|
|
IListScriptsQueryKey[]
|
|
|
|
|
>(
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
scope: "scripts",
|
|
|
|
|
team_id: teamId,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
({ queryKey }) => {
|
|
|
|
|
return scriptsAPI.getScripts(queryKey[0]);
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
...DEFAULT_USE_QUERY_OPTIONS,
|
|
|
|
|
keepPreviousData: true,
|
|
|
|
|
select: (data) => data.scripts || [],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onRunScriptBatch = useCallback(
|
|
|
|
|
async (script: IScript) => {
|
|
|
|
|
setIsUpdating(true);
|
2025-05-22 23:45:43 +00:00
|
|
|
const body = runByFilters
|
|
|
|
|
? // satisfy IScriptBatchSupportedFilters
|
|
|
|
|
{ script_id: script.id, filters: { ...filters, team_id: teamId } }
|
|
|
|
|
: { script_id: script.id, host_ids: selectedHostIds };
|
2025-04-28 23:32:41 +00:00
|
|
|
try {
|
2025-05-22 23:45:43 +00:00
|
|
|
await scriptsAPI.runScriptBatch(body);
|
2025-04-28 23:32:41 +00:00
|
|
|
renderFlash(
|
|
|
|
|
"success",
|
2025-05-22 23:45:43 +00:00
|
|
|
`Script is running on ${
|
|
|
|
|
runByFilters
|
|
|
|
|
? totalFilteredHostsCount.toLocaleString()
|
|
|
|
|
: selectedHostIds.length.toLocaleString()
|
|
|
|
|
} hosts, or will run as each host comes online. See host details for individual results.`
|
2025-04-28 23:32:41 +00:00
|
|
|
);
|
|
|
|
|
} catch (error) {
|
2025-05-22 23:45:43 +00:00
|
|
|
let errorMessage = "Could not run script.";
|
|
|
|
|
if (getErrorReason(error).includes("too many hosts")) {
|
|
|
|
|
errorMessage =
|
|
|
|
|
"Could not run script: too many hosts targeted. Please try again with fewer hosts.";
|
|
|
|
|
}
|
|
|
|
|
renderFlash("error", errorMessage);
|
2025-04-28 23:32:41 +00:00
|
|
|
// can determine more specific error case with additional call to upcoming summary endpoint
|
|
|
|
|
} finally {
|
|
|
|
|
setIsUpdating(false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[renderFlash, selectedHostIds]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const renderModalContent = () => {
|
|
|
|
|
if (scripts === undefined) {
|
|
|
|
|
return <Spinner />;
|
|
|
|
|
}
|
|
|
|
|
if (!scripts.length) {
|
|
|
|
|
return (
|
|
|
|
|
<EmptyTable
|
|
|
|
|
header="No scripts available for this team"
|
|
|
|
|
info={
|
|
|
|
|
<>
|
|
|
|
|
You can add saved scripts{" "}
|
|
|
|
|
<a href={`/controls/scripts?team_id=${teamId}`}>here</a>.
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-05-22 23:45:43 +00:00
|
|
|
const targetCount = runByFilters
|
|
|
|
|
? totalFilteredHostsCount
|
|
|
|
|
: selectedHostIds.length;
|
2025-04-28 23:32:41 +00:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<p>
|
2025-05-14 19:59:58 +00:00
|
|
|
Will run on{" "}
|
|
|
|
|
<b>
|
2025-05-22 23:45:43 +00:00
|
|
|
{targetCount.toLocaleString()} host{targetCount > 1 ? "s" : ""}
|
2025-05-14 19:59:58 +00:00
|
|
|
</b>
|
|
|
|
|
. You can see individual script results on the host details page.
|
2025-04-28 23:32:41 +00:00
|
|
|
</p>
|
|
|
|
|
<RunScriptBatchPaginatedList
|
|
|
|
|
onRunScript={onRunScriptBatch}
|
|
|
|
|
isUpdating={isUpdating}
|
|
|
|
|
teamId={teamId}
|
|
|
|
|
scriptCount={scripts.length}
|
|
|
|
|
setScriptForDetails={setScriptForDetails}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const classes = classnames(baseClass, {
|
|
|
|
|
[`${baseClass}__hide-main`]: !!scriptForDetails,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
title="Run script"
|
|
|
|
|
onExit={onCancel}
|
|
|
|
|
onEnter={onCancel}
|
|
|
|
|
className={classes}
|
2025-05-22 23:45:43 +00:00
|
|
|
disableClosingModal={isUpdating}
|
2025-04-28 23:32:41 +00:00
|
|
|
>
|
|
|
|
|
<>
|
|
|
|
|
{renderModalContent()}
|
|
|
|
|
<div className="modal-cta-wrap">
|
2025-05-22 23:45:43 +00:00
|
|
|
<Button disabled={isUpdating} onClick={onCancel}>
|
|
|
|
|
Done
|
|
|
|
|
</Button>
|
2025-04-28 23:32:41 +00:00
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
</Modal>
|
|
|
|
|
{!!scriptForDetails && (
|
|
|
|
|
<ScriptDetailsModal
|
|
|
|
|
onCancel={() => setScriptForDetails(undefined)}
|
|
|
|
|
selectedScriptDetails={scriptForDetails}
|
|
|
|
|
suppressSecondaryActions
|
|
|
|
|
customPrimaryButtons={
|
|
|
|
|
<div className="modal-cta-wrap">
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
onRunScriptBatch(scriptForDetails);
|
|
|
|
|
}}
|
|
|
|
|
isLoading={isUpdating}
|
|
|
|
|
>
|
|
|
|
|
Run
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => setScriptForDetails(undefined)}
|
|
|
|
|
variant="inverse"
|
|
|
|
|
>
|
|
|
|
|
Go back
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default RunScriptBatchModal;
|