3 # Copyright (C) 2019 Bell Canada.
4 # Modifications Copyright © 2018-2019 AT&T Intellectual Property.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from builtins import KeyboardInterrupt
19 from concurrent import futures
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
29 logger = logging.getLogger("Server")
31 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
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')
39 header_validator = RequestHeaderValidatorInterceptor(
40 'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
43 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)),
44 interceptors=(header_validator,))
46 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
47 BluePrintProcessingServer(configuration), server)
49 server.add_insecure_port('[::]:' + port)
52 logger.info("Command Executor Server started on %s" % port)
56 time.sleep(_ONE_DAY_IN_SECONDS)
57 except KeyboardInterrupt:
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'),
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)