Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import, print_function |
Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 17 | import json |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 18 | import os |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 19 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 20 | from grpc_client import GRPC_Client |
| 21 | from klein import Klein |
| 22 | from multistructlog import create_logger |
| 23 | from tosca.default import TOSCA_DEFS_DIR |
| 24 | from tosca.parser import TOSCA_Parser |
| 25 | from xosconfig import Config |
| 26 | |
| 27 | log = create_logger(Config().get("logging")) |
| 28 | |
| 29 | |
| 30 | BANNER = r""" |
| 31 | _ ______ _____ __________ _____ _________ |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 32 | | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ | |
| 33 | | / / / /\__ \ / / / / / /\__ \/ / / /| | |
| 34 | / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ | |
| 35 | /_/|_\____//____/ /_/ \____//____/\____/_/ |_| |
| 36 | """ |
| 37 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 38 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 39 | class TOSCA_WebServer: |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 40 | |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 41 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 42 | template_dir = os.path.join(current_dir, "templates/") |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 43 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 44 | app = Klein() |
| 45 | |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 46 | def execute_tosca(self, parser): |
| 47 | parser.execute() |
| 48 | if parser.delete: |
| 49 | response_text = "Deleted models: %s" % str(parser.ordered_models_name) |
Matteo Scandolo | c468075 | 2018-05-17 12:16:30 -0700 | [diff] [blame] | 50 | else: |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 51 | response_text = "Created models: %s" % str(parser.ordered_models_name) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 52 | return response_text |
| 53 | |
| 54 | def errorCallback(self, failure, request): |
| 55 | request.setResponseCode(500) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 56 | try: |
Matteo Scandolo | b2eff2c | 2018-03-02 12:58:10 -0800 | [diff] [blame] | 57 | f = failure.getErrorMessage() |
Scott Baker | 30c3d75 | 2018-10-04 13:08:41 -0700 | [diff] [blame] | 58 | if f.startswith("XOSPermissionDenied"): |
| 59 | request.setResponseCode(401) |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 60 | log.info("[XOS-TOSCA] Error while loading TOSCA: \n\n", failure=f) |
Matteo Scandolo | b2eff2c | 2018-03-02 12:58:10 -0800 | [diff] [blame] | 61 | return f |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 62 | except Exception: |
| 63 | log.info("[XOS-TOSCA] Fatal Error: \n\n", failure=failure) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 64 | return "Internal server error, please report this along with the failed recipe." |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 65 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 66 | @app.route("/", methods=["GET"]) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 67 | def index(self, request): |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 68 | request.responseHeaders.addRawHeader(b"content-type", b"application/json") |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 69 | tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith(".")] |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 70 | |
| 71 | response = {} |
| 72 | for d in tosca_defs: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 73 | name = d.replace(".yaml", "") |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 74 | response[name] = "/custom_type/%s" % name |
| 75 | return json.dumps(response) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 76 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 77 | @app.route("/custom_type/<name>") |
| 78 | def custom_type(self, request, name): |
| 79 | request.responseHeaders.addRawHeader(b"content-type", b"text/plain") |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 80 | custom_type = open(TOSCA_DEFS_DIR + "/" + name + ".yaml").read() |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 81 | return custom_type |
| 82 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 83 | @app.route("/run", methods=["POST"]) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 84 | def run(self, request): |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 85 | return self._handle_post(request, delete=False) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 86 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 87 | @app.route("/delete", methods=["POST"]) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 88 | def delete(self, request): |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 89 | return self._handle_post(request, delete=True) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 90 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 91 | def _handle_post(self, request, delete=False): |
| 92 | |
| 93 | headers = request.getAllHeaders() |
| 94 | username = headers["xos-username"] |
| 95 | password = headers["xos-password"] |
| 96 | recipe = request.content.read() |
| 97 | |
| 98 | parser = TOSCA_Parser(recipe, username, password, delete=delete) |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 99 | d = GRPC_Client().create_secure_client(username, password, parser) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 100 | tosca_execution = d.addCallback(self.execute_tosca) |
| 101 | tosca_execution.addErrback(self.errorCallback, request) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 102 | return d |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 103 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 104 | def __init__(self): |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 105 | self.app.run("0.0.0.0", "9102") |