Commit 10e31708 authored by mamunozgil's avatar mamunozgil
Browse files

Moved the paths bindings to the runContainer function

No related merge requests found
Pipeline #10960 passed with stage
in 18 seconds
Showing with 104 additions and 96 deletions
+104 -96
...@@ -25,6 +25,10 @@ import java.util.Arrays; ...@@ -25,6 +25,10 @@ import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.nio.file.Path;
import java.nio.file.Paths;
@Component @Component
public class DockerUtil { public class DockerUtil {
private static final Logger LOG = LogManager.getLogger(DockerUtil.class); private static final Logger LOG = LogManager.getLogger(DockerUtil.class);
...@@ -46,8 +50,9 @@ public class DockerUtil { ...@@ -46,8 +50,9 @@ public class DockerUtil {
dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient); dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient);
} }
public int runContainer(String image, Bind... binds) throws InterruptedException, IOException { public int runContainer(String image, Path testPath, Path srcPath, Path resultPath)
LOG.debug(String.format("pull image: %s", image)); throws InterruptedException, IOException {
LOG.debug(String.format("Pulling image: %s", image));
try { try {
dockerClient.pullImageCmd(image) dockerClient.pullImageCmd(image)
.start() .start()
...@@ -55,61 +60,55 @@ public class DockerUtil { ...@@ -55,61 +60,55 @@ public class DockerUtil {
.close(); .close();
} catch (DockerException e) { } catch (DockerException e) {
LOG.error(String.format( LOG.error(String.format(
"pulling docker image %s failed with %s, trying with local image", "Pulling Docker image %s failed with %s, trying with local image",
image, image, e.getMessage()));
e.getMessage()));
} }
LOG.debug("creating container"); LOG.debug("Creating container");
CreateContainerResponse containerResponse; CreateContainerResponse containerResponse;
try { try {
// Extract paths from binds // Validate paths are absolute
String testPath = null; String absTestPath = ensureAbsolutePath(testPath);
String srcPath = null; String absSrcPath = ensureAbsolutePath(srcPath);
String resultPath = null; String absResultPath = ensureAbsolutePath(resultPath);
for (Bind bind : binds) {
Volume volume = bind.getVolume();
if (volume.getPath().contains("test")) {
testPath = bind.getPath();
} else if (volume.getPath().contains("src")) {
srcPath = bind.getPath();
} else if (volume.getPath().contains("result")) {
resultPath = bind.getPath();
}
}
if (testPath == null || srcPath == null || resultPath == null) { // Debugging the paths
throw new IllegalArgumentException("All required paths (testPath, srcPath, resultPath) must be provided."); LOG.debug(String.format(
} "Volume bindings: %s -> /data/test, %s -> /data/src, %s -> /data/result",
absTestPath, absSrcPath, absResultPath));
// Create the container
containerResponse = dockerClient.createContainerCmd("testcontainer") containerResponse = dockerClient.createContainerCmd("testcontainer")
.withImage(image) .withImage(image)
.withHostConfig( .withHostConfig(
HostConfig.newHostConfig() HostConfig.newHostConfig()
.withBinds(binds) .withBinds(
new Bind(absTestPath, new Volume("/data/test")),
new Bind(absSrcPath, new Volume("/data/src")),
new Bind(absResultPath, new Volume("/data/result"))
)
) )
.withCmd( .withCmd(
"java", "java",
"-Djava.security.egd=file:/dev/./urandom", "-Djava.security.egd=file:/dev/./urandom",
"-jar", "-jar",
"/data/app.jar", "/data/app.jar",
String.format("%s:%s", srcPath, testPath), "/data/src:/data/test",
"/data/libs/*:/data/test/libs/*", "/data/libs/*:/data/test/libs/*",
resultPath "/data/result"
) )
.exec(); .exec();
} catch (DockerException e) { } catch (DockerException e) {
LOG.error(String.format( LOG.error(String.format("Creating Docker Testrunner container failed with %s", e.getMessage()));
"Creating Docker Testrunner container failed with %s", e.getMessage()));
throw e; throw e;
} }
LOG.debug(String.format("container created: %s", containerResponse.getId())); LOG.debug(String.format("Container created: %s", containerResponse.getId()));
LOG.debug(String.format("starting container %s", containerResponse.getId())); LOG.debug(String.format("Starting container %s", containerResponse.getId()));
dockerClient.startContainerCmd(containerResponse.getId()).exec(); dockerClient.startContainerCmd(containerResponse.getId()).exec();
LOG.debug(String.format("retrieving logs for container %s", containerResponse.getId())); LOG.debug(String.format("Retrieving logs for container %s", containerResponse.getId()));
dockerClient.logContainerCmd(containerResponse.getId()) dockerClient.logContainerCmd(containerResponse.getId())
.withStdOut(true) .withStdOut(true)
.withStdErr(true) .withStdErr(true)
...@@ -117,24 +116,31 @@ public class DockerUtil { ...@@ -117,24 +116,31 @@ public class DockerUtil {
.exec(new ResultCallback.Adapter<Frame>() { .exec(new ResultCallback.Adapter<Frame>() {
@Override @Override
public void onNext(Frame frame) { public void onNext(Frame frame) {
// Process logs here
LOG.debug(String.format("LOG: %s", new String(frame.getPayload(), StandardCharsets.UTF_8))); LOG.debug(String.format("LOG: %s", new String(frame.getPayload(), StandardCharsets.UTF_8)));
} }
}); });
LOG.debug(String.format("waiting for completion of container %s", containerResponse.getId())); LOG.debug(String.format("Waiting for completion of container %s", containerResponse.getId()));
int ret = dockerClient int ret = dockerClient
.waitContainerCmd(containerResponse.getId()) .waitContainerCmd(containerResponse.getId())
.start() .start()
.awaitCompletion() .awaitCompletion()
.awaitStatusCode(); .awaitStatusCode();
LOG.debug(String.format("container completed with status %d", ret)); LOG.debug(String.format("Container completed with status %d", ret));
LOG.debug(String.format("deleting container %s", containerResponse.getId())); LOG.debug(String.format("Deleting container %s", containerResponse.getId()));
dockerClient.removeContainerCmd(containerResponse.getId()) dockerClient.removeContainerCmd(containerResponse.getId())
.withRemoveVolumes(true) .withRemoveVolumes(true)
.exec(); .exec();
return ret; return ret;
}
private String ensureAbsolutePath(Path path) {
if (!path.isAbsolute()) {
throw new IllegalArgumentException("Path must be absolute: " + path.toString());
} }
return path.toString();
}
} }
...@@ -95,19 +95,21 @@ public class ExecuteTestUtil { ...@@ -95,19 +95,21 @@ public class ExecuteTestUtil {
image = config.group(5); image = config.group(5);
} }
Path testCodePath = Paths.get(testPath.toString(), "test");
Path srcCodePath = Paths.get(srcPath.toString(), "src");
// Log the paths before binding // Log the paths before binding
LOG.debug("Binding paths for Docker container:"); LOG.debug("Binding paths for Docker container:");
LOG.debug("Test Path: {}", testPath); LOG.debug("Test Path: {}", testCodePath);
LOG.debug("Source Path: {}", srcPath); LOG.debug("Source Path: {}", srcCodePath);
LOG.debug("Result Path: {}", resultPath); LOG.debug("Result Path: {}", resultPath);
// Start the test-container with professor-given image and submission-specific volume mounts // Start the test-container with professor-given image and specific paths
dockerUtil.runContainer( dockerUtil.runContainer(
image, image,
new Bind(testPath.toString(), new Volume("test")), testCodePath,
new Bind(srcPath.toString(), new Volume("src")), srcCodePath,
new Bind(resultPath.toString(), new Volume("result")) resultPath);
);
return generateResult(assignmentId, resultPath, testPath); return generateResult(assignmentId, resultPath, testPath);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment