From 9cd8a046dc94ae9ac1f5d4cb28cf7443be36722b Mon Sep 17 00:00:00 2001 From: yyhenryyy Date: Fri, 18 Oct 2024 10:38:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(mongodb):=20mongos=E8=87=AA=E6=84=88?= =?UTF-8?q?=EF=BC=8Cmongo=E5=AE=9E=E4=BE=8B=E4=B8=8B=E6=9E=B6=20#7010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mongodb/autofix/mongodb_autofix_ticket.py | 13 +++++++- .../bamboo/scene/mongodb/mongodb_autofix.py | 33 ++++++++++--------- .../ticket/builders/mongodb/mongo_autofix.py | 2 ++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/dbm-ui/backend/db_services/mongodb/autofix/mongodb_autofix_ticket.py b/dbm-ui/backend/db_services/mongodb/autofix/mongodb_autofix_ticket.py index 7bc527075b..d0f7a77f67 100644 --- a/dbm-ui/backend/db_services/mongodb/autofix/mongodb_autofix_ticket.py +++ b/dbm-ui/backend/db_services/mongodb/autofix/mongodb_autofix_ticket.py @@ -9,15 +9,20 @@ specific language governing permissions and limitations under the License. """ import logging +import datetime +from django.utils import timezone from django.utils.translation import ugettext_lazy as _ +from django.utils.crypto import get_random_string from backend.configuration.constants import DBType from backend.configuration.models.dba import DBAdministrator from backend.db_services.dbbase.constants import IpSource from backend.db_services.redis.autofix.models import RedisAutofixCore from backend.ticket.constants import TicketType from backend.ticket.models import Ticket +from backend.db_services.redis.autofix.enums import AutofixStatus +from backend.utils.time import datetime2str logger = logging.getLogger("root") @@ -44,6 +49,7 @@ def get_resource_spec(mongos_list: list, mongod_list: list) -> dict: mongod["ip"]: { "spec_id": mongod["spec_id"], "count": 1, + "spec_config": mongod["spec_config"], "Location_spec": {"city": mongod["city"], "sub_zone_ids": [mongod["bk_sub_zone_id"]]}, } } @@ -88,10 +94,15 @@ def mongo_create_ticket(cluster: RedisAutofixCore, cluster_ids: list, mongos_lis } # 创建单据 - Ticket.create_ticket( + ticket = Ticket.create_ticket( ticket_type=TicketType.MONGODB_AUTOFIX.value, creator=mongodb_dba[0], bk_biz_id=cluster.bk_biz_id, remark=_("自动发起-自愈任务-{}".format(cluster.immute_domain)), details=details, ) + cluster.ticket_id = ticket.id + cluster.status_version = get_random_string(12) + cluster.deal_status = AutofixStatus.AF_WFLOW.value + cluster.update_at = datetime2str(datetime.datetime.now(timezone.utc)) + cluster.save(update_fields=["ticket_id", "status_version", "deal_status", "update_at"]) diff --git a/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/mongodb_autofix.py b/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/mongodb_autofix.py index fb15ca02af..c8684aa522 100644 --- a/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/mongodb_autofix.py +++ b/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/mongodb_autofix.py @@ -14,6 +14,7 @@ from backend.db_meta.enums import ClusterType from backend.flow.utils.mongodb.mongodb_repo import MongoRepository +from . import mongodb_cluster_autofix from . import mongodb_replace logger = logging.getLogger("flow") @@ -31,11 +32,12 @@ def __init__(self, root_id: str, data: Optional[Dict]): self.root_id = root_id self.data = data + self.autofix_info = self.data["infos"][0] def get_public_data(self) -> Dict: """参数公共部分""" - bk_biz_id = self.data["infos"]["bk_biz_id"] + bk_biz_id = self.autofix_info["bk_biz_id"] return { "bk_biz_id": bk_biz_id, "uid": self.data["uid"], @@ -48,8 +50,9 @@ def shard_get_data(self) -> Dict: """分片集群获取参数""" flow_parameter = self.get_public_data() - bk_cloud_id = self.data["infos"]["bk_cloud_id"] - cluster_id = self.data["infos"]["cluster_ids"][0] + + bk_cloud_id = self.autofix_info["bk_cloud_id"] + cluster_id = self.autofix_info["cluster_ids"][0] cluster_info = MongoRepository().fetch_one_cluster(withDomain=False, id=cluster_id) config = cluster_info.get_config() shards = cluster_info.get_shards() @@ -58,7 +61,7 @@ def shard_get_data(self) -> Dict: mongo_config = [] mongodb = [] # 获取mongos参数 - for mongos in self.data["infos"]["mongos_list"]: + for mongos in self.autofix_info["mongos_list"]: mongos.append( { "ip": mongos["ip"], @@ -66,25 +69,25 @@ def shard_get_data(self) -> Dict: "spec_id": mongos["spec_id"], "down": True, "spec_config": mongos["spec_config"], - "target": self.data["infos"][mongos["ip"]], + "target": self.autofix_info[mongos["ip"]], "instances": [ { "cluster_id": cluster_id, "db_version": cluster_info.major_version, - "domain": self.data["infos"]["immute_domain"], + "domain": self.autofix_info["immute_domain"], "port": int(cluster_info.get_mongos()[0].port), } ], } ) - for mongod in self.data["infos"]["mongod_list"]: + for mongod in self.autofix_info["mongod_list"]: ip_info = { "ip": mongod["ip"], "bk_cloud_id": bk_cloud_id, "spec_id": mongod["spec_id"], "down": True, "spec_config": mongod["spec_config"], - "target": self.data["infos"][mongod["ip"]], + "target": self.autofix_info[mongod["ip"]], "instances": [], } instances = [] @@ -131,9 +134,9 @@ def rs_get_data(self) -> Dict: """副本集获取参数""" flow_parameter = self.get_public_data() - bk_cloud_id = self.data["infos"]["bk_cloud_id"] - cluster_ids = self.data["infos"]["cluster_ids"] - for mongod in self.data["infos"]["mongod_list"]: + bk_cloud_id = self.autofix_info["bk_cloud_id"] + cluster_ids = self.autofix_info["cluster_ids"] + for mongod in self.autofix_info["mongod_list"]: instances = [] for cluster_id in cluster_ids: cluster_info = MongoRepository().fetch_one_cluster(withDomain=True, id=cluster_id) @@ -155,7 +158,7 @@ def rs_get_data(self) -> Dict: "spec_id": mongod["spec_id"], "down": True, "spec_config": mongod["spec_config"], - "target": self.data["infos"][mongod["ip"]], + "target": self.autofix_info[mongod["ip"]], "instances": instances, } ) @@ -165,8 +168,8 @@ def autofix(self): """进行自愈""" # 副本集 - if self.data["infos"]["cluster_type"] == ClusterType.MongoReplicaSet.value: + if self.autofix_info["cluster_type"] == ClusterType.MongoReplicaSet.value: mongodb_replace.MongoReplaceFlow(self.root_id, self.rs_get_data()).multi_host_replace_flow() # 分片集群 - elif self.data["infos"]["cluster_type"] == ClusterType.MongoShardedCluster.value: - mongodb_replace.MongoReplaceFlow(self.root_id, self.shard_get_data()).multi_host_replace_flow() + elif self.autofix_info["cluster_type"] == ClusterType.MongoShardedCluster.value: + mongodb_cluster_autofix.MongoClusterAutofixFlow(self.root_id, self.shard_get_data()).cluster_autofix_flow() diff --git a/dbm-ui/backend/ticket/builders/mongodb/mongo_autofix.py b/dbm-ui/backend/ticket/builders/mongodb/mongo_autofix.py index 06c5f8278e..db4652e25b 100644 --- a/dbm-ui/backend/ticket/builders/mongodb/mongo_autofix.py +++ b/dbm-ui/backend/ticket/builders/mongodb/mongo_autofix.py @@ -67,3 +67,5 @@ class MongoDBAutofixFlowBuilder(BaseMongoShardedTicketFlowBuilder): inner_flow_builder = MongoDBAutofixFlowParamBuilder inner_flow_name = _("MongoDB 故障自愈") resource_batch_apply_builder = MongoDBAutofixResourceParamBuilder + default_need_itsm = True + default_need_manual_confirm = True \ No newline at end of file