Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Teingi committed May 27, 2024
1 parent aceee61 commit e990562
Show file tree
Hide file tree
Showing 90 changed files with 14,750 additions and 4,599 deletions.
941 changes: 699 additions & 242 deletions cmd.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
@time: 2022/6/20
@file: __init__.py
@desc:
"""
"""
287 changes: 208 additions & 79 deletions common/command.py

Large diffs are not rendered by default.

157 changes: 107 additions & 50 deletions common/config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,68 @@ def __init__(self, context):
self.context = context
self.stdio = context.stdio
options = self.context.options
self.sys_tenant_user = Util.get_option(options, 'u')
self.sys_tenant_password = Util.get_option(options, 'p')
self.db_host = Util.get_option(options, 'h')
self.db_port = Util.get_option(options, 'P')
self.config_path = os.path.expanduser('~/.obdiag/config.yml')
self.sys_tenant_user = Util.get_option(options, "u")
self.sys_tenant_password = Util.get_option(options, "p")
self.db_host = Util.get_option(options, "h")
self.db_port = Util.get_option(options, "P")
self.config_path = os.path.expanduser("~/.obdiag/config.yml")
self.inner_config = self.context.inner_config
self.ob_cluster = {"db_host": self.db_host, "db_port": self.db_port, "tenant_sys": {"password": self.sys_tenant_password, "user": self.sys_tenant_user, }}
self.ob_cluster = {
"db_host": self.db_host,
"db_port": self.db_port,
"tenant_sys": {
"password": self.sys_tenant_password,
"user": self.sys_tenant_user,
},
}

def get_cluster_name(self):
ob_version = get_observer_version_by_sql(self.ob_cluster, self.stdio)
obConnetcor = OBConnector(
ip=self.db_host,
port=self.db_port,
ip=self.db_host,
port=self.db_port,
username=self.sys_tenant_user,
password=self.sys_tenant_password,
password=self.sys_tenant_password,
stdio=self.stdio,
timeout=100)
timeout=100,
)
if ob_version.startswith("3") or ob_version.startswith("2"):
sql = "select cluster_name from oceanbase.v$ob_cluster"
res = obConnetcor.execute_sql(sql)
if len(res) == 0:
self.stdio.error("Failed to get cluster name, please check whether the cluster config correct!!!")
self.stdio.error(
"Failed to get cluster name, please check whether the cluster config correct!!!"
)
else:
return res[0][0]
else:
return "obcluster"

def get_host_info_list_by_cluster(self):
ob_version = get_observer_version_by_sql(self.ob_cluster, self.stdio)
obConnetcor = OBConnector(ip=self.db_host,
port=self.db_port,
username=self.sys_tenant_user,
password=self.sys_tenant_password,
stdio=self.stdio,
timeout=100)
sql = "select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.DBA_OB_SERVERS"
if ob_version.startswith("3") or ob_version.startswith("2") or ob_version.startswith("1"):
obConnetcor = OBConnector(
ip=self.db_host,
port=self.db_port,
username=self.sys_tenant_user,
password=self.sys_tenant_password,
stdio=self.stdio,
timeout=100,
)
sql = (
"select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.DBA_OB_SERVERS"
)
if (
ob_version.startswith("3")
or ob_version.startswith("2")
or ob_version.startswith("1")
):
sql = "select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.__all_server"
res = obConnetcor.execute_sql(sql)
if len(res) == 0:
raise Exception("Failed to get the node from cluster config, "
"please check whether the cluster config correct!!!")
raise Exception(
"Failed to get the node from cluster config, "
"please check whether the cluster config correct!!!"
)
host_info_list = []
for row in res:
host_info = OrderedDict()
Expand All @@ -86,7 +106,9 @@ def get_host_info_list_by_cluster(self):
return host_info_list

def build_configuration(self):
self.stdio.verbose("Getting all the node information of the cluster, please wait a moment ...")
self.stdio.verbose(
"Getting all the node information of the cluster, please wait a moment ..."
)
all_host_info_list = self.get_host_info_list_by_cluster()
self.stdio.verbose("get node list %s", all_host_info_list)
all_host_ip_list = []
Expand All @@ -104,49 +126,73 @@ def build_configuration(self):
ob_cluster_name = self.get_cluster_name()
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_password = self.input_password_with_default(
"oceanbase host ssh password", ""
)
global_ssh_port = self.input_with_default("oceanbase host ssh_port", "22")
global_home_path = self.input_with_default("oceanbase install home_path", const.OB_INSTALL_DIR_DEFAULT)
global_home_path = self.input_with_default(
"oceanbase install home_path", const.OB_INSTALL_DIR_DEFAULT
)
default_data_dir = os.path.join(global_home_path, "store")
global_data_dir = self.input_with_default("oceanbase data_dir", default_data_dir)
global_redo_dir = self.input_with_default("oceanbase redo_dir", default_data_dir)
tenant_sys_config = {"user": self.sys_tenant_user, "password": self.sys_tenant_password}
global_data_dir = self.input_with_default(
"oceanbase data_dir", default_data_dir
)
global_redo_dir = self.input_with_default(
"oceanbase redo_dir", default_data_dir
)
tenant_sys_config = {
"user": self.sys_tenant_user,
"password": self.sys_tenant_password,
}
global_config = {
"ssh_username": global_ssh_username,
"ssh_password": global_ssh_password,
"ssh_port": global_ssh_port,
"ssh_key_file": "",
"home_path": global_home_path,
"data_dir": global_data_dir,
"redo_dir": global_redo_dir
"redo_dir": global_redo_dir,
}
new_config = {
"obcluster": {
"ob_cluster_name": ob_cluster_name,
"db_host": self.db_host,
"db_port": self.db_port,
"tenant_sys": tenant_sys_config,
"servers": {
"nodes": nodes_config,
"global": global_config
}}}
"servers": {"nodes": nodes_config, "global": global_config},
}
}
YamlUtils.write_yaml_data(new_config, self.config_path)
need_config_obproxy = self.input_choice_default("need config obproxy [y/N]", "N")
need_config_obproxy = self.input_choice_default(
"need config obproxy [y/N]", "N"
)
if need_config_obproxy:
self.build_obproxy_configuration(self.config_path)
self.stdio.verbose("Node information has been rewritten to the configuration file {0}, and you can enjoy the journey !".format(self.config_path))
self.stdio.verbose(
"Node information has been rewritten to the configuration file {0}, and you can enjoy the journey !".format(
self.config_path
)
)

def build_obproxy_configuration(self, path):
obproxy_servers = self.input_with_default("obproxy server eg:'192.168.1.1;192.168.1.2;192.168.1.3'", "")
obproxy_servers = self.input_with_default(
"obproxy server eg:'192.168.1.1;192.168.1.2;192.168.1.3'", ""
)
obproxy_server_list = StringUtils.split_ip(obproxy_servers)
if len(obproxy_server_list) > 0:
nodes_config = []
for server in obproxy_server_list:
nodes_config.append({"ip": server})
global_ssh_username = self.input_with_default("obproxy host ssh username", "")
global_ssh_password = self.input_password_with_default("obproxy host ssh password", "")
global_ssh_username = self.input_with_default(
"obproxy host ssh username", ""
)
global_ssh_password = self.input_password_with_default(
"obproxy host ssh password", ""
)
global_ssh_port = self.input_with_default("obproxy host ssh port", "22")
global_home_path = self.input_with_default("obproxy install home_path", const.OBPROXY_INSTALL_DIR_DEFAULT)
global_home_path = self.input_with_default(
"obproxy install home_path", const.OBPROXY_INSTALL_DIR_DEFAULT
)
global_config = {
"ssh_username": global_ssh_username,
"ssh_password": global_ssh_password,
Expand All @@ -157,10 +203,9 @@ def build_obproxy_configuration(self, path):
new_config = {
"obproxy": {
"obproxy_cluster_name": "obproxy",
"servers": {
"nodes": nodes_config,
"global": global_config
}}}
"servers": {"nodes": nodes_config, "global": global_config},
}
}
YamlUtils.write_yaml_data_append(new_config, path)

def get_old_configuration(self, path):
Expand All @@ -171,29 +216,41 @@ def get_old_configuration(self, path):
pass

def save_old_configuration(self, config):
backup_config_dir = os.path.expanduser(self.inner_config["obdiag"]["basic"]["config_backup_dir"])
filename = "config_backup_{0}.yml".format(TimeUtils.timestamp_to_filename_time(int(round(time.time() * 1000000))))
backup_config_dir = os.path.expanduser(
self.inner_config["obdiag"]["basic"]["config_backup_dir"]
)
filename = "config_backup_{0}.yml".format(
TimeUtils.timestamp_to_filename_time(int(round(time.time() * 1000000)))
)
backup_config_path = os.path.join(backup_config_dir, filename)
DirectoryUtil.mkdir(path=backup_config_dir)
YamlUtils.write_yaml_data(config, backup_config_path)

def input_with_default(self, prompt, default):
value = input("\033[32mEnter your {0} (default:'{1}'): \033[0m".format(prompt, default)).strip()
if value == '' or value.lower() == "y" or value.lower() == "yes":
value = input(
"\033[32mEnter your {0} (default:'{1}'): \033[0m".format(prompt, default)
).strip()
if value == "" or value.lower() == "y" or value.lower() == "yes":
return default
else:
return value

def input_password_with_default(self, prompt, default):
value = pwinput.pwinput(prompt="\033[32mEnter your {0} (default:'{1}'): \033[0m".format(prompt, default),
mask='*')
if value == '' or value.lower() == "y" or value.lower() == "yes":
value = pwinput.pwinput(
prompt="\033[32mEnter your {0} (default:'{1}'): \033[0m".format(
prompt, default
),
mask="*",
)
if value == "" or value.lower() == "y" or value.lower() == "yes":
return default
else:
return value

def input_choice_default(self, prompt, default):
value = input("\033[32mEnter your {0} (default:'{1}'): \033[0m".format(prompt, default)).strip()
value = input(
"\033[32mEnter your {0} (default:'{1}'): \033[0m".format(prompt, default)
).strip()
if value.lower() == "y" or value.lower() == "yes":
return True
else:
Expand Down
61 changes: 28 additions & 33 deletions common/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __setattr__(self, name, value):

const.GATHER_LOG_TEMPORARY_DIR_DEFAULT = "/tmp"

const.OBSTACK2_DEFAULT_INSTALL_PATH = '/tmp/obstack_x86_64_7'
const.OBSTACK2_DEFAULT_INSTALL_PATH = "/tmp/obstack_x86_64_7"
const.OBSTACK2_LOCAL_STORED_PATH = "./dependencies/bin/obstack_x86_64_7"

# 限制收集任务的并发线程数量 10
Expand All @@ -54,48 +54,43 @@ def __setattr__(self, name, value):

const.MAX_OB_VERSION_SUPPORT_GATHER_OBADMIN = "4.0.0"

const.DEFAULT_CONFIG_PATH = os.path.join(os.path.expanduser('~'), ".obdiag/config.yml")
const.DEFAULT_CONFIG_PATH = os.path.join(os.path.expanduser("~"), ".obdiag/config.yml")

const.FLT_TRACE_TREE_MAX_RECURSION = 5
const.FLT_TRACE_TREE_TOP_LEAF = 5
const.FLT_TRACE_WORKER = 4
const.FLT_TRACE_OUTPUT = 50

const.OBDIAG_BASE_DEFAULT_CONFIG = {
"obdiag": {
"basic": {
"config_path": "~/.obdiag/config.yml",
"config_backup_dir": "~/.obdiag/backup_conf",
"file_number_limit": 20,
"file_size_limit": "2G"
},
"logger": {
"file_handler_log_level": "DEBUG",
"log_dir": "~/.obdiag/log",
"log_filename": "obdiag.log",
"log_level": "INFO",
"mode": "obdiag",
"stdout_handler_log_level": "INFO"
"obdiag": {
"basic": {
"config_path": "~/.obdiag/config.yml",
"config_backup_dir": "~/.obdiag/backup_conf",
"file_number_limit": 20,
"file_size_limit": "2G",
},
"logger": {
"file_handler_log_level": "DEBUG",
"log_dir": "~/.obdiag/log",
"log_filename": "obdiag.log",
"log_level": "INFO",
"mode": "obdiag",
"stdout_handler_log_level": "INFO",
},
}
}
}

const.OBDIAG_CHECK_DEFAULT_CONFIG = {
"check": {
"ignore_version": "false",
"report": {
"report_path": "./check_report/",
"export_type": "table"
},
"package_file": "~/.obdiag/check/check_package.yaml",
"tasks_base_path": "~/.obdiag/check/tasks/"
}
"check": {
"ignore_version": "false",
"report": {"report_path": "./check_report/", "export_type": "table"},
"package_file": "~/.obdiag/check/check_package.yaml",
"tasks_base_path": "~/.obdiag/check/tasks/",
}
}

const.OBDIAG_GATHER_DEFAULT_CONFIG = {
"gather": {
"cases_base_path": "~/.obdiag/gather/tasks"
}
"gather": {"cases_base_path": "~/.obdiag/gather/tasks"}
}

const.OBDIAG_RCA_DEFAULT_CONFIG = {
Expand All @@ -107,7 +102,7 @@ def __setattr__(self, name, value):
const.TELEMETRY_CONTENT_REPORTER = "obdiag"
const.TELEMETRY_URL = "openwebapi.oceanbase.com"
const.TELEMETRY_PATH = "/api/web/oceanbase/report"
const.UPDATE_REMOTE_SERVER = 'https://obbusiness-private.oss-cn-shanghai.aliyuncs.com'
const.UPDATE_REMOTE_VERSION_FILE_NAME = 'https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/download-center/opensource/obdiag/version.yaml'
const.UPDATE_REMOTE_UPDATE_FILE_NAME = 'https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/download-center/opensource/obdiag/data.tar'
const.RCA_WORK_PATH= '~/.obdiag/rca'
const.UPDATE_REMOTE_SERVER = "https://obbusiness-private.oss-cn-shanghai.aliyuncs.com"
const.UPDATE_REMOTE_VERSION_FILE_NAME = "https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/download-center/opensource/obdiag/version.yaml"
const.UPDATE_REMOTE_UPDATE_FILE_NAME = "https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/download-center/opensource/obdiag/data.tar"
const.RCA_WORK_PATH = "~/.obdiag/rca"
4 changes: 2 additions & 2 deletions common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def __init__(self, name, level=logging.DEBUG):
self.buffer = []
self.buffer_size = 0

def _log(self, level, msg, args, end='\n', **kwargs):
return super(Logger, self)._log(level, msg, args, **kwargs)
def _log(self, level, msg, args, end="\n", **kwargs):
return super(Logger, self)._log(level, msg, args, **kwargs)
Loading

0 comments on commit e990562

Please sign in to comment.