def get_volume_dependent(self, tenant, service): mnts = mnt_repo.get_by_dep_service_id(tenant.tenant_id, service.service_id) if not mnts: return None service_ids = [mnt.service_id for mnt in mnts] services = service_repo.get_services_by_service_ids(service_ids) # to dict id_to_services = {} for svc in services: if not id_to_services.get(svc.service_id, None): id_to_services[svc.service_id] = [svc] continue id_to_services[svc.service_id].append(svc) result = [] for mnt in mnts: # get volume vol = volume_repo.get_service_volume_by_name(service.service_id, mnt.mnt_name) # services that depend on this volume services_dep_vol = id_to_services[mnt.service_id] for svc in services_dep_vol: result.append({ "volume_name": vol.volume_name, "service_name": svc.service_cname, "service_alias": svc.service_alias, }) return result
def get_group_app_metadata(self, group_id, tenant): all_data = dict() compose_group_info = compose_repo.get_group_compose_by_group_id( group_id) compose_service_relation = None if compose_group_info: compose_service_relation = compose_relation_repo.get_compose_service_relation_by_compose_id( compose_group_info.compose_id) group_info = group_repo.get_group_by_id(group_id) service_group_relations = group_service_relation_repo.get_services_by_group( group_id) service_ids = [sgr.service_id for sgr in service_group_relations] services = service_repo.get_services_by_service_ids(*service_ids) all_data["compose_group_info"] = compose_group_info.to_dict( ) if compose_group_info else None all_data["compose_service_relation"] = [ relation.to_dict() for relation in compose_service_relation ] if compose_service_relation else None all_data["group_info"] = group_info.to_dict() all_data["service_group_relation"] = [ sgr.to_dict() for sgr in service_group_relations ] apps = [] for service in services: app_info = self.get_service_details(tenant, service) apps.append(app_info) all_data["apps"] = apps return json.dumps(all_data)
def get_group_app_metadata(self, group_id, tenant): all_data = dict() compose_group_info = compose_repo.get_group_compose_by_group_id( group_id) compose_service_relation = None if compose_group_info: compose_service_relation = compose_relation_repo.get_compose_service_relation_by_compose_id( compose_group_info.compose_id) group_info = group_repo.get_group_by_id(group_id) service_group_relations = group_service_relation_repo.get_services_by_group( group_id) service_ids = [sgr.service_id for sgr in service_group_relations] services = service_repo.get_services_by_service_ids(*service_ids) all_data["compose_group_info"] = compose_group_info.to_dict( ) if compose_group_info else None all_data["compose_service_relation"] = [ relation.to_dict() for relation in compose_service_relation ] if compose_service_relation else None all_data["group_info"] = group_info.to_dict() all_data["service_group_relation"] = [ sgr.to_dict() for sgr in service_group_relations ] apps = [] total_memory = 0 for service in services: if service.service_source == "third_party" or service.create_status != "complete": continue total_memory += service.min_memory * service.min_node app_info = self.get_service_details(tenant, service) apps.append(app_info) all_data["apps"] = apps return total_memory, json.dumps(all_data)
def delete(self, request, *args, **kwargs): """ 批量删除组件 --- parameters: - name: tenantName description: 租户名 required: true type: string paramType: path - name: service_ids description: 批量操作的组件ID 多个以英文逗号分隔 required: true type: string paramType: form """ service_ids = request.data.get("service_ids", None) service_id_list = service_ids.split(",") services = service_repo.get_services_by_service_ids(service_id_list) msg_list = [] for service in services: code, msg = app_manage_service.batch_delete(self.user, self.tenant, service, is_force=True) msg_dict = dict() msg_dict['status'] = code msg_dict['msg'] = msg msg_dict['service_id'] = service.service_id msg_dict['service_cname'] = service.service_cname msg_list.append(msg_dict) code = 200 result = general_message(code, "success", "操作成功", list=msg_list) return Response(result, status=result['code'])
def get_current_region_service_events(self, region, team): events = event_repo.get_specified_region_events(team.tenant_id, region) # paginator = JuncheePaginator(events, int(page_size)) # show_events = paginator.page(int(page)) service_ids = [e.service_id for e in events] services = service_repo.get_services_by_service_ids(service_ids) id_service_map = {s.service_id: s for s in services} event_list = [] try: self.__sync_region_service_event_status(region, team.tenant_name, events, False) except Exception as e: logger.exception("synchorized services events error !") for event in events: result = event.to_dict() result["nick_name"] = result["user_name"] result["type_cn"] = result["user_name"] s = id_service_map.get(event.service_id, None) if s: result["service_alias"] = s.service_alias result["service_cname"] = s.service_cname else: if event.type == "truncate": result["service_alias"] = event.message result["service_cname"] = event.message event_list.append(result) return event_list
def __is_service_mnt_related(self, tenant, service): sms = mnt_repo.get_mount_current_service(tenant.tenant_id, service.service_id) if sms: sids = [sm.service_id for sm in sms] services = service_repo.get_services_by_service_ids(*sids).values_list("service_cname", flat=True) mnt_service_names = ",".join(list(services)) return True, mnt_service_names return False, ""
def get_group_app_metadata(self, group_id, tenant): all_data = dict() compose_group_info = compose_repo.get_group_compose_by_group_id(group_id) compose_service_relation = None if compose_group_info: compose_service_relation = compose_relation_repo.get_compose_service_relation_by_compose_id( compose_group_info.compose_id) group_info = group_repo.get_group_by_id(group_id) service_group_relations = group_service_relation_repo.get_services_by_group(group_id) service_ids = [sgr.service_id for sgr in service_group_relations] services = service_repo.get_services_by_service_ids(service_ids) all_data["compose_group_info"] = compose_group_info.to_dict() if compose_group_info else None all_data["compose_service_relation"] = [ relation.to_dict() for relation in compose_service_relation] if compose_service_relation else None all_data["group_info"] = group_info.to_dict() all_data["service_group_relation"] = [sgr.to_dict() for sgr in service_group_relations] apps = [] total_memory = 0 plugin_ids = [] for service in services: if service.service_source == "third_party" or service.create_status != "complete": continue total_memory += service.min_memory * service.min_node app_info, pids = self.get_service_details(tenant, service) plugin_ids.extend(pids) apps.append(app_info) all_data["apps"] = apps # plugin plugins = [] plugin_build_versions = [] plugin_config_groups = [] plugin_config_items = [] for plugin_id in plugin_ids: plugin = plugin_repo.get_plugin_by_plugin_id(tenant.tenant_id, plugin_id) if plugin is None: continue plugins.append(plugin.to_dict()) bv = build_version_repo.get_last_ok_one(plugin_id, tenant.tenant_id) if bv is None: continue plugin_build_versions.append(bv.to_dict()) pcgs = plugin_config_group_repo.list_by_plugin_id(plugin_id) if pcgs: plugin_config_groups.extend([p.to_dict() for p in pcgs]) pcis = plugin_config_items_repo.list_by_plugin_id(plugin_id) if pcis: plugin_config_items.extend([p.to_dict() for p in pcis]) all_data["plugin_info"] = {} all_data["plugin_info"]["plugins"] = plugins all_data["plugin_info"]["plugin_build_versions"] = plugin_build_versions all_data["plugin_info"]["plugin_config_groups"] = plugin_config_groups all_data["plugin_info"]["plugin_config_items"] = plugin_config_items return total_memory, json.dumps(all_data)
def get_service_unmnt_details(self, tenant, service, service_ids, page, page_size): services = service_repo.get_services_by_service_ids(*service_ids) current_tenant_services_id = service_ids # 已挂载的服务路径 dep_mnt_names = mnt_repo.get_service_mnts( tenant.tenant_id, service.service_id).values_list('mnt_name', flat=True) # 当前未被挂载的共享路径 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( volume_name__in=dep_mnt_names) # 只展示无状态的服务组件(有状态服务的存储类型为config-file也可) logger.debug('----------volumes----->{0}'.format( type(service_volumes))) volumes = list(service_volumes) for volume in volumes: service_obj = service_repo.get_service_by_service_id( volume.service_id) if service_obj: if service_obj.extend_method != "stateless" and 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(self, request, team_id, region_name, app_id, *args, **kwargs): is_outer = request.GET.get("is_outer", False) if is_outer == "true": is_outer = True data = [] start = request.GET.get("start") end = request.GET.get("end") step = request.GET.get("step", 60) if not start or not end: raise ServiceHandleException(msg="params error", msg_show="缺少query参数") services_relation = group_service_relation_repo.get_services_by_group(self.app.ID) service_ids = services_relation.values_list('service_id', flat=True) if service_ids: services = service_repo.get_services_by_service_ids(service_ids).exclude(service_source="third_party") for service in services: is_outer_service = True has_plugin = False service_abled_plugins = app_plugin_service.get_service_abled_plugin(service) for plugin in service_abled_plugins: if plugin.category == PluginCategoryConstants.PERFORMANCE_ANALYSIS: has_plugin = True if is_outer: is_outer_service = False tenant_service_ports = port_service.get_service_ports(service) for service_port in tenant_service_ports: if service_port.is_outer_service: is_outer_service = True break if has_plugin and is_outer_service: dt = { "service_id": service.service_id, "service_cname": service.service_cname, "service_alias": service.service_alias, "monitors": [] } for k, v in list(monitor_query_range_items.items()): monitor = {"monitor_item": k} body = {} try: res, body = region_api.get_query_range_data(self.region_name, self.team.tenant_name, v % (service.service_id, start, end, step)) except Exception as e: logger.debug(e) if body.get("data"): if body["data"]["result"]: result_list = [] for result in body["data"]["result"]: result["value"] = [str(value) for value in result["value"]] result_list.append(result) body["data"]["result"] = result_list monitor.update(body) dt["monitors"].append(monitor) data.append(dt) serializers = ComponentMonitorSerializers(data=data, many=True) serializers.is_valid(raise_exception=True) return Response(serializers.data, status=200)
def delete(self, req, app_id, *args, **kwargs): msg_list = [] try: force = int(req.GET.get("force", 0)) except ValueError: raise ServiceHandleException(msg='force value error', msg_show=u"参数错误") service_ids = app_service.get_group_services_by_id(self.app.ID) services = service_repo.get_services_by_service_ids(service_ids) if services: status_list = base_service.status_multi_service( region=self.app.region_name, tenant_name=self.team.tenant_name, service_ids=service_ids, enterprise_id=self.team.enterprise_id) status_list = filter(lambda x: x not in ["closed", "undeploy"], map(lambda x: x["status"], status_list)) if len(status_list) > 0: raise ServiceHandleException( msg= "There are running components under the current application", msg_show=u"当前应用下有运行态的组件,不可删除") else: code_status = 200 for service in services: code, msg = app_manage_service.batch_delete(self.user, self.team, service, is_force=True) msg_dict = dict() msg_dict['status'] = code msg_dict['msg'] = msg msg_dict['service_id'] = service.service_id msg_dict['service_cname'] = service.service_cname msg_list.append(msg_dict) if code != 200: code_status = code if force: code_status = 200 app_manage_service.delete_again(self.user, self.team, service, is_force=True) if code_status != 200: raise ServiceHandleException(msg=msg_list, msg_show=u"请求错误") else: code, msg, data = group_service.delete_group_no_service( self.app.ID) if code != 200: raise ServiceHandleException(msg=msg, msg_show=u"请求错误") return Response(None, status=code) code, msg, data = group_service.delete_group_no_service(self.app.ID) if code != 200: raise ServiceHandleException(msg=msg, msg_show=u"请求错误") return Response(None, status=code)
def __is_service_related(self, tenant, service): tsrs = dep_relation_repo.get_dependency_by_dep_id(tenant.tenant_id, service.service_id) if tsrs: sids = [tsr.service_id for tsr in tsrs] services = service_repo.get_services_by_service_ids(*sids).values_list("service_cname", flat=True) if not services: return False, "" dep_service_names = ",".join(list(services)) return True, dep_service_names return False, ""
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_services_events(self, page, page_size, create_time, status, team): query = Q() status = "success" if status == "complete" else status if team: query &= Q(tenant_id=team.tenant_id) if create_time: query &= Q(start_time__gte=create_time) if status: query &= Q(status=status) events = ServiceEvent.objects.filter(query).order_by("-ID") logger.debug(events.query) total = events.count() paginator = JuncheePaginator(events, int(page_size)) show_events = paginator.page(int(page)) service_ids = list(set([e.service_id for e in show_events])) team_ids = list(set([e.tenant_id for e in show_events])) teams = team_repo.get_team_by_team_ids(team_ids) services = service_repo.get_services_by_service_ids(service_ids) id_service_map = {s.service_id: s for s in services} id_team_map = {t.tenant_id: t for t in teams} # 数据中心对应的event region_events_map = {} for event in show_events: service = id_service_map.get(event.service_id, None) t = id_team_map.get(event.tenant_id, None) if service: event.service_cname = service.service_cname event.service_alias = service.service_alias event.team_name = t.tenant_name if t else None event.service_region = service.service_region # 处理数据中心对应的event if event.final_status == "" and not status: region_events = region_events_map.get( service.service_region, []) if region_events: region_events.append(event) else: region_events_map[service.service_region] = [event] else: event.service_cname = None event.service_alias = None event.team_name = None event.service_region = None if not status: # 从数据中心更新信息 for region, events in region_events_map.iteritems(): # 同步数据中心信息 self.__sync_events(region, events) return show_events, total
def patch_add_dependency(self, tenant, service, dep_service_ids): dep_service_relations = dep_relation_repo.get_dependency_by_dep_service_ids(tenant.tenant_id, service.service_id, dep_service_ids) dep_ids = [dep.dep_service_id for dep in dep_service_relations] services = service_repo.get_services_by_service_ids(*dep_ids) if dep_service_relations: service_cnames = [s.service_cname for s in services] return 412, u"应用{0}已被关联".format(service_cnames) for dep_id in dep_service_ids: code, msg, relation = self.add_service_dependency(tenant, service, dep_id) if code != 200: return code, msg return 200, u"success"
def get_service_by_service_key_and_group_id(self, service_key, group_id): rst = None services = group_service_relation_repo.get_services_by_group(group_id) if not services: return rst service_ids = [service.service_id for service in services] services = service_repo.get_services_by_service_ids(service_ids) if not services: return rst for service in services: if service.service_key == service_key: rst = service break return rst
def get_plugin_used_services(self, plugin_id, tenant_id, page, page_size): aprr = app_plugin_relation_repo.get_used_plugin_services(plugin_id) service_ids = [r.service_id for r in aprr] service_plugin_version_map = {r.service_id: r.build_version for r in aprr} services = service_repo.get_services_by_service_ids(*service_ids).filter(tenant_id=tenant_id) paginator = JuncheePaginator(services, int(page_size)) show_apps = paginator.page(int(page)) data = dict() for s in show_apps: data["service_id"] = s.service_id data["service_alias"] = s.service_alias data["service_cname"] = s.service_cname data["build_version"] = service_plugin_version_map[s.service_id] return data
def delete(self, request, *args, **kwargs): """ 批量删除应用 --- parameters: - name: tenantName description: 租户名 required: true type: string paramType: path - name: service_ids description: 批量操作的服务ID 多个以英文逗号分隔 required: true type: string paramType: form """ try: service_ids = request.data.get("service_ids", None) identitys = team_services.get_user_perm_identitys_in_permtenant( user_id=self.user.user_id, tenant_name=self.tenant_name) perm_tuple = team_services.get_user_perm_in_tenant( user_id=self.user.user_id, tenant_name=self.tenant_name) if "delete_service" not in perm_tuple and "owner" not in identitys and "admin" \ not in identitys and "developer" not in identitys: return Response(general_message(400, "Permission denied", "没有删除应用权限"), status=400) service_id_list = service_ids.split(",") services = service_repo.get_services_by_service_ids( service_id_list) msg_list = [] for service in services: code, msg = app_manage_service.batch_delete(self.user, self.tenant, service, is_force=True) msg_dict = dict() msg_dict['status'] = code msg_dict['msg'] = msg msg_dict['service_id'] = service.service_id msg_dict['service_cname'] = service.service_cname msg_list.append(msg_dict) code = 200 result = general_message(code, "success", "操作成功", list=msg_list) return Response(result, status=result['code']) except Exception as e: logger.exception(e)
def get_service_unmnt_details(self, tenant, service, service_ids, page, page_size): services = service_repo.get_services_by_service_ids(*service_ids) current_tenant_services_id = service_ids # 已挂载的服务路径 dep_mnt_names = mnt_repo.get_service_mnts( tenant.tenant_id, service.service_id).values_list('mnt_name', flat=True) # 当前未被挂载的共享路径 volumes = volume_repo.get_services_volumes( current_tenant_services_id).filter(volume_type=self.SHARE).exclude( service_id=service.service_id).exclude( volume_name__in=dep_mnt_names) 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 batch_action(self, tenant, user, action, service_ids): services = service_repo.get_services_by_service_ids(*service_ids) code = 500 msg = "系统异常" fail_service_name = [] for service in services: try: if action == "start": self.start(tenant, service, user) elif action == "stop": self.stop(tenant, service, user) elif action == "restart": self.restart(tenant, service, user) code = 200 msg = "success" except Exception as e: fail_service_name.append(service.service_cname) logger.exception(e) logger.debug("fail service names {0}".format(fail_service_name)) return code, msg
def get_compose_services(self, compose_id): compse_service_relations = compose_relation_repo.get_compose_service_relation_by_compose_id(compose_id) service_ids = [csr.service_id for csr in compse_service_relations] service_ids = list(service_ids) return service_repo.get_services_by_service_ids(service_ids)
def get_group_services(self, group_id): """查询某一应用下的组件""" gsr = group_service_relation_repo.get_services_by_group(group_id) service_ids = [gs.service_id for gs in gsr] services = service_repo.get_services_by_service_ids(service_ids) return services
def get_service_dependencies(self, tenant, service): dep_ids = self.__get_dep_service_ids(tenant, service) services = service_repo.get_services_by_service_ids(*dep_ids) return services
def get_services_dependend_on_current_services(self, tenant, service): relations = dep_relation_repo.get_services_dep_current_service(tenant.tenant_id, service.service_id) service_ids = [r.service_id for r in relations] return service_repo.get_services_by_service_ids(*service_ids)