1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
22 export const RUN_PROCESS_BASIC_FORM = 'runProcessBasicForm';
23 export const RUN_PROCESS_INPUTS_FORM = 'runProcessInputsForm';
25 export interface RunProcessBasicFormData {
27 owner?: ProjectResource | UserResource;
30 export const RUN_PROCESS_ADVANCED_FORM = 'runProcessAdvancedForm';
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';
40 export interface RunProcessAdvancedFormData {
41 [DESCRIPTION_FIELD]?: string;
42 [OUTPUT_FIELD]?: string;
43 [RUNTIME_FIELD]?: number;
45 [VCPUS_FIELD]: number;
46 [KEEP_CACHE_RAM_FIELD]: number;
47 [RUNNER_IMAGE_FIELD]: string;
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<{}>(),
63 export interface RunProcessSecondStepDataFormProps {
68 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
69 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
71 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
73 export const loadRunProcessPanel = () =>
74 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
76 dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
77 const response = await services.workflowService.list({limit: 200});
78 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
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,
92 title: 'Form will be cleared',
93 text: 'Changing a workflow will clean all input fields in next step.',
94 confirmButtonLabel: 'Change Workflow',
99 dispatch<any>(setWorkflow(workflow, false));
103 export const getWorkflowRunnerSettings = (workflow: WorkflowResource) => {
104 const advancedFormValues = {};
105 Object.assign(advancedFormValues, DEFAULT_ADVANCED_FORM_VALUES);
107 const wf = getWorkflow(parseWorkflowDefinition(workflow));
108 const hints = wf ? wf.hints : undefined;
110 const resc = hints.find(item => item.class === 'http://arvados.org/cwl#WorkflowRunnerResources') as WorkflowRunnerResources | undefined;
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; }
118 return advancedFormValues;
121 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
122 (dispatch: Dispatch<any>, getState: () => RootState) => {
123 const isStepChanged = getState().runProcessPanel.isStepChanged;
125 const advancedFormValues = getWorkflowRunnerSettings(workflow);
127 let owner = getResource<ProjectResource | UserResource>(getState().runProcessPanel.processOwnerUuid)(getState().resources);
128 if (!owner || !owner.canWrite) {
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));
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));
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));
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) => ({
159 [input.id]: input.default,
161 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
164 export const goToStep = (step: number) =>
165 (dispatch: Dispatch) => {
167 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
169 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
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: {
193 vcpus: advancedForm[VCPUS_FIELD],
194 ram: (advancedForm[KEEP_CACHE_RAM_FIELD] + advancedForm[RAM_FIELD]),
196 schedulingParameters: {
197 max_run_time: advancedForm[RUNTIME_FIELD]
199 containerImage: advancedForm[RUNNER_IMAGE_FIELD],
200 cwd: '/var/spool/cwl',
202 'arvados-cwl-runner',
205 '--no-log-timestamps',
207 `--project-uuid=${ownerUUid}`,
208 '/var/lib/cwl/workflow.json#main',
209 '/var/lib/cwl/cwl.input.json'
211 outputPath: '/var/spool/cwl',
213 outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : `Output from ${basicForm.name}`,
215 template_uuid: selectedWorkflow.uuid,
216 workflowName: selectedWorkflow.name
220 const newProcess = await services.containerRequestService.create(newProcessData);
221 dispatch(navigateTo(newProcess.uuid));
225 const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
227 [RAM_FIELD]: 1073741824,
228 [KEEP_CACHE_RAM_FIELD]: 268435456,
229 [RUNNER_IMAGE_FIELD]: "arvados/jobs"
232 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
233 Object.keys(inputs).reduce((normalizedInputs, key) => ({
235 [key.split('/').slice(1).join('/')]: inputs[key],
237 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);