|
| 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