05721430648a1692b7ea59f468c300fc4be70602
[ccsdk/cds.git] / ms / command-executor / src / main / python / command_executor_server.py
1 #!/usr/bin/python
2
3 #
4 # Copyright (C) 2019 - 2020 Bell Canada.
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, sys
20 import proto.CommandExecutor_pb2_grpc as CommandExecutor_pb2_grpc
21
22 from command_executor_handler import CommandExecutorHandler
23 import utils
24
25 class CommandExecutorServer(CommandExecutor_pb2_grpc.CommandExecutorServiceServicer):
26
27     def __init__(self):
28         self.logger = logging.getLogger(self.__class__.__name__)
29
30     def uploadBlueprint(self, request, context):
31         # handler for 'uploadBluleprint' call - extracts compressed cbaData to a  bpname/bpver/bpuuid dir.
32         blueprint_name_version_uuid = utils.blueprint_name_version_uuid(request)
33         extra = utils.getExtraLogData(request)
34         self.logger.info("{} - Received uploadBlueprint request".format(blueprint_name_version_uuid), extra=extra)
35         handler = CommandExecutorHandler(request)
36         return handler.uploadBlueprint(request)
37         
38     def prepareEnv(self, request, context):
39         blueprint_id = utils.blueprint_name_version_uuid(request)
40         extra = utils.getExtraLogData(request)
41         self.logger.info("{} - Received prepareEnv request".format(blueprint_id), extra=extra)
42         self.logger.info(request, extra=extra)
43
44         handler = CommandExecutorHandler(request)
45         prepare_env_response = handler.prepare_env(request)
46         if prepare_env_response[utils.CDS_IS_SUCCESSFUL_KEY]:
47             self.logger.info("{} - Package installation logs {}".format(blueprint_id, prepare_env_response[utils.RESULTS_LOG_KEY]), extra=extra)
48         else:
49             self.logger.info("{} - Failed to prepare python environment. {}".format(blueprint_id, prepare_env_response[utils.ERR_MSG_KEY]), extra=extra)
50         self.logger.info("Prepare Env Response returned : %s" % prepare_env_response, extra=extra)
51         return utils.build_grpc_response(request.requestId, prepare_env_response)
52
53     def executeCommand(self, request, context):
54         blueprint_id = utils.blueprint_name_version_uuid(request)
55         extra = utils.getExtraLogData(request)
56         self.logger.info("{} - Received executeCommand request".format(blueprint_id), extra=extra)
57         if os.environ.get('CE_DEBUG','false') == "true":
58             self.logger.info(request, extra=extra)
59
60         handler = CommandExecutorHandler(request)
61         exec_cmd_response = handler.execute_command(request)
62         if exec_cmd_response[utils.CDS_IS_SUCCESSFUL_KEY]:
63             self.logger.info("{} - Execution finished successfully.".format(blueprint_id), extra=extra)
64         else:
65             self.logger.info("{} - Failed to executeCommand. {}".format(blueprint_id, exec_cmd_response[utils.RESULTS_LOG_KEY]), extra=extra)
66
67         ret = utils.build_grpc_response(request.requestId, exec_cmd_response)
68         self.logger.info("Payload returned : {}".format(exec_cmd_response), extra=extra)
69
70         return ret