Merge branch '22143-process-page-crash'
[arvados.git] / services / workbench2 / 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 { getUserUuid } from "common/getuser";
10 import { WorkflowResource, WorkflowRunnerResources, getWorkflow, getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
11 import { getFormValues, initialize } from 'redux-form';
12 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from 'views/run-process-panel/run-process-basic-form';
13 import { RUN_PROCESS_INPUTS_FORM } from 'views/run-process-panel/run-process-inputs-form';
14 import { WorkflowInputsData } from 'models/workflow';
15 import { createWorkflowMounts, createWorkflowSecretMounts } from 'models/process';
16 import { ContainerRequestState } from 'models/container-request';
17 import { navigateTo } from '../navigation/navigation-action';
18 import {
19     RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM, VCPUS_FIELD,
20     KEEP_CACHE_RAM_FIELD, RAM_FIELD, RUNTIME_FIELD, OUTPUT_FIELD, RUNNER_IMAGE_FIELD
21 } from 'views/run-process-panel/run-process-advanced-form';
22 import { dialogActions } from 'store/dialog/dialog-actions';
23 import { setBreadcrumbs } from 'store/breadcrumbs/breadcrumbs-actions';
24 import { getResource } from 'store/resources/resources';
25 import { ProjectResource } from "models/project";
26 import { UserResource } from "models/user";
27
28 export const runProcessPanelActions = unionize({
29     SET_PROCESS_PATHNAME: ofType<string>(),
30     SET_PROCESS_OWNER_UUID: ofType<string>(),
31     SET_CURRENT_STEP: ofType<number>(),
32     SET_STEP_CHANGED: ofType<boolean>(),
33     SET_WORKFLOWS: ofType<WorkflowResource[]>(),
34     SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
35     SET_WORKFLOW_PRESETS: ofType<WorkflowResource[]>(),
36     SELECT_WORKFLOW_PRESET: ofType<WorkflowResource>(),
37     SEARCH_WORKFLOWS: ofType<string>(),
38     RESET_RUN_PROCESS_PANEL: ofType<{}>(),
39 });
40
41 export interface RunProcessSecondStepDataFormProps {
42     name: string;
43     description: string;
44 }
45
46 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
47 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
48
49 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
50
51 export const loadRunProcessPanel = () =>
52     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
53         try {
54             dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
55             const response = await services.workflowService.list({limit: 200});
56             dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
57         } catch (e) {
58             return;
59         }
60     };
61
62 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
63     (dispatch: Dispatch, getState: () => RootState) => {
64         const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
65         const isStepChanged = getState().runProcessPanel.isStepChanged;
66         if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
67             dispatch(dialogActions.OPEN_DIALOG({
68                 id: SET_WORKFLOW_DIALOG,
69                 data: {
70                     title: 'Form will be cleared',
71                     text: 'Changing a workflow will clean all input fields in next step.',
72                     confirmButtonLabel: 'Change Workflow',
73                     workflow
74                 }
75             }));
76         } else {
77             dispatch<any>(setWorkflow(workflow, false));
78         }
79     };
80
81 export const getWorkflowRunnerSettings = (workflow: WorkflowResource) => {
82     const advancedFormValues = {};
83     Object.assign(advancedFormValues, DEFAULT_ADVANCED_FORM_VALUES);
84
85     const wf = getWorkflow(parseWorkflowDefinition(workflow));
86     const hints = wf ? wf.hints : undefined;
87     if (hints) {
88         const resc = hints.find(item => item.class === 'http://arvados.org/cwl#WorkflowRunnerResources') as WorkflowRunnerResources | undefined;
89         if (resc) {
90             if (resc.ramMin) { advancedFormValues[RAM_FIELD] = resc.ramMin * (1024 * 1024); }
91             if (resc.coresMin) { advancedFormValues[VCPUS_FIELD] = resc.coresMin; }
92             if (resc.keep_cache) { advancedFormValues[KEEP_CACHE_RAM_FIELD] = resc.keep_cache * (1024 * 1024); }
93             if (resc.acrContainerImage) { advancedFormValues[RUNNER_IMAGE_FIELD] = resc.acrContainerImage; }
94         }
95     }
96     return advancedFormValues;
97 };
98
99 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
100     (dispatch: Dispatch<any>, getState: () => RootState) => {
101         const isStepChanged = getState().runProcessPanel.isStepChanged;
102
103         const advancedFormValues = getWorkflowRunnerSettings(workflow);
104
105         let owner = getResource<ProjectResource | UserResource>(getState().runProcessPanel.processOwnerUuid)(getState().resources);
106         if (!owner || !owner.canWrite) {
107             owner = undefined;
108         }
109
110         if (isStepChanged && isWorkflowChanged) {
111             dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
112             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
113             dispatch<any>(loadPresets(workflow.uuid));
114             dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name: workflow.name, owner }));
115             dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
116         }
117         if (!isWorkflowChanged) {
118             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
119             dispatch<any>(loadPresets(workflow.uuid));
120             dispatch(initialize(RUN_PROCESS_BASIC_FORM, { name: workflow.name, owner }));
121             dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
122         }
123     };
124
125 export const loadPresets = (workflowUuid: string) =>
126     async (dispatch: Dispatch<any>, _: () => RootState, { workflowService }: ServiceRepository) => {
127         const { items } = await workflowService.presets(workflowUuid);
128         dispatch(runProcessPanelActions.SET_WORKFLOW_PRESETS(items));
129     };
130
131 export const selectPreset = (preset: WorkflowResource) =>
132     (dispatch: Dispatch<any>) => {
133         dispatch(runProcessPanelActions.SELECT_WORKFLOW_PRESET(preset));
134         const inputs = getWorkflowInputs(parseWorkflowDefinition(preset)) || [];
135         const values = inputs.reduce((values, input) => ({
136             ...values,
137             [input.id]: input.default,
138         }), {});
139         dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
140     };
141
142 export const goToStep = (step: number) =>
143     (dispatch: Dispatch) => {
144         if (step === 1) {
145             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
146         }
147         dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
148     };
149
150 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
151     const state = getState();
152     const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
153     const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
154     const userUuid = getUserUuid(getState());
155     if (!userUuid) { return; }
156     const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
157     const ownerUUid = basicForm.owner ? basicForm.owner.uuid : (processOwnerUuid ? processOwnerUuid : userUuid);
158     if (selectedWorkflow) {
159         const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData || getWorkflowRunnerSettings(selectedWorkflow);
160         const inputObject = normalizeInputKeys(inputsForm);
161         const secret_mounts = createWorkflowSecretMounts(selectedWorkflow, inputObject);
162         const newProcessData = {
163             ownerUuid: ownerUUid,
164             name: basicForm.name,
165             description: advancedForm.description,
166             state: ContainerRequestState.COMMITTED,
167             mounts: createWorkflowMounts(selectedWorkflow, inputObject),
168             secret_mounts: secret_mounts,
169             runtimeConstraints: {
170                 API: true,
171                 vcpus: advancedForm[VCPUS_FIELD],
172                 ram: (advancedForm[KEEP_CACHE_RAM_FIELD] + advancedForm[RAM_FIELD]),
173             },
174             schedulingParameters: {
175                 max_run_time: advancedForm[RUNTIME_FIELD]
176             },
177             containerImage: advancedForm[RUNNER_IMAGE_FIELD],
178             cwd: '/var/spool/cwl',
179             command: [
180                 'arvados-cwl-runner',
181                 '--local',
182                 '--api=containers',
183                 '--no-log-timestamps',
184                 '--disable-color',
185                 `--project-uuid=${ownerUUid}`,
186                 '/var/lib/cwl/workflow.json#main',
187                 '/var/lib/cwl/cwl.input.json'
188             ],
189             outputPath: '/var/spool/cwl',
190             priority: 500,
191             outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : `Output from ${basicForm.name}`,
192             properties: {
193                 template_uuid: selectedWorkflow.uuid,
194                 workflowName: selectedWorkflow.name
195             },
196             useExisting: false
197         };
198         const newProcess = await services.containerRequestService.create(newProcessData);
199         dispatch(navigateTo(newProcess.uuid));
200     }
201 };
202
203 const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
204     [VCPUS_FIELD]: 1,
205     [RAM_FIELD]: 1073741824,
206     [KEEP_CACHE_RAM_FIELD]: 268435456,
207     [RUNNER_IMAGE_FIELD]: "arvados/jobs"
208 };
209
210 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
211     Object.keys(inputs).reduce((normalizedInputs, key) => ({
212         ...normalizedInputs,
213         [key.split('/').slice(1).join('/')]: inputs[key],
214     }), {});
215 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);