Newer
Older
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.
"""
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
# DEBUGGING SWITCH:
# True for 'spawn containers' (normal operation)
# False for 'just connect to localhost:RUNPY_PORT' for runpy'
SPAWN_CONTAINERS_FOR_RUNPY = False
# {{{ python code question
class PythonCodeForm(StyledForm):
Andreas Klöckner
committed
# prevents form submission with codemirror's empty textarea
use_required_attribute = False
Andreas Klöckner
committed
def __init__(self, read_only, interaction_mode, initial_code, *args, **kwargs):
super(PythonCodeForm, self).__init__(*args, **kwargs)
Andreas Klöckner
committed
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,
Andreas Klöckner
committed
help_text=cm_help_text,
def clean(self):
# FIXME Should try compilation
pass
class InvalidPingResponse(RuntimeError):
pass
def request_python_run(run_req, run_timeout, image=None):
import json
import docker
import socket
import errno
from docker.errors import APIError as DockerAPIError
debug = False
if debug:
def debug_print(s):
else:
def debug_print(s):
pass
docker_timeout = 15
if SPAWN_CONTAINERS_FOR_RUNPY:
docker_url = getattr(settings, "RELATE_DOCKER_URL",
"unix://var/run/docker.sock")
docker_tls = getattr(settings, "RELATE_DOCKER_TLS_CONFIG",
None)
base_url=docker_url,
tls=docker_tls,
timeout=docker_timeout,
version="1.19")
dresult = docker_cnx.create_container(
image=image,
command=[
"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" % 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:
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()),
connection = http_client.HTTPConnection(connect_host_ip, port)
connection.request('GET', '/ping')
response = connection.getresponse()
response_data = response.read().decode()
raise InvalidPingResponse()
break
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
Loading
Loading full blame...