|
| 1 | +# This module is adapted from mooc-grader/scripts/docker-run.py. |
| 2 | +# This module copies the directories, which should be mounted to |
| 3 | +# the grading container, to the path /tmp/aplus. This is required because |
| 4 | +# Docker does not support bind mounts from a container into another container. |
| 5 | +# The bind mount directory must be located in the host (outside containers). |
| 6 | +# The path /tmp/aplus must be mounted from the host into the run-mooc-grader |
| 7 | +# container (in docker-compose.yml). |
| 8 | + |
| 9 | +import logging |
| 10 | +from typing import Any, Dict, Tuple |
| 11 | +import os |
| 12 | +import os.path |
| 13 | +import shutil |
| 14 | + |
| 15 | +import docker |
| 16 | +docker_client = docker.from_env() |
| 17 | + |
| 18 | +from access.config import ConfigError |
| 19 | + |
| 20 | + |
| 21 | +logger = logging.getLogger("runner.docker") |
| 22 | + |
| 23 | + |
| 24 | +def get_host_path_and_copy(mounts: Dict[str, str], path: str, submission_id: str): |
| 25 | + path = os.path.realpath(path) |
| 26 | + for k,v in mounts.items(): |
| 27 | + if path.startswith(k): |
| 28 | + host_path = path.replace(k, v, 1) |
| 29 | + # host_path is under /tmp/aplus |
| 30 | + # submission_id is inserted into the host_path so that the path |
| 31 | + # does not collide with other submissions in case their grading |
| 32 | + # is running simultaneously. |
| 33 | + host_path = os.path.join(v, submission_id, os.path.relpath(host_path, v)) |
| 34 | + if os.path.isfile(path): |
| 35 | + os.makedirs(os.path.dirname(host_path), mode=0o777, exist_ok=True) |
| 36 | + shutil.copy2(path, host_path) |
| 37 | + else: |
| 38 | + shutil.rmtree(host_path, ignore_errors=True) |
| 39 | + shutil.copytree(path, host_path, dirs_exist_ok=True) |
| 40 | + return host_path |
| 41 | + |
| 42 | + raise ConfigError(f"Could not find where {path} is mounted") |
| 43 | + |
| 44 | + |
| 45 | +def run( |
| 46 | + submission_id: str, |
| 47 | + host_url: str, |
| 48 | + readwrite_mounts: Dict[str, str], |
| 49 | + readonly_mounts: Dict[str, str], |
| 50 | + image: str, |
| 51 | + cmd: str, |
| 52 | + settings: Dict[str, Any], |
| 53 | + **kwargs, |
| 54 | + ) -> Tuple[int, str, str]: |
| 55 | + """ |
| 56 | + Grades the submission asynchronously and returns (return_code, out, err). |
| 57 | + out and err as in stdout and stderr output of a program. |
| 58 | + """ |
| 59 | + network = settings.get("network") |
| 60 | + if "mounts" not in settings: |
| 61 | + return 1, "", 'Missing "mounts" in settings!' |
| 62 | + |
| 63 | + volumes = { |
| 64 | + get_host_path_and_copy(settings["mounts"], k, submission_id): {"bind": v, "mode": "rw"} |
| 65 | + for k,v in readwrite_mounts.items() |
| 66 | + } |
| 67 | + volumes.update({ |
| 68 | + get_host_path_and_copy(settings["mounts"], k, submission_id): {"bind": v, "mode": "ro"} |
| 69 | + for k,v in readonly_mounts.items() |
| 70 | + }) |
| 71 | + |
| 72 | + try: |
| 73 | + container = docker_client.containers.run( |
| 74 | + image, |
| 75 | + cmd, |
| 76 | + network = network if network else "bridge", |
| 77 | + remove = True, |
| 78 | + detach = True, |
| 79 | + environment = { |
| 80 | + "SID": submission_id, |
| 81 | + "REC": host_url, |
| 82 | + }, |
| 83 | + volumes = volumes, |
| 84 | + ) |
| 85 | + |
| 86 | + return 0, f"{', '.join(container.image.tags)} - {container.name} - {container.short_id}", "" |
| 87 | + except Exception as e: |
| 88 | + logger.exception("An exception while trying to run grading container") |
| 89 | + return 1, "", str(e) |
0 commit comments