Connect running a process
[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, CommandInputParameter } 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
18 export const runProcessPanelActions = unionize({
19     SET_PROCESS_OWNER_UUID: ofType<string>(),
20     SET_CURRENT_STEP: ofType<number>(),
21     SET_WORKFLOWS: ofType<WorkflowResource[]>(),
22     SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
23 });
24
25 export interface RunProcessSecondStepDataFormProps {
26     name: string;
27     description: string;
28 }
29
30 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
31
32 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
33
34 export const loadRunProcessPanel = () =>
35     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
36         try {
37             const response = await services.workflowService.list();
38             dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
39         } catch (e) {
40             return;
41         }
42     };
43
44 export const setWorkflow = (workflow: WorkflowResource) =>
45     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
46         dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
47     };
48
49 export const goToStep = (step: number) => runProcessPanelActions.SET_CURRENT_STEP(step);
50
51 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
52     const state = getState();
53     const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
54     const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
55     const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
56     if (selectedWorkflow) {
57         const newProcessData = {
58             ownerUuid: processOwnerUuid,
59             name: basicForm.name,
60             description: basicForm.description,
61             state: ContainerRequestState.COMMITTED,
62             mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
63             runtimeConstraints: {
64                 API: true,
65                 vcpus: 1,
66                 ram: 1073741824,
67             },
68             containerImage: 'arvados/jobs:1.1.4.20180618144723',
69             cwd: '/var/spool/cwl',
70             command: [
71                 'arvados-cwl-runner',
72                 '--local',
73                 '--api=containers',
74                 `--project-uuid=${processOwnerUuid}`,
75                 '/var/lib/cwl/workflow.json#main',
76                 '/var/lib/cwl/cwl.input.json'
77             ],
78             outputPath: '/var/spool/cwl',
79             priority: 1,
80         };
81         const newProcess = await services.containerRequestService.create(newProcessData);
82         dispatch(navigateToProcess(newProcess.uuid));
83     }
84 };
85
86 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
87     Object.keys(inputs).reduce((normalizedInputs, key) => ({
88         ...normalizedInputs,
89         [key.split('/').slice(1).join('/')]: inputs[key],
90     }), {});