CCSDK-3531 improve cmd-exec returned err msg
[ccsdk/cds.git] / ms / command-executor / src / main / python / server.py
1
2 #!/usr/bin/python
3
4 #
5 # Copyright (C) 2019 - 2020 Bell Canada.
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 from builtins import KeyboardInterrupt
20 from concurrent import futures
21 import logging
22 import time
23 import sys
24 import utils
25
26 import grpc
27
28 import proto.CommandExecutor_pb2_grpc as CommandExecutor_pb2_grpc
29
30 from request_header_validator_interceptor import RequestHeaderValidatorInterceptor
31 from command_executor_server import CommandExecutorServer
32
33 logger = logging.getLogger("Server")
34
35 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
36
37
38 def serve():
39     port = sys.argv[1]
40     basic_auth = sys.argv[2] + ' ' + sys.argv[3]
41
42     header_validator = RequestHeaderValidatorInterceptor(
43         'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
44         'Access denied!')
45
46     server = grpc.server(
47         futures.ThreadPoolExecutor(max_workers=15),
48         interceptors=(header_validator,))
49
50     CommandExecutor_pb2_grpc.add_CommandExecutorServiceServicer_to_server(
51         CommandExecutorServer(), server)
52
53     server.add_insecure_port('[::]:' + port)
54     server.start()
55
56     logger.info("Command Executor Server started on %s" % port, extra=utils.getExtraLogData())
57
58     try:
59         while True:
60             time.sleep(_ONE_DAY_IN_SECONDS)
61     except KeyboardInterrupt:
62         server.stop(0)
63
64
65 if __name__ == '__main__':
66     logging_formater = '%(asctime)s|%(request_id)s|%(subrequest_id)s|%(originator_id)s|%(threadName)s|%(name)s|%(levelname)s|%(message)s'
67     logging.basicConfig(level=logging.INFO, format=logging_formater)
68     serve()