From fa8c0a82f9d5f358425c858d1bae96fe74416388 Mon Sep 17 00:00:00 2001 From: dmitriy kalinin Date: Wed, 1 Jun 2016 20:34:36 -0700 Subject: [PATCH] WIP add openwisk python --- bin/compile | 3 ++ bin/detect | 2 +- serverless/Procfile | 1 + serverless/requirements.txt | 16 +++++++++ serverless/serverless.py | 71 +++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 serverless/Procfile create mode 100644 serverless/requirements.txt create mode 100644 serverless/serverless.py diff --git a/bin/compile b/bin/compile index a8b45a5a6..4f2657349 100755 --- a/bin/compile +++ b/bin/compile @@ -27,6 +27,9 @@ BUILD_DIR=$1 CACHE_DIR=$2 ENV_DIR=$3 +# todo requirements by the function are not specified. +( cd $ROOT_DIR/serverless && cp Procfile serverless.py requirements.txt $BUILD_DIR/ ) + # Use microconda if environment.yml exists and exit if [ -f $BUILD_DIR/environment.yml ]; then echo "----------------- USING CONDA BUILDPACK -----------------" diff --git a/bin/detect b/bin/detect index caebbf9e6..bc822bf88 100755 --- a/bin/detect +++ b/bin/detect @@ -16,7 +16,7 @@ BUILD_DIR=$1 BP=$(dirname $(dirname $0)) # Exit early if app is clearly not Python. -if [ ! -f $BUILD_DIR/requirements.txt ] && [ ! -f $BUILD_DIR/setup.py ] && [ ! -f $BUILD_DIR/environment.yml ]; then +if [ ! -f $BUILD_DIR/requirements.txt ] && [ ! -f $BUILD_DIR/setup.py ] && [ ! -f $BUILD_DIR/environment.yml ] && [ ! -f $BUILD_DIR/function.py ]; then exit 1 fi diff --git a/serverless/Procfile b/serverless/Procfile new file mode 100644 index 000000000..4e7d16168 --- /dev/null +++ b/serverless/Procfile @@ -0,0 +1 @@ +web: python serverless.py \ No newline at end of file diff --git a/serverless/requirements.txt b/serverless/requirements.txt new file mode 100644 index 000000000..daa659e24 --- /dev/null +++ b/serverless/requirements.txt @@ -0,0 +1,16 @@ +Flask==0.11 +Jinja2==2.8 +MarkupSafe==0.23 +Werkzeug==0.11.10 +argparse==1.2.1 +chardet==2.1.1 +click==6.6 +colorama==0.2.5 +html5lib==0.999 +itsdangerous==0.24 +requests==2.2.1 +six==1.5.2 +urllib3==1.7.1 +wheel==0.24.0 +gevent==1.1.1 +wsgiref==0.1.2 \ No newline at end of file diff --git a/serverless/serverless.py b/serverless/serverless.py new file mode 100644 index 000000000..7144178f8 --- /dev/null +++ b/serverless/serverless.py @@ -0,0 +1,71 @@ +# +# Copyright 2015-2016 IBM Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import sys +import os +import json +import subprocess +import codecs +import traceback +import flask +from gevent.wsgi import WSGIServer + +proxy = flask.Flask(__name__) +proxy.debug = False +with open("function.py") as f: + flask.g = f.read() + +@proxy.route("/run", methods=['POST']) +def run(): + message = flask.request.get_json(force=True,silent=True) + + if not message or not isinstance(message, dict): + flask.abort(403) + + if not "value" in message: + flask.abort(403) + + value = message["value"] + + if not isinstance(value, dict): + flask.abort(403) + + # initialize the namespace for the execution + namespace = {} + result = None + try: + exec(flask.g, namespace) + exec("param = " + json.dumps(value), namespace) + exec("fun = main(param)", namespace) + result = namespace['fun'] + except Exception: + traceback.print_exc(file = sys.stderr) + sys.stdout.flush() + sys.stderr.flush() + if result and isinstance(result, dict): + response = flask.jsonify(result) + response.status_code = 200 + return response + else: + response = flask.jsonify({ "error": "the action did not return a dictionary", "action_output": result }) + response.status_code = 502 + return response + + +# start server in a forever loop +if __name__ == "__main__": + server = WSGIServer(('', int(os.getenv("PORT", 8080))), proxy, log=None) + server.serve_forever()