Commit 2a4e7f82 authored by mamunozgil's avatar mamunozgil
Browse files

+Add logs to runContainer -Delete runContainerVolumes

parent 49f19419
Showing with 18 additions and 76 deletions
+18 -76
package de.hftstuttgart.dtabackend.utils; package de.hftstuttgart.dtabackend.utils;
import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerResponse; import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.DockerException; import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.Bind; import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Mount; import com.github.dockerjava.api.model.Mount;
import com.github.dockerjava.api.model.MountType; import com.github.dockerjava.api.model.MountType;
...@@ -18,6 +20,7 @@ import org.springframework.core.env.Environment; ...@@ -18,6 +20,7 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -43,8 +46,7 @@ public class DockerUtil { ...@@ -43,8 +46,7 @@ public class DockerUtil {
dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient); dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient);
} }
public int runContainerWithBinds(String image, Bind... binds) throws InterruptedException, IOException public int runContainer(String image, Bind... binds) throws InterruptedException, IOException {
{
LOG.debug(String.format("pull image: %s", image)); LOG.debug(String.format("pull image: %s", image));
try { try {
dockerClient.pullImageCmd(image) dockerClient.pullImageCmd(image)
...@@ -78,6 +80,19 @@ public class DockerUtil { ...@@ -78,6 +80,19 @@ public class DockerUtil {
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()));
dockerClient.logContainerCmd(containerResponse.getId())
.withStdOut(true)
.withStdErr(true)
.withFollowStream(true)
.exec(new ResultCallback.Adapter<Frame>() {
@Override
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("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())
...@@ -93,77 +108,4 @@ public class DockerUtil { ...@@ -93,77 +108,4 @@ public class DockerUtil {
return ret; return ret;
} }
public int runContainerWithVolumes(String image, Volume... volumes) throws InterruptedException, IOException {
LOG.debug(String.format("Pulling image: %s", image));
// Pull the Docker image or log and proceed with local image
try {
dockerClient.pullImageCmd(image).start().awaitCompletion();
LOG.debug(String.format("Image %s pulled successfully", image));
} catch (DockerException e) {
LOG.warn(String.format(
"Failed to pull image %s from remote: %s. Attempting to use local image.",
image, e.getMessage()));
if (dockerClient.inspectImageCmd(image).exec() == null) {
LOG.error(String.format("Image %s not found locally. Aborting.", image));
throw new IOException("Image not available locally or remotely: " + image);
}
}
String containerId = null;
try {
LOG.debug("Configuring container with volumes");
// Prepare the host configuration for volumes
HostConfig hostConfig = HostConfig.newHostConfig()
.withMounts(Arrays.stream(volumes)
.map(volume -> new Mount()
.withTarget(volume.getPath())
.withType(MountType.VOLUME))
.collect(Collectors.toList()));
// Create the container
LOG.debug("Creating container");
CreateContainerResponse containerResponse = dockerClient.createContainerCmd(image)
.withHostConfig(hostConfig)
.withName("testcontainer-" + UUID.randomUUID())
.exec();
containerId = containerResponse.getId();
LOG.debug(String.format("Container created with ID: %s", containerId));
// Start the container
LOG.debug(String.format("Starting container %s", containerId));
dockerClient.startContainerCmd(containerId).exec();
// Wait for container completion
LOG.debug(String.format("Waiting for completion of container %s", containerId));
int statusCode = dockerClient.waitContainerCmd(containerId)
.start()
.awaitCompletion()
.awaitStatusCode();
LOG.debug(String.format("Container %s completed with status code %d", containerId, statusCode));
return statusCode;
} catch (DockerException | InterruptedException e) {
LOG.error(String.format("An error occurred: %s", e.getMessage()), e);
throw e;
} finally {
if (containerId != null) {
try {
LOG.debug(String.format("Cleaning up container %s", containerId));
dockerClient.removeContainerCmd(containerId).withRemoveVolumes(false).exec();
LOG.debug(String.format("Container %s removed successfully", containerId));
} catch (DockerException cleanupException) {
LOG.error(String.format(
"Failed to clean up container %s: %s",
containerId, cleanupException.getMessage()), cleanupException);
}
}
}
}
} }
...@@ -96,7 +96,7 @@ public class ExecuteTestUtil { ...@@ -96,7 +96,7 @@ public class ExecuteTestUtil {
} }
// Start the test-container with professor-given image and submission-specific volume mounts // Start the test-container with professor-given image and submission-specific volume mounts
dockerUtil.runContainerWithBinds( dockerUtil.runContainer(
image, image,
new Bind(testPath.toString(), new Volume("/data/test")), new Bind(testPath.toString(), new Volume("/data/test")),
new Bind(srcPath.toString(), new Volume("/data/src")), new Bind(srcPath.toString(), new Volume("/data/src")),
......
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