Merge "Added Complex type to Resource source"
[ccsdk/cds.git] / ms / command-executor / src / main / python / server.py
1
2 #!/usr/bin/python
3
4 #
5 # Copyright (C) 2019 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
25 import grpc
26
27 import proto.CommandExecutor_pb2_grpc as CommandExecutor_pb2_grpc
28
29 from request_header_validator_interceptor import RequestHeaderValidatorInterceptor
30 from command_executor_server import CommandExecutorServer
31
32 logger = logging.getLogger("Server")
33
34 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
35
36
37 def serve():
38     port = sys.argv[1]
39     basic_auth = sys.argv[2] + ' ' + sys.argv[3]
40
41     header_validator = RequestHeaderValidatorInterceptor(
42         'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
43         'Access denied!')
44
45     server = grpc.server(
46         futures.ThreadPoolExecutor(max_workers=10),
47         interceptors=(header_validator,))
48
49     CommandExecutor_pb2_grpc.add_CommandExecutorServiceServicer_to_server(
50         CommandExecutorServer(), server)
51
52     server.add_insecure_port('[::]:' + port)
53     server.start()
54
55     logger.info("Command Executor Server started on %s" % port)
56
57     try:
58         while True:
59             time.sleep(_ONE_DAY_IN_SECONDS)
60     except KeyboardInterrupt:
61         server.stop(0)
62
63
64 if __name__ == '__main__':
65     logging_formater = '%(asctime)s - %(name)s - %(threadName)s - %(levelname)s - %(message)s'
66     logging.basicConfig(filename='/opt/app/onap/logs/application.log', 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()