From a0c47a1cc456f5294fb4e3318a1b16faa3af1a68 Mon Sep 17 00:00:00 2001 From: yyhenryyy Date: Tue, 26 Nov 2024 14:32:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(mongodb):=20=E8=BF=81=E7=A7=BB=E5=85=83?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=9C=A8=E6=B5=81=E7=A8=8B=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=86=85=E5=88=A4=E6=96=AD=E5=89=AF=E6=9C=AC=E9=9B=86=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E6=98=AF=E5=90=A6=E5=A4=8D=E7=94=A8=20#8184?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db_services/dbresource/handlers.py | 2 +- .../scene/mongodb/sub_task/migrate_meta.py | 5 +- .../flow/utils/mongodb/migrate_meta.py | 55 ++++++++++++++++++- .../mongodb/mongodb_migrate_dataclass.py | 16 +----- 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/dbm-ui/backend/db_services/dbresource/handlers.py b/dbm-ui/backend/db_services/dbresource/handlers.py index 0e4a3d843e..807b39e421 100644 --- a/dbm-ui/backend/db_services/dbresource/handlers.py +++ b/dbm-ui/backend/db_services/dbresource/handlers.py @@ -18,7 +18,7 @@ from backend.components.dbresource.client import DBResourceApi from backend.components.gse.client import GseApi from backend.db_meta.enums.spec import SpecClusterType, SpecMachineType -from backend.db_meta.models import Spec, AppCache +from backend.db_meta.models import AppCache, Spec from backend.db_services.dbresource.exceptions import SpecOperateException from backend.db_services.ipchooser.query.resource import ResourceQueryHelper diff --git a/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/sub_task/migrate_meta.py b/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/sub_task/migrate_meta.py index e8b84df818..e9e17ae4b9 100644 --- a/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/sub_task/migrate_meta.py +++ b/dbm-ui/backend/flow/engine/bamboo/scene/mongodb/sub_task/migrate_meta.py @@ -21,9 +21,6 @@ from backend.flow.plugins.components.collections.common.install_nodeman_plugin import ( InstallNodemanPluginServiceComponent, ) -from backend.flow.plugins.components.collections.mongodb.add_relationship_to_meta import ( - ExecAddRelationshipOperationComponent, -) from backend.flow.plugins.components.collections.mongodb.exec_actuator_job import ExecuteDBActuatorJobComponent from backend.flow.plugins.components.collections.mongodb.migrate_meta import MongoDBMigrateMetaComponent from backend.flow.plugins.components.collections.mongodb.send_media import ExecSendMediaOperationComponent @@ -83,7 +80,7 @@ def cluster_migrate( kwargs = sub_get_kwargs.get_migrate_info() sub_pipeline.add_act( act_name=_("迁移meta"), - act_component_code=ExecAddRelationshipOperationComponent.code, + act_component_code=MongoDBMigrateMetaComponent.code, kwargs=kwargs, ) diff --git a/dbm-ui/backend/flow/utils/mongodb/migrate_meta.py b/dbm-ui/backend/flow/utils/mongodb/migrate_meta.py index 38ea052a0a..db62338df7 100644 --- a/dbm-ui/backend/flow/utils/mongodb/migrate_meta.py +++ b/dbm-ui/backend/flow/utils/mongodb/migrate_meta.py @@ -16,9 +16,11 @@ from backend.components import DBConfigApi, DnsApi from backend.components.dbconfig.constants import LevelName, ReqType from backend.configuration.handlers.dba import DBAdministratorHandler +from backend.db_meta.api.cluster.mongocluster import pkg_create_mongo_cluster +from backend.db_meta.api.cluster.mongorepset import pkg_create_mongoset from backend.db_meta.enums import ClusterEntryType from backend.db_meta.enums.cluster_type import ClusterType -from backend.db_meta.models import CLBEntryDetail, Cluster, ClusterEntry +from backend.db_meta.models import CLBEntryDetail, Cluster, ClusterEntry, Machine from backend.flow.consts import DEFAULT_CONFIG_CONFIRM, DEFAULT_DB_MODULE_ID, MongoDBManagerUser from backend.flow.utils import dns_manage from backend.flow.utils.mongodb.mongodb_password import MongoDBPassword @@ -154,6 +156,57 @@ def save_password(self): ) return False + def migrate_cluster(self): + """迁移集群""" + + # 副本集判断机器是否复用 + if self.info["cluster_type"] == ClusterType.MongoReplicaSet.value: + for node in self.info["replicaset_storages"]: + if Machine.objects.filter(ip=node["ip"], bk_cloud_id=node["bk_cloud_id"]).count() > 0: + self.info["skip_machine"] = True + break + # 写入meta + try: + if self.info["cluster_type"] == ClusterType.MongoReplicaSet.value: + pkg_create_mongoset( + bk_biz_id=self.info["bk_biz_id"], + name=self.info["name"], + immute_domain=self.info["immute_domain"], + alias=self.info["alias"], + major_version=self.info["major_version"], + storages=self.info["storages"], + creator=self.info["creator"], + bk_cloud_id=self.info["bk_cloud_id"], + db_module_id=self.info["db_module_id"], + region=self.info["region"], + skip_machine=self.info["skip_machine"], + spec_id=self.info["spec_id"], + spec_config=self.info["spec_config"], + disaster_tolerance_level=self.info.get("disaster_tolerance_level", "NONE"), + ) + elif self.info["cluster_type"] == ClusterType.MongoShardedCluster.value: + pkg_create_mongo_cluster( + bk_biz_id=self.info["bk_biz_id"], + name=self.info["name"], + immute_domain=self.info["immute_domain"], + alias=self.info["alias"], + db_module_id=self.info["db_module_id"], + major_version=self.info["major_version"], + proxies=self.info["proxies"], + configs=self.info["configs"], + storages=self.info["storages"], + creator=self.info["creator"], + bk_cloud_id=self.info["bk_cloud_id"], + region=self.info["region"], + machine_specs=self.info["machine_specs"], + disaster_tolerance_level=self.info.get("disaster_tolerance_level", "NONE"), + ) + except Exception as e: + logger.error("add relationship to meta fail, error:{}".format(str(e))) + return False + logger.info("add mongodb relationship to meta successfully") + return True + def change_domain_app(self): """修改dns的app字段""" diff --git a/dbm-ui/backend/flow/utils/mongodb/mongodb_migrate_dataclass.py b/dbm-ui/backend/flow/utils/mongodb/mongodb_migrate_dataclass.py index df05932416..e47be54e98 100644 --- a/dbm-ui/backend/flow/utils/mongodb/mongodb_migrate_dataclass.py +++ b/dbm-ui/backend/flow/utils/mongodb/mongodb_migrate_dataclass.py @@ -16,7 +16,6 @@ from backend.db_meta.enums.cluster_type import ClusterType from backend.db_meta.enums.instance_role import InstanceRole from backend.db_meta.enums.spec import SpecClusterType, SpecMachineType -from backend.db_meta.models import Machine from backend.db_meta.models.spec import Spec from backend.flow.consts import ( DEFAULT_DB_MODULE_ID, @@ -99,17 +98,6 @@ def __init__(self): # os配置 self.os_conf: dict = None - def skip_machine(self): - """副本集机器复用""" - - if self.source_cluster_info.get("cluster_type") == ClusterType.MongoReplicaSet.value: - for node in self.source_cluster_info.get("storages"): - if Machine.objects.filter(ip=node["ip"], bk_cloud_id=node["bk_cloud_id"]).count() > 0: - self.source_cluster_info["skip_machine"] = True - break - else: - self.source_cluster_info["skip_machine"] = False - @staticmethod def get_mongodb_spec_info(cluster_type: str, machine_type: str) -> dict: """获取机器规格""" @@ -256,6 +244,7 @@ def get_migrate_info(self) -> dict: """获取迁移信息""" info = { + "meta_func_name": MongoDBMigrateMeta.migrate_cluster.__name__, "bk_biz_id": self.bk_biz_id, "major_version": self.source_cluster_info.get("major_version"), "creator": self.source_cluster_info.get("mongodb_dbas").split(",")[0], @@ -290,7 +279,8 @@ def get_migrate_info(self) -> dict: storages[index]["role"] = self.instance_role[index] info.update( { - "skip_machine": self.source_cluster_info.get("skip_machine"), + "replicaset_storages": self.source_cluster_info.get("storages"), + "skip_machine": False, "immute_domain": immute_domain, "name": self.source_cluster_info.get("replsetname"), "alias": self.source_cluster_info.get("replsetname"),