Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14270_warning_mess...
[arvados.git] / src / store / run-process-panel / run-process-panel-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 { unionize, ofType, UnionOf } from "~/common/unionize";
7 import { ServiceRepository } from "~/services/services";
8 import { RootState } from '~/store/store';
9 import { WorkflowResource } from '~/models/workflow';
10 import { getFormValues } from 'redux-form';
11 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from '~/views/run-process-panel/run-process-basic-form';
12 import { RUN_PROCESS_INPUTS_FORM } from '~/views/run-process-panel/run-process-inputs-form';
13 import { WorkflowInputsData } from '~/models/workflow';
14 import { createWorkflowMounts } from '~/models/process';
15 import { ContainerRequestState } from '~/models/container-request';
16 import { navigateToProcess } from '../navigation/navigation-action';
17 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from '~/views/run-process-panel/run-process-advanced-form';
18 import { isItemNotInProject, isProjectOrRunProcessRoute } from '~/store/projects/project-create-actions';
19 import { dialogActions } from '~/store/dialog/dialog-actions';
20
21 export const runProcessPanelActions = unionize({
22     SET_PROCESS_OWNER_UUID: ofType<string>(),
23     SET_CURRENT_STEP: ofType<number>(),
24     SET_STEP_CHANGED: ofType<boolean>(),
25     SET_WORKFLOWS: ofType<WorkflowResource[]>(),
26     SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
27     SEARCH_WORKFLOWS: ofType<string>(),
28     RESET_RUN_PROCESS_PANEL: ofType<{}>(),
29 });
30
31 export interface RunProcessSecondStepDataFormProps {
32     name: string;
33     description: string;
34 }
35
36 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
37 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
38
39 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
40
41 export const loadRunProcessPanel = () =>
42     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
43         try {
44             dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
45             const response = await services.workflowService.list();
46             dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
47         } catch (e) {
48             return;
49         }
50     };
51
52 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
53     (dispatch: Dispatch, getState: () => RootState) => {
54         const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
55         const isStepChanged = getState().runProcessPanel.isStepChanged;
56         if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
57             dispatch(dialogActions.OPEN_DIALOG({
58                 id: SET_WORKFLOW_DIALOG,
59                 data: {
60                     title: 'Data loss warning',
61                     text: 'Changing a workflow will clean all input fields in next step.',
62                     confirmButtonLabel: 'Change Workflow',
63                     workflow
64                 }
65             }));
66         } else {
67             dispatch<any>(setWorkflow(workflow, false));
68         }
69     };
70
71 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
72     (dispatch: Dispatch<any>, getState: () => RootState) => {
73         const isStepChanged = getState().runProcessPanel.isStepChanged;
74         if (isStepChanged && isWorkflowChanged) {
75             dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
76             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
77         }
78         if (!isWorkflowChanged) {
79             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
80         }
81     };
82
83 export const goToStep = (step: number) =>
84     (dispatch: Dispatch, getState: () => RootState) => {
85         if (step === 1) {
86             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
87         }
88         dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
89     };
90
91 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
92     const state = getState();
93     const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
94     const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
95     const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData;
96     const userUuid = getState().auth.user!.uuid;
97     const router = getState();
98     const properties = getState().properties;
99     const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
100     if (selectedWorkflow) {
101         const newProcessData = {
102             ownerUuid: isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router) ? userUuid : processOwnerUuid,
103             name: basicForm.name,
104             description: basicForm.description,
105             state: ContainerRequestState.COMMITTED,
106             mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
107             runtimeConstraints: {
108                 API: true,
109                 vcpus: 1,
110                 ram: 1073741824,
111             },
112             containerImage: 'arvados/jobs',
113             cwd: '/var/spool/cwl',
114             command: [
115                 'arvados-cwl-runner',
116                 '--local',
117                 '--api=containers',
118                 `--project-uuid=${processOwnerUuid}`,
119                 '/var/lib/cwl/workflow.json#main',
120                 '/var/lib/cwl/cwl.input.json'
121             ],
122             outputPath: '/var/spool/cwl',
123             priority: 1,
124             outputName: advancedForm && advancedForm.output ? advancedForm.output : undefined,
125         };
126         const newProcess = await services.containerRequestService.create(newProcessData);
127         dispatch(navigateToProcess(newProcess.uuid));
128     }
129 };
130
131 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
132     Object.keys(inputs).reduce((normalizedInputs, key) => ({
133         ...normalizedInputs,
134         [key.split('/').slice(1).join('/')]: inputs[key],
135     }), {});
136 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);