Exemple #1
0
    def __init__(self, tenant, region: RegionConfig, app: ServiceGroup, upgrade_group_id, support_labels=None):
        self.tenant = tenant
        self.tenant_id = tenant.tenant_id
        self.region = region
        self.region_name = region.region_name
        self.app_id = app.app_id
        self.upgrade_group_id = upgrade_group_id
        self.app = group_repo.get_group_by_pk(tenant.tenant_id, region.region_name, app.app_id)
        self.governance_mode = app.governance_mode

        self.support_labels = support_labels

        self._component_ids = self._component_ids()
        self._components = self._create_components(app.app_id, upgrade_group_id)

        # dependency
        component_deps = dep_relation_repo.list_by_component_ids(self.tenant_id,
                                                                 [cpt.component.component_id for cpt in self._components])
        self.component_deps = list(component_deps)
        self.volume_deps = self._volume_deps()

        # plugins
        self.plugin_deps = self._plugin_deps()
        self.plugin_configs = self._plugin_configs()

        # config groups
        self.config_groups = self._config_groups()
        self.config_group_items = self._config_group_items()
        self.config_group_components = self._config_group_components()

        # labels
        self.labels = list(label_repo.get_all_labels())
    def get(self, request, *args, **kwargs):
        """
        添加特性获取可用标签
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        # 节点添加的标签和数据中心查询回来的标签才可被组件使用
        node_labels = node_label_repo.get_all_labels()
        labels_list = list()
        labels_name_list = list()
        if node_labels:
            node_labels_id_list = [label.label_id for label in node_labels]
            label_obj_list = label_repo.get_labels_by_label_ids(
                node_labels_id_list)
            for label_obj in label_obj_list:
                labels_name_list.append(label_obj.label_name)
        # 查询数据中心可使用的标签
        try:
            code, msg, data_list = label_service.get_region_labels(
                self.tenant, self.service)
            if code == 200 and data_list:
                label_name_list = []
                labels = label_repo.get_all_labels()
                if labels:
                    for label in labels:
                        label_name_list.append(label.label_name)
                for label_name in data_list:
                    if label_name not in label_name_list:
                        label_repo.create_label(label_name, label_name)
                    labels_name_list.append(label_name)
        except Exception as e:
            logger.exception(e)

        # 去除该组件已绑定的标签
        service_labels = service_label_repo.get_service_labels(
            self.service.service_id)
        if service_labels:
            service_labels_id_list = [
                label.label_id for label in service_labels
            ]
            label_obj_list = label_repo.get_labels_by_label_ids(
                service_labels_id_list)
            service_labels_name_list = [
                label.label_name for label in label_obj_list
            ]
            for service_labels_name in service_labels_name_list:
                if service_labels_name in labels_name_list:
                    labels_name_list.remove(service_labels_name)
        for labels_name in labels_name_list:
            label_dict = dict()
            label_oj = label_repo.get_labels_by_label_name(labels_name)
            label_dict["label_id"] = label_oj.label_id
            label_dict["label_alias"] = label_oj.label_alias
            labels_list.append(label_dict)

        result = general_message(200, "success", "查询成功", list=labels_list)
        return Response(result, status=result["code"])
    def __init__(self, original_app: OriginalApp, new_app: NewApp):
        self.original_app = original_app
        self.new_app = new_app

        self.tenant_name = self.new_app.tenant.tenant_name
        self.region_name = self.new_app.region_name

        self.labels = {label.label_id: label for label in label_repo.get_all_labels()}
    def list_available_labels(self, tenant, region_name):
        try:
            labels = self.get_region_labels(tenant, region_name)
            if not labels:
                return

            self._sync_labels(labels)

            label_names = [label_name for label_name in labels]
            return [
                label for label in label_repo.get_all_labels()
                if label.label_name in label_names
            ]
        except Exception as e:
            logger.exception(e)
            return []
    def _sync_labels(labels):
        label_names = [
            label.label_name for label in label_repo.get_all_labels()
        ]

        new_labels = []
        for label_name in labels:
            if label_name in label_names:
                continue
            new_labels.append(
                Labels(
                    label_id=make_uuid(),
                    label_name=label_name,
                    label_alias=label_name,
                    create_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                ))
        label_repo.bulk_create(new_labels)
    def get(self, request, *args, **kwargs):
        """
        添加特性获取可用标签
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        try:
            # 节点添加的标签和数据中心查询回来的标签才可被组件使用
            node_labels = node_label_repo.get_all_labels()
            labels_list = list()
            labels_name_list = list()
            if node_labels:
                node_labels_id_list = [label.label_id for label in node_labels]
                label_obj_list = label_repo.get_labels_by_label_ids(
                    node_labels_id_list)
                for label_obj in label_obj_list:
                    labels_name_list.append(label_obj.label_name)
            # 查询数据中心可使用的标签
            try:
                code, msg, data_list = label_service.get_region_labels(
                    self.tenant, self.service)
                if code == 200:
                    if data_list:
                        label_name_list = []
                        labels = label_repo.get_all_labels()
                        if labels:
                            for label in labels:
                                label_name_list.append(label.label_name)
                        for label_name in data_list:
                            if label_name not in label_name_list:
                                label_id = make_uuid("labels")
                                create_time = datetime.datetime.now().strftime(
                                    '%Y-%m-%d %H:%M:%S')
                                label = Labels(label_id=label_id,
                                               label_name=label_name,
                                               label_alias=label_name,
                                               create_time=create_time)
                                label.save()
                            labels_name_list.append(label_name)

            except Exception as e:
                logger.exception(e)
                pass

            # 去除该组件已绑定的标签
            service_labels = service_label_repo.get_service_labels(
                self.service.service_id)
            if service_labels:
                service_labels_id_list = [l.label_id for l in service_labels]
                label_obj_list = label_repo.get_labels_by_label_ids(
                    service_labels_id_list)
                service_labels_name_list = [
                    l.label_name for l in label_obj_list
                ]
                for service_labels_name in service_labels_name_list:
                    if service_labels_name in labels_name_list:
                        labels_name_list.remove(service_labels_name)
            for labels_name in labels_name_list:
                label_dict = dict()
                label_oj = label_repo.get_labels_by_label_name(labels_name)
                label_dict["label_id"] = label_oj.label_id
                label_dict["label_alias"] = label_oj.label_alias
                labels_list.append(label_dict)

            result = general_message(200, "success", "查询成功", list=labels_list)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
Exemple #7
0
 def get(self, request, *args, **kwargs):
     """
     添加特性获取可用标签
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     try:
         # 判断表中是否有windows标签
         labels = label_repo.get_all_labels()
         if labels:
             labels_name_list = [label.label_name for label in labels]
             if "windows" not in labels_name_list:
                 label_id = make_uuid("windows")
                 create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                 label = Labels(label_id=label_id, label_name="windows", label_alias="windows", create_time=create_time)
                 label.save()
         else:
             label_id = make_uuid("windows")
             create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
             label = Labels(label_id=label_id, label_name="windows", label_alias="windows", create_time=create_time)
             label.save()
         # 节点添加的标签和windows标签才可被服务使用
         node_labels = node_label_repo.get_all_labels()
         labels_list = list()
         if node_labels:
             node_labels_id_list = [label.label_id for label in node_labels]
             label_obj_list = label_repo.get_labels_by_label_ids(node_labels_id_list)
             labels_name_list = [label.label_name for label in label_obj_list]
             # 如果节点添加的标签中没有windows标签,就添加进去
             if "windows" not in labels_name_list:
                 labels_name_list.append("windows")
             # 去除该服务已绑定的标签
             service_labels = service_label_repo.get_service_labels(self.service.service_id)
             if service_labels:
                 service_labels_id_list = [label.label_id for label in service_labels]
                 label_obj_list = label_repo.get_labels_by_label_ids(service_labels_id_list)
                 service_labels_name_list = [label.label_name for label in label_obj_list]
                 for service_labels_name in service_labels_name_list:
                     if service_labels_name in labels_name_list:
                         labels_name_list.remove(service_labels_name)
             for labels_name in labels_name_list:
                 label_dict = dict()
                 label_oj = label_repo.get_labels_by_label_name(labels_name)
                 label_dict["label_id"] = label_oj.label_id
                 label_dict["label_alias"] = label_oj.label_alias
                 labels_list.append(label_dict)
         # 暂时只支持除节点标签外的windows标签
         else:
             label_name = "windows"
             label_dict = dict()
             win_label = label_repo.get_labels_by_label_name(label_name)
             label_dict["label_id"] = win_label.label_id
             label_dict["label_alias"] = win_label.label_alias
             labels_list.append(label_dict)
         result = general_message(200, "success", "查询成功", list=labels_list)
     except Exception as e:
         logger.exception(e)
         result = error_message(e.message)
     return Response(result, status=result["code"])