def get_service_unmnt_details(self, tenant, service, service_ids, page, page_size, q): services = service_repo.get_services_by_service_ids(service_ids) current_tenant_services_id = service_ids # 已挂载的组件路径 dep_mnts = mnt_repo.get_service_mnts(tenant.tenant_id, service.service_id) dep_volume_ids = [dep_mnt.volume_id for dep_mnt in dep_mnts] # 当前未被挂载的共享路径 service_volumes = volume_repo.get_services_volumes(current_tenant_services_id) \ .filter(volume_type__in=[self.SHARE, self.CONFIG]) \ .exclude(service_id=service.service_id) \ .exclude(ID__in=dep_volume_ids).filter(q) # 只展示无状态的组件(有状态组件的存储类型为config-file也可) volumes = list(service_volumes) copy_volumes = copy.copy(volumes) for volume in copy_volumes: service_obj = service_repo.get_service_by_service_id( volume.service_id) if service_obj: if is_state(service_obj.extend_method): if volume.volume_type != "config-file": volumes.remove(volume) total = len(volumes) volume_paginator = JuncheePaginator(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
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
def get_service_support_volume_options(self, tenant, service): base_opts = [{"volume_type": "share-file", "name_show": "共享存储(文件)"}, {"volume_type": "memoryfs", "name_show": "临时存储"}] state = False # state service if is_state(service.extend_method): state = True base_opts.append({"volume_type": "local", "name_show": "本地存储"}) body = region_api.get_volume_options(service.service_region, tenant.tenant_name) for opt in body.list: if len(opt["access_mode"]) > 0 and opt["access_mode"][0] == "RWO": if state: base_opts.append(opt) else: base_opts.append(opt) return base_opts
def check_backup_condition(self, tenant, region, group_id): """ 检测备份条件,有状态组件备份应该 """ services = group_service.get_group_services(group_id) service_ids = [s.service_id for s in services] body = region_api.service_status(region, tenant.tenant_name, { "service_ids": service_ids, "enterprise_id": tenant.enterprise_id }) status_list = body["list"] service_status_map = {status_map["service_id"]: status_map["status"] for status_map in status_list} # 处于运行中的有状态 running_state_services = [] for service in services: if is_state(service.extend_method): if service_status_map.get(service.service_id) not in ("closed", "undeploy"): running_state_services.append(service.service_cname) return 200, running_state_services
def put(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ service_name = request.data.get("service_name", None) if not service_name: return Response(general_message(400, "select the application type", "请输入修改后的名称"), status=400) extend_method = self.service.extend_method if not is_state(extend_method): return Response(general_message( 400, "stateless applications cannot be modified", "无状态组件不可修改"), status=400) self.service.service_name = service_name self.service.save() result = general_message(200, "success", "操作成功") return Response(result, status=result["code"])