20538: Add 403 error handling to remove process action and suppress errors from the...
[arvados-workbench2.git] / src / store / processes / processes-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { updateResources } from 'store/resources/resources-actions';
9 import { Process } from './process';
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { projectPanelActions } from 'store/project-panel/project-panel-action';
13 import { navigateToRunProcess } from 'store/navigation/navigation-action';
14 import { goToStep, runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
15 import { getResource } from 'store/resources/resources';
16 import { initialize } from "redux-form";
17 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from "views/run-process-panel/run-process-basic-form";
18 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from "views/run-process-panel/run-process-advanced-form";
19 import { MOUNT_PATH_CWL_WORKFLOW, MOUNT_PATH_CWL_INPUT } from 'models/process';
20 import { CommandInputParameter, getWorkflow, getWorkflowInputs, getWorkflowOutputs, WorkflowInputsData } from "models/workflow";
21 import { ProjectResource } from "models/project";
22 import { UserResource } from "models/user";
23 import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
24 import { ContainerResource } from "models/container";
25 import { ContainerRequestResource, ContainerRequestState } from "models/container-request";
26 import { FilterBuilder } from "services/api/filter-builder";
27 import { CommonResourceServiceError, getCommonResourceServiceError } from "services/common-service/common-resource-service";
28
29 export const loadProcess = (containerRequestUuid: string) =>
30     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process | undefined> => {
31         let containerRequest: ContainerRequestResource | undefined = undefined;
32         try {
33             containerRequest = await services.containerRequestService.get(containerRequestUuid);
34             dispatch<any>(updateResources([containerRequest]));
35         } catch {
36             return undefined;
37         }
38
39         if (containerRequest.outputUuid) {
40             try {
41                 const collection = await services.collectionService.get(containerRequest.outputUuid, false);
42                 dispatch<any>(updateResources([collection]));
43             } catch {}
44         }
45
46         if (containerRequest.containerUuid) {
47             let container: ContainerResource | undefined = undefined;
48             try {
49                 container = await services.containerService.get(containerRequest.containerUuid, false);
50                 dispatch<any>(updateResources([container]));
51             } catch {}
52
53             try{
54                 if (container && container.runtimeUserUuid) {
55                     const runtimeUser = await services.userService.get(container.runtimeUserUuid, false);
56                     dispatch<any>(updateResources([runtimeUser]));
57                 }
58             } catch {}
59
60             return { containerRequest, container };
61         }
62         return { containerRequest };
63     };
64
65 export const loadContainers = (containerUuids: string[], loadMounts: boolean = true) =>
66     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
67         let args: any = {
68             filters: new FilterBuilder().addIn('uuid', containerUuids).getFilters(),
69             limit: containerUuids.length,
70          };
71         if (!loadMounts) {
72             args.select = containerFieldsNoMounts;
73         }
74         const { items } = await services.containerService.list(args);
75         dispatch<any>(updateResources(items));
76         return items;
77     };
78
79 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
80 const containerFieldsNoMounts = [
81     "auth_uuid",
82     "command",
83     "container_image",
84     "cost",
85     "created_at",
86     "cwd",
87     "environment",
88     "etag",
89     "exit_code",
90     "finished_at",
91     "gateway_address",
92     "href",
93     "interactive_session_started",
94     "kind",
95     "lock_count",
96     "locked_by_uuid",
97     "log",
98     "modified_at",
99     "modified_by_client_uuid",
100     "modified_by_user_uuid",
101     "output_path",
102     "output_properties",
103     "output_storage_classes",
104     "output",
105     "owner_uuid",
106     "priority",
107     "progress",
108     "runtime_auth_scopes",
109     "runtime_constraints",
110     "runtime_status",
111     "runtime_user_uuid",
112     "scheduling_parameters",
113     "started_at",
114     "state",
115     "uuid",
116 ]
117
118 export const cancelRunningWorkflow = (uuid: string) =>
119     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
120         try {
121             const process = await services.containerRequestService.update(uuid, { priority: 0 });
122             dispatch<any>(updateResources([process]));
123             if (process.containerUuid) {
124                 const container = await services.containerService.get(process.containerUuid, false);
125                 dispatch<any>(updateResources([container]));
126             }
127             return process;
128         } catch (e) {
129             throw new Error('Could not cancel the process.');
130         }
131     };
132
133 export const resumeOnHoldWorkflow = (uuid: string) =>
134     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
135         try {
136             const process = await services.containerRequestService.update(uuid, { priority: 500 });
137             dispatch<any>(updateResources([process]));
138             if (process.containerUuid) {
139                 const container = await services.containerService.get(process.containerUuid, false);
140                 dispatch<any>(updateResources([container]));
141             }
142             return process;
143         } catch (e) {
144             throw new Error('Could not resume the process.');
145         }
146     };
147
148 export const startWorkflow = (uuid: string) =>
149     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
150         try {
151             const process = await services.containerRequestService.update(uuid, { state: ContainerRequestState.COMMITTED });
152             if (process) {
153                 dispatch<any>(updateResources([process]));
154                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process started', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
155             } else {
156                 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
157             }
158         } catch (e) {
159             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to start process`, kind: SnackbarKind.ERROR }));
160         }
161     };
162
163 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
164     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
165         const process = getResource<any>(processUuid)(getState().resources);
166         const workflows = getState().runProcessPanel.searchWorkflows;
167         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
168         if (workflow && process) {
169             const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
170             if (mainWf) { mainWf.inputs = getInputs(process); }
171             const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
172             const newWorkflow = { ...workflow, definition: stringifiedDefinition };
173
174             const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
175             const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
176             dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
177
178             const advancedInitialData: RunProcessAdvancedFormData = {
179                 output: process.outputName,
180                 runtime: process.schedulingParameters.max_run_time,
181                 ram: process.runtimeConstraints.ram,
182                 vcpus: process.runtimeConstraints.vcpus,
183                 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
184                 acr_container_image: process.containerImage
185             };
186             dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
187
188             dispatch<any>(navigateToRunProcess);
189             dispatch<any>(goToStep(1));
190             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
191             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
192         } else {
193             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
194         }
195     };
196
197 /*
198  * Fetches raw inputs from containerRequest mounts with fallback to properties
199  * Returns undefined if containerRequest not loaded
200  * Returns {} if inputs not found in mounts or props
201  */
202 export const getRawInputs = (data: any): WorkflowInputsData | undefined => {
203     if (!data) { return undefined; }
204     const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
205     const propsInput = data.properties?.cwl_input;
206     if (!mountInput && !propsInput) { return {}; }
207     return (mountInput || propsInput);
208 }
209
210 export const getInputs = (data: any): CommandInputParameter[] => {
211     // Definitions from mounts are needed so we return early if missing
212     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
213     const content  = getRawInputs(data) as any;
214     // Only escape if content is falsy to allow displaying definitions if no inputs are present
215     // (Don't check raw content length)
216     if (!content) { return []; }
217
218     const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
219     return inputs ? inputs.map(
220         (it: any) => (
221             {
222                 type: it.type,
223                 id: it.id,
224                 label: it.label,
225                 default: content[it.id],
226                 value: content[it.id.split('/').pop()] || [],
227                 doc: it.doc
228             }
229         )
230     ) : [];
231 };
232
233 /*
234  * Fetches raw outputs from containerRequest properties
235  * Assumes containerRequest is loaded
236  */
237 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
238     if (!data || !data.properties || !data.properties.cwl_output) { return undefined; }
239     return (data.properties.cwl_output);
240 }
241
242 export type InputCollectionMount = {
243     path: string;
244     pdh: string;
245 }
246
247 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
248     if (!data || !data.mounts) { return []; }
249     return Object.keys(data.mounts)
250         .map(key => ({
251             ...data.mounts[key],
252             path: key,
253         }))
254         .filter(mount => mount.kind === 'collection' &&
255                 mount.portable_data_hash &&
256                 mount.path)
257         .map(mount => ({
258             path: mount.path,
259             pdh: mount.portable_data_hash,
260         }));
261 };
262
263 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
264     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
265     const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
266     return outputs ? outputs.map(
267         (it: any) => (
268             {
269                 type: it.type,
270                 id: it.id,
271                 label: it.label,
272                 doc: it.doc
273             }
274         )
275     ) : [];
276 };
277
278 export const openRemoveProcessDialog = (uuid: string) =>
279     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
280         dispatch(dialogActions.OPEN_DIALOG({
281             id: REMOVE_PROCESS_DIALOG,
282             data: {
283                 title: 'Remove process permanently',
284                 text: 'Are you sure you want to remove this process?',
285                 confirmButtonLabel: 'Remove',
286                 uuid
287             }
288         }));
289     };
290
291 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
292
293 export const removeProcessPermanently = (uuid: string) =>
294     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
295         try {
296             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
297             await services.containerRequestService.delete(uuid, false);
298             dispatch(projectPanelActions.REQUEST_ITEMS());
299             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
300         } catch(e) {
301             const error = getCommonResourceServiceError(e);
302             if (error === CommonResourceServiceError.PERMISSION_ERROR_FORBIDDEN) {
303                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Access denied`, hideDuration: 2000, kind: SnackbarKind.ERROR }));
304             } else {
305                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: `Deletion failed`, hideDuration: 2000, kind: SnackbarKind.ERROR }));
306             }
307         }
308     };