Skip to content

Commit

Permalink
fix: 修复随机账号授权或者删除异常捕捉的问题 #3627
Browse files Browse the repository at this point in the history
  • Loading branch information
yksitu committed Mar 21, 2024
1 parent 4f49404 commit 3c5438e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _execute(self, data, parent_data, callback=None) -> bool:
"role": "",
}

err_num = 0
for cluster_id in kwargs["cluster_ids"]:
# 获取每个cluster_id对应的对象
try:
Expand All @@ -110,25 +111,23 @@ def _execute(self, data, parent_data, callback=None) -> bool:
instance_list = self._get_instance_for_cluster(cluster=cluster)

# 开始遍历集群每个实例,添加临时账号
err_num = 0
for inst in instance_list:
if not inst.get("priv_role"):
self.log_error(_("不支持改实例的主机类型授权[{}]: machine_type: {}").format(inst.ip_port, inst.machine_type))
err_num = err_num + 1
continue

# 按照实例维度进行添加账号
common_param["address"] = inst["instance"]
common_param["hosts"] = ["localhost", inst["instance"].split(":")[0]]
common_param["role"] = inst["priv_role"]
# if not self.__add_priv(common_param) and global_data["ticket_type"] not in allow_list:
# return False
if not self.__add_priv(common_param):
err_num = err_num + 1

if err_num == len(instance_list):
# 如果一套集群内所有节点添加失败,则直接返回异常
self.log_error(f"all instances add priv failed in the cluster[{cluster_id}]")
return False
if err_num > 0:
# 有错误先返回则直接返回异常
self.log_error("instances add priv failed")
return False

return True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def drop_jor_user(self, cluster: Cluster, root_id: str):
"""
# 拼接临时用户的名称
user = generate_mysql_tmp_user(root_id)

err_num = 0
try:
# 删除localhost和 local_ip用户
for instance in self._get_instance_for_cluster(cluster=cluster):
Expand Down Expand Up @@ -102,20 +102,26 @@ def drop_jor_user(self, cluster: Cluster, root_id: str):
f"The result [drop user if exists `{user}`] in {info['address']}"
f"is [{info['error_msg']}]"
)
err_num = err_num + 1
else:
self.log_info(f"The result [drop user if exists `{user}`] in {info['address']} is [success]")

except Exception as e: # pylint: disable=broad-except
self.log_error(f"drop user error in cluster [{cluster.name}]: {e}")
return False

if err_num > 0:
self.log_error(f"drop user error in cluster [{cluster.name}]")
return False

self.log_info(f"drop user finish in cluster [{cluster.name}]")
return True

def _execute(self, data, parent_data, callback=None) -> bool:
kwargs = data.get_one_of_inputs("kwargs")
global_data = data.get_one_of_inputs("global_data")

err_num = 0
for cluster_id in kwargs["cluster_ids"]:
# 获取每个cluster_id对应的对象
try:
Expand All @@ -125,7 +131,11 @@ def _execute(self, data, parent_data, callback=None) -> bool:
cluster_id=cluster_id, bk_biz_id=global_data["bk_biz_id"], message=_("集群不存在")
)
if not self.drop_jor_user(cluster=cluster, root_id=global_data["job_root_id"]):
return False
err_num = err_num + 1

if err_num > 0:
self.log_error("drop user error")
return False

return True

Expand Down

0 comments on commit 3c5438e

Please sign in to comment.