Skip to content

Commit

Permalink
feat(tasks): add an dockerOptions.volumes on bash tasks
Browse files Browse the repository at this point in the history
close #589
  • Loading branch information
tchiotludo committed May 4, 2022
1 parent c36610f commit 6929bc8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,13 @@ public static class DockerOptions {
)
@PluginProperty(dynamic = true)
protected String networkMode;

@Schema(
title = "List of volumes to mount",
description = "Must be a valid mount expression as string, example : `/home/user:/app`\n\n" +
"Volumes mount are disabled by default for security reasons, you must enabled on server configuration with `kestra.tasks.scripts.docker.volume-enabled` to `true`"
)
@PluginProperty(dynamic = true)
protected List<String> volumes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
public class DockerScriptRunner implements ScriptRunnerInterface {
private final RetryUtils retryUtils;

private final Boolean volumesEnabled;

public DockerScriptRunner(ApplicationContext applicationContext) {
this.retryUtils = applicationContext.getBean(RetryUtils.class);
this.volumesEnabled = applicationContext.getProperty("kestra.tasks.scripts.docker.volume-enabled", Boolean.class).orElse(false);
}

private DockerClient getDockerClient(AbstractBash abstractBash, RunContext runContext, Path workingDirectory) throws IllegalVariableEvaluationException, IOException {
Expand Down Expand Up @@ -172,6 +175,14 @@ public RunResult run(
hostConfig.withExtraHosts(runContext.render(abstractBash.getDockerOptions().getExtraHosts(), additionalVars).toArray(String[]::new));
}

if (this.volumesEnabled && abstractBash.getDockerOptions().getVolumes() != null) {
hostConfig.withBinds(runContext.render(abstractBash.getDockerOptions().getVolumes())
.stream()
.map(Bind::parse)
.collect(Collectors.toList())
);
}

if (abstractBash.getDockerOptions().getNetworkMode() != null) {
hostConfig.withNetworkMode(runContext.render(abstractBash.getDockerOptions().getNetworkMode(), additionalVars));
}
Expand Down
45 changes: 45 additions & 0 deletions core/src/test/java/io/kestra/core/tasks/DockerBashTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package io.kestra.core.tasks;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import io.kestra.core.runners.RunContext;
import io.kestra.core.tasks.scripts.AbstractBash;
import io.kestra.core.tasks.scripts.Bash;
import io.kestra.core.tasks.scripts.ScriptOutput;
import io.kestra.core.utils.TestsUtils;
import io.micronaut.context.annotation.Property;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@MicronautTest
@Property(name = "kestra.tasks.scripts.docker.volume-enabled", value = "true")
class DockerBashTest extends AbstractBashTest {
@Override
protected Bash.BashBuilder<?, ?> configure(Bash.BashBuilder<?, ?> builder) {
Expand All @@ -17,4 +36,30 @@ class DockerBashTest extends AbstractBashTest {
.build()
);
}

@Test
void volume() throws Exception {
Path tmpDir = Files.createTempDirectory("tmpDirPrefix");
Path tmpFile = tmpDir.resolve("tmp.txt");
Files.write(tmpFile, "I'm here".getBytes());


Bash bash = configure(Bash.builder()
.commands(new String[]{
"echo '::{\"outputs\": {\"extract\":\"'$(cat /host/tmp.txt)'\"}}::'",
})
)
.dockerOptions(AbstractBash.DockerOptions.builder()
.image("ubuntu")
.volumes(List.of(tmpDir.toFile() + ":/host" ))
.build()
)
.build();

RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
ScriptOutput run = bash.run(runContext);

assertThat(run.getStdOutLineCount(), is(1));
assertThat(run.getVars().get("extract"), is("I'm here"));
}
}

0 comments on commit 6929bc8

Please sign in to comment.