Skip to content

Commit 3725d29

Browse files
yyhenryyyiSecloud
authored andcommitted
feat(backend): oracle迁移元数据 #14411
1 parent cbf286f commit 3725d29

File tree

4 files changed

+481
-0
lines changed

4 files changed

+481
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
5+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at https://opensource.org/licenses/MIT
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
specific language governing permissions and limitations under the License.
10+
"""
11+
import logging.config
12+
from typing import Dict, Optional
13+
14+
from django.utils.translation import gettext as _
15+
16+
from backend.flow.consts import DEPENDENCIES_PLUGINS
17+
from backend.flow.engine.bamboo.scene.common.builder import Builder
18+
from backend.flow.plugins.components.collections.common.install_nodeman_plugin import (
19+
InstallNodemanPluginServiceComponent,
20+
)
21+
from backend.flow.plugins.components.collections.oracle.oracle_migrate_meta import OracleMigrateMetaComponent
22+
from backend.flow.utils.oracle.oracle_migrate_meta_dataclass import MigrateActKwargs
23+
24+
logger = logging.getLogger("flow")
25+
26+
27+
class OracleMigrateMetaFlow(object):
28+
"""元数据迁移flow"""
29+
30+
def __init__(self, root_id: str, data: Optional[Dict]):
31+
"""
32+
传入参数
33+
@param root_id : 任务流程定义的root_id
34+
@param data : 单据传递过来的参数列表,是dict格式
35+
"""
36+
37+
self.root_id = root_id
38+
self.data = data
39+
self.get_kwargs = MigrateActKwargs()
40+
self.get_kwargs.source_cluster_info = data
41+
self.get_kwargs.get_storages()
42+
self.get_kwargs.bk_biz_id = data.get("bk_biz_id")
43+
44+
def cluster_migrate_flow(self):
45+
"""
46+
cluster migrate流程
47+
"""
48+
49+
# 创建流程实例
50+
pipeline = Builder(root_id=self.root_id, data=self.data)
51+
52+
# 检查 是否已经迁移 从目标环境检查迁移ip是否复用
53+
kwargs = self.get_kwargs.get_check_dest_cluster_info(
54+
cluster_name=self.get_kwargs.source_cluster_info.get("name")
55+
)
56+
pipeline.add_act(
57+
act_name=_("检查cluster目标端是否存在"), act_component_code=OracleMigrateMetaComponent.code, kwargs=kwargs
58+
)
59+
60+
# 检查机器规格是否在目标端存在
61+
kwargs = self.get_kwargs.get_check_spec_info()
62+
pipeline.add_act(act_name=_("检查目标端机器规格"), act_component_code=OracleMigrateMetaComponent.code, kwargs=kwargs)
63+
64+
# 目标业务更新dba 检查目标业务的dba,不一致则更新
65+
kwargs = self.get_kwargs.get_dba_info()
66+
pipeline.add_act(act_name=_("更新dba"), act_component_code=OracleMigrateMetaComponent.code, kwargs=kwargs)
67+
68+
# 保存密码到密码服务 perfstat execute_user
69+
kwargs = self.get_kwargs.get_save_password_info()
70+
pipeline.add_act(act_name=_("保存密码"), act_component_code=OracleMigrateMetaComponent.code, kwargs=kwargs)
71+
72+
# 迁移数据
73+
kwargs = self.get_kwargs.get_migrate_info()
74+
pipeline.add_act(
75+
act_name=_("迁移meta"),
76+
act_component_code=OracleMigrateMetaComponent.code,
77+
kwargs=kwargs,
78+
)
79+
80+
# 修改dns的app字段
81+
kwargs = self.get_kwargs.get_change_dns_app_info()
82+
pipeline.add_act(act_name=_("更新dns的app"), act_component_code=OracleMigrateMetaComponent.code, kwargs=kwargs)
83+
84+
# 安装蓝鲸插件
85+
acts_list = []
86+
for plugin_name in DEPENDENCIES_PLUGINS:
87+
acts_list.append(
88+
{
89+
"act_name": _("安装[{}]插件".format(plugin_name)),
90+
"act_component_code": InstallNodemanPluginServiceComponent.code,
91+
"kwargs": self.get_kwargs.get_install_plugin_info(plugin_name=plugin_name),
92+
}
93+
)
94+
pipeline.add_parallel_acts(acts_list=acts_list)
95+
96+
# 运行流程
97+
pipeline.run_pipeline()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
5+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at https://opensource.org/licenses/MIT
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
specific language governing permissions and limitations under the License.
10+
"""
11+
import logging
12+
from typing import List
13+
14+
from pipeline.component_framework.component import Component
15+
from pipeline.core.flow.activity import Service
16+
17+
from backend.flow.plugins.components.collections.common.base_service import BaseService
18+
from backend.flow.utils.oracle.migrate_meta import OracleMigrateMeta
19+
20+
logger = logging.getLogger("flow")
21+
22+
23+
class OracleMigrateMetaService(BaseService):
24+
"""
25+
根据单据类型执行相关功能
26+
"""
27+
28+
def _execute(self, data, parent_data) -> bool:
29+
kwargs = data.get_one_of_inputs("kwargs")
30+
# global_data = data.get_one_of_inputs("global_data")
31+
trans_data = data.get_one_of_inputs("trans_data")
32+
33+
# if trans_data is None or trans_data == "${trans_data}":
34+
# # 表示没有加载上下文内容,则在此添加
35+
# trans_data = getattr(flow_context, kwargs["set_trans_data_dataclass"])()
36+
#
37+
# if kwargs["is_update_trans_data"]:
38+
# # 表示合并上下文的内容
39+
# cluster_info = {**asdict(trans_data), **kwargs["cluster"]}
40+
# self.log_info(_("集群元信息:{}").format(cluster_info))
41+
result = OracleMigrateMeta(info=kwargs).action()
42+
self.log_info("oracle operate successfully")
43+
data.outputs["trans_data"] = trans_data
44+
return result
45+
46+
def inputs_format(self) -> List:
47+
return [
48+
Service.InputItem(name="kwargs", key="kwargs", type="dict", required=True),
49+
Service.InputItem(name="global_data", key="global_data", type="dict", required=True),
50+
]
51+
52+
53+
class OracleMigrateMetaComponent(Component):
54+
name = __name__
55+
code = "oracle_migrate_meta"
56+
bound_service = OracleMigrateMetaService
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
5+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at https://opensource.org/licenses/MIT
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
specific language governing permissions and limitations under the License.
10+
"""
11+
12+
import logging
13+
14+
from django.utils.translation import gettext as _
15+
16+
from backend.components import DnsApi
17+
from backend.configuration.constants import DBType
18+
from backend.configuration.handlers.dba import DBAdministratorHandler
19+
from backend.db_meta.api.cluster.oracle.primary_standby_create import pkg_create_oracle
20+
from backend.db_meta.models import Cluster
21+
from backend.flow.utils.oracle.oracle_password import OraclePassword
22+
23+
logger = logging.getLogger("flow")
24+
25+
26+
class OracleMigrateMeta(object):
27+
"""oracle迁移元数据flow节点函数"""
28+
29+
def __init__(self, info: dict):
30+
self.info = info
31+
32+
def action(self) -> bool:
33+
function_name = self.info["meta_func_name"].lower()
34+
if hasattr(self, function_name):
35+
return getattr(self, function_name)()
36+
37+
logger.error(_("找不到单据类型,请联系系统管理员"))
38+
return False
39+
40+
def check_dest_cluster(self):
41+
"""检查目标环境是否已经存在该cluster"""
42+
43+
if Cluster.objects.filter(name=self.info["cluster_name"], bk_biz_id=self.info["bk_biz_id"]).count() > 0:
44+
logger.error(
45+
"error: cluster:{} has of bk_biz_id:{} been existed".format(
46+
self.info["cluster_name"], str(self.info["bk_biz_id"])
47+
)
48+
)
49+
raise ValueError(
50+
"error: cluster:{} has of bk_biz_id:{} been existed".format(
51+
self.info["cluster_name"], str(self.info["bk_biz_id"])
52+
)
53+
)
54+
55+
def check_machine_spec(self):
56+
"""检查机器规格"""
57+
58+
if not self.info["spec"]["spec_id"]:
59+
logger.error("error: machine spec of destination is not exist about {}".format(DBType.Oracle.value))
60+
raise ValueError("error: machine spec of destination is not exist about {}".format(DBType.Oracle.value))
61+
62+
def upsert_dba(self):
63+
"""更新dba"""
64+
65+
DBAdministratorHandler.upsert_biz_admins(self.info["bk_biz_id"], self.info["db_admins"])
66+
67+
def save_password(self):
68+
"""保存密码到密码服务 perfstat execute_user"""
69+
70+
for username in self.info["usernames"]:
71+
for password_info in self.info["password_infos"]:
72+
result = OraclePassword().save_password_to_db(
73+
instances=password_info["nodes"],
74+
username=username,
75+
password=password_info["password"][username],
76+
operator=self.info["operator"],
77+
)
78+
if result:
79+
logger.error(
80+
"nodes:{} save user:{} password fail, error: {}".format(
81+
password_info["nodes"], username, result
82+
)
83+
)
84+
return False
85+
86+
def migrate_cluster(self):
87+
"""迁移集群"""
88+
89+
# 写入meta
90+
try:
91+
pkg_create_oracle(
92+
bk_biz_id=self.info["bk_biz_id"],
93+
name=self.info["name"],
94+
immute_domain=self.info["immute_domain"],
95+
db_module_id=self.info["db_module_id"],
96+
alias=self.info["alias"],
97+
major_version=self.info["major_version"],
98+
storages=self.info["storages"],
99+
creator=self.info["creator"],
100+
bk_cloud_id=self.info["bk_cloud_id"],
101+
region=self.info["region"],
102+
spec_id=self.info["spec_id"],
103+
spec_config=self.info["spec_config"],
104+
cluster_type=self.info["cluster_type"],
105+
disaster_tolerance_level=self.info["disaster_tolerance_level"],
106+
)
107+
except Exception as e:
108+
logger.error("add relationship to meta fail, error:{}".format(str(e)))
109+
return False
110+
logger.info("add mongodb relationship to meta successfully")
111+
return True
112+
113+
def change_domain_app(self):
114+
"""修改dns的app字段"""
115+
116+
for domain in self.info["change_domain_app"]:
117+
domain_name = domain if domain.endswith(".") else "{}.".format(domain)
118+
try:
119+
DnsApi.update_domain_belong_app(
120+
{
121+
"app": self.info["app"],
122+
"new_app": self.info["new_app"],
123+
"domain_name": domain_name,
124+
"bk_cloud_id": self.info["bk_cloud_id"],
125+
}
126+
)
127+
except Exception as e:
128+
logger.error(
129+
"change domain:{} dns app fail, from old app:{} to new app:{}, error:{}".format(
130+
domain_name, self.info["app"], self.info["new_app"], str(e)
131+
)
132+
)
133+
raise ValueError(
134+
"change domain:{} dns app fail, from old app:{} to new app:{}, error:{}".format(
135+
domain_name, self.info["app"], self.info["new_app"], str(e)
136+
)
137+
)

0 commit comments

Comments
 (0)