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