Skip to content

Commit

Permalink
coderabbit feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
BBesrour committed Oct 16, 2024
1 parent 7c98073 commit 08b22d3
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.ExecCreateCmd;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
Expand Down Expand Up @@ -102,21 +101,18 @@ public CreateContainerResponse configureContainer(String containerName, String i
envVars.add("NO_PROXY=" + noProxy);
}
envVars.add("SCRIPT=" + buildScript);
CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(image).withName(containerName).withHostConfig(hostConfig).withEnv(envVars)
if (exerciseEnvVars != null && !exerciseEnvVars.isEmpty()) {
envVars.addAll(exerciseEnvVars);
}
return dockerClient.createContainerCmd(image).withName(containerName).withHostConfig(hostConfig).withEnv(envVars)
// Command to run when the container starts. This is the command that will be executed in the container's main process, which runs in the foreground and blocks the
// container from exiting until it finishes.
// It waits until the script that is running the tests (see below execCreateCmdResponse) is completed, and until the result files are extracted which is indicated
// by the creation of a file "stop_container.txt" in the container's root directory.
.withCmd("sh", "-c", "while [ ! -f " + LOCALCI_WORKING_DIRECTORY + "/stop_container.txt ]; do sleep 0.5; done");
// .withCmd("tail", "-f", "/dev/null"); // Activate for debugging purposes instead of the above command to get a running container that you can peek into using
// "docker exec -it <container-id> /bin/bash".

if (exerciseEnvVars != null && !exerciseEnvVars.isEmpty()) {
envVars.addAll(exerciseEnvVars);
createContainerCmd.withEnv(envVars);
}

return createContainerCmd.exec();
.withCmd("sh", "-c", "while [ ! -f " + LOCALCI_WORKING_DIRECTORY + "/stop_container.txt ]; do sleep 0.5; done")
// .withCmd("tail", "-f", "/dev/null") // Activate for debugging purposes instead of the above command to get a running container that you can peek into using
// "docker exec -it <container-id> /bin/bash".
.exec();
}

/**
Expand All @@ -140,7 +136,12 @@ public void runScriptInContainer(String containerId, String buildJobId, boolean
// The "sh preScript.sh" execution command specified here is need to install dependencies and prepare the environment for the build script.
log.info("Started running the pre-script for build job in container with id {}", containerId);
executeDockerCommand(containerId, buildJobId, true, true, false, "bash", LOCALCI_WORKING_DIRECTORY + "/preScript.sh");
dockerClient.disconnectFromNetworkCmd().withContainerId(containerId).withNetworkId("bridge").exec();
try {
dockerClient.disconnectFromNetworkCmd().withContainerId(containerId).withNetworkId("bridge").exec();
}
catch (Exception e) {
log.error("Failed to disconnect container with id {} from network: {}", containerId, e.getMessage());
}
}

log.info("Started running the build script for build job in container with id {}", containerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public BuildResult runBuildJob(BuildJobQueueItem buildJob, String containerName)
index++;
}

List<String> envVars = new ArrayList<>();
List<String> envVars = null;
boolean isNetworkDisabled = false;
if (buildJob.buildConfig().dockerRunConfig() != null) {
envVars = buildJob.buildConfig().dockerRunConfig().getEnv();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ public DockerRunConfig getDockerRunConfig() {
for (List<String> entry : list) {
if (entry.size() != 2 || entry.get(0) == null || entry.get(1) == null || entry.get(0).isBlank() || entry.get(1).isBlank()
|| !DockerRunConfig.AllowedDockerFlags.isAllowed(entry.get(0))) {
log.error("Invalid Docker flag entry: {}. Skipping.", entry);
log.warn("Invalid Docker flag entry: {}. Skipping.", entry);
continue;
}
switch (entry.get(0)) {
case "network":
dockerRunConfig.setNetworkDisabled(entry.get(1).equals("none"));
dockerRunConfig.setNetworkDisabled(entry.get(1).equalsIgnoreCase("none"));
break;
case "env":
dockerRunConfig.setEnv(parseEnvVariableString(entry.get(1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ private static String getBaseScript() {
cd /var/tmp/testing-dir
#!/usr/bin/env bash
set -e
echo "Starting dependency download script"
""";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ProgrammingExerciseBuildConfigurationComponent implements OnInit {

ngOnInit() {
if (this.programmingExercise()?.buildConfig?.dockerFlags) {
const dockerFlags = JSON.parse(this.programmingExercise()?.buildConfig?.dockerFlags || '{}') as [string, string][];
const dockerFlags = JSON.parse(this.programmingExercise()?.buildConfig?.dockerFlags || '[]') as [string, string][];
dockerFlags.forEach(([key, value]) => {
if (key === this.NETWORK_KEY) {
this.isNetworkDisabled = value === 'none';
Expand All @@ -64,9 +64,6 @@ export class ProgrammingExerciseBuildConfigurationComponent implements OnInit {

updateDockerFlags(key: string, value: string) {
let existingFlags = JSON.parse(this.programmingExercise()?.buildConfig?.dockerFlags || '[]') as [string, string][];
if (!existingFlags || existingFlags.length === 0) {
existingFlags = [] as [string, string][];
}
existingFlags = existingFlags.filter(([flag]) => ALLOWED_DOCKER_FLAG_OPTIONS.includes(flag) && flag !== key) || [];
if (ALLOWED_DOCKER_FLAG_OPTIONS.includes(key) && value.trim() !== '') {
existingFlags.push([key, value]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ void testFetchPush_instructorPracticeRepository() throws Exception {
}

@Nested
class DisabledBuildAgentTest {
class BuildJobConfigurationTest {

@BeforeEach
void setUp() {
Expand Down

0 comments on commit 08b22d3

Please sign in to comment.