Skip to content

Commit

Permalink
fix: append grader to image tag for build
Browse files Browse the repository at this point in the history
Docker does not allow setting the image repository
(essentially, a tag) as a 64-byte SHA Sum string.
  • Loading branch information
williams-jack committed Jul 30, 2024
1 parent a9b84f4 commit bd82c58
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const buildImage = ({ dockerfile_sha_sum }: GraderImageBuildRequest, buildLogs:
const dockerBuildArgs = [
"build",
"-t",
dockerfile_sha_sum,
`grader-${dockerfile_sha_sum}`,
"-f",
path.join(CONFIG.dockerImageFolder, `${dockerfile_sha_sum}.Dockerfile`),
".",
Expand All @@ -61,7 +61,7 @@ const buildImage = ({ dockerfile_sha_sum }: GraderImageBuildRequest, buildLogs:
execFile(
"docker",
dockerBuildArgs,
(err, stdout, stderr) => {
(err, _stdout, stderr) => {
const step: ImageBuildStep = "Run docker build on Dockerfile.";
if (err) {
buildLogs.push({
Expand Down Expand Up @@ -101,12 +101,12 @@ const removeDockerfileAfterBuild = (dockerfilePath: string, buildLogs: Array<Ima
});
};

const saveImageToTgz = (imageName: string, buildLogs: Array<ImageBuildLog>): Promise<void> => {
const saveImageToTgz = (shaSum: string, buildLogs: Array<ImageBuildLog>): Promise<void> => {
const dockerSaveCommandArgs = [
"save",
"-o",
path.join(CONFIG.dockerImageFolder, `${imageName}.tgz`),
imageName,
path.join(CONFIG.dockerImageFolder, `${shaSum}.tgz`),
`grader-${shaSum}`,
];
return new Promise<void>((resolve, reject) => {
execFile(
Expand All @@ -118,7 +118,7 @@ const saveImageToTgz = (imageName: string, buildLogs: Array<ImageBuildLog>): Pro
buildLogs.push({ step, error: stderr });
reject({ was_successful: false, logs: buildLogs });
} else {
buildLogs.push({ step, output: `Successfully saved image to ${imageName}.tgz.` });
buildLogs.push({ step, output: `Successfully saved image to ${shaSum}.tgz.` });
resolve();
}
},
Expand Down
32 changes: 16 additions & 16 deletions worker/orca_grader/docker_utils/images/utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import subprocess
from typing import List


def does_image_exist_locally(container_sha: str) -> bool:
program_args = [
"docker",
"image",
"ls",
"--format",
"{{.Repository}}"
]
proc_res = subprocess.run(program_args, capture_output=True, check=True)
image_names = proc_res.stdout.decode().split('\n')[:-1]
return container_sha in image_names
program_args = [
"docker",
"image",
"ls",
"--format",
"{{.Repository}}"
]
proc_res = subprocess.run(program_args, capture_output=True, check=True)
image_names = proc_res.stdout.decode().split('\n')[:-1]
return f"grader-{container_sha}" in image_names


def get_all_docker_images() -> List[str]:
res = subprocess.run(["docker", "image", "ls", "--format", "{{.Repository}}"],
capture_output=True)
output = res.stdout.decode()
image_names = output.split('\n')[:-1]
return image_names

res = subprocess.run(["docker", "image", "ls", "--format", "{{.Repository}}"],
capture_output=True)
output = res.stdout.decode()
image_names = output.split('\n')[:-1]
return image_names
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class DockerGradingJobExecutorBuilder(GradingJobExecutorBuilder):

def __init__(self, container_sha: str,
container_command: List[str] = __DEFAULT_CONTIANER_CMD) -> None:
self.__container_sha = container_sha
self.__container_tag = f"grader-{container_sha}"
self.__container_command = container_command
self.__file_mappings: Dict[str, str] = dict()
self.__env_variable_mappings: Dict[str, str] = dict()

def __num_containers_with_same_sha(self) -> int:
filter_op: Callable[[str], bool] = lambda n: n.startswith(
self.__container_sha)
self.__container_tag)
return len(list(filter(filter_op, get_all_container_names())))

def add_docker_volume_mapping(self, local_path: str, container_path: str) -> None:
Expand All @@ -32,7 +32,7 @@ def add_docker_environment_variable_mapping(self, name: str, value: str) -> None

def build(self) -> GradingJobExecutor:
# TODO: How could we maintain a 'reference count' for a unique id?
container_name = f"{self.__container_sha}_{int(time.time() * 100_000_000)}"
container_name = f"{self.__container_tag}_{int(time.time() * 100_000_000)}"
program_sequence = ["docker", "run", "--rm", "--name", container_name]
program_sequence.extend(["--network", "orca-testing"])
for name, value in self.__env_variable_mappings.items():
Expand All @@ -41,6 +41,6 @@ def build(self) -> GradingJobExecutor:
for local_path, container_path in self.__file_mappings.items():
program_sequence.append("-v")
program_sequence.append(f"{local_path}:{container_path}")
program_sequence.append(self.__container_sha)
program_sequence.append(self.__container_tag)
program_sequence.extend(self.__container_command)
return DockerGradingJobExecutor(create_runnable_job_subprocess(program_sequence), container_name)

0 comments on commit bd82c58

Please sign in to comment.