Skip to content

Commit

Permalink
Merge branch 'refactor/consistent-image-f-strs' into refactor/use-doc…
Browse files Browse the repository at this point in the history
…ker-truth-image-names
  • Loading branch information
williams-jack committed Aug 22, 2024
2 parents 8b8bc06 + 1d0b78f commit 688de72
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
8 changes: 4 additions & 4 deletions worker/orca_grader/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from orca_grader.job_retrieval.postgres.grading_job_retriever import PostgresGradingJobRetriever
from orca_grader.docker_utils.images.utils import does_image_exist_locally
from orca_grader.docker_utils.images.image_name import get_image_name_for_sha
from orca_grader.docker_utils.images.image_loading import retrieve_image_tgz_for_sha, load_image_from_tgz
from orca_grader.docker_utils.images.image_loading import retrieve_image_tgz_for_unique_name, load_image_from_tgz
from orca_grader.job_termination.nonblocking_thread_executor import NonBlockingThreadPoolExecutor
from orca_grader.job_termination.stop_worker import GracefulKiller
from orca_grader.validations.exceptions import InvalidGradingJobJSONException
Expand Down Expand Up @@ -130,11 +130,11 @@ def run_grading_job(grading_job: GradingJobJSON, no_container: bool,
image_name = get_image_name_for_sha(container_sha)
if image_name is None:
_LOGGER.info(f"No image {image_name} found in local docker registry.")
retrieve_image_tgz_for_sha(container_sha)
image_name = load_image_from_tgz("{0}.tgz".format(container_sha))
tgz_file_name = retrieve_image_tgz_for_unique_name(container_sha)
image_name = load_image_from_tgz(tgz_file_name)
if image_name is None:
raise NoImageNameFoundException("No image was found from either the "
"local registry of `docker load`"
"local registry or `docker load`"
f"command for {container_sha}")
if container_command:
handle_grading_job(grading_job, image_name, container_command)
Expand Down
37 changes: 24 additions & 13 deletions worker/orca_grader/docker_utils/images/image_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,50 @@
import os
import re
import subprocess
from typing import Optional
from orca_grader.common.services.download_file import download_file
from orca_grader.config import APP_CONFIG


_LOGGER = logging.getLogger(__name__)


def retrieve_image_tgz_for_sha(container_sha: str) -> None:
file_name = f"{container_sha}.tgz"
def retrieve_image_tgz_for_unique_name(unique_name: str) -> str:
file_name = f"{unique_name}.tgz"
images_url = f"{APP_CONFIG.orca_web_server_host}/images/{file_name}"
_LOGGER.debug(f"Attempting to download image from {images_url}")
download_file(images_url, file_name)
_LOGGER.debug("Image downloaded.")
return file_name


def load_image_from_tgz(tgz_file_path: str) -> str:
def load_image_from_tgz(tgz_file_path: str) -> Optional[str]:
program_args = [
"docker",
"load",
"-i",
tgz_file_path
]
_LOGGER.debug(f"Attempting to load image from file {tgz_file_path}")
result = subprocess.run(program_args, check=True, capture_output=True)
_LOGGER.debug(f"Docker save stdout: {result.stdout.decode() if result.stdout is not None else '<None>' }")
_LOGGER.debug(f"Docker save stderr: {result.stderr.decode() if result.stderr is not None else '<None>' }")
_LOGGER.debug("Image loaded.")
# os.remove(tgz_file_path) # To save resources, clean up tgz after load.
# _LOGGER.debug("Image tgz file removed.")
return result.stderr and get_name_from_load_output(result.stderr.decode())

try:
_LOGGER.debug(f"Attempting to load image from file {tgz_file_path}")
result = subprocess.run(program_args, check=True, capture_output=True)
_LOGGER.debug(
f"Docker save stdout: {result.stdout.decode() if result.stdout is not None else '<None>' }")
_LOGGER.debug(
f"Docker save stderr: {result.stderr.decode() if result.stderr is not None else '<None>' }")
_LOGGER.debug("Image loaded.")
# os.remove(tgz_file_path) # To save resources, clean up tgz after load.
# _LOGGER.debug("Image tgz file removed.")
return result.stderr and get_name_from_load_output(result.stderr)
except Exception as e:
if isinstance(e, subprocess.CalledProcessError):
_LOGGER.debug(
f"Docker save stdout: {e.stdout.decode() if e.stdout is not None else '<None>' }")
_LOGGER.debug(
f"Docker save stderr: {e.stderr.decode() if e.stderr is not None else '<None>' }")
raise e

def get_name_from_load_output(stderr: str) -> Optional[str]:
pattern = re.compile(r"^Loaded image\: (.*)$")
match = pattern.match(stderr)
return match.group(1) if len(match.groups()) == 2 else None
return match.group(1) if len(match.groups()) == 2 else None
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from os import path

from orca_grader.docker_utils.images.image_loading import retrieve_image_tgz_for_sha, \
from orca_grader.docker_utils.images.image_loading import retrieve_image_tgz_for_unique_name, \
load_image_from_tgz
from orca_grader.docker_utils.images.utils import does_image_exist_locally

Expand All @@ -20,7 +20,7 @@ def setUpClass(cls) -> None:
return super().setUpClass()

def test_image_download_and_loading(self):
retrieve_image_tgz_for_sha("hello-world")
retrieve_image_tgz_for_unique_name("hello-world")
self.assertTrue(path.exists("hello-world.tgz"))
load_image_from_tgz("hello-world.tgz")
self.assertTrue(does_image_exist_locally("hello-world"))
Expand Down

0 comments on commit 688de72

Please sign in to comment.