grim/anzu

Parents
Children bd21b0a9e90a
Initial revision, lots of work, not a lot of anything working
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,3 @@
+syntax: regexp
+\/__pycache__\/
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Dockerfile Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,15 @@
+FROM python:3-alpine
+
+RUN apk add --no-cache gcc libc-dev luajit-dev
+
+RUN mkdir -p /usr/src/app
+WORKDIR /usr/src/app
+
+COPY requirements.txt /usr/src/app/
+RUN pip install --no-cache-dir -r requirements.txt
+
+COPY . /usr/src/app
+
+ENV PYTHONPATH=`pwd`/src
+
+CMD python -m anzu
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/docker-compose.yml Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,7 @@
+anzu:
+ build: .
+ links:
+ - redis:redis
+
+redis:
+ image: redis:3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/requirements.txt Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,3 @@
+flask
+celery
+lupa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/__init__.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,5 @@
+import pkg_resources
+
+
+def get_resource_path(path):
+ return pkg_resources.resource_filename(__name__, path)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/__main__.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,3 @@
+from anzu.app import app
+
+app.run(port=11011, debug=True)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/app.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,39 @@
+import os
+
+from celery import Celery
+
+from flask import Flask
+
+from anzu.scenario import Scenario
+
+
+def make_celery(app):
+ celery_config = {
+ 'BROKER_URL': os.environ['REDIS_TCP_PORT_6379'].replace('tcp:', 'redis:'),
+ 'CELERY_RESULT_BACKEND': os.environ['REDIS_TCP_PORT_6379'].replace('tcp:', 'redis:'),
+ 'CELERY_TASK_SERIALIZER': 'json',
+ 'CELERY_RESULT_SERIALIZER': 'json',
+ 'CELERY_ACCEPT_CONTENT': ['json'],
+ 'CELERY_ENABLE_UTC': True,
+ }
+
+ celery = Celery(app.import_name)
+ celery.conf.update(app.config)
+ celery.conf.update(celery_config)
+
+ TaskBase = celery.Task
+ class ContextTask(TaskBase):
+ abstract = True
+ def __call__(self, *args, **kwargs):
+ with app.app_context():
+ return TaskBase.__call__(self, *args, **kwargs)
+
+ celery.Task = ContextTask
+
+ return celery
+
+
+app = Flask(__name__)
+celery = make_celery(app)
+
+app.register_blueprint(Scenario.as_blueprint())
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/data/scenarios/example.lua Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,5 @@
+socket:write("hello, my name is anzu, what's yours?\n")
+local name = socket:read(10)
+socket:write("nice to meet you " .. name .. "!\n")
+socket:write("goodbye!\n")
+socket:close()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/job.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,7 @@
+from anzu.views import BlueprintView
+
+class Job(BlueprintView):
+ url_prefix = '/job/'
+ url_rules = {
+ 'run': ['/', ('POST',)]
+ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/scenario.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,19 @@
+import os
+
+from flask import jsonify
+
+import anzu
+
+from anzu.views import BlueprintView
+
+class Scenario(BlueprintView):
+ url_prefix = '/scenario/'
+ url_rules = {
+ 'index': ['', ('GET',), {'id': None}],
+ 'select': ['/<id>', ('GET',)]
+ }
+
+ def get(self, id):
+ return jsonify({
+ 'data': os.listdir(anzu.get_resource_path('data/scenarios')),
+ })
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/views.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,36 @@
+from flask import Blueprint
+from flask.views import MethodView
+
+
+# this is ripped from https://gist.github.com/mattupstate/9257466
+class BlueprintView(MethodView):
+ endpoint = None
+ url_prefix = None
+ url_rules = {}
+
+ @classmethod
+ def as_blueprint(cls, name=None):
+ name = name or cls.endpoint or cls.__name__
+
+ bp = Blueprint(name, cls.__module__, url_prefix=cls.url_prefix)
+
+ for endpoint, options in cls.url_rules.items():
+ url_rule = options
+ methods = ('GET',)
+ defaults = {}
+
+ if len(options) == 2:
+ url_rule, methods = options
+
+ elif len(options) == 3:
+ url_rule, methods, defaults = options
+
+ bp.add_url_rule(
+ url_rule,
+ endpoint=endpoint,
+ methods=methods,
+ defaults=defaults,
+ view_func=cls.as_view(endpoint)
+ )
+
+ return bp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/anzu/worker.py Wed Feb 17 22:41:58 2016 -0600
@@ -0,0 +1,1 @@
+from concurrent.futures import ThreadPoolExecutor