Skip to content
code.py 49.5 KiB
Newer Older
# -*- coding: utf-8 -*-

from __future__ import division, print_function

__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import six

from course.validation import ValidationError
import django.forms as forms
from django.core.exceptions import ObjectDoesNotExist
from django.utils.html import escape
from django.utils.translation import ugettext as _
from django.conf import settings
from relate.utils import StyledForm, string_concat
from course.page.base import (
        PageBaseWithTitle, markup_to_html, PageBaseWithValue,
        PageBaseWithHumanTextFeedback,
        AnswerFeedback, get_auto_feedback,
        get_editor_interaction_mode)
from course.constants import flow_permission
# DEBUGGING SWITCH:
# True for 'spawn containers' (normal operation)
Neal Davis's avatar
Neal Davis committed
# False for 'just connect to localhost:CODE_QUESTION_CONTAINER_PORT' for runpy'
Neal Davis's avatar
Neal Davis committed
SPAWN_CONTAINERS = True
Dong Zhuang's avatar
Dong Zhuang committed

    # prevents form submission with codemirror's empty textarea
    use_required_attribute = False

    def __init__(self, read_only, interaction_mode, initial_code,
            language_mode, data=None, *args, **kwargs):
        super(CodeForm, self).__init__(data, *args, **kwargs)
        from course.utils import get_codemirror_widget
        cm_widget, cm_help_text = get_codemirror_widget(
                read_only=read_only,

                # Automatically focus the text field once there has
                # been some input.
                autofocus=(
                    not read_only
                    and (data is not None and "answer" in data)))
        self.fields["answer"] = forms.CharField(required=True,
            initial=initial_code,
ifaint's avatar
ifaint committed
            widget=cm_widget,
            label=_("Answer"))
        self.style_codemirror_widget()

    def clean(self):
        # FIXME Should try compilation
        pass


Neal Davis's avatar
Neal Davis committed
CODE_QUESTION_CONTAINER_PORT = 9941
Dong Zhuang's avatar
Dong Zhuang committed
DOCKER_TIMEOUT = 15


class InvalidPingResponse(RuntimeError):
    pass


def request_run(run_req, run_timeout, image=None):
    from six.moves import http_client
    import docker
    import socket
    import errno
    from docker.errors import APIError as DockerAPIError

    if debug:
        def debug_print(s):
            print(s)
    else:
        def debug_print(s):
            pass

    command_path = '/opt/runpy/runpy'
    user = 'runpy'
        docker_url = getattr(settings, "RELATE_DOCKER_URL",
                "unix://var/run/docker.sock")
        docker_tls = getattr(settings, "RELATE_DOCKER_TLS_CONFIG",
                None)
        docker_cnx = docker.Client(
                base_url=docker_url,
                tls=docker_tls,
Dong Zhuang's avatar
Dong Zhuang committed
                timeout=DOCKER_TIMEOUT,
                version="1.19")
Neal Davis's avatar
Neal Davis committed
        if image is None:
            image = self.container_image
        debug_print( image )
Neal Davis's avatar
Neal Davis committed

        dresult = docker_cnx.create_container(
                image=image,
                command=[
                    "Memory": 384*10**6,
                    "MemorySwap": -1,
                    "PublishAllPorts": True,
                    # Do not enable: matplotlib stops working if enabled.
                    # "ReadonlyRootfs": True,

        container_id = dresult["Id"]
    else:
        container_id = None

    connect_host_ip = 'localhost'

    try:
        # FIXME: Prohibit networking

        if container_id is not None:
            docker_cnx.start(container_id)
            container_props = docker_cnx.inspect_container(container_id)
            (port_info,) = (container_props
                    ["NetworkSettings"]["Ports"]["%d/tcp" % CODE_QUESTION_CONTAINER_PORT])
            port_host_ip = port_info.get("HostIp")

            if port_host_ip != "0.0.0.0":
                connect_host_ip = port_host_ip

            port = int(port_info["HostPort"])
        else:

        from time import time, sleep
        start_time = time()

        # {{{ ping until response received

        from traceback import format_exc

        def check_timeout():
            if time() - start_time < DOCKER_TIMEOUT:
                sleep(0.1)
                # and retry
            else:
                return {
                        "result": "uncaught_error",
                        "message": "Timeout waiting for container.",
                        "traceback": "".join(format_exc()),
                        "exec_host": connect_host_ip,
                        }
                connection = http_client.HTTPConnection(connect_host_ip, port)

                connection.request('GET', '/ping')

                response = connection.getresponse()
                response_data = response.read().decode()
                if response_data != "OK":
                    raise InvalidPingResponse()

                break

Andreas Klöckner's avatar
Andreas Klöckner committed
            except (http_client.BadStatusLine, InvalidPingResponse):
Loading
Loading full blame...