コード例 #1
0
    def get_service_details(self, tenant, service):
        service_base = service.to_dict()
        service_labels = service_label_repo.get_service_labels(
            service.service_id)
        service_domains = domain_repo.get_service_domains(service.service_id)
        service_events = event_repo.get_specified_num_events(
            tenant.tenant_id, service.service_id)
        service_perms = service_perm_repo.get_service_perms_by_service_pk(
            service.ID)
        service_probes = probe_repo.get_service_probe(service.service_id)
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        service_auths = auth_repo.get_service_auth(service.service_id)
        service_env_vars = env_var_repo.get_service_env(
            tenant.tenant_id, service.service_id)
        service_compile_env = compile_env_repo.get_service_compile_env(
            service.service_id)
        service_extend_method = extend_repo.get_extend_method_by_service(
            service)
        image_service_relation = image_service_relation_repo.get_image_service_relation(
            tenant.tenant_id, service.service_id)
        service_mnts = mnt_repo.get_service_mnts(tenant.tenant_id,
                                                 service.service_id)
        service_plugin_relation = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
            service.service_id)
        service_plugin_config = service_plugin_config_repo.get_service_plugin_all_config(
            service.service_id)
        service_relation = dep_relation_repo.get_service_dependencies(
            tenant.tenant_id, service.service_id)
        service_volumes = volume_repo.get_service_volumes(service.service_id)
        service_ports = port_repo.get_service_ports(tenant.tenant_id,
                                                    service.service_id)

        app_info = {
            "service_base":
            service_base,
            "service_labels": [label.to_dict() for label in service_labels],
            "service_domains":
            [domain.to_dict() for domain in service_domains],
            "service_events": [event.to_dict() for event in service_events],
            "service_perms": [perm.to_dict() for perm in service_perms],
            "service_probes": [probe.to_dict() for probe in service_probes],
            "service_source":
            service_source.to_dict() if service_source else None,
            "service_auths": [auth.to_dict() for auth in service_auths],
            "service_env_vars":
            [env_var.to_dict() for env_var in service_env_vars],
            "service_compile_env":
            service_compile_env.to_dict() if service_compile_env else None,
            "service_extend_method":
            service_extend_method.to_dict() if service_extend_method else None,
            "image_service_relation":
            image_service_relation.to_dict()
            if image_service_relation else None,
            "service_mnts": [mnt.to_dict() for mnt in service_mnts],
            "service_plugin_relation": [
                plugin_relation.to_dict()
                for plugin_relation in service_plugin_relation
            ],
            "service_plugin_config":
            [config.to_dict() for config in service_plugin_config],
            "service_relation":
            [relation.to_dict() for relation in service_relation],
            "service_volumes":
            [volume.to_dict() for volume in service_volumes],
            "service_ports": [port.to_dict() for port in service_ports]
        }
        return app_info
コード例 #2
0
    def add_service_volume(self,
                           tenant,
                           service,
                           volume_path,
                           volume_type,
                           volume_name,
                           file_content=None):
        volume_name = volume_name.strip()
        volume_path = volume_path.strip()
        code, msg, volume_name = self.check_volume_name(service, volume_name)
        dep_mnt_names = mnt_repo.get_service_mnts(
            tenant.tenant_id, service.service_id).values_list('mnt_dir',
                                                              flat=True)
        local_path = []
        if dep_mnt_names:
            local_path.append(
                dep_mnt_names.values("mnt_dir")[0].get("mnt_dir"))
        if code != 200:
            return code, msg, None
        code, msg = self.check_volume_path(service, volume_path, local_path)
        if code != 200:
            return code, msg, None
        host_path = "/grdata/tenant/{0}/service/{1}{2}".format(
            tenant.tenant_id, service.service_id, volume_path)
        volume_data = {
            "service_id": service.service_id,
            "category": service.category,
            "host_path": host_path,
            "volume_type": volume_type,
            "volume_path": volume_path,
            "volume_name": volume_name
        }
        # region端添加数据
        if service.create_status == "complete":
            if volume_type == "config-file":
                data = {
                    "category": service.category,
                    "volume_name": volume_name,
                    "volume_path": volume_path,
                    "volume_type": volume_type,
                    "file_content": file_content,
                    "enterprise_id": tenant.enterprise_id
                }
            else:
                data = {
                    "category": service.category,
                    "volume_name": volume_name,
                    "volume_path": volume_path,
                    "volume_type": volume_type,
                    "enterprise_id": tenant.enterprise_id
                }
            res, body = region_api.add_service_volumes(service.service_region,
                                                       tenant.tenant_name,
                                                       service.service_alias,
                                                       data)
            logger.debug(body)

        volume = volume_repo.add_service_volume(**volume_data)
        if volume_type == "config-file":
            file_data = {
                "service_id": service.service_id,
                "volume_id": volume.ID,
                "file_content": file_content
            }
            cf_file = volume_repo.add_service_config_file(**file_data)
        return 200, "success", volume
コード例 #3
0
    def get_service_unmount_volume_list(self,
                                        tenant,
                                        service,
                                        service_ids,
                                        page,
                                        page_size,
                                        is_config=False):
        """
        1. 获取租户下其他所有组件列表,方便后续进行名称的冗余
        2. 获取其他组件的所有可共享的存储
        3. 获取已经使用的存储,方便后续过滤
        4. 遍历存储,组装信息
        """

        for serviceID in service_ids:
            if serviceID == service.service_id:
                service_ids.remove(serviceID)
        services = service_repo.get_services_by_service_ids(service_ids)
        state_services = []  # 有状态组件
        for svc in services:
            if is_state(svc.extend_method):
                state_services.append(svc)
        state_service_ids = [svc.service_id for svc in state_services]

        current_tenant_services_id = service_ids
        # 已挂载的组件路径
        mounted = mnt_repo.get_service_mnts(tenant.tenant_id,
                                            service.service_id)
        mounted_ids = [mnt.volume_id for mnt in mounted]
        # 当前未被挂载的共享路径
        service_volumes = []
        # 配置文件无论组件是否是共享存储都可以共享,只需过滤掉已经挂载的存储;其他存储类型则需要考虑排除有状态组件的存储
        if is_config:
            service_volumes = volume_repo.get_services_volumes(current_tenant_services_id).filter(volume_type=self.CONFIG) \
                .exclude(ID__in=mounted_ids)
        else:
            service_volumes = volume_repo.get_services_volumes(current_tenant_services_id).filter(volume_type=self.SHARE) \
                .exclude(ID__in=mounted_ids).exclude(service_id__in=state_service_ids)
        # TODO 使用函数进行存储的排查,确定哪些存储不可以进行共享,哪些存储可以共享,而不是现在这样简单的提供一个self.SHARE

        total = len(service_volumes)
        volume_paginator = JuncheePaginator(service_volumes, int(page_size))
        page_volumes = volume_paginator.page(page)
        un_mount_dependencies = []
        for volume in page_volumes:
            gs_rel = group_service_relation_repo.get_group_by_service_id(
                volume.service_id)
            group = None
            if gs_rel:
                group = group_repo.get_group_by_pk(tenant.tenant_id,
                                                   service.service_region,
                                                   gs_rel.group_id)
            un_mount_dependencies.append({
                "dep_app_name":
                services.get(service_id=volume.service_id).service_cname,
                "dep_app_group":
                group.group_name if group else '未分组',
                "dep_vol_name":
                volume.volume_name,
                "dep_vol_path":
                volume.volume_path,
                "dep_vol_type":
                volume.volume_type,
                "dep_vol_id":
                volume.ID,
                "dep_group_id":
                group.ID if group else -1,
                "dep_app_alias":
                services.get(service_id=volume.service_id).service_alias
            })
        return un_mount_dependencies, total
コード例 #4
0
    def create_region_service(self,
                              tenant,
                              service,
                              user_name,
                              do_deploy=True,
                              dep_sids=None):
        data = self.__init_create_data(tenant, service, user_name, do_deploy,
                                       dep_sids)
        service_dep_relations = dep_relation_repo.get_service_dependencies(
            tenant.tenant_id, service.service_id)
        # 依赖
        depend_ids = [{
            "dep_order": dep.dep_order,
            "dep_service_type": dep.dep_service_type,
            "depend_service_id": dep.dep_service_id,
            "service_id": dep.service_id,
            "tenant_id": dep.tenant_id
        } for dep in service_dep_relations]
        data["depend_ids"] = depend_ids
        # 端口
        ports = port_repo.get_service_ports(tenant.tenant_id,
                                            service.service_id)
        ports_info = ports.values('container_port', 'mapping_port', 'protocol',
                                  'port_alias', 'is_inner_service',
                                  'is_outer_service')

        for port_info in ports_info:
            port_info["is_inner_service"] = False
            port_info["is_outer_service"] = False

        if ports_info:
            data["ports_info"] = list(ports_info)
        # 环境变量
        envs_info = env_var_repo.get_service_env(tenant.tenant_id,
                                                 service.service_id).values(
                                                     'container_port', 'name',
                                                     'attr_name', 'attr_value',
                                                     'is_change', 'scope')
        if envs_info:
            data["envs_info"] = list(envs_info)
        # 持久化目录
        volume_info = volume_repo.get_service_volumes_with_config_file(
            service.service_id).values('ID', 'service_id', 'category',
                                       'volume_name', 'volume_path',
                                       'volume_type')
        if volume_info:
            logger.debug('--------volume_info----->{0}'.format(volume_info))
            for volume in volume_info:
                volume_id = volume['ID']
                config_file = volume_repo.get_service_config_file(volume_id)
                if config_file:
                    volume.update({"file_content": config_file.file_content})
            logger.debug(
                '--------volume_info22222----->{0}'.format(volume_info))
            data["volumes_info"] = list(volume_info)

        logger.debug(tenant.tenant_name + " start create_service:" +
                     datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
        # 挂载信息
        mnt_info = mnt_repo.get_service_mnts(service.tenant_id,
                                             service.service_id)
        if mnt_info:
            data["dep_volumes_info"] = [{
                "dep_service_id": mnt.dep_service_id,
                "volume_path": mnt.mnt_dir,
                "volume_name": mnt.mnt_name
            } for mnt in mnt_info]

        # etcd keys
        data["etcd_key"] = service.check_uuid

        # runtime os name
        data["os_type"] = label_service.get_service_os_name(service)
        # 数据中心创建
        app_id = service_group_relation_repo.get_group_id_by_service(service)
        region_app_id = region_app_repo.get_region_app_id(
            service.service_region, app_id)
        data["app_id"] = region_app_id
        region_api.create_service(service.service_region, tenant.tenant_name,
                                  data)
        # 将组件创建状态变更为创建完成
        service.create_status = "complete"
        self.__handle_service_ports(tenant, service, ports)
        service.save()
        return service
コード例 #5
0
    def get_service_details(self, tenant, service):
        service_base = service.to_dict()
        service_labels = service_label_repo.get_service_labels(
            service.service_id)
        service_domains = domain_repo.get_service_domains(service.service_id)
        service_tcpdomains = tcp_domain.get_service_tcpdomains(
            service.service_id)
        service_probes = probe_repo.get_service_probe(service.service_id)
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        service_auths = auth_repo.get_service_auth(service.service_id)
        service_env_vars = env_var_repo.get_service_env(
            tenant.tenant_id, service.service_id)
        service_compile_env = compile_env_repo.get_service_compile_env(
            service.service_id)
        service_extend_method = extend_repo.get_extend_method_by_service(
            service)
        service_mnts = mnt_repo.get_service_mnts(tenant.tenant_id,
                                                 service.service_id)
        service_volumes = volume_repo.get_service_volumes_with_config_file(
            service.service_id)
        service_config_file = volume_repo.get_service_config_files(
            service.service_id)
        service_ports = port_repo.get_service_ports(tenant.tenant_id,
                                                    service.service_id)
        service_relation = dep_relation_repo.get_service_dependencies(
            tenant.tenant_id, service.service_id)
        service_monitors = service_monitor_repo.get_component_service_monitors(
            tenant.tenant_id, service.service_id)
        component_graphs = component_graph_repo.list(service.service_id)
        # plugin
        service_plugin_relation = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
            service.service_id)
        service_plugin_config = service_plugin_config_repo.get_service_plugin_all_config(
            service.service_id)
        # third_party_service
        third_party_service_endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
            service.service_id)
        if service.service_source == "third_party":
            if not third_party_service_endpoints:
                raise ServiceHandleException(
                    msg="third party service endpoints can't be null",
                    msg_show="第三方组件实例不可为空")
        app_info = {
            "service_base":
            service_base,
            "service_labels": [label.to_dict() for label in service_labels],
            "service_domains":
            [domain.to_dict() for domain in service_domains],
            "service_tcpdomains":
            [tcpdomain.to_dict() for tcpdomain in service_tcpdomains],
            "service_probes": [probe.to_dict() for probe in service_probes],
            "service_source":
            service_source.to_dict() if service_source else None,
            "service_auths": [auth.to_dict() for auth in service_auths],
            "service_env_vars":
            [env_var.to_dict() for env_var in service_env_vars],
            "service_compile_env":
            service_compile_env.to_dict() if service_compile_env else None,
            "service_extend_method":
            service_extend_method.to_dict() if service_extend_method else None,
            "service_mnts": [mnt.to_dict() for mnt in service_mnts],
            "service_plugin_relation": [
                plugin_relation.to_dict()
                for plugin_relation in service_plugin_relation
            ],
            "service_plugin_config":
            [config.to_dict() for config in service_plugin_config],
            "service_relation":
            [relation.to_dict() for relation in service_relation],
            "service_volumes":
            [volume.to_dict() for volume in service_volumes],
            "service_config_file":
            [config_file.to_dict() for config_file in service_config_file],
            "service_ports": [port.to_dict() for port in service_ports],
            "third_party_service_endpoints":
            [endpoint.to_dict() for endpoint in third_party_service_endpoints],
            "service_monitors":
            [monitor.to_dict() for monitor in service_monitors],
            "component_graphs":
            [graph.to_dict() for graph in component_graphs]
        }
        plugin_ids = [pr.plugin_id for pr in service_plugin_relation]

        return app_info, plugin_ids