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