|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.executors; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.RunShell; |
| 19 | +import io.serverlessworkflow.api.types.RunTaskConfiguration; |
| 20 | +import io.serverlessworkflow.api.types.Shell; |
| 21 | +import io.serverlessworkflow.impl.TaskContext; |
| 22 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 23 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 24 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 25 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 26 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 27 | +import io.serverlessworkflow.impl.expressions.ExpressionUtils; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.OutputStream; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.CompletableFuture; |
| 33 | + |
| 34 | +public class RunShellExecutor implements RunnableTask<RunShell> { |
| 35 | + |
| 36 | + private ProcessResultSupplier processResultSupplier; |
| 37 | + private CommandSupplier commandSupplier; |
| 38 | + |
| 39 | + @FunctionalInterface |
| 40 | + private interface ProcessResultSupplier { |
| 41 | + ProcessResult apply(TaskContext taskContext, ProcessBuilder processBuilder); |
| 42 | + } |
| 43 | + |
| 44 | + private interface CommandSupplier { |
| 45 | + ProcessBuilder apply(TaskContext taskContext, WorkflowContext workflowContext); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public CompletableFuture<WorkflowModel> apply( |
| 50 | + WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
| 51 | + ProcessBuilder processBuilder = this.commandSupplier.apply(taskContext, workflowContext); |
| 52 | + ProcessResult processResult = this.processResultSupplier.apply(taskContext, processBuilder); |
| 53 | + return CompletableFuture.completedFuture( |
| 54 | + workflowContext.definition().application().modelFactory().fromAny(processResult)); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void init(RunShell taskConfiguration, WorkflowDefinition definition) { |
| 59 | + Shell shell = taskConfiguration.getShell(); |
| 60 | + final String shellCommand = shell.getCommand(); |
| 61 | + |
| 62 | + this.commandSupplier = |
| 63 | + (taskContext, workflowContext) -> { |
| 64 | + WorkflowApplication application = definition.application(); |
| 65 | + |
| 66 | + String command = |
| 67 | + ExpressionUtils.isExpr(shellCommand) |
| 68 | + ? WorkflowUtils.buildStringResolver( |
| 69 | + application, shellCommand, taskContext.input().asJavaObject()) |
| 70 | + .apply(workflowContext, taskContext, taskContext.input()) |
| 71 | + : shellCommand; |
| 72 | + |
| 73 | + ProcessBuilder builder = new ProcessBuilder("sh", "-c", command); |
| 74 | + |
| 75 | + if (shell.getEnvironment() != null |
| 76 | + && shell.getEnvironment().getAdditionalProperties() != null) { |
| 77 | + for (Map.Entry<String, Object> entry : |
| 78 | + shell.getEnvironment().getAdditionalProperties().entrySet()) { |
| 79 | + String value = |
| 80 | + ExpressionUtils.isExpr(entry.getValue()) |
| 81 | + ? WorkflowUtils.buildStringResolver( |
| 82 | + application, |
| 83 | + entry.getValue().toString(), |
| 84 | + taskContext.input().asJavaObject()) |
| 85 | + .apply(workflowContext, taskContext, taskContext.input()) |
| 86 | + : entry.getValue().toString(); |
| 87 | + builder.environment().put(entry.getKey(), value); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return builder; |
| 92 | + }; |
| 93 | + |
| 94 | + this.processResultSupplier = |
| 95 | + (taskContext, processBuilder) -> { |
| 96 | + if (shellCommand == null || shellCommand.isBlank()) { |
| 97 | + throw new IllegalStateException( |
| 98 | + "Missing shell command in RunShell task: " + taskContext.taskName()); |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + Process process = processBuilder.start(); |
| 103 | + StringBuilder stdout = new StringBuilder(); |
| 104 | + StringBuilder stderr = new StringBuilder(); |
| 105 | + |
| 106 | + process.getInputStream().transferTo(the(stdout)); |
| 107 | + process.getErrorStream().transferTo(the(stderr)); |
| 108 | + |
| 109 | + int exitCode = process.waitFor(); |
| 110 | + |
| 111 | + return new ProcessResult(exitCode, stdout.toString().trim(), stderr.toString().trim()); |
| 112 | + |
| 113 | + } catch (IOException | InterruptedException e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean accept(Class<? extends RunTaskConfiguration> clazz) { |
| 121 | + return RunShell.class.equals(clazz); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + /** |
| 126 | + * Helper to create an OutputStream that writes to a StringBuilder |
| 127 | + * @param output the {@link StringBuilder} to write to |
| 128 | + * @return the {@link OutputStream} |
| 129 | + */ |
| 130 | + private static OutputStream the(StringBuilder output) { |
| 131 | + return new OutputStream() { |
| 132 | + @Override |
| 133 | + public void write(int b) { |
| 134 | + output.append((char) b); |
| 135 | + } |
| 136 | + }; |
| 137 | + } |
| 138 | +} |
0 commit comments