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 | |
| 42 | def execute_tosca(self, recipe): |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 43 | self.parser.execute() |
Matteo Scandolo | c468075 | 2018-05-17 12:16:30 -0700 | [diff] [blame] | 44 | if self.parser.delete: |
| 45 | response_text = "Deleted models: %s" % str(self.parser.ordered_models_name) |
| 46 | else: |
| 47 | response_text = "Created models: %s" % str(self.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() |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 54 | log.info("[XOS-TOSCA] Error while loading TOSCA: \n\n", failure=f) |
Matteo Scandolo | b2eff2c | 2018-03-02 12:58:10 -0800 | [diff] [blame] | 55 | return f |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 56 | except Exception: |
| 57 | log.info("[XOS-TOSCA] Fatal Error: \n\n", failure=failure) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 58 | return "Internal server error, please report this along with the failed recipe." |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 59 | |
| 60 | @app.route('/', methods=['GET']) |
| 61 | def index(self, request): |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 62 | request.responseHeaders.addRawHeader(b"content-type", b"application/json") |
| 63 | tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')] |
| 64 | |
| 65 | response = {} |
| 66 | for d in tosca_defs: |
| 67 | name = d.replace('.yaml', '') |
| 68 | response[name] = "/custom_type/%s" % name |
| 69 | return json.dumps(response) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 70 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 71 | @app.route("/custom_type/<name>") |
| 72 | def custom_type(self, request, name): |
| 73 | request.responseHeaders.addRawHeader(b"content-type", b"text/plain") |
| 74 | custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read() |
| 75 | return custom_type |
| 76 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 77 | @app.route('/run', methods=['POST']) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 78 | def run(self, request): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 79 | recipe = request.content.read() |
| 80 | headers = request.getAllHeaders() |
| 81 | username = headers['xos-username'] |
| 82 | password = headers['xos-password'] |
| 83 | |
| 84 | d = GRPC_Client().create_secure_client(username, password, recipe) |
| 85 | self.parser = TOSCA_Parser(recipe, username, password) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 86 | tosca_execution = d.addCallback(self.execute_tosca) |
| 87 | tosca_execution.addErrback(self.errorCallback, request) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 88 | return d |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 89 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 90 | @app.route('/delete', methods=['POST']) |
| 91 | def delete(self, request): |
| 92 | recipe = request.content.read() |
| 93 | headers = request.getAllHeaders() |
| 94 | username = headers['xos-username'] |
| 95 | password = headers['xos-password'] |
| 96 | |
| 97 | d = GRPC_Client().create_secure_client(username, password, recipe) |
| 98 | self.parser = TOSCA_Parser(recipe, username, password, delete=True) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 99 | tosca_execution = d.addCallback(self.execute_tosca) |
| 100 | tosca_execution.addErrback(self.errorCallback, request) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 101 | return d |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 102 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 103 | def __init__(self): |
Matteo Scandolo | 5c0af1b | 2017-07-05 14:51:21 -0700 | [diff] [blame] | 104 | self.app.run('0.0.0.0', '9102') |