4 # Copyright (C) 2019 Bell Canada.
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.
20 import proto.CommandExecutor_pb2_grpc as CommandExecutor_pb2_grpc
22 from command_executor_handler import CommandExecutorHandler
25 class CommandExecutorServer(CommandExecutor_pb2_grpc.CommandExecutorServiceServicer):
28 self.logger = logging.getLogger(self.__class__.__name__)
30 def prepareEnv(self, request, context):
31 blueprint_id = utils.get_blueprint_id(request)
32 self.logger.info("{} - Received prepareEnv request".format(blueprint_id))
33 self.logger.info(request)
35 handler = CommandExecutorHandler(request)
36 prepare_env_response = handler.prepare_env(request)
37 if prepare_env_response[utils.CDS_IS_SUCCESSFUL_KEY]:
38 self.logger.info("{} - Package installation logs {}".format(blueprint_id, prepare_env_response[utils.RESULTS_LOG_KEY]))
40 self.logger.info("{} - Failed to prepare python environment. {}".format(blueprint_id, prepare_env_response[utils.ERR_MSG_KEY]))
41 self.logger.info("Prepare Env Response returned : %s" % prepare_env_response)
42 return utils.build_grpc_response(request.requestId, prepare_env_response)
44 def executeCommand(self, request, context):
45 blueprint_id = utils.get_blueprint_id(request)
46 self.logger.info("{} - Received executeCommand request".format(blueprint_id))
47 if os.environ.get('CE_DEBUG','false') == "true":
48 self.logger.info(request)
50 handler = CommandExecutorHandler(request)
51 exec_cmd_response = handler.execute_command(request)
52 if exec_cmd_response[utils.CDS_IS_SUCCESSFUL_KEY]:
53 self.logger.info("{} - Execution finished successfully.".format(blueprint_id))
55 self.logger.info("{} - Failed to executeCommand. {}".format(blueprint_id, exec_cmd_response[utils.RESULTS_LOG_KEY]))
57 ret = utils.build_grpc_response(request.requestId, exec_cmd_response)
58 self.logger.info("Payload returned : {}".format(exec_cmd_response))