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