From 8fc9a51032c0e0d2b0cb2c14067ba401211e6e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A0=E7=A3=8A?= Date: Wed, 14 Aug 2024 14:58:00 +0800 Subject: [PATCH] update --- config.py | 17 +++++++++- core.py | 45 +++++++++++++++------------ diag_cmd.py | 12 +++++-- handler/analyzer/analyze_flt_trace.py | 13 +++++--- handler/analyzer/analyze_log.py | 11 +++++-- handler/analyzer/analyze_parameter.py | 19 +++++++---- handler/analyzer/analyze_sql.py | 9 +++--- handler/analyzer/analyze_variable.py | 10 ++++-- handler/checker/check_list.py | 2 +- handler/gather/gather_ash_report.py | 5 +-- handler/rca/rca_handler.py | 13 +++++++- handler/rca/rca_list.py | 4 ++- result_type.py | 29 ++++++++++++----- update/update.py | 31 +++++++++++------- 14 files changed, 152 insertions(+), 68 deletions(-) diff --git a/config.py b/config.py index c3aa9ce1..bd32cbbe 100644 --- a/config.py +++ b/config.py @@ -262,7 +262,22 @@ def get_node_config(self, type, node_ip, config_item): class InnerConfigManager(Manager): - def __init__(self, stdio=None): + def __init__(self, stdio=None, inner_config_change_map=None): + if inner_config_change_map is None: + inner_config_change_map = {} inner_config_abs_path = os.path.abspath(INNER_CONFIG_FILE) super().__init__(inner_config_abs_path, stdio=stdio) self.config = self.load_config_with_defaults(DEFAULT_INNER_CONFIG) + if inner_config_change_map != {}: + self.config = self._change_inner_config(self.config, inner_config_change_map) + + def _change_inner_config(self, conf_map, change_conf_map): + for key, value in change_conf_map.items(): + if key in conf_map: + if isinstance(value, dict): + self._change_inner_config(conf_map[key], value) + else: + conf_map[key] = value + else: + conf_map[key] = value + return conf_map diff --git a/core.py b/core.py index 4aa6cf92..ebb52df9 100644 --- a/core.py +++ b/core.py @@ -51,6 +51,7 @@ from handler.gather.gather_tabledump import GatherTableDumpHandler from handler.gather.gather_parameters import GatherParametersHandler from handler.gather.gather_variables import GatherVariablesHandler +from result_type import ObdiagResult from telemetry.telemetry import telemetry from update.update import UpdateHandler from colorama import Fore, Style @@ -200,7 +201,7 @@ def gather_function(self, function_type, opt): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.stdio.print("{0} start ...".format(function_type)) self.set_context(function_type, 'gather', config) @@ -267,7 +268,7 @@ def gather_obproxy_log(self, opt): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.set_context_skip_cluster_conn('gather_obproxy_log', 'gather', config) handler = GatherObProxyLogHandler(self.context) @@ -282,33 +283,34 @@ def analyze_fuction(self, function_type, opt): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.stdio.print("{0} start ...".format(function_type)) if function_type == 'analyze_log': self.set_context(function_type, 'analyze', config) handler = AnalyzeLogHandler(self.context) - handler.handle() + return handler.handle() elif function_type == 'analyze_log_offline': self.set_context_skip_cluster_conn(function_type, 'analyze', config) handler = AnalyzeLogHandler(self.context) - handler.handle() + return handler.handle() elif function_type == 'analyze_flt_trace': self.set_context(function_type, 'analyze', config) handler = AnalyzeFltTraceHandler(self.context) - handler.handle() + return handler.handle() elif function_type == 'analyze_parameter_default': self.set_context(function_type, 'analyze', config) handler = AnalyzeParameterHandler(self.context, 'default') - handler.handle() + return handler.handle() elif function_type == 'analyze_parameter_diff': self.set_context_skip_cluster_conn(function_type, 'analyze', config) handler = AnalyzeParameterHandler(self.context, 'diff') - handler.handle() + return handler.handle() elif function_type == 'analyze_variable_diff': self.set_context(function_type, 'analyze', config) handler = AnalyzeVariableHandler(self.context, 'diff') - handler.handle() + return handler.handle() + # todo not support silent elif function_type == 'analyze_sql': self.set_context(function_type, 'analyze', config) handler = AnalyzeSQLHandler(self.context) @@ -319,13 +321,13 @@ def analyze_fuction(self, function_type, opt): handler.handle() else: self._call_stdio('error', 'Not support analyze function: {0}'.format(function_type)) - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='Not support analyze function: {0}'.format(function_type)) def check(self, opts): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.stdio.print("check start ...") self.set_context('check', 'check', config) @@ -347,59 +349,62 @@ def check(self, opts): observer_report_path = os.path.expanduser(observer_check_handler.report.get_report_path()) if os.path.exists(observer_report_path): self.stdio.print("Check observer finished. For more details, please run cmd'" + Fore.YELLOW + " cat {0} ".format(observer_check_handler.report.get_report_path()) + Style.RESET_ALL + "'") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={}) def check_list(self, opts): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.set_offline_context('check_list', 'check_list') handler = CheckListHandler(self.context) - handler.handle() + return handler.handle() def rca_run(self, opts): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.set_context('rca_run', 'rca_run', config) try: handler = RCAHandler(self.context) handler.handle() - handler.execute() + return handler.execute() except Exception as e: self.stdio.error("rca run Exception: {0}".format(e)) self.stdio.verbose(traceback.format_exc()) + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="rca run Exception: {0}".format(e)) def rca_list(self, opts): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.set_offline_context('rca_list', 'rca_list') handler = RcaScenesListHandler(context=self.context) - handler.handle() + return handler.handle() def update(self, opts): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.stdio.print("update start ...") self.set_offline_context('update', 'update') handler = UpdateHandler(self.context) - handler.execute() + return handler.execute() def config(self, opt): config = self.config_manager if not config: self._call_stdio('error', 'No such custum config') - return False + return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config') else: self.set_offline_context('config', 'config') config_helper = ConfigHelper(context=self.context) config_helper.build_configuration() + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"msg": "config success"}) diff --git a/diag_cmd.py b/diag_cmd.py index 7803fbca..5af3bc2a 100644 --- a/diag_cmd.py +++ b/diag_cmd.py @@ -273,13 +273,19 @@ def do_command(self): obdiag.set_cmds(self.cmds) ret = self._do_command(obdiag) if isinstance(ret, ObdiagResult) is False: - ObdiagResult(code=ObdiagResult.SERVER_ERROR_CODE, data={"err_info": "The return value of the command is not ObdiagResult. Please contact thebase community."}) - return + ROOT_IO.error('The return value of the command is not ObdiagResult. Please contact thebase community. The return value is: {0}'.format(ret)) + ret = ObdiagResult(code=ObdiagResult.SERVER_ERROR_CODE, error_data={"err_info": "The return value of the command is not ObdiagResult. Please contact thebase community."}) ret.set_trace_id(self.trace_id) - telemetry.put_data() + ret.set_command(self.prev_cmd) + # if silent is true ,print ret + if ROOT_IO.silent: + ROOT_IO.set_silent(False) + ROOT_IO.print(ret.get_result()) + ROOT_IO.set_silent(True) if self.has_trace: ROOT_IO.print('Trace ID: %s' % self.trace_id) ROOT_IO.print('If you want to view detailed obdiag logs, please run: {0} display-trace {1}'.format(obdiag_bin, self.trace_id)) + telemetry.put_data() except NotImplementedError: ROOT_IO.exception('command \'%s\' is not implemented' % self.prev_cmd) except SystemExit: diff --git a/handler/analyzer/analyze_flt_trace.py b/handler/analyzer/analyze_flt_trace.py index cac530a1..8d9dff6b 100644 --- a/handler/analyzer/analyze_flt_trace.py +++ b/handler/analyzer/analyze_flt_trace.py @@ -28,6 +28,7 @@ from common.tool import Util from common.tool import DirectoryUtil from common.tool import FileUtil +from result_type import ObdiagResult class AnalyzeFltTraceHandler(object): @@ -86,10 +87,10 @@ def init_option(self): def handle(self): if not self.init_option(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data='init option failed') if not self.init_config(): self.stdio.error('init config failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data='init config failed') local_store_parent_dir = os.path.join(self.gather_pack_dir, "obdiag_analyze_flt_result_{0}".format(TimeUtils.timestamp_to_filename_time(self.gather_timestamp))) self.stdio.verbose("Use {0} as pack dir.".format(local_store_parent_dir)) analyze_tuples = [] @@ -119,8 +120,7 @@ def handle_from_node(node): data = future.result() tree.build(data) # output tree - self.__output(local_store_parent_dir, tree, self.output) - return analyze_tuples + return self.__output(local_store_parent_dir, tree, self.output) def __handle_from_node(self, node, old_files, local_store_parent_dir): resp = {"skip": False, "error": ""} @@ -346,6 +346,11 @@ def __output(self, result_dir, tree, output_terminal=60): self.stdio.verbose('Result saved: {}'.format(os.path.abspath(filename))) last_info = "For more details, please run cmd \033[32m' cat {0} '\033[0m\n".format(filename) self.stdio.print(last_info) + result_info = "" + with open(filename, 'r', encoding='utf-8') as f: + for line in f: + result_info += line + return result_info def parse_file(self, file): self.stdio.verbose('parse file: {}'.format(file[1])) diff --git a/handler/analyzer/analyze_log.py b/handler/analyzer/analyze_log.py index 439f7928..1794f480 100644 --- a/handler/analyzer/analyze_log.py +++ b/handler/analyzer/analyze_log.py @@ -32,6 +32,7 @@ from common.tool import FileUtil from common.tool import TimeUtils import common.ssh_client.local_client as ssh_client_local_client +from result_type import ObdiagResult class AnalyzeLogHandler(BaseShellHandler): @@ -125,10 +126,10 @@ def init_option(self): def handle(self): if not self.init_option(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed") if not self.init_config(): self.stdio.error('init config failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init config failed") local_store_parent_dir = os.path.join(self.gather_pack_dir, "obdiag_analyze_pack_{0}".format(TimeUtils.timestamp_to_filename_time(TimeUtils.get_current_us_timestamp()))) self.stdio.verbose("Use {0} as pack dir.".format(local_store_parent_dir)) analyze_tuples = [] @@ -160,7 +161,11 @@ def handle_from_node(node): FileUtil.write_append(os.path.join(local_store_parent_dir, "result_details.txt"), field_names[n] + ": " + str(summary_details_list[m][n]) + extend) last_info = "For more details, please run cmd \033[32m' cat {0} '\033[0m\n".format(os.path.join(local_store_parent_dir, "result_details.txt")) self.stdio.print(last_info) - return analyze_tuples + # get info from local_store_parent_dir+/result_details.txt + analyze_info = "" + with open(os.path.join(local_store_parent_dir, "result_details.txt"), "r", encoding="utf-8") as f: + analyze_info = f.read() + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": analyze_info}) def __handle_from_node(self, node, local_store_parent_dir): resp = {"skip": False, "error": ""} diff --git a/handler/analyzer/analyze_parameter.py b/handler/analyzer/analyze_parameter.py index 26c6c31a..f4db52a8 100644 --- a/handler/analyzer/analyze_parameter.py +++ b/handler/analyzer/analyze_parameter.py @@ -26,6 +26,8 @@ import datetime from colorama import Fore, Style +from result_type import ObdiagResult + class AnalyzeParameterHandler(object): def __init__(self, context, analyze_type='default'): @@ -67,14 +69,14 @@ def handle(self): if self.analyze_type == 'default': if not self.init_option_default(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed") else: if not self.init_option_diff(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed") self.stdio.verbose("Use {0} as pack dir.".format(self.export_report_path)) DirectoryUtil.mkdir(path=self.export_report_path, stdio=self.stdio) - self.execute() + return self.execute() def check_file_valid(self): with open(self.parameter_file_name, 'r') as f: @@ -167,10 +169,11 @@ def analyze_parameter_default(self): fp.write(report_default_tb.get_string() + "\n") self.stdio.print(report_default_tb.get_string()) self.stdio.print("Analyze parameter default finished. For more details, please run cmd '" + Fore.YELLOW + " cat {0} ".format(file_name) + Style.RESET_ALL + "'") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": report_default_tb.get_string(), "file_name": file_name}) else: if self.parameter_file_name is None: self.stdio.error("the version of OceanBase is lower than 4.2.2, an initialization parameter file must be provided to find non-default values") - return + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="the version of OceanBase is lower than 4.2.2, an initialization parameter file must be provided to find non-default values") else: sql = '''select substr(version(),8), svr_ip,svr_port,zone,scope,TENANT_ID,name,value,section, EDIT_LEVEL, now(),'','' from GV$OB_PARAMETERS order by 5,2,3,4,7''' @@ -262,6 +265,7 @@ def alalyze_parameter_diff(self): file_name = self.export_report_path + '/parameter_diff_{0}.table'.format(date_format) fp = open(file_name, 'a+', encoding="utf8") is_empty = True + report_diff_tbs = [] for tenant, value_list in diff_parameter_dict.items(): if len(value_list) > 0: report_diff_tb = PrettyTable(["name", "diff"]) @@ -279,17 +283,20 @@ def alalyze_parameter_diff(self): fp.write(report_diff_tb.get_string() + "\n") self.stdio.print(report_diff_tb.get_string()) is_empty = False + report_diff_tbs.append(report_diff_tb.get_string()) fp.close() if not is_empty: self.stdio.print("Analyze parameter diff finished. For more details, please run cmd '" + Fore.YELLOW + " cat {0} ".format(file_name) + Style.RESET_ALL + "'") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": report_diff_tbs, "file_name": file_name}) else: self.stdio.print("Analyze parameter diff finished. All parameter settings are consistent among observers") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": "Analyze parameter diff finished. All parameter settings are consistent among observers"}) def execute(self): try: if self.analyze_type == 'default': - self.analyze_parameter_default() + return self.analyze_parameter_default() elif self.analyze_type == 'diff': - self.alalyze_parameter_diff() + return self.alalyze_parameter_diff() except Exception as e: self.stdio.error("parameter info analyze failed, error message: {0}".format(e)) diff --git a/handler/analyzer/analyze_sql.py b/handler/analyzer/analyze_sql.py index e6ab6374..ded4f21b 100644 --- a/handler/analyzer/analyze_sql.py +++ b/handler/analyzer/analyze_sql.py @@ -30,6 +30,7 @@ from handler.analyzer.sql.meta.sys_tenant_meta import SysTenantMeta from handler.gather.gather_scenes import GatherSceneHandler from common.command import get_observer_version +from result_type import ObdiagResult class AnalyzeSQLHandler(object): @@ -208,16 +209,16 @@ def handle(self): self.start_time = time.time() if not self.init_option(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed") if not self.init_inner_config(): self.stdio.error('init inner config failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init inner config failed") if not self.init_config(): self.stdio.error('init config failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init config failed") if not self.init_ob_version(): self.stdio.error('init ob version failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init ob version failed") self.init_db_connector() self.local_store_path = os.path.join(self.local_stored_parrent_path, "obdiag_analyze_sql_result_{0}_{1}.html".format(TimeUtils.timestamp_to_filename_time(self.from_timestamp), TimeUtils.timestamp_to_filename_time(self.to_timestamp))) self.stdio.print("use {0} as result store path.".format(self.local_store_path)) diff --git a/handler/analyzer/analyze_variable.py b/handler/analyzer/analyze_variable.py index e3bbc5d9..9199e77a 100644 --- a/handler/analyzer/analyze_variable.py +++ b/handler/analyzer/analyze_variable.py @@ -24,6 +24,8 @@ import datetime from colorama import Fore, Style +from result_type import ObdiagResult + class AnalyzeVariableHandler(object): def __init__(self, context, analyze_type='diff'): @@ -55,10 +57,10 @@ def __init__(self, context, analyze_type='diff'): def handle(self): if not self.init_option(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed") self.stdio.verbose("Use {0} as pack dir.".format(self.export_report_path)) DirectoryUtil.mkdir(path=self.export_report_path, stdio=self.stdio) - self.execute() + return self.execute() def check_file_valid(self): with open(self.variable_file_name, 'r') as f: @@ -149,11 +151,13 @@ def analyze_variable(self): self.stdio.print(Fore.RED + "Since {0}, the following variables have changed:".format(last_gather_time) + Style.RESET_ALL) self.stdio.print(report_default_tb.get_string()) self.stdio.print("Analyze variables changed finished. For more details, please run cmd '" + Fore.YELLOW + " cat {0} ".format(file_name) + Style.RESET_ALL + "'") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": report_default_tb.get_string()}) else: self.stdio.print("Analyze variables changed finished. Since {0}, No changes in variables".format(last_gather_time)) + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": "Since {0}, No changes in variables".format(last_gather_time)}) def execute(self): try: - self.analyze_variable() + return self.analyze_variable() except Exception as e: self.stdio.error("variable info analyze failed, error message: {0}".format(e)) diff --git a/handler/checker/check_list.py b/handler/checker/check_list.py index 668e612d..3a5c5d9d 100644 --- a/handler/checker/check_list.py +++ b/handler/checker/check_list.py @@ -76,6 +76,6 @@ def handle(self): } Util.print_title("check cases about {0}".format(target), stdio=self.stdio) Util.print_scene(cases_map, stdio=self.stdio) - return ObdiagResult(ObdiagResult.SUCCESS_CODE, result_map) + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data=result_map) except Exception as e: return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data=str(e)) diff --git a/handler/gather/gather_ash_report.py b/handler/gather/gather_ash_report.py index fc1e4eb1..883e58fa 100644 --- a/handler/gather/gather_ash_report.py +++ b/handler/gather/gather_ash_report.py @@ -22,6 +22,7 @@ from common.ob_connector import OBConnector from common.obdiag_exception import OBDIAGFormatException, OBDIAGException from common.tool import DirectoryUtil, TimeUtils, Util, StringUtils +from result_type import ObdiagResult from stdio import SafeStdio from colorama import Fore, Style @@ -60,10 +61,10 @@ def __init__(self, context, gather_pack_dir='./'): def handle(self): if not self.version_check(): self.stdio.error('version check failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="version check failed") if not self.init_option(): self.stdio.error('init option failed') - return False + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed") self.__init_report_path() self.execute() self.__print_result() diff --git a/handler/rca/rca_handler.py b/handler/rca/rca_handler.py index 4699c37d..d72715eb 100644 --- a/handler/rca/rca_handler.py +++ b/handler/rca/rca_handler.py @@ -33,6 +33,8 @@ from common.tool import StringUtils from colorama import Fore, Style +from result_type import ObdiagResult + class RCAHandler: def __init__(self, context): @@ -173,7 +175,7 @@ def execute(self): self.rca_scene.execute() except RCANotNeedExecuteException as e: self.stdio.warn("rca_scene.execute not need execute: {0}".format(e)) - pass + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, data="rca_scene.execute not need execute: {0}") except Exception as e: raise Exception("rca_scene.execute err: {0}".format(e)) try: @@ -181,6 +183,7 @@ def execute(self): except Exception as e: raise Exception("rca_scene.export_result err: {0}".format(e)) self.stdio.print("rca finished. For more details, the result on '" + Fore.YELLOW + self.get_result_path() + Style.RESET_ALL + "' \nYou can get the suggest by '" + Fore.YELLOW + "cat " + self.get_result_path() + "/record" + Style.RESET_ALL + "'") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"store_dir": self.get_result_path(), "record": self.rca_scene.records_data()}) class RcaScene: @@ -277,6 +280,14 @@ def export(self): f.write(record.export_suggest()) f.write("\n") + def records_data(self): + records_data = [] + for record in self.records: + if record.records is None or len(record.records) == 0: + continue + records_data.append({"record": record.records, "suggest": record.suggest}) + return records_data + class RCA_ResultRecord: def __init__(self, stdio=None): diff --git a/handler/rca/rca_list.py b/handler/rca/rca_list.py index 5df2cecc..bd6c3914 100644 --- a/handler/rca/rca_list.py +++ b/handler/rca/rca_list.py @@ -19,6 +19,7 @@ from common.constant import const from common.tool import DynamicLoading from common.tool import Util +from result_type import ObdiagResult class RcaScenesListHandler: @@ -63,9 +64,10 @@ def handle(self): self.stdio.verbose("list rca scenes") scene_info_list, scene_itme_list = self.get_all_scenes() Util.print_scene(scene_info_list, stdio=self.stdio) + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data=scene_info_list) except Exception as e: self.stdio.error("RcaScenesListHandler Exception: {0}".format(e)) - raise e + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="RcaScenesListHandler Exception:".format(e)) def __find_rca_files(self): files = [] diff --git a/result_type.py b/result_type.py index 2d144b2a..c5755f5a 100644 --- a/result_type.py +++ b/result_type.py @@ -1,3 +1,6 @@ +import json + + class ObdiagResult: # ObdiagResult is the result of obdiag. # It contains the code and result of obdiag. @@ -10,6 +13,7 @@ class ObdiagResult: SUCCESS_CODE = 200 def __init__(self, code, data=None, error_data=None): + self.command = None self.trace_id = None self.data = data self.error_data = error_data @@ -19,14 +23,16 @@ def __init__(self, code, data=None, error_data=None): self.code = str(code) else: raise TypeError("ObdiagResult code is not int. Please contact the Oceanbase community. ") - if isinstance(data, dict): - self.result = data - else: - raise TypeError("ObdiagResult result is not str. Please contact the Oceanbase community. ") - if isinstance(error_data, str): - self.error_data = error_data - else: - raise TypeError("ObdiagResult error_data is not str. Please contact the Oceanbase community. ") + if data is not None: + if isinstance(data, dict): + self.result = data + else: + raise TypeError("ObdiagResult data is not dict. Please contact the Oceanbase community. ") + if error_data is not None: + if isinstance(error_data, str): + self.error_data = error_data + else: + raise TypeError("ObdiagResult error_data is not str. Please contact the Oceanbase community. ") def set_trace_id(self, trace_id): if trace_id is None: @@ -35,3 +41,10 @@ def set_trace_id(self, trace_id): self.trace_id = trace_id else: raise TypeError("ObdiagResult trace_id is not str. Please contact the Oceanbase community. ") + + def set_command(self, command): + self.command = command + + def get_result(self): + result = {"code": self.code, "data": self.result, "error_data": self.error_data, "trace_id": self.trace_id, "command": self.command} + return json.dumps(result) diff --git a/update/update.py b/update/update.py index 125a230b..a59b1117 100644 --- a/update/update.py +++ b/update/update.py @@ -26,6 +26,8 @@ from common.version import OBDIAG_VERSION import yaml +from result_type import ObdiagResult + # for update obdiag files without obdiag class UpdateHandler: @@ -55,17 +57,16 @@ def execute(self): local_update_file_name = os.path.expanduser('~/.obdiag/data.tar') local_update_log_file_name = os.path.expanduser('~/.obdiag/data_version.yaml') if file_path and file_path != "": - self.handle_update_offline(file_path) - return + return self.handle_update_offline(file_path) if NetUtils.network_connectivity(remote_server) is False: self.stdio.warn("[update] network connectivity failed. Please check your network connection.") - return + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data={"msg": "network connectivity failed. Please check your network connection."}) NetUtils.download_file(remote_version_file_name, os.path.expanduser(local_version_file_name)) with open(local_version_file_name, 'r') as file: remote_data = yaml.safe_load(file) if remote_data.get("obdiag_version") is None: self.stdio.warn("obdiag_version is None. Do not perform the upgrade process.") - return + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data={"msg": "obdiag_version is None. Do not perform the upgrade process."}) else: self.remote_obdiag_version = remote_data["obdiag_version"].strip() if StringUtils.compare_versions_greater(self.remote_obdiag_version, self.local_obdiag_version): @@ -74,10 +75,17 @@ def execute(self): "remote_obdiag_version>local_obdiag_version. Unable to update dependency files, please upgrade " "obdiag. Do not perform the upgrade process.".format(self.remote_obdiag_version, self.local_obdiag_version) ) - return + return ObdiagResult( + ObdiagResult.SERVER_ERROR_CODE, + error_data={ + "msg": "remote_obdiag_version is {0}. local_obdiag_version is {1}. " + "remote_obdiag_version>local_obdiag_version. Unable to update dependency files, please upgrade " + "obdiag. Do not perform the upgrade process.".format(self.remote_obdiag_version, self.local_obdiag_version) + }, + ) if remote_data.get("remote_tar_sha") is None: self.stdio.warn("remote_tar_sha is None. Do not perform the upgrade process.") - return + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data={"msg": "remote_tar_sha is None. Do not perform the upgrade process."}) else: self.remote_tar_sha = remote_data["remote_tar_sha"] # need update? @@ -88,7 +96,7 @@ def execute(self): local_data = yaml.safe_load(file) if local_data.get("remote_tar_sha") is not None and local_data.get("remote_tar_sha") == self.remote_tar_sha: self.stdio.warn("[update] remote_tar_sha as local_tar_sha. No need to update.") - return + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"msg": "remote_tar_sha as local_tar_sha. No need to update."}) # get data_update_time if local_data.get("data_update_time") is not None and time.time() - local_data["data_update_time"] < 3600 * 24 * 7: self.stdio.warn("[update] data_update_time No need to update.") @@ -123,9 +131,10 @@ def execute(self): with open(os.path.expanduser("~/.obdiag/data_version.yaml"), 'w') as f: yaml.dump({"data_update_time": int(time.time()), "remote_tar_sha": self.remote_tar_sha}, f) self.stdio.print("[update] Successfully updated. The original data is stored in the *. d folder.") - return + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"msg": "Successfully updated. The original data is stored in the *. d folder."}) except Exception as e: self.stdio.warn('[update] Failed to update. Error message: {0}'.format(e)) + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="Failed to update. Error message: {0}".format(e)) def handle_update_offline(self, file): file = os.path.expanduser(file) @@ -133,10 +142,10 @@ def handle_update_offline(self, file): self.local_update_file_sha = FileUtil.calculate_sha256(file) if os.path.exists(file) is False: self.stdio.error('{0} does not exist.'.format(file)) - return + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="{0} does not exist.".format(file)) if not file.endswith('.tar'): self.stdio.error('{0} is not a tar file.'.format(file)) - return + return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="{0} is not a tar file.".format(file)) ## check_old_files if os.path.exists(os.path.expanduser("~/.obdiag/check.d")): shutil.rmtree(os.path.expanduser("~/.obdiag/check.d")) @@ -147,7 +156,6 @@ def handle_update_offline(self, file): shutil.rmtree(os.path.expanduser("~/.obdiag/gather.d")) if os.path.exists(os.path.expanduser("~/.obdiag/gather")): os.rename(os.path.expanduser("~/.obdiag/gather"), os.path.expanduser("~/.obdiag/gather.d")) - ## rca if os.path.exists(os.path.expanduser("~/.obdiag/rca.d")): shutil.rmtree(os.path.expanduser("~/.obdiag/rca.d")) @@ -159,3 +167,4 @@ def handle_update_offline(self, file): with open(os.path.expanduser("~/.obdiag/data_version.yaml"), 'w') as f: yaml.dump({"data_update_time": int(time.time()), "remote_tar_sha": self.remote_tar_sha}, f) self.stdio.print("[update] Successfully updated. The original data is stored in the *. d folder.") + return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"msg": "Successfully updated. The original data is stored in the *. d folder."})