add ability to run tosca recipes over REST API

Change-Id: I87deca5847fecd7aed56966053f9b850d0bf7825
diff --git a/xos/api/utility/toscaapi.py b/xos/api/utility/toscaapi.py
new file mode 100644
index 0000000..1375969
--- /dev/null
+++ b/xos/api/utility/toscaapi.py
@@ -0,0 +1,59 @@
+import json
+import os
+import sys
+import traceback
+from django.http import HttpResponse
+from rest_framework.decorators import api_view
+from rest_framework.response import Response
+from rest_framework.reverse import reverse
+from rest_framework import serializers
+from rest_framework import generics
+from rest_framework import status
+from core.models import *
+from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
+from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
+
+# The Tosca engine expects to be run from /opt/xos/tosca/ or equivalent. It
+# needs some sys.path fixing up.
+import inspect
+currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
+toscadir = os.path.join(currentdir, "../../tosca")
+
+class ToscaViewSet(XOSViewSet):
+    base_name = "tosca"
+    method_name = "tosca"
+    method_kind = "viewset"
+
+    @classmethod
+    def get_urlpatterns(self, api_path="^"):
+        patterns = []
+
+        patterns.append( self.list_url("run/$", {"post": "post_run"}, "tosca_run") )
+
+        return patterns
+
+    def post_run(self, request):
+        result = []
+
+        recipe = request.data.get("recipe", None)
+
+        sys_path_save = sys.path
+        try:
+            sys.path.append(toscadir)
+            from tosca.engine import XOSTosca
+            xt = XOSTosca(recipe, parent_dir=toscadir, log_to_console=False)
+            xt.execute(request.user)
+        except:
+            return Response( {"error_text": traceback.format_exc()}, status=500 )
+        finally:
+            sys.path = sys_path_save
+
+
+        return Response( {"log_msgs": xt.log_msgs} )
+
+
+
+
+
+
+
diff --git a/xos/configurations/common/run_tosca.py b/xos/configurations/common/run_tosca.py
new file mode 100755
index 0000000..35a0fb7
--- /dev/null
+++ b/xos/configurations/common/run_tosca.py
@@ -0,0 +1,44 @@
+#! /usr/bin/env python
+
+import json
+import os
+import requests
+import sys
+import traceback
+
+def main():
+    global opencloud_auth
+
+    if len(sys.argv)!=5:
+        print >> sys.stderr, "syntax: run_tosca.py <port> <username> <password> <fn>"
+        sys.exit(-1)
+
+    port = int(sys.argv[1])
+    username = sys.argv[2]
+    password = sys.argv[3]
+    tosca_fn = sys.argv[4]
+
+    xos_auth=(username, password)
+
+    hostname = "127.0.0.1"
+    url = "http://%s:%d/api/utility/tosca/run/" % (hostname, port)
+
+    recipe = open(tosca_fn).read()
+
+    r = requests.post(url, data={"recipe": recipe}, auth=xos_auth)
+    if (r.status_code != 200):
+        print >> sys.stderr, "ERR: recieved status %d" % r.status_code
+        try:
+            print >> sys.stderr, r.json()["error_text"]
+        except:
+            traceback.print_exc("error while printing the error!")
+            print r.text
+        sys.exit(-1)
+
+    result = r.json()
+    if "log_msgs" in result:
+        print "\n".join(result["log_msgs"])+"\n"
+
+if __name__ == "__main__":
+    main()
+