def _update_probe(self, probe): old_probes = {probe.mode: probe for probe in self.probes} new_probes = [] add = probe.get("add") if add: new_probes.extend(add) upd = probe.get("upd") if upd: new_probes.extend(upd) # There can only be one probe of the same mode # Dedup new probes based on mode new_probes = {probe["mode"]: probe for probe in new_probes} probes = [] for key in new_probes: probe = new_probes[key] old_probe = old_probes.get(probe["mode"]) if not old_probe: # create new probe probe["probe_id"] = make_uuid() probe["service_id"] = self.component.component_id probes.append(ServiceProbe(**probe)) continue # update probe probe = ServiceProbe(**probe) probe.ID = old_probe.ID probe.service_id = self.component.component_id probes.append(probe) self.probes = probes self.update_action_type(ActionType.UPDATE.value)
def post(self, request, *args, **kwargs): """ 探针信息添加 --- serializer: ServerProbeSerializer """ try: mode = request.POST.get("mode", None) if (not mode) or ((mode != "readiness") and (mode != "liveness")): return errResponseJson( "mode can not be empty and only is readiness os liveness", "参数错误,探针模式未指定", 412) port = request.POST.get("port", None) if not port: return errResponseJson("port can not be empty.", "参数错误,端口未指定", 412) port = int(port) service_probe = ServiceProbe( service_id=self.service.service_id, scheme=request.POST.get("scheme", "tcp"), path=request.POST.get("path", ""), port=port, cmd=request.POST.get("cmd", ""), http_header=request.POST.get("http_header", ""), initial_delay_second=int(request.POST.get("initial_delay_second", 1)), period_second=int(request.POST.get("period_second", 3)), timeout_second=int(request.POST.get("timeout_second", 30)), failure_threshold=int(request.POST.get("failure_threshold", 3)), success_threshold=int(request.POST.get("success_threshold", 1)), is_used=bool(request.POST.get("is_used", True)), probe_id=make_uuid(), mode=mode) if not ServiceProbe.objects.filter( service_id=service_probe.service_id, mode=service_probe.mode): json_data = model_to_dict(service_probe) is_used = 1 if json_data["is_used"] else 0 json_data.update({"is_used": is_used}) json_data["enterprise_id"] = self.tenant.enterprise_id res, body = region_api.add_service_probe(self.service.service_region, self.tenantName, self.service.service_alias, json_data) if 400 <= res.status <= 600: return errResponseJson("region error.", "数据中心操作失败", 500) service_probe.save() else: return errResponseJson( "this {} probe can only have one in same service.".format( service_probe.mode), "应用探测设置重复", 400) return successResponseJson(bean=model_to_dict(service_probe)) except Exception as e: logger.debug("---------------- {}".format(json_data)) logger.exception(e) return errResponseJson(e.message, "系统异常,处理错误", 500)
def __save_service_probes(self, service, service_probes): service_probe_list = [] for probe in service_probes: probe.pop("ID") new_service_probe = ServiceProbe(**probe) new_service_probe.service_id = service.service_id service_probe_list.append(new_service_probe) if service_probe_list: ServiceProbe.objects.bulk_create(service_probe_list)
def post(self, request, probe_id, *args, **kwargs): """ 探针信息更改 --- serializer: ServerProbeSerializer parameters: - name: probe_id description: 探针id required: true type: string paramType: path """ try: probe_list = ServiceProbe.objects.filter(probe_id=probe_id) if not probe_list: return errResponseJson("the probe not exist.", "配置不存在", 404) probe = probe_list[0] mode = request.POST.get("mode", None) if (not mode) or ((mode != "readiness") and (mode != "liveness")): return errResponseJson( "mode can not be empty and only is readiness os liveness", "参数错误,探针模式未指定", 412) port = int(request.POST.get("port", None)) if not port: return errResponseJson("port can not be empty.", "参数错误,端口未指定", 412) service_probe = ServiceProbe( probe_id=probe_id, service_id=self.service.service_id, scheme=request.POST.get("scheme", "tcp"), path=request.POST.get("path", ""), port=port, cmd=request.POST.get("cmd", ""), http_header=request.POST.get("http_header", ""), initial_delay_second=int(request.POST.get("initial_delay_second", 1)), period_second=int(request.POST.get("period_second", 3)), timeout_second=int(request.POST.get("timeout_second", 30)), failure_threshold=int(request.POST.get("failure_threshold", 3)), success_threshold=int(request.POST.get("success_threshold", 1)), is_used=bool(request.POST.get("is_used", 1)), mode=mode) json_data = model_to_dict(probe) is_used = 1 if json_data["is_used"] else 0 json_data.update({"is_used": is_used}) json_data["enterprise_id"] = self.tenant.enterprise_id res, body = region_api.update_service_probe(self.service.service_region, self.tenantName, self.service.service_alias, json_data) if 400 <= res.status <= 600: return errResponseJson("region error.", "数据中心操作失败", 500) probe.delete() service_probe.save() return successResponseJson(bean=model_to_dict(service_probe)) except Exception as e: logger.exception(e) return errResponseJson(e.message, "系统异常,处理错误", 500)
def _update_probe(self, probe): add = probe.get("add") if add: add["probe_id"] = make_uuid() self.probe = ServiceProbe(**add) self.probe.service_id = self.component.component_id upd = probe.get("upd", None) if upd: probe = ServiceProbe(**upd) probe.ID = self.probe.ID probe.service_id = self.probe.service_id self.probe = probe
def create_probe(self, tenant, service, data): code, msg = self.__check_probe_data(data) if code != 200: raise AbortRequest("invalid probe", msg_show=msg) probe = probe_repo.get_probe_by_mode(service.service_id, data["mode"]) if probe: raise AbortRequest("probe exists", msg_show="已设置过该类型探针", status_code=409, error_code=409) is_used = 1 if data.get("is_used", 1) else 0 probe = { "service_id": service.service_id, "scheme": data.get("scheme", "tcp"), "path": data.get("path", ""), "port": data.get("port"), "cmd": data.get("cmd", ""), "http_header": data.get("http_header", ""), "initial_delay_second": data.get("initial_delay_second", 4), "period_second": data.get("period_second", 3), "timeout_second": data.get("timeout_second", 5), "failure_threshold": data.get("failure_threshold", 3), "success_threshold": data.get("success_threshold", 1), "is_used": is_used, "probe_id": make_uuid(), "mode": data["mode"], } return ServiceProbe(**probe)
def _create_component(snap): # component component = TenantServiceInfo(**snap["service_base"]) # component source component_source = ServiceSourceInfo(**snap["service_source"]) # environment envs = [TenantServiceEnvVar(**env) for env in snap["service_env_vars"]] # ports ports = [TenantServicesPort(**port) for port in snap["service_ports"]] # service_extend_method extend_info = ServiceExtendMethod(**snap["service_extend_method"]) # volumes volumes = [TenantServiceVolume(**volume) for volume in snap["service_volumes"]] # configuration files config_files = [TenantServiceConfigurationFile(**config_file) for config_file in snap["service_config_file"]] # probe probes = [ServiceProbe(**probe) for probe in snap["service_probes"]] # monitors monitors = [ServiceMonitor(**monitor) for monitor in snap["service_monitors"]] # graphs graphs = [ComponentGraph(**graph) for graph in snap["component_graphs"]] return Component( component=component, component_source=component_source, envs=envs, ports=ports, volumes=volumes, config_files=config_files, probe=probes[0] if probes else None, extend_info=extend_info, monitors=monitors, graphs=graphs, plugin_deps=[], )
def _create_component(self, snap): # component component = TenantServiceInfo(**snap["service_base"]) # component source component_source = ServiceSourceInfo(**snap["service_source"]) # environment envs = [TenantServiceEnvVar(**env) for env in snap["service_env_vars"]] # ports ports = [TenantServicesPort(**port) for port in snap["service_ports"]] # service_extend_method extend_info = None if snap.get("service_extend_method"): extend_info = ServiceExtendMethod( **snap.get("service_extend_method")) # volumes volumes = [ TenantServiceVolume(**volume) for volume in snap["service_volumes"] ] # configuration files config_files = [ TenantServiceConfigurationFile(**config_file) for config_file in snap["service_config_file"] ] # probe probes = [ServiceProbe(**probe) for probe in snap["service_probes"]] # monitors monitors = [ ServiceMonitor(**monitor) for monitor in snap["service_monitors"] ] # graphs graphs = [ ComponentGraph(**graph) for graph in snap["component_graphs"] ] service_labels = [ ServiceLabels(**label) for label in snap["service_labels"] ] cpt = Component( component=component, component_source=component_source, envs=envs, ports=ports, volumes=volumes, config_files=config_files, probes=probes, extend_info=extend_info, monitors=monitors, graphs=graphs, plugin_deps=[], labels=service_labels, support_labels=self.support_labels, ) cpt.action_type = snap.get("action_type", ActionType.BUILD.value) return cpt
def init(self): self.sources = [ Tenants(), TenantRegionInfo(), TenantRegionResource(), ServiceInfo(), TenantServiceInfo(), TenantServiceInfoDelete(), TenantServiceLog(), TenantServiceRelation(), TenantServiceEnv(), TenantServiceAuth(), TenantServiceExtendMethod(), ServiceDomain(), ServiceDomainCertificate(), PermRelService(), PermRelTenant(), PhoneCode(), TenantServiceL7Info(), TenantServiceEnvVar(), TenantServicesPort(), TenantServiceMountRelation(), TenantServiceVolume(), TenantServiceConfigurationFile(), ServiceGroup(), ServiceGroupRelation(), ImageServiceRelation(), ComposeServiceRelation(), ServiceRule(), ServiceRuleHistory(), ServiceCreateStep(), ServiceProbe(), ConsoleConfig(), TenantEnterprise(), TenantEnterpriseToken(), TenantServiceGroup(), ServiceTcpDomain(), ThirdPartyServiceEndpoints(), ServiceWebhooks(), GatewayCustomConfiguration(), ConsoleSysConfig(), RainbondCenterApp(), RainbondCenterAppInherit(), RainbondCenterPlugin(), ServiceShareRecord(), EnterpriseUserPerm(), TenantUserRole(), TenantUserPermission(), TenantUserRolePermission(), PermGroup(), ServiceRelPerms(), AppExportRecord(), UserMessage(), AppImportRecord(), GroupAppBackupRecord(), GroupAppMigrateRecord(), GroupAppBackupImportRecord(), Applicants(), DeployRelation(), ServiceBuildSource(), TenantServiceBackup(), AppUpgradeRecord(), ServiceUpgradeRecord(), RegionConfig(), CloundBangImages(), Announcement(), ]