Skip to content

Commit

Permalink
fix(backend): 主机主备负责人处理 #4426
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 authored and iSecloud committed May 15, 2024
1 parent 7df2686 commit 8de99ec
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 35 deletions.
4 changes: 2 additions & 2 deletions dbm-ui/backend/components/bkmonitorv3/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def __init__(self):
url="save_duty_rule/",
description=_("保存轮值规则"),
)
self.search_duty_rule = self.generate_data_api(
self.search_duty_rules = self.generate_data_api(
method="POST",
url="search_duty_rule/",
url="search_duty_rules/",
description=_("查询轮值规则列表"),
)
self.delete_duty_rules = self.generate_data_api(
Expand Down
11 changes: 11 additions & 0 deletions dbm-ui/backend/configuration/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
default_app_config = "backend.configuration.apps.ConfigurationConfig"
74 changes: 74 additions & 0 deletions dbm-ui/backend/configuration/handlers/dba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import logging
from typing import Dict, List, Union

from backend.configuration.models import DBAdministrator
from backend.db_meta.enums import ClusterType
from backend.db_meta.models import Machine
from backend.flow.utils.cc_manage import CcManage

logger = logging.getLogger("root")


class DBAdministratorHandler(object):
"""DBA人员处理"""

@staticmethod
def upsert_biz_admins(bk_biz_id: int, db_admins: List[Dict[str, Union[str, List[str]]]]):
from backend.db_periodic_task.local_tasks.db_monitor import update_dba_notice_group

# 平台管理员
db_type_platform_dba = {dba.db_type: dba.users for dba in DBAdministrator.objects.filter(bk_biz_id=0)}

# 业务管理员
db_type_biz_dba = {dba.db_type: dba.users for dba in DBAdministrator.objects.filter(bk_biz_id=bk_biz_id)}

# 更新或创建业务管理员
for dba in db_admins:
db_type = dba["db_type"]
new_dba = dba["users"]
platform_dba = db_type_platform_dba.get(db_type, [])
biz_dba = db_type_biz_dba.get(db_type, [])
if set(new_dba) == set(platform_dba) and not biz_dba:
# 业务新设置的与平台人员一致,则无需新建
continue
if set(new_dba) == set(biz_dba):
# 新 DBA 与 旧DBA 一致,也无需更新
continue
dba_obj, created = DBAdministrator.objects.update_or_create(
bk_biz_id=bk_biz_id, db_type=db_type, defaults={"users": new_dba}
)
# 更新主机主备负责人
operator = biz_dba[0]
bk_bak_operator = biz_dba[1] if len(biz_dba) > 1 else operator
cluster_types = ClusterType.db_type_to_cluster_types(db_type)
for cluster_type in cluster_types:
bk_host_ids = [
machine.bk_host_id
for machine in Machine.objects.filter(cluster_type=cluster_type, bk_biz_id=bk_biz_id)
]
if not bk_host_ids:
continue
CcManage.batch_update_host(
[
{
"bk_host_id": bk_host_id,
"operator": operator,
"bk_bak_operator": bk_bak_operator,
}
for bk_host_id in bk_host_ids
],
need_monitor=True,
)

# 更新告警组
update_dba_notice_group.apply_async(kwargs={"dba_id": dba_obj.id})
27 changes: 0 additions & 27 deletions dbm-ui/backend/configuration/models/dba.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,6 @@ def list_biz_admins(cls, bk_biz_id: int) -> List[Dict[str, Union[str, List[str]]

return db_admins

@classmethod
def upsert_biz_admins(cls, bk_biz_id: int, db_admins: List[Dict[str, Union[str, List[str]]]]):
from backend.db_periodic_task.local_tasks.db_monitor import update_dba_notice_group

# 平台管理员
db_type_platform_dba = {dba.db_type: dba.users for dba in cls.objects.filter(bk_biz_id=0)}

# 业务管理员
db_type_biz_dba = {dba.db_type: dba.users for dba in cls.objects.filter(bk_biz_id=bk_biz_id)}

# 更新或创建业务管理员
for dba in db_admins:
db_type = dba["db_type"]
new_dba = dba["users"]
platform_dba = db_type_platform_dba.get(db_type, [])
biz_dba = db_type_biz_dba.get(db_type, [])
if set(new_dba) == set(platform_dba) and not biz_dba:
# 业务新设置的与平台人员一致,则无需新建
continue
if set(new_dba) == set(biz_dba):
# 新 DBA 与 旧DBA 一致,也无需更新
continue
dba_obj, created = cls.objects.update_or_create(
bk_biz_id=bk_biz_id, db_type=db_type, defaults={"users": new_dba}
)
update_dba_notice_group.apply_async(kwargs={"dba_id": dba_obj.id})

@classmethod
def get_biz_db_type_admins(cls, bk_biz_id: int, db_type: str) -> List[str]:
biz_admins = cls.list_biz_admins(bk_biz_id)
Expand Down
11 changes: 11 additions & 0 deletions dbm-ui/backend/configuration/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
default_app_config = "backend.configuration.apps.ConfigurationConfig"
3 changes: 2 additions & 1 deletion dbm-ui/backend/configuration/views/dba.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from backend.bk_web import viewsets
from backend.bk_web.swagger import common_swagger_auto_schema
from backend.configuration.handlers.dba import DBAdministratorHandler
from backend.configuration.models.dba import DBAdministrator
from backend.configuration.serializers import ListDBAdminSerializer, UpsertDBAdminSerializer
from backend.iam_app.handlers.drf_perm import DBManageIAMPermission, GlobalManageIAMPermission
Expand Down Expand Up @@ -47,4 +48,4 @@ def upsert_admins(self, request, *args, **kwargs):
validated_data = self.params_validate(self.get_serializer_class())
bk_biz_id = validated_data["bk_biz_id"]
db_admins = validated_data["db_admins"]
return Response(DBAdministrator.upsert_biz_admins(bk_biz_id, db_admins))
return Response(DBAdministratorHandler.upsert_biz_admins(bk_biz_id, db_admins))
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_monitor/models/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def save_monitor_group(self) -> int:
if self.monitor_duty_rule_id:
save_duty_rule_params["id"] = self.monitor_duty_rule_id
else:
rules = BKMonitorV3Api.search_duty_rule({"bk_biz_ids": [env.DBA_APP_BK_BIZ_ID], "name": rule_name})
rules = BKMonitorV3Api.search_duty_rules({"bk_biz_ids": [env.DBA_APP_BK_BIZ_ID], "name": rule_name})
for rule in rules:
if rule["name"] == rule_name:
save_duty_rule_params["id"] = rule["id"]
Expand Down
6 changes: 4 additions & 2 deletions dbm-ui/backend/flow/utils/cc_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ def update_host_properties(
# 这里可以认为一批操作的机器的数据库类型是相同的
db_type = ClusterType.cluster_type_to_db_type(machines.first().cluster_type)
biz_dba = DBAdministrator.get_biz_db_type_admins(bk_biz_id=self.bk_biz_id, db_type=db_type)
operator = biz_dba[0]
bk_bak_operator = biz_dba[1] if len(biz_dba) > 1 else operator
host_info_list = [
{
"bk_host_id": machine.bk_host_id,
# 主要维护人
"operator": ",".join(biz_dba),
"operator": operator,
# 备份维护人
"bk_bak_operator": ",".join(biz_dba),
"bk_bak_operator": bk_bak_operator,
# db_meta信息
CC_HOST_DBM_ATTR: json.dumps(machine.dbm_meta),
}
Expand Down
4 changes: 2 additions & 2 deletions dbm-ui/backend/flow/utils/redis/redis_db_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from backend.components import DBConfigApi
from backend.components.dbconfig.constants import FormatType, LevelName
from backend.configuration.models import DBAdministrator
from backend.configuration.handlers.dba import DBAdministratorHandler
from backend.db_meta import api
from backend.db_meta.api.cluster.nosqlcomm.create_cluster import update_cluster_type
from backend.db_meta.api.cluster.rediscluster.handler import RedisClusterHandler
Expand Down Expand Up @@ -1128,7 +1128,7 @@ def update_nosql_dba(self):
"""
更新dba
"""
DBAdministrator.upsert_biz_admins(self.ticket_data["bk_biz_id"], self.cluster["db_admins"])
DBAdministratorHandler.upsert_biz_admins(self.ticket_data["bk_biz_id"], self.cluster["db_admins"])

def storageinstance_bind_entry(self) -> bool:
"""
Expand Down

0 comments on commit 8de99ec

Please sign in to comment.