Skip to content

Commit

Permalink
feat(mongodb): mongodb卸载 #3445
Browse files Browse the repository at this point in the history
  • Loading branch information
yyhenryyy authored and iSecloud committed Mar 11, 2024
1 parent 3bbddfc commit e3fbb0d
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type DeInstallConfParams struct {
SetId string `json:"setId" validate:"required"`
NodeInfo []string `json:"nodeInfo" validate:"required"` // []string ip,ip 如果为复制集节点,则为复制集所有节点的ip;如果为mongos,则为mongos的ip
InstanceType string `json:"instanceType" validate:"required"` // mongod mongos
Force bool `json:"force"` // 不检查连接,强制卸载
}

// DeInstall 添加分片到集群
Expand Down Expand Up @@ -178,8 +179,10 @@ func (d *DeInstall) shutdownProcess() error {
if d.ServiceStatus == true {
d.runtime.Logger.Info("start to shutdown service")
// 检查连接
if err := d.checkConnection(); err != nil {
return err
if d.ConfParams.Force == false {
if err := d.checkConnection(); err != nil {
return err
}
}

// 关闭进程
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,12 @@ def mongodb_pkg(self, db_version: str) -> list:
f"{env.BKREPO_PROJECT}/{env.BKREPO_BUCKET}{mongodb_pkg.path}",
# f"{env.BKREPO_PROJECT}/{env.BKREPO_BUCKET}/{bkdbmon_pkg.path}",
]

def mongodb_actuator_pkg(self) -> list:
"""
mongodb的actuator包
"""

return [
f"{env.BKREPO_PROJECT}/{env.BKREPO_BUCKET}{self.actuator_pkg.path}",
]
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def prepare_job(self, pipeline: Builder):
"""

# 介质下发——job的api可以多个IP并行执行
kwargs = self.get_kwargs.get_send_media_kwargs()
kwargs = self.get_kwargs.get_send_media_kwargs(media_type="actuator")
pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand All @@ -63,8 +63,10 @@ def multi_cluster_deinstall_flow(self):
# 创建流程实例
pipeline = Builder(root_id=self.root_id, data=self.data)

# 获取信息
# 获取所有的cluster主机信息
self.get_kwargs.get_hosts_deinstall()
self.get_kwargs.payload["app"] = self.get_kwargs.payload["bk_app_abbr"]

# 下发介质
self.prepare_job(pipeline=pipeline)
Expand All @@ -73,7 +75,11 @@ def multi_cluster_deinstall_flow(self):
sub_pipelines = []
for cluster_id in self.data["cluster_ids"]:
sub_pipline = deinstall(
root_id=self.root_id, ticket_data=self.data, sub_kwargs=self.get_kwargs, cluster_id=cluster_id
root_id=self.root_id,
ticket_data=self.data,
sub_kwargs=self.get_kwargs,
cluster_id=cluster_id,
reduce_mongos=False,
)
sub_pipelines.append(sub_pipline)
pipeline.add_parallel_sub_pipeline(sub_flow_list=sub_pipelines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def prepare_job(self, pipeline: Builder):
"""

# 介质下发——job的api可以多个IP并行执行
kwargs = self.get_kwargs.get_send_media_kwargs()
kwargs = self.get_kwargs.get_send_media_kwargs(media_type="all")
pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def cluster_replace(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKw
sub_get_kwargs.get_mongos_host_replace()

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
ExecDeletePasswordFromDBOperationComponent,
)
from backend.flow.plugins.components.collections.mongodb.exec_actuator_job import ExecuteDBActuatorJobComponent
from backend.flow.plugins.components.collections.mongodb.mongo_shutdown_meta import MongosShutdownMetaComponent
from backend.flow.utils.mongodb.mongodb_dataclass import ActKwargs


def mongo_deinstall_parallel(sub_get_kwargs: ActKwargs, nodes: list, instance_type: str) -> list:
def mongo_deinstall_parallel(sub_get_kwargs: ActKwargs, nodes: list, instance_type: str, force: bool) -> list:
acts_list = []
for node in nodes:
kwargs = sub_get_kwargs.get_mongo_deinstall_kwargs(
node_info=node, nodes_info=nodes, instance_type=instance_type
node_info=node, nodes_info=nodes, instance_type=instance_type, force=force
)
acts_list.append(
{
Expand All @@ -58,6 +59,9 @@ def deinstall(
# 获取单个cluster信息
if not reduce_mongos:
sub_get_kwargs.get_cluster_info_deinstall(cluster_id=cluster_id)
mongos_deinstall_force = False
else:
mongos_deinstall_force = True

# mongo卸载
# 复制集卸载
Expand All @@ -67,6 +71,7 @@ def deinstall(
sub_get_kwargs=sub_get_kwargs,
nodes=sub_get_kwargs.payload["nodes"],
instance_type=MongoDBInstanceType.MongoD.value,
force=False,
)
if acts_list:
sub_pipeline.add_parallel_acts(acts_list=acts_list)
Expand All @@ -77,6 +82,7 @@ def deinstall(
sub_get_kwargs=sub_get_kwargs,
nodes=sub_get_kwargs.payload["mongos_nodes"],
instance_type=MongoDBInstanceType.MongoS.value,
force=mongos_deinstall_force,
)
if acts_list:
sub_pipeline.add_parallel_acts(acts_list=acts_list)
Expand All @@ -87,6 +93,7 @@ def deinstall(
sub_get_kwargs=sub_get_kwargs,
nodes=shard_nodes["nodes"],
instance_type=MongoDBInstanceType.MongoD.value,
force=True,
)
many_acts_list.extend(acts_list)
if many_acts_list:
Expand All @@ -96,6 +103,7 @@ def deinstall(
sub_get_kwargs=sub_get_kwargs,
nodes=sub_get_kwargs.payload["config_nodes"],
instance_type=MongoDBInstanceType.MongoD.value,
force=True,
)
if acts_list:
sub_pipeline.add_parallel_acts(acts_list=acts_list)
Expand All @@ -118,10 +126,10 @@ def deinstall(

# 删除db_meta关系
if not reduce_mongos:
kwargs = {"cluster_id": cluster_id}
kwargs = sub_get_kwargs.get_meta_deinstall_kwargs(cluster_id=cluster_id)
sub_pipeline.add_act(
act_name=_("MongoDB-删除db_meta关系"),
act_component_code=ExecDeletePasswordFromDBOperationComponent.code,
act_component_code=MongosShutdownMetaComponent.code,
kwargs=kwargs,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def exec_script(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs
sub_get_kwargs.get_cluster_info_user(cluster_id=cluster_id, admin_user=admin_user)

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def increase_mongos(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKw
sub_get_kwargs.get_host_scale_mongos(info=info, increase=True)

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def instance_restart(
sub_get_kwargs.get_file_path()
sub_get_kwargs.payload["db_version"] = instances_info["instances"][0]["db_version"]
sub_get_kwargs.payload["hosts"] = instances_info["hosts"]
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def mongod_replace(
new_node = info["target"]
sub_sub_get_kwargs.payload["app"] = sub_sub_get_kwargs.payload["bk_app_abbr"]
sub_sub_get_kwargs.replicaset_info["port"] = sub_sub_get_kwargs.db_instance["port"]
force = True
if cluster_role:
sub_sub_get_kwargs.cluster_type = ClusterType.MongoShardedCluster.value
cluster_name = sub_sub_get_kwargs.db_instance["seg_range"]
Expand Down Expand Up @@ -174,6 +175,7 @@ def mongod_replace(
node_info=sub_sub_get_kwargs.payload["nodes"][0],
instance_type=MongoDBInstanceType.MongoD.value,
nodes_info=sub_sub_get_kwargs.payload["nodes"],
force=force,
)
sub_sub_pipeline.add_act(
act_name=_("MongoDB-老实例下架-{}:{}".format(info["ip"], str(sub_sub_get_kwargs.db_instance["port"]))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def mongos_replace(root_id: str, ticket_data: Optional[Dict], sub_sub_kwargs: Ac
node_info=sub_sub_get_kwargs.payload["mongos_nodes"][0],
instance_type=MongoDBInstanceType.MongoS.value,
nodes_info=sub_sub_get_kwargs.payload["mongos_nodes"],
force=True,
)
sub_sub_pipeline.add_act(
act_name=_("MongoDB-老实例下架-{}:{}".format(info["ip"], str(sub_sub_get_kwargs.db_instance["port"]))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def reduce_mongos(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwar
sub_get_kwargs.get_host_scale_mongos(info=info, increase=False)

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def replicaset_replace(
sub_get_kwargs.get_host_replace(mongodb_type=ClusterType.MongoReplicaSet.value, info=info)

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def user(
sub_get_kwargs.get_cluster_info_user(cluster_id=cluster_id, admin_user=MongoDBManagerUser.DbaUser.value)

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
kwargs = sub_get_kwargs.get_send_media_kwargs(media_type="actuator")
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)
Expand Down
13 changes: 11 additions & 2 deletions dbm-ui/backend/flow/engine/controller/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from backend.flow.engine.bamboo.scene.mongodb.mongodb_backup import MongoBackupFlow
from backend.flow.engine.bamboo.scene.mongodb.mongodb_cluster_scale_mongos import ScaleMongoSFlow
from backend.flow.engine.bamboo.scene.mongodb.mongodb_deinstall import MongoDBDeInstallFlow
from backend.flow.engine.bamboo.scene.mongodb.mongodb_exec_script import MongoExecScriptFlow
from backend.flow.engine.bamboo.scene.mongodb.mongodb_fake_install import MongoFakeInstallFlow
from backend.flow.engine.bamboo.scene.mongodb.mongodb_install import MongoDBInstallFlow
Expand Down Expand Up @@ -71,15 +72,15 @@ def create_user(self):
"""

flow = MongoUserFlow(root_id=self.root_id, data=self.ticket_data)
flow.multi_cluster_user_flow(True)
flow.multi_cluster_user_flow(create=True)

def delete_user(self):
"""
删除用户
"""

flow = MongoUserFlow(root_id=self.root_id, data=self.ticket_data)
flow.multi_cluster_user_flow(False)
flow.multi_cluster_user_flow(create=False)

def exec_script(self):
"""
Expand Down Expand Up @@ -126,3 +127,11 @@ def reduce_mongos(self):

flow = ScaleMongoSFlow(root_id=self.root_id, data=self.ticket_data)
flow.multi_cluster_mongos_flow(increase=False)

def deinstall_cluster(self):
"""
下架集群
"""

flow = MongoDBDeInstallFlow(root_id=self.root_id, data=self.ticket_data)
flow.multi_cluster_deinstall_flow()
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def decommission_proxies(self, cluster: Cluster, proxies: List[Dict]):
if _t:
raise Exception(_("{} 存在不是本集群的实例下架列表").format(_t))

machine_obj, cc_manage = defaultdict(dict), CcManage(cluster.bk_biz_id)
machine_obj, cc_manage = defaultdict(dict), CcManage(cluster.bk_biz_id, DBType.MongoDB.value)
cc_manage.delete_service_instance(bk_instance_ids=[obj.bk_instance_id for obj in proxy_objs])
for proxy_obj in proxy_objs:
logger.info("cluster proxy {} for cluster {}".format(proxy_obj, cluster.immute_domain))
Expand Down Expand Up @@ -89,7 +89,7 @@ def decommission_proxies(self, cluster: Cluster, proxies: List[Dict]):
@transaction.atomic
def decommission_backends(self, cluster: Cluster, backends: List[Dict]):
logger.info("user request decmmission backends {} {}".format(cluster.immute_domain, backends))
cc_manage = CcManage(cluster.bk_biz_id)
cc_manage = CcManage(cluster.bk_biz_id, DBType.MongoDB.value)
try:
storage_objs = common.filter_out_instance_obj(backends, cluster.storageinstance_set.all())
_t = common.not_exists(backends, storage_objs)
Expand Down Expand Up @@ -145,7 +145,9 @@ def _execute(self, data, parent_data) -> bool:
cluster_entry_obj.delete()
logger.info("cluster detail {}".format(cluster.__dict__))

CcManage(cluster.bk_biz_id).delete_cluster_modules(db_type=DBType.MongoDB.value, cluster=cluster)
CcManage(cluster.bk_biz_id, DBType.MongoDB.value).delete_cluster_modules(
db_type=DBType.MongoDB.value, cluster=cluster
)
cluster.delete()
except Exception as e:
logger.error(traceback.format_exc())
Expand Down
2 changes: 2 additions & 0 deletions dbm-ui/backend/flow/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
ClusterInstallApiView,
MongoBackupApiView,
MongoDBCreateUserView,
MongoDBDeInstallSView,
MongoDBDeleteUserView,
MongoDBExecScriptView,
MongoDBIncreaseMongoSView,
Expand Down Expand Up @@ -291,6 +292,7 @@
url(r"^scene/multi_hosts_replace$", MongoDBReplaceView.as_view()),
url(r"^scene/multi_cluster_increase_mongos$", MongoDBIncreaseMongoSView.as_view()),
url(r"^scene/multi_cluster_reduce_mongos$", MongoDBReduceMongoSView.as_view()),
url(r"^scene/cluster_deinstall$", MongoDBDeInstallSView.as_view()),
# mongodb end
# mysql upgrade
url(r"^scene/upgrade_mysql_proxy$", UpgradeMySQLProxySceneApiView.as_view()),
Expand Down
Loading

0 comments on commit e3fbb0d

Please sign in to comment.