diff --git a/README.rst b/README.rst index b9888d2..764378a 100644 --- a/README.rst +++ b/README.rst @@ -6,7 +6,7 @@ This is a plugin for `Tutor `_ that provides the Installation ------------ -The plugin is currently bundled with the `binary releases of Tutor `_. If you have installed Tutor from source, you will have to install this plugin from source, too:: +The plugin is currently bundled with the `binary releases of Tutor `__. If you have installed Tutor from source, you will have to install this plugin from source, too:: pip install tutor-xqueue @@ -14,15 +14,89 @@ Then, to enable this plugin, run:: tutor plugins enable xqueue +Usage +----- + +In the Open edX studio, edit a course and add a new "Advanced blank problem" ("Problem" 🠆 "Advanced" 🠆 "Advanced blank problem"). Then, click "Edit" and copy-paste the following in the editor:: + +``` + + + + + + + # students write your program here + print "" + + + print "hello world" + + + {"output": "hello world", "max_length": 2} + + + + +``` + +.. note:: + The queue name must be "openedx". + +Save and publish the created unit. Then, access the unit from the LMS and attempt to answer the problem. The answer is sent to the Xqueue service. If you know how to use the Xqueue API, you can access it at http(s)://xqueue.LMS_HOST (in production) or http://xqueue.localhost (in development). However, the Xqueue API is a bit awkward to use. Tutor provides a simple command-line interface to interact with the Xqueue service. + +Count the number of submissions that need to be graded:: + + $ tutor xqueue submissions -u http://xqueue.localhost count + { + "content": 0, + "return_code": 0 + } + +.. note:: + By default, ``tutor xqueue submissions`` will hit the Xqueue API running at http(s)://xqueue.LMS_HOST. When running locally, you will want to interact with http://xqueue.localhost. To do so, you should pass the ``--url=http://xqueue.localhost`` option to the CLI. + +Show the first submission that should be graded:: + + $ tutor xqueue submissions show + { + "id": 1, + "key": "692c2896cdfc8bdc2d073bc3b3daf928", + "body": { + "student_info": "{\"random_seed\": 1, \"anonymous_student_id\": \"af46c9d6c05627aee45257d155ec0b79\", \"submission_time\": \"20200504101653\"}", + "grader_payload": "\n {\"output\": \"hello world\", \"max_length\": 2}\n ", + "student_response": " # students write your program here\r\n print \"42\"\r\n " + }, + "return_code": 0 + } + +Grade the submission (in this case, mark it as being correct):: + + $ tutor xqueue submissions grade 1 692c2896cdfc8bdc2d073bc3b3daf928 0.9 true "Good job\!" + { + "content": "", + "return_code": 0 + } + +.. warning:: + When running locally, the Xqueue client will report back to the LMS at the production url, so there are great chances that it will not work. A workaround is to manually edit the Submission record stored in the Xqueue database and modify the ``lms_callback_url`` attribute, but this is for advanced users only. + +The submission should then appear as correct with the message that you provided on the command line: + +.. image:: https://github.com/overhangio/tutor-xqueue/raw/master/screenshots/correctanswer.png + :alt: Correct answer + :align: center + Configuration ------------- - ``XQUEUE_AUTH_PASSWORD`` (default: ``"{{ 8|random_string }}"``) -- ``XQUEUE_MYSQL_PASSWORD`` (default: ``"{{ 8|random_string }}"``) -- ``XQUEUE_SECRET_KEY`` (default: ``"{{ 24|random_string }}"``) -- ``XQUEUE_DOCKER_IMAGE`` (default: ``"overhangio/openedx-xqueue:{{ TUTOR_VERSION }}"``) - ``XQUEUE_AUTH_USERNAME`` (default: ``"lms"``) +- ``XQUEUE_DOCKER_IMAGE`` (default: ``"overhangio/openedx-xqueue:{{ TUTOR_VERSION }}"``) +- ``XQUEUE_HOST`` (default: ``"xqueue.{{ LMS_HOST }}"``) +- ``XQUEUE_MYSQL_PASSWORD`` (default: ``"{{ 8|random_string }}"``) - ``XQUEUE_MYSQL_DATABASE`` (default: ``"xqueue"`` - ``XQUEUE_MYSQL_USERNAME`` (default: ``"xqueue"``) +- ``XQUEUE_SECRET_KEY`` (default: ``"{{ 24|random_string }}"``) These values can be modified with ``tutor config save --set PARAM_NAME=VALUE`` commands. diff --git a/screenshots/correctanswer.png b/screenshots/correctanswer.png new file mode 100644 index 0000000..bf55528 Binary files /dev/null and b/screenshots/correctanswer.png differ diff --git a/setup.py b/setup.py index d88a41c..e7624c6 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ setup( packages=find_packages(exclude=["tests*"]), include_package_data=True, python_requires=">=3.5", - install_requires=["tutor-openedx"], + install_requires=["tutor-openedx", "requests"], entry_points={"tutor.plugin.v0": ["xqueue = tutorxqueue.plugin"]}, classifiers=[ "Development Status :: 3 - Alpha", diff --git a/tutorxqueue/__about__.py b/tutorxqueue/__about__.py index 0a8da88..d3ec452 100644 --- a/tutorxqueue/__about__.py +++ b/tutorxqueue/__about__.py @@ -1 +1 @@ -__version__ = "0.1.6" +__version__ = "0.2.0" diff --git a/tutorxqueue/patches/https-create b/tutorxqueue/patches/https-create new file mode 100644 index 0000000..9d5d58d --- /dev/null +++ b/tutorxqueue/patches/https-create @@ -0,0 +1 @@ +certbot certonly --standalone -n --agree-tos -m admin@{{ LMS_HOST }} -d {{ XQUEUE_HOST }} \ No newline at end of file diff --git a/tutorxqueue/patches/k8s-deployments b/tutorxqueue/patches/k8s-deployments index 7cc0f14..79796e7 100644 --- a/tutorxqueue/patches/k8s-deployments +++ b/tutorxqueue/patches/k8s-deployments @@ -18,7 +18,7 @@ spec: - name: xqueue image: {{ DOCKER_REGISTRY }}{{ XQUEUE_DOCKER_IMAGE }} ports: - - containerPort: 8040 + - containerPort: 8000 env: - name: DJANGO_SETTINGS_MODULE value: xqueue.tutor diff --git a/tutorxqueue/patches/k8s-ingress-rules b/tutorxqueue/patches/k8s-ingress-rules new file mode 100644 index 0000000..1c5b7c3 --- /dev/null +++ b/tutorxqueue/patches/k8s-ingress-rules @@ -0,0 +1,6 @@ +- host: {{ XQUEUE_HOST }} + http: + paths: + - backend: + serviceName: nginx + servicePort: {% if ACTIVATE_HTTPS %}443{% else %}80{% endif %} \ No newline at end of file diff --git a/tutorxqueue/patches/k8s-ingress-tls-hosts b/tutorxqueue/patches/k8s-ingress-tls-hosts new file mode 100644 index 0000000..50e7b1a --- /dev/null +++ b/tutorxqueue/patches/k8s-ingress-tls-hosts @@ -0,0 +1 @@ +- {{ XQUEUE_HOST }} \ No newline at end of file diff --git a/tutorxqueue/patches/k8s-services b/tutorxqueue/patches/k8s-services index d4a227e..057d2c6 100644 --- a/tutorxqueue/patches/k8s-services +++ b/tutorxqueue/patches/k8s-services @@ -6,7 +6,7 @@ metadata: spec: type: NodePort ports: - - port: 8040 + - port: 8000 protocol: TCP selector: app.kubernetes.io/name: xqueue \ No newline at end of file diff --git a/tutorxqueue/patches/nginx-extra b/tutorxqueue/patches/nginx-extra new file mode 100644 index 0000000..56d9317 --- /dev/null +++ b/tutorxqueue/patches/nginx-extra @@ -0,0 +1,37 @@ +### Xqueue +upstream xqueue-backend { + server xqueue:8000 fail_timeout=0; +} + +{% if ACTIVATE_HTTPS %} +server { + server_name {{ XQUEUE_HOST }}; + listen 80; + return 301 https://$server_name$request_uri; +} +{% endif %} + +server { + {% if ACTIVATE_HTTPS %}listen 443 {{ "" if WEB_PROXY else "ssl" }};{% else %}listen 80;{% endif %} + server_name xqueue.localhost {{ XQUEUE_HOST }}; + + {% if ACTIVATE_HTTPS and not WEB_PROXY %} + ssl_certificate /etc/letsencrypt/live/{{ XQUEUE_HOST }}/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/{{ XQUEUE_HOST }}/privkey.pem; + {% endif %} + + # Disables server version feedback on pages and in headers + server_tokens off; + + location / { + {% if not WEB_PROXY %} + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Port $server_port; + proxy_set_header X-Forwarded-For $remote_addr; + {% endif %} + proxy_set_header Host $http_host; + proxy_redirect off; + + proxy_pass http://xqueue-backend; + } +} \ No newline at end of file diff --git a/tutorxqueue/patches/openedx-common-settings b/tutorxqueue/patches/openedx-common-settings index cdb3f10..7335b47 100644 --- a/tutorxqueue/patches/openedx-common-settings +++ b/tutorxqueue/patches/openedx-common-settings @@ -3,5 +3,5 @@ XQUEUE_INTERFACE = { "username": "{{ XQUEUE_AUTH_USERNAME }}", "password": "{{ XQUEUE_AUTH_PASSWORD }}" }, - "url": "http://xqueue:8040" + "url": "http://xqueue:8000" } \ No newline at end of file diff --git a/tutorxqueue/plugin.py b/tutorxqueue/plugin.py index e7946fb..fe22cc1 100644 --- a/tutorxqueue/plugin.py +++ b/tutorxqueue/plugin.py @@ -1,7 +1,13 @@ from glob import glob +import json import os +import click import pkg_resources +import requests + +from tutor import config as tutor_config +from tutor.exceptions import TutorError from .__about__ import __version__ @@ -14,8 +20,9 @@ config = { }, "defaults": { "VERSION": __version__, - "DOCKER_IMAGE": "overhangio/openedx-xqueue:{{ XQUEUE_VERSION }}", "AUTH_USERNAME": "lms", + "DOCKER_IMAGE": "overhangio/openedx-xqueue:{{ XQUEUE_VERSION }}", + "HOST": "xqueue.{{ LMS_HOST }}", "MYSQL_DATABASE": "xqueue", "MYSQL_USERNAME": "xqueue", }, @@ -39,3 +46,144 @@ def patches(): content = patch_file.read() all_patches[name] = content return all_patches + + +@click.group(help="Interact with the Xqueue server") +def command(): + pass + + +@click.group(help="List and grade submissions") +@click.pass_obj +@click.option("-q", "--queue", default="openedx", show_default=True, help="Queue name") +@click.option( + "-u", + "--url", + help="Xqueue server base url. By default, this value will be defined from the plugin configuration.", +) +def submissions(context, queue, url): + context.queue = queue + context.url = url + + +@click.command(name="count", help="Count submissions in queue") +@click.pass_obj +def count_submissions(context): + print_result(context, "count_submissions", context.queue) + + +@click.command(name="show", help="Show last submission") +@click.pass_obj +def show_submission(context): + print_result(context, "show_submission", context.queue) + + +@click.command(name="grade", help="Grade a specific submission") +@click.argument("submission_id") +@click.argument("submission_key") +@click.argument("grade", type=click.FLOAT) +@click.argument("correct", type=click.BOOL) +@click.argument("message") +@click.pass_obj +def grade_submission(context, submission_id, submission_key, grade, correct, message): + print_result( + context, + "grade_submission", + submission_id, + submission_key, + grade, + correct, + message, + ) + + +def print_result(context, client_func_name, *args, **kwargs): + user_config = tutor_config.load(context.root) + client = Client(user_config, url=context.url) + func = getattr(client, client_func_name) + result = func(*args, **kwargs) + print(json.dumps(result, indent=2)) + + +class Client: + def __init__(self, user_config, url=None): + self._session = None + self.username = user_config["XQUEUE_AUTH_USERNAME"] + self.password = user_config["XQUEUE_AUTH_PASSWORD"] + + self.base_url = url + if not self.base_url: + scheme = "https" if user_config["ACTIVATE_HTTPS"] else "http" + host = host or user_config["XQUEUE_HOST"] + self.base_url = "{}://{}".format(scheme, host) + self.login() + + @property + def session(self): + if self._session is None: + self._session = requests.Session() + return self._session + + def url(self, endpoint): + # Don't forget to add a trailing slash to all endpoints: this is how xqueue + # works... + return self.base_url + endpoint + + def login(self): + response = self.request( + "/xqueue/login/", + method="POST", + data={"username": self.username, "password": self.password}, + ) + message = response.get("content") + if message != "Logged in": + raise TutorError( + "Could not login to xqueue server at {}. Response: '{}'".format( + self.base_url, message + ) + ) + + def show_submission(self, queue): + response = self.request("/xqueue/get_submission/", params={"queue_name": queue}) + if response["return_code"] != 0: + return response + data = json.loads(response["content"]) + header = json.loads(data["xqueue_header"]) + submission_body = json.loads(data["xqueue_body"]) + submission_id = header["submission_id"] + submission_key = header["submission_key"] + return { + "id": submission_id, + "key": submission_key, + "body": submission_body, + "return_code": response["return_code"], + } + + def count_submissions(self, queue): + return self.request("/xqueue/get_queuelen/", params={"queue_name": queue}) + + def grade_submission(self, submission_id, submission_key, grade, correct, msg): + return self.request( + "/xqueue/put_result/", + method="POST", + data={ + "xqueue_header": json.dumps( + {"submission_id": submission_id, "submission_key": submission_key} + ), + "xqueue_body": json.dumps( + {"correct": correct, "score": grade, "msg": msg} + ), + }, + ) + + def request(self, endpoint, method="GET", data=None, params=None): + func = getattr(self.session, method.lower()) + response = func(self.url(endpoint), data=data, params=params) + # TODO handle errors >= 400 and non-parsable json responses + return response.json() + + +submissions.add_command(count_submissions) +submissions.add_command(show_submission) +submissions.add_command(grade_submission) +command.add_command(submissions) diff --git a/tutorxqueue/templates/xqueue/apps/settings/tutor.py b/tutorxqueue/templates/xqueue/apps/settings/tutor.py index 423cf62..33dfc1c 100644 --- a/tutorxqueue/templates/xqueue/apps/settings/tutor.py +++ b/tutorxqueue/templates/xqueue/apps/settings/tutor.py @@ -1,21 +1,27 @@ from .settings import * +ALLOWED_HOSTS = [ + "{{ XQUEUE_HOST }}", + "xqueue", + "xqueue.localhost", +] + DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", "HOST": "{{ MYSQL_HOST }}", - "PORT": {{MYSQL_PORT}}, + "PORT": {{ MYSQL_PORT }}, "NAME": "{{ XQUEUE_MYSQL_DATABASE }}", "USER": "{{ XQUEUE_MYSQL_USERNAME }}", "PASSWORD": "{{ XQUEUE_MYSQL_PASSWORD }}", - "OPTIONS": { - "init_command": "SET sql_mode='STRICT_TRANS_TABLES'", - }, + "OPTIONS": {"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",}, } } LOGGING = get_logger_config(log_dir="/openedx/data/", logging_env="tutor", dev_env=True) +LOGGING["loggers"][""]["handlers"].append("console") SECRET_KEY = "{{ XQUEUE_SECRET_KEY }}" -XQUEUE_USERS = {"{{ XQUEUE_AUTH_USERNAME }}": "{{ XQUEUE_AUTH_PASSWORD}}"} +USERS = {"{{ XQUEUE_AUTH_USERNAME }}": "{{ XQUEUE_AUTH_PASSWORD}}"} +XQUEUES = {"openedx": None} diff --git a/tutorxqueue/templates/xqueue/build/xqueue/Dockerfile b/tutorxqueue/templates/xqueue/build/xqueue/Dockerfile index 45273ae..8168fce 100644 --- a/tutorxqueue/templates/xqueue/build/xqueue/Dockerfile +++ b/tutorxqueue/templates/xqueue/build/xqueue/Dockerfile @@ -11,5 +11,5 @@ WORKDIR /openedx/xqueue RUN pip install -r requirements.txt -EXPOSE 8040 -CMD gunicorn --workers=2 --name xqueue --bind=0.0.0.0:8040 --max-requests=1000 xqueue.wsgi:application +EXPOSE 8000 +CMD gunicorn --workers=2 --name xqueue --bind=0.0.0.0:8000 --max-requests=1000 xqueue.wsgi:application diff --git a/tutorxqueue/templates/xqueue/hooks/xqueue/init b/tutorxqueue/templates/xqueue/hooks/xqueue/init index f0c3d44..d65fc76 100644 --- a/tutorxqueue/templates/xqueue/hooks/xqueue/init +++ b/tutorxqueue/templates/xqueue/hooks/xqueue/init @@ -1 +1,2 @@ ./manage.py migrate +./manage.py update_users