Skip to content
code.py 41.9 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.utils import translation
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
# {{{ python code question

class PythonCodeForm(StyledForm):
    # prevents form submission with codemirror's empty textarea
    use_required_attribute = False

    def __init__(self, read_only, interaction_mode, initial_code, *args, **kwargs):
        super(PythonCodeForm, self).__init__(*args, **kwargs)

        from course.utils import get_codemirror_widget
        cm_widget, cm_help_text = get_codemirror_widget(
                language_mode="python",
                interaction_mode=interaction_mode,
                read_only=read_only)
        self.fields["answer"] = forms.CharField(required=True,
            initial=initial_code,
ifaint's avatar
ifaint committed
            widget=cm_widget,
            label=_("Answer"))

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


Andreas Klöckner's avatar
Andreas Klöckner committed
RUNPY_PORT = 9941


class InvalidPingResponse(RuntimeError):
    pass


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

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

    docker_timeout = 15

    # DEBUGGING SWITCH: 1 for 'spawn container', 0 for 'static container'
    if 1:
        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,
                timeout=docker_timeout,
                version="1.19")
Andreas Klöckner's avatar
Andreas Klöckner committed
            image = settings.RELATE_DOCKER_RUNPY_IMAGE

        dresult = docker_cnx.create_container(
                image=image,
                command=[
Andreas Klöckner's avatar
Andreas Klöckner committed
                    "/opt/runpy/runpy",
                    "Memory": 384*10**6,
                    "MemorySwap": -1,
                    "PublishAllPorts": True,
                    # Do not enable: matplotlib stops working if enabled.
                    # "ReadonlyRootfs": True,
Andreas Klöckner's avatar
Andreas Klöckner committed
                user="runpy")

        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" % RUNPY_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:
Andreas Klöckner's avatar
Andreas Klöckner committed
            port = RUNPY_PORT

        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):
                ct_res = check_timeout()
                if ct_res is not None:
                    return ct_res

            except socket.error as e:
                if e.errno in [errno.ECONNRESET, errno.ECONNREFUSED]:
                    ct_res = check_timeout()
                    if ct_res is not None:
                        return ct_res

        # }}}

        debug_print("PING SUCCESSFUL")
Loading
Loading full blame...