Skip to content

Commit

Permalink
2.4.0 silent print (#388)
Browse files Browse the repository at this point in the history
* update io: add stderr

* update io: add stderr

* add --inner_config
add print_type

* support remote_client_sudo

* build test package

* fix --inner_config

* fix io stream

* test

* test

* test

* test

* test

* 取消测试分支打包

* test

* test

* test

* test

* test

* add new io buffer

* add new io buffer

* build test package

* silent change

* delete init.sh cp gather dir more

* update

* update

* update

* update

* update

* update

* update

* update

* fix some bug

* delete build tag

* delete silent note

* code fix
  • Loading branch information
wayyoungboy authored Aug 21, 2024
1 parent 2a80ff5 commit 6138999
Show file tree
Hide file tree
Showing 40 changed files with 449 additions and 183 deletions.
16 changes: 8 additions & 8 deletions common/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def get_obproxy_version(context):
obproxy_version_info = ssh_client.exec_cmd(cmd)
stdio.verbose("get obproxy version, run cmd = [{0}] ".format(cmd))
if obproxy_version_info is not None:
ob_version = re.findall(r'[(]OceanBase.(.+? +?)[)]', obproxy_version_info)
pattern = r"(\d+\.\d+\.\d+\.\d+)"
ob_version = re.findall(pattern, obproxy_version_info)
if len(ob_version) > 0:
return ob_version[0]
else:
Expand All @@ -295,7 +296,6 @@ def get_obproxy_version(context):
stdio.verbose("get obproxy version with LD_LIBRARY_PATH,cmd:{0}, result:{1}".format(cmd, obproxy_version_info))
if "REVISION" not in obproxy_version_info:
raise Exception("Please check conf about proxy,{0}".format(obproxy_version_info))
pattern = r"(\d+\.\d+\.\d+\.\d+)"
match = re.search(pattern, obproxy_version_info)
if match:
obproxy_version_info = match.group(1)
Expand Down Expand Up @@ -405,12 +405,12 @@ def is_empty_file(ssh_client, file_path, stdio=None):
return False


def get_obdiag_display(log_dir, trace_id, stdio=None):
cmd = 'grep -h "\[{}\]" {}* | sed "s/\[{}\] //g" '.format(trace_id, log_dir, trace_id)
stdout = LocalClient(stdio).run(cmd)
print_stdout = str(stdout).replace('\\n', '\n').replace('\\t', '\t')
if len(print_stdout) > 0:
print(print_stdout)
# def get_obdiag_display(log_dir, trace_id, stdio=None):
# cmd = 'grep -h "\[{}\]" {}* | sed "s/\[{}\] //g" '.format(trace_id, log_dir, trace_id)
# stdout = LocalClient(stdio).run(cmd)
# print_stdout = str(stdout).replace('\\n', '\n').replace('\\t', '\t')
# if len(print_stdout) > 0:
# print(print_stdout)


def uzip_dir_local(uzip_dir, stdio=None):
Expand Down
2 changes: 1 addition & 1 deletion common/config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def build_configuration(self):
self.save_old_configuration(old_config)
# rewrite config
ob_cluster_name = self.get_cluster_name()
print("\033[33mPlease enter the following configuration !!!\033[0m")
self.stdio.print("\033[33mPlease enter the following configuration !!!\033[0m")
global_ssh_username = self.input_with_default("oceanbase host ssh username", "")
global_ssh_password = self.input_password_with_default("oceanbase host ssh password", "")
global_ssh_port = self.input_with_default("oceanbase host ssh_port", "22")
Expand Down
1 change: 0 additions & 1 deletion common/ssh_client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def progress_bar(self, transferred, to_be_transferred, suffix=''):
sys.stdout.write('Downloading [%s] %s%s%s %s %s\r' % (bar, '\033[32;1m%s\033[0m' % print_percents, '% [', self.translate_byte(transferred), ']', suffix))
if transferred == to_be_transferred:
sys.stdout.write('Downloading [%s] %s%s%s %s %s\r' % (bar, '\033[32;1m%s\033[0m' % print_percents, '% [', self.translate_byte(transferred), ']', suffix))
print()

def translate_byte(self, B):
if B < 0:
Expand Down
1 change: 0 additions & 1 deletion common/ssh_client/remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def progress_bar(self, transferred, to_be_transferred, suffix=''):
sys.stdout.write('Downloading [%s] %s%s%s %s %s\r' % (bar, '\033[32;1m%s\033[0m' % print_percents, '% [', self.translate_byte(transferred), ']', suffix))
if transferred == to_be_transferred:
sys.stdout.write('Downloading [%s] %s%s%s %s %s\r' % (bar, '\033[32;1m%s\033[0m' % print_percents, '% [', self.translate_byte(transferred), ']', suffix))
print()

def upload(self, remote_path, local_path):
transport = self._ssh_fd.get_transport()
Expand Down
16 changes: 8 additions & 8 deletions common/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,26 +1358,26 @@ def convert_to_number(s, stdio=None):
return s

@staticmethod
def print_scene(scene_dict, stdio=None):
def print_scene(scene_dict, stdio):
columns_to_print = ['command', 'info_en', 'info_cn']
keys = columns_to_print
table_data = [[value[key] for key in keys] for value in scene_dict.values()]
column_widths = [max(len(str(item)) * (StringUtils.is_chinese(item) or 1) for item in column) for column in zip(*table_data)]
table_data.insert(0, keys)
Util.print_line(length=sum(column_widths) + 5)
Util.print_line(length=sum(column_widths) + 5, stdio=stdio)
for i in range(len(table_data)):
print(Fore.GREEN + " ".join(f"{item:<{width}}" for item, width in zip(table_data[i], column_widths)) + Style.RESET_ALL)
stdio.print(Fore.GREEN + " ".join(f"{item:<{width}}" for item, width in zip(table_data[i], column_widths)) + Style.RESET_ALL)
if i == 0:
Util.print_line(length=sum(column_widths) + 5)
Util.print_line(length=sum(column_widths) + 5)
Util.print_line(length=sum(column_widths) + 5, stdio=stdio)
Util.print_line(length=sum(column_widths) + 5, stdio=stdio)

@staticmethod
def print_line(char='-', length=50, stdio=None):
print(char * length)
stdio.print(char * length)

@staticmethod
def print_title(name, stdio=None):
print("\n[{0}]:".format(name))
def print_title(name, stdio):
stdio.print("\n[{0}]:".format(name))

@staticmethod
def gen_password(length=8, chars=string.ascii_letters + string.digits, stdio=None):
Expand Down
1 change: 1 addition & 0 deletions conf/inner_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ obdiag:
mode: obdiag
stdout_handler_log_level: INFO
error_stream: sys.stdout
silent: false
ssh_client:
remote_client_sudo: 0
check:
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
'mode': 'obdiag',
'stdout_handler_log_level': 'INFO',
'error_stream': 'sys.stdout',
'silent': False,
},
'ssh_client': {
'remote_client_sudo': False,
Expand Down
68 changes: 42 additions & 26 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,10 +73,19 @@ def __init__(self, stdio=None, config_path=os.path.expanduser('~/.obdiag/config.
self.set_stdio(stdio)
self.context = None
self.inner_config_manager = InnerConfigManager(stdio=stdio, inner_config_change_map=inner_config_change_map)
# obdiag.logger.error_stream
if self.inner_config_manager.config.get("obdiag") is not None and self.inner_config_manager.config.get("obdiag").get("basic") is not None and self.inner_config_manager.config.get("obdiag").get("basic").get("print_type") is not None:
stdio.set_err_stream(self.inner_config_manager.config.get("obdiag").get("logger").get("error_stream"))

# obdiag.logger.silent
if self.inner_config_manager.config.get("obdiag") is not None and self.inner_config_manager.config.get("obdiag").get("logger") is not None and self.inner_config_manager.config.get("obdiag").get("logger").get("silent") is not None:
stdio.set_silent(self.inner_config_manager.config.get("obdiag").get("logger").get("silent"))
self.set_stdio(stdio)
if config_path:
if os.path.exists(os.path.abspath(config_path)):
config_path = config_path
else:
stdio.error('The option you provided with -c: {0} is not exist.'.format(config_path))
return
self.config_manager = ConfigManager(config_path, stdio)
if (
self.inner_config_manager.config.get("obdiag") is not None
Expand Down Expand Up @@ -191,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)
Expand Down Expand Up @@ -230,8 +240,7 @@ def gather_function(self, function_type, opt):
handler_log = GatherLogHandler(self.context)
handler_log.handle()
handler_obproxy = GatherObProxyLogHandler(self.context)
handler_obproxy.handle()
return True
return handler_obproxy.handle()
elif function_type == 'gather_sysstat':
handler = GatherOsInfoHandler(self.context)
return handler.handle()
Expand All @@ -252,13 +261,13 @@ def gather_function(self, function_type, opt):
return handler.handle()
else:
self._call_stdio('error', 'Not support gather function: {0}'.format(function_type))
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='Not support gather function: {0}'.format(function_type))

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)
Expand All @@ -273,33 +282,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)
Expand All @@ -310,26 +320,29 @@ 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)
obproxy_check_handler = None
observer_check_handler = None
result_data = {}
if self.context.obproxy_config.get("servers") is not None and len(self.context.obproxy_config.get("servers")) > 0:
obproxy_check_handler = CheckHandler(self.context, check_target_type="obproxy")
obproxy_check_handler.handle()
obproxy_check_handler.execute()
obproxy_result = obproxy_check_handler.execute()
result_data['obproxy'] = obproxy_result
if self.context.cluster_config.get("servers") is not None and len(self.context.cluster_config.get("servers")) > 0:
observer_check_handler = CheckHandler(self.context, check_target_type="observer")
observer_check_handler.handle()
observer_check_handler.execute()
observer_result = observer_check_handler.execute()
result_data['observer'] = observer_result
if obproxy_check_handler is not None:
obproxy_report_path = os.path.expanduser(obproxy_check_handler.report.get_report_path())
if os.path.exists(obproxy_report_path):
Expand All @@ -338,59 +351,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=result_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"})
Loading

0 comments on commit 6138999

Please sign in to comment.