From 2a4e7f82891e5c0ea6f9fb38aad5fef63928ca2d Mon Sep 17 00:00:00 2001 From: mamunozgil <miguel.munoz-gil@hft-stuttgart.de> Date: Wed, 15 Jan 2025 14:12:43 +0100 Subject: [PATCH] +Add logs to runContainer -Delete runContainerVolumes --- .../dtabackend/utils/DockerUtil.java | 92 ++++--------------- .../dtabackend/utils/ExecuteTestUtil.java | 2 +- 2 files changed, 18 insertions(+), 76 deletions(-) diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java index b59d882..17f1b2e 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java @@ -1,9 +1,11 @@ package de.hftstuttgart.dtabackend.utils; 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.exception.DockerException; 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.Mount; import com.github.dockerjava.api.model.MountType; @@ -18,6 +20,7 @@ import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.UUID; import java.util.stream.Collectors; @@ -43,8 +46,7 @@ public class DockerUtil { 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)); try { dockerClient.pullImageCmd(image) @@ -78,6 +80,19 @@ public class DockerUtil { LOG.debug(String.format("starting container %s", containerResponse.getId())); 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())); int ret = dockerClient .waitContainerCmd(containerResponse.getId()) @@ -93,77 +108,4 @@ public class DockerUtil { 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); - } - } - } - } - } diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java index e8f1dc2..d0995b0 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java @@ -96,7 +96,7 @@ public class ExecuteTestUtil { } // Start the test-container with professor-given image and submission-specific volume mounts - dockerUtil.runContainerWithBinds( + dockerUtil.runContainer( image, new Bind(testPath.toString(), new Volume("/data/test")), new Bind(srcPath.toString(), new Volume("/data/src")), -- GitLab