Fix: Run both sonar and clm scans in parallel
[ccsdk/cds.git] / ms / py-executor / server.py
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 import logging
19 import os
20 import time
21 import yaml
22 from builtins import KeyboardInterrupt
23 from concurrent import futures
24 from pathlib import Path, PurePath
25
26 import grpc
27 from manager.servicer import ArtifactManagerServicer
28 from proto.BluePrintManagement_pb2_grpc import add_BluePrintManagementServiceServicer_to_server
29
30 from blueprints_grpc import BluePrintProcessing_pb2_grpc, ScriptExecutorConfiguration
31 from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer
32 from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor
33
34 logger = logging.getLogger("Server")
35
36 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
37
38
39 def serve(configuration: ScriptExecutorConfiguration):
40     port = configuration.script_executor_property('port')
41     authType = configuration.script_executor_property('authType')
42     maxWorkers = configuration.script_executor_property('maxWorkers')
43
44     if authType == 'tls-auth':
45         cert_chain_file = configuration.script_executor_property('certChain')
46         private_key_file = configuration.script_executor_property('privateKey')
47         logger.info("Setting GRPC server TLS authentication, cert file(%s) private key file(%s)", cert_chain_file,
48                     private_key_file)
49         # read in key and certificate
50         with open(cert_chain_file, 'rb') as f:
51             certificate_chain = f.read()
52         with open(private_key_file, 'rb') as f:
53             private_key = f.read()
54
55         # create server credentials
56         server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain),))
57
58         # create server
59         server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)))
60         BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
61             BluePrintProcessingServer(configuration), server
62         )
63         add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
64
65         # add secure port using credentials
66         server.add_secure_port('[::]:' + port, server_credentials)
67         server.start()
68     else:
69         logger.info("Setting GRPC server base authentication")
70         basic_auth = configuration.script_executor_property('token')
71         header_validator = RequestHeaderValidatorInterceptor(
72             'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
73             'Access denied!')
74         # create server with token authentication interceptors
75         server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)),
76                              interceptors=(header_validator,))
77         BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
78             BluePrintProcessingServer(configuration), server
79         )
80         add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
81
82         server.add_insecure_port('[::]:' + port)
83         server.start()
84
85     logger.info("Command Executor Server started on %s" % port)
86
87     try:
88         while True:
89             time.sleep(_ONE_DAY_IN_SECONDS)
90     except KeyboardInterrupt:
91         server.stop(0)
92
93
94 if __name__ == '__main__':
95     default_configuration_file = str(PurePath(Path().absolute(), "../../configuration.ini"))
96     supplied_configuration_file = os.environ.get("CONFIGURATION")
97     config_file = str(os.path.expanduser(Path(supplied_configuration_file or default_configuration_file)))
98
99     configuration = ScriptExecutorConfiguration(config_file)
100     log_file_name = configuration.script_executor_property('logFile')
101     log_file = os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "logging.yaml")
102     print(log_file)
103     with open(log_file) as log:
104         log_config = yaml.safe_load(log)
105         print(log_config)
106     logging_formater = log_config["formatters"]["default"]["format"]
107     print(log_config["loglevel"])
108     if log_config["loglevel"] == "debug":
109         loglevel = logging.DEBUG
110     elif log_config["loglevel"] == "info":
111         loglevel = logging.INFO
112     elif log_config["loglevel"] == "error":
113         loglevel = logging.ERROR
114     logging.basicConfig(filename=log_file_name,
115                         level=loglevel,
116                         format=logging_formater)
117     console = logging.handlers.RotatingFileHandler(log_file_name, maxBytes=log_config["logfilesize"],
118                                                    backupCount=log_config["rollovercount"])
119     
120     console.setLevel(loglevel)
121     formatter = logging.Formatter(logging_formater)
122     console.setFormatter(formatter)
123     logging.getLogger('').addHandler(console)
124     serve(configuration)
125