summaryrefslogtreecommitdiffstats
path: root/ms/command-executor/src/main/python/command_executor_handler.py
diff options
context:
space:
mode:
authorJulien Fontaine <julien.fontaine@bell.ca>2020-04-20 11:53:41 -0400
committerJulien Fontaine <julien.fontaine@bell.ca>2020-05-05 18:17:52 -0400
commit888f2f78a580a3deccb66bef50f2375a04b39eb6 (patch)
tree2b76202c7a7f9f73cb36a1bc7d38496d939de9e3 /ms/command-executor/src/main/python/command_executor_handler.py
parentc86d7262d03b299dc635b3ba68e4a7c2c0fd6a6e (diff)
Truncate message published on Kafka / Spike: Define solution for logs separation
Refactoring of cmd-exec component - Improve display of error messages within the response - Fix gRPC buffer limitation (4Mb) by truncating error messages and cmd-exec logs if too heavy (>3Mb) Truncation of BP responses (<4Kb) before sending them in kafka audit topics. - Truncation if needed of error messages for every response - Truncation of cmd-exec logs in cmd-exec responses (Spike) Add a flag in the application.properties to enable/disable the display of cmd-exec responses on the BP side (Fix) Correction of BP processing with kafka regression (Fix) Changed default SSL Endpoint Algo Issue-ID: CCSDK-2326 Change-Id: If4d0e661117d1dd156cf19c95774824e754d870a Signed-off-by: Julien Fontaine <julien.fontaine@bell.ca>
Diffstat (limited to 'ms/command-executor/src/main/python/command_executor_handler.py')
-rw-r--r--ms/command-executor/src/main/python/command_executor_handler.py72
1 files changed, 37 insertions, 35 deletions
diff --git a/ms/command-executor/src/main/python/command_executor_handler.py b/ms/command-executor/src/main/python/command_executor_handler.py
index 1e6f03b81..0c476b23e 100644
--- a/ms/command-executor/src/main/python/command_executor_handler.py
+++ b/ms/command-executor/src/main/python/command_executor_handler.py
@@ -43,43 +43,48 @@ class CommandExecutorHandler():
def is_installed(self):
return os.path.exists(self.installed)
- def prepare_env(self, request, results):
+ def prepare_env(self, request):
+ results_log = []
if not self.is_installed():
create_venv_status = self.create_venv()
- if not create_venv_status["cds_is_successful"]:
- err_msg = "ERROR: failed to prepare environment for request {} due to error in creating virtual Python env. Original error {}".format(self.blueprint_id, create_venv_status["err_msg"])
+ if not create_venv_status[utils.CDS_IS_SUCCESSFUL_KEY]:
+ err_msg = "ERROR: failed to prepare environment for request {} due to error in creating virtual Python env. Original error {}".format(self.blueprint_id, create_venv_status[utils.ERR_MSG_KEY])
self.logger.error(err_msg)
- return utils.build_ret_data(False, err_msg)
+ return utils.build_ret_data(False, error=err_msg)
activate_venv_status = self.activate_venv()
- if not activate_venv_status["cds_is_successful"]:
- err_msg = "ERROR: failed to prepare environment for request {} due Python venv_activation. Original error {}".format(self.blueprint_id, activate_venv_status["err_msg"])
+ if not activate_venv_status[utils.CDS_IS_SUCCESSFUL_KEY]:
+ err_msg = "ERROR: failed to prepare environment for request {} due Python venv_activation. Original error {}".format(self.blueprint_id, activate_venv_status[utils.ERR_MSG_KEY])
self.logger.error(err_msg)
- return utils.build_ret_data(False, err_msg)
+ return utils.build_ret_data(False, error=err_msg)
try:
with open(self.installed, "w+") as f:
- if not self.install_packages(request, CommandExecutor_pb2.pip, f, results):
- return utils.build_ret_data(False, "ERROR: failed to prepare environment for request {} during pip package install.".format(self.blueprint_id))
+ if not self.install_packages(request, CommandExecutor_pb2.pip, f, results_log):
+ err_msg = "ERROR: failed to prepare environment for request {} during pip package install.".format(self.blueprint_id)
+ return utils.build_ret_data(False, results_log=results_log, error=err_msg)
f.write("\r\n") # TODO: is \r needed?
- results.append("\n")
- if not self.install_packages(request, CommandExecutor_pb2.ansible_galaxy, f, results):
- return utils.build_ret_data(False, "ERROR: failed to prepare environment for request {} during Ansible install.".format(self.blueprint_id))
+ results_log.append("\n")
+ if not self.install_packages(request, CommandExecutor_pb2.ansible_galaxy, f, results_log):
+ err_msg = "ERROR: failed to prepare environment for request {} during Ansible install.".format(self.blueprint_id)
+ return utils.build_ret_data(False, results_log=results_log, error=err_msg)
except Exception as ex:
err_msg = "ERROR: failed to prepare environment for request {} during installing packages. Exception: {}".format(self.blueprint_id, ex)
self.logger.error(err_msg)
- return utils.build_ret_data(False, err_msg)
+ return utils.build_ret_data(False, error=err_msg)
else:
try:
with open(self.installed, "r") as f:
- results.append(f.read())
+ results_log.append(f.read())
except Exception as ex:
- return utils.build_ret_data(False, "ERROR: failed to prepare environment during reading 'installed' file {}. Exception: {}".format(self.installed, ex))
+ err_msg="ERROR: failed to prepare environment during reading 'installed' file {}. Exception: {}".format(self.installed, ex)
+ return utils.build_ret_data(False, error=err_msg)
# deactivate_venv(blueprint_id)
- return utils.build_ret_data(True, "")
+ return utils.build_ret_data(True, results_log=results_log)
- def execute_command(self, request, results):
- payload_result = {}
+ def execute_command(self, request):
+ results_log = []
+ result = {}
# workaround for when packages are not specified, we may not want to go through the install step
# can just call create_venv from here.
if not self.is_installed():
@@ -87,16 +92,14 @@ class CommandExecutorHandler():
try:
if not self.is_installed():
create_venv_status = self.create_venv
- if not create_venv_status["cds_is_successful"]:
- err_msg = "{} - Failed to execute command during venv creation. Original error: {}".format(self.blueprint_id, create_venv_status["err_msg"])
- results.append(err_msg)
- return utils.build_ret_data(False, err_msg)
+ if not create_venv_status[utils.CDS_IS_SUCCESSFUL_KEY]:
+ err_msg = "{} - Failed to execute command during venv creation. Original error: {}".format(self.blueprint_id, create_venv_status[utils.ERR_MSG_KEY])
+ return utils.build_ret_data(False, error=err_msg)
activate_response = self.activate_venv()
- if not activate_response["cds_is_successful"]:
- orig_error = activate_response["err_msg"]
+ if not activate_response[utils.CDS_IS_SUCCESSFUL_KEY]:
+ orig_error = activate_response[utils.ERR_MSG_KEY]
err_msg = "{} - Failed to execute command during environment activation. Original error: {}".format(self.blueprint_id, orig_error)
- results.append(err_msg) #TODO: get rid of results and just rely on the return data struct.
- return utils.build_ret_data(False, err_msg)
+ return utils.build_ret_data(False, error=err_msg)
cmd = "cd " + self.venv_home
@@ -131,26 +134,25 @@ class CommandExecutorHandler():
payload = '\n'.join(payload_section)
msg = email.parser.Parser().parsestr(payload)
for part in msg.get_payload():
- payload_result = json.loads(part.get_payload())
+ result = json.loads(part.get_payload())
if output and not is_payload_section:
self.logger.info(output.strip())
- results.append(output.strip())
+ results_log.append(output.strip())
else:
payload_section.append(output.strip())
rc = newProcess.poll()
except Exception as e:
err_msg = "{} - Failed to execute command. Error: {}".format(self.blueprint_id, e)
- self.logger.info(err_msg)
- results.append(e)
- payload_result.update(utils.build_ret_data(False, err_msg))
- return payload_result
+ return utils.build_ret_data(False, results=result, results_log=results_log, error=err_msg)
# deactivate_venv(blueprint_id)
#Since return code is only used to check if it's zero (success), we can just return success flag instead.
self.logger.debug("python return_code : {}".format(rc))
- is_execution_successful = rc == 0
- payload_result.update(utils.build_ret_data(is_execution_successful, ""))
- return payload_result
+ if rc == 0:
+ return utils.build_ret_data(True, results=result, results_log=results_log)
+ else:
+ err_msg = "{} - Something wrong happened during command execution. See execute command logs for more information.".format(self.blueprint_id)
+ return utils.build_ret_data(False, results=result, results_log=results_log, error=err_msg)
def install_packages(self, request, type, f, results):
success = self.install_python_packages('UTILITY', results)