21128: readonly collections good Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa.knox...
[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 { 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 { 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();
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 newProcessData = {
161             ownerUuid: ownerUUid,
162             name: basicForm.name,
163             description: basicForm.description,
164             state: ContainerRequestState.COMMITTED,
165             mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
166             runtimeConstraints: {
167                 API: true,
168                 vcpus: advancedForm[VCPUS_FIELD],
169                 ram: (advancedForm[KEEP_CACHE_RAM_FIELD] + advancedForm[RAM_FIELD]),
170             },
171             schedulingParameters: {
172                 max_run_time: advancedForm[RUNTIME_FIELD]
173             },
174             containerImage: advancedForm[RUNNER_IMAGE_FIELD],
175             cwd: '/var/spool/cwl',
176             command: [
177                 'arvados-cwl-runner',
178                 '--api=containers',
179                 '--local',
180                 `--project-uuid=${ownerUUid}`,
181                 '/var/lib/cwl/workflow.json#main',
182                 '/var/lib/cwl/cwl.input.json'
183             ],
184             outputPath: '/var/spool/cwl',
185             priority: 500,
186             outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : `Output from ${basicForm.name}`,
187             properties: {
188                 template_uuid: selectedWorkflow.uuid,
189                 workflowName: selectedWorkflow.name
190             },
191             useExisting: false
192         };
193         const newProcess = await services.containerRequestService.create(newProcessData);
194         dispatch(navigateTo(newProcess.uuid));
195     }
196 };
197
198 const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
199     [VCPUS_FIELD]: 1,
200     [RAM_FIELD]: 1073741824,
201     [KEEP_CACHE_RAM_FIELD]: 268435456,
202     [RUNNER_IMAGE_FIELD]: "arvados/jobs"
203 };
204
205 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
206     Object.keys(inputs).reduce((normalizedInputs, key) => ({
207         ...normalizedInputs,
208         [key.split('/').slice(1).join('/')]: inputs[key],
209     }), {});
210 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);