Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (C) 2019 Bell Canada. |
| 4 | # Modifications Copyright © 2018-2019 AT&T Intellectual Property. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | from builtins import KeyboardInterrupt |
| 19 | from concurrent import futures |
| 20 | import logging |
| 21 | import time |
| 22 | import grpc |
| 23 | from pathlib import Path, PurePath |
| 24 | from blueprints_grpc import BluePrintProcessing_pb2_grpc |
| 25 | from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor |
| 26 | from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer |
| 27 | from blueprints_grpc import ScriptExecutorConfiguration |
| 28 | |
| 29 | logger = logging.getLogger("Server") |
| 30 | |
| 31 | _ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
| 32 | |
| 33 | |
| 34 | def serve(configuration: ScriptExecutorConfiguration): |
| 35 | port = configuration.script_executor_property('port') |
| 36 | basic_auth = configuration.script_executor_property('auth') |
| 37 | maxWorkers = configuration.script_executor_property('maxWorkers') |
| 38 | |
| 39 | header_validator = RequestHeaderValidatorInterceptor( |
| 40 | 'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED, |
| 41 | 'Access denied!') |
| 42 | |
| 43 | server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)), |
| 44 | interceptors=(header_validator,)) |
| 45 | |
| 46 | BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server( |
| 47 | BluePrintProcessingServer(configuration), server) |
| 48 | |
| 49 | server.add_insecure_port('[::]:' + port) |
| 50 | server.start() |
| 51 | |
| 52 | logger.info("Command Executor Server started on %s" % port) |
| 53 | |
| 54 | try: |
| 55 | while True: |
| 56 | time.sleep(_ONE_DAY_IN_SECONDS) |
| 57 | except KeyboardInterrupt: |
| 58 | server.stop(0) |
| 59 | |
| 60 | |
| 61 | if __name__ == '__main__': |
| 62 | config_file = str(PurePath(Path().absolute())) + '/configuration.ini' |
| 63 | configuration = ScriptExecutorConfiguration(config_file) |
| 64 | logging_formater = '%(asctime)s - %(name)s - %(threadName)s - %(levelname)s - %(message)s' |
| 65 | logging.basicConfig(filename=configuration.script_executor_property('logFile'), |
| 66 | level=logging.DEBUG, |
| 67 | format=logging_formater) |
| 68 | console = logging.StreamHandler() |
| 69 | console.setLevel(logging.INFO) |
| 70 | formatter = logging.Formatter(logging_formater) |
| 71 | console.setFormatter(formatter) |
| 72 | logging.getLogger('').addHandler(console) |
| 73 | serve(configuration) |