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