Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

expand shell task to run series of cmds as text #506

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.redhat.parodos.workflow.task.shell;
package com.redhat.parodos.tasks.shell;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
Expand Down Expand Up @@ -49,8 +50,7 @@ private static class ShellRunnerTaskParams {
if (command == null || command.isBlank()) {
throw new IllegalArgumentException("argument 'command' is empty or blank");
}
this.cmdline = new ArrayList<>() {
};
this.cmdline = new ArrayList<>();
this.cmdline.add(command);
if (workContext.get("args") != null) {
this.cmdline.addAll(List.of(workContext.get("args").toString().split(" ")));
Expand All @@ -64,17 +64,28 @@ private static class ShellRunnerTaskParams {

@Override
public WorkReport execute(WorkContext workContext) {
try {
workContext.put("command", getRequiredParameterValue("command"));
}
catch (Exception e) {
log.error("error command");
}
var params = new ShellRunnerTaskParams(workContext);
var pb = new ProcessBuilder(params.cmdline);
File tmpDir = null;
File tmpFile = null;
try {
tmpDir = Files
.createTempDirectory("parodos-shelltask-runner",
tmpFile = Files
.createTempFile("parodos-shelltask-runner", "",
PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE)))
.toFile();
pb.directory(tmpDir);
pb.redirectErrorStream(true);

FileWriter writer = new FileWriter(tmpFile.getAbsolutePath(), true);
writer.append((String) workContext.get("command"));
writer.close();
}
catch (Exception e) {
log.error("file write failed");
}
try {
var pb = new ProcessBuilder(tmpFile.getAbsolutePath());
log.info("begin shell task invocation");
Process p = pb.start();
var elapsed = !p.waitFor(params.timeoutInSeconds, TimeUnit.SECONDS);
Expand All @@ -87,15 +98,18 @@ public WorkReport execute(WorkContext workContext) {
try (var r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
r.lines().forEach(outputConsumer);
}
try (var r = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
r.lines().forEach(outputConsumer);
}
log.info("ended shell task invocation");
return new DefaultWorkReport(p.exitValue() == 0 ? WorkStatus.COMPLETED : WorkStatus.FAILED, workContext);
}
catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
finally {
if (tmpDir != null) {
tmpDir.delete();
if (tmpFile != null) {
tmpFile.delete();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redhat.parodos.workflow.task.shell;
package com.redhat.parodos.tasks.shell;

import java.util.UUID;

Expand Down Expand Up @@ -26,11 +26,9 @@ public void setUp() {
@Test
public void executeWithOutput() {
UUID randomUUID = UUID.randomUUID();
ctx.put("command", "echo");
ctx.put("args", randomUUID);
ctx.put("command", "#!/bin/bash\n echo \"%s\"\n".formatted(randomUUID));
var output = new StringBuilder();
underTest.outputConsumer = output::append;

assertThat(underTest.execute(ctx).getStatus(), equalTo(WorkStatus.COMPLETED));
assertThat(output.toString(), equalTo(randomUUID.toString()));
}
Expand Down Expand Up @@ -64,4 +62,4 @@ public void failsOnMissingCommand() {
assertThat("argument 'command' is empty or blank", equalTo(thrown.getMessage()));
}

}
}
Loading