def post(self, request, *args, **kwargs): """ 服务构建 --- parameters: - name: tenantName description: 租户名 required: true type: string paramType: path - name: serviceAlias description: 服务别名 required: true type: string paramType: path """ probe = None try: is_deploy = request.data.get("is_deploy", True) # 数据中心创建应用 new_service = app_service.create_region_service(self.tenant, self.service, self.user.nick_name) self.service = new_service # 为服务添加默认探针 if self.is_need_to_add_default_probe(): code, msg, probe = app_service.add_service_default_porbe(self.tenant, self.service) if is_deploy: # 添加服务有无状态标签 label_service.update_service_state_label(self.tenant, self.service) # 部署应用 app_manage_service.deploy(self.tenant, self.service, self.user, is_upgrade=True) # 添加应用部署关系 deploy_repo.create_deploy_relation_by_service_id(service_id=self.service.service_id) result = general_message(200, "success", "构建成功") except Exception as e: logger.exception(e) result = error_message(e.message) # 删除probe # 删除region端数据 if probe: probe_service.delete_service_probe(self.tenant, self.service, probe.probe_id) event_service.delete_service_events(self.service) port_service.delete_region_port(self.tenant, self.service) volume_service.delete_region_volumes(self.tenant, self.service) env_var_service.delete_region_env(self.tenant, self.service) dependency_service.delete_region_dependency(self.tenant, self.service) app_manage_service.delete_region_service(self.tenant, self.service) self.service.create_status = "checked" self.service.save() return Response(result, status=result["code"])
def build_services(self, user, tenant, region_name, group_id, change_services_map): group_services = base_service.get_group_services_list( tenant.tenant_id, region_name, group_id) change_service_ids = [ change_service["ServiceID"] for change_service in change_services_map.values() ] if not group_services: return [] service_ids = [ group_service.get("service_id") for group_service in group_services ] services = service_repo.get_service_by_service_ids( service_ids=service_ids) for service in services: if service.service_id in change_service_ids: # probe = None if service.service_source == "third_party": # 数据中心连接创建第三方组件 new_service = app_service.create_third_party_service( tenant, service, user.nick_name) else: # 数据中心创建组件 new_service = app_service.create_region_service( tenant, service, user.nick_name) service = new_service # 为组件添加默认探针 if self.is_need_to_add_default_probe(service): code, msg, probe = app_service.add_service_default_porbe( tenant, service) logger.debug("add default probe; code: {}; msg: {}".format( code, msg)) # 添加组件有无状态标签 label_service.update_service_state_label(tenant, service) # 部署组件 app_manage_service.deploy(tenant, service, user, group_version=None) # 添加组件部署关系 deploy_repo.create_deploy_relation_by_service_id( service_id=service.service_id)
def post(self, request, *args, **kwargs): """ 组件构建 --- parameters: - name: tenantName description: 租户名 required: true type: string paramType: path - name: serviceAlias description: 组件别名 required: true type: string paramType: path """ probe = None is_deploy = request.data.get("is_deploy", True) status = 200 try: if self.service.service_source == "third_party": is_deploy = False # 数据中心连接创建第三方组件 new_service = app_service.create_third_party_service( self.tenant, self.service, self.user.nick_name) else: # 数据中心创建组件 new_service = app_service.create_region_service( self.tenant, self.service, self.user.nick_name) self.service = new_service # 为组件添加默认探针 if self.is_need_to_add_default_probe(): code, msg, probe = app_service.add_service_default_porbe( self.tenant, self.service) logger.debug("add default probe; code: {}; msg: {}".format( code, msg)) if is_deploy: # 添加组件有无状态标签 label_service.update_service_state_label( self.tenant, self.service) # 部署组件 app_manage_service.deploy(self.tenant, self.service, self.user, group_version=None) # 添加组件部署关系 deploy_repo.create_deploy_relation_by_service_id( service_id=self.service.service_id) result = general_message(200, "success", "构建成功") return Response(result, status=result["code"]) except HttpClient.CallApiError as e: logger.exception(e) if e.status == 403: result = general_message(10407, "no cloud permission", e.message) status = e.status elif e.status == 400: if "is exist" in e.message.get("body", ""): result = general_message(400, "the service is exist in region", "该组件名称在数据中心已存在") else: result = general_message(400, "call cloud api failure", e.message) status = e.status else: result = general_message(500, "call cloud api failure", e.message) status = 500 # 删除probe # 删除region端数据 if probe: probe_service.delete_service_probe(self.tenant, self.service, probe.probe_id) if self.service.service_source != "third_party": event_service.delete_service_events(self.service) port_service.delete_region_port(self.tenant, self.service) volume_service.delete_region_volumes(self.tenant, self.service) env_var_service.delete_region_env(self.tenant, self.service) dependency_service.delete_region_dependency( self.tenant, self.service) app_manage_service.delete_region_service(self.tenant, self.service) self.service.create_status = "checked" self.service.save() return Response(result, status=status)
def build_services(self, user, tenant, region_name, group_id, change_services_map): group_services = base_service.get_group_services_list( tenant.tenant_id, region_name, group_id) change_service_ids = [ change_service["ServiceID"] for change_service in list(change_services_map.values()) ] if not group_services: return [] service_ids = [ group_service.get("service_id") for group_service in group_services ] services = service_repo.get_service_by_service_ids( service_ids=service_ids) result = [] for service in services: if service.service_id in change_service_ids: if service.service_source == "third_party": # 数据中心连接创建第三方组件 new_service = app_service.create_third_party_service( tenant, service, user.nick_name) else: # 数据中心创建组件 new_service = app_service.create_region_service( tenant, service, user.nick_name) service = new_service # 为组件添加默认探针 if self.is_need_to_add_default_probe(service): code, msg, probe = app_service.add_service_default_porbe( tenant, service) logger.debug("add default probe; code: {}; msg: {}".format( code, msg)) else: probes = probe_repo.get_service_probe(service.service_id) if probes: for probe in probes: prob_data = { "service_id": service.service_id, "scheme": probe.scheme, "path": probe.path, "port": probe.port, "cmd": probe.cmd, "http_header": probe.http_header, "initial_delay_second": probe.initial_delay_second, "period_second": probe.period_second, "timeout_second": probe.timeout_second, "failure_threshold": probe.failure_threshold, "success_threshold": probe.success_threshold, "is_used": (1 if probe.is_used else 0), "probe_id": probe.probe_id, "mode": probe.mode, } try: res, body = region_api.add_service_probe( service.service_region, tenant.tenant_name, service.service_alias, prob_data) if res.get("status") != 200: logger.debug(body) probe.delete() except Exception as e: logger.debug("error", e) probe.delete() # 添加组件有无状态标签 label_service.update_service_state_label(tenant, service) # 部署组件 app_manage_service.deploy(tenant, service, user, group_version=None) # 添加组件部署关系 deploy_repo.create_deploy_relation_by_service_id( service_id=service.service_id) result.append(service) # 为组件创建插件 build_error_plugin_ids = [] service_plugins = app_plugin_relation_repo.get_service_plugin_relation_by_service_id( service.service_id) for service_plugin in service_plugins: plugin = plugin_repo.get_by_plugin_id( tenant.tenant_id, service_plugin.plugin_id) plugin_version = plugin_repo.get_plugin_buildversion( service_plugin.plugin_id, service_plugin.build_version) # 在数据中心创建插件 try: event_id = make_uuid() plugin_version.event_id = event_id image_tag = (plugin_version.image_tag if plugin_version.image_tag else "latest") plugin_service.create_region_plugin( region_name, tenant, plugin, image_tag=image_tag) ret = plugin_service.build_plugin( region_name, plugin, plugin_version, user, tenant, event_id) plugin_version.build_status = ret.get('bean').get( 'status') plugin_version.save() except Exception as e: logger.debug(e) # 为组件开通插件 try: region_config = app_plugin_service.get_region_config_from_db( service, service_plugin.plugin_id, service_plugin.build_version) data = dict() data["plugin_id"] = service_plugin.plugin_id data["switch"] = True data["version_id"] = service_plugin.build_version data.update(region_config) region_api.install_service_plugin( region_name, tenant.tenant_name, service.service_alias, data) except region_api.CallApiError as e: logger.debug(e) build_error_plugin_ids.append(service_plugin.plugin_id) if build_error_plugin_ids: app_plugin_relation_repo.get_service_plugin_relation_by_service_id( service.service_id).filter( plugin_id__in=build_error_plugin_ids).delete() # create service_monitor in region service_monitors = service_monitor_repo.get_component_service_monitors( tenant.tenant_id, service.service_id) for monitor in service_monitors: req = { "name": monitor.name, "path": monitor.path, "port": monitor.port, "service_show_name": monitor.service_show_name, "interval": monitor.interval } try: region_api.create_service_monitor( tenant.enterprise_id, service.service_region, tenant.tenant_name, service.service_alias, req) except region_api.CallApiError as e: ServiceMonitor.objects.filter( tenant_id=tenant.tenant_id, service_id=service.service_id, name=monitor.name).delete() logger.debug(e) return result
def build_services(self, user, tenant, region_name, group_id, change_services_map): group_services = base_service.get_group_services_list( tenant.tenant_id, region_name, group_id) change_service_ids = [ change_service["ServiceID"] for change_service in list(change_services_map.values()) ] if not group_services: return [] service_ids = [ group_service.get("service_id") for group_service in group_services ] services = service_repo.list_by_component_ids(service_ids=service_ids) result = [] for service in services: if service.service_id in change_service_ids: if service.service_source == "third_party": # 数据中心连接创建第三方组件 new_service = app_service.create_third_party_service( tenant, service, user.nick_name) else: # 数据中心创建组件 new_service = app_service.create_region_service( tenant, service, user.nick_name) service = new_service # 部署组件 app_manage_service.deploy(tenant, service, user, group_version=None) # 添加组件部署关系 deploy_repo.create_deploy_relation_by_service_id( service_id=service.service_id) result.append(service) # 为组件创建插件 build_error_plugin_ids = [] service_plugins = app_plugin_relation_repo.get_service_plugin_relation_by_service_id( service.service_id) for service_plugin in service_plugins: plugin = plugin_repo.get_by_plugin_id( tenant.tenant_id, service_plugin.plugin_id) plugin_version = plugin_repo.get_plugin_buildversion( service_plugin.plugin_id, service_plugin.build_version) # 在数据中心创建插件 try: event_id = make_uuid() plugin_version.event_id = event_id image_tag = (plugin_version.image_tag if plugin_version.image_tag else "latest") plugin_service.create_region_plugin( region_name, tenant, plugin, image_tag=image_tag) ret = plugin_service.build_plugin( region_name, plugin, plugin_version, user, tenant, event_id) plugin_version.build_status = ret.get('bean').get( 'status') plugin_version.save() except Exception as e: logger.debug(e) # 为组件开通插件 try: region_config = app_plugin_service.get_region_config_from_db( service, service_plugin.plugin_id, service_plugin.build_version) data = dict() data["plugin_id"] = service_plugin.plugin_id data["switch"] = True data["version_id"] = service_plugin.build_version data["plugin_cpu"] = service_plugin.min_cpu data["plugin_memory"] = service_plugin.min_memory data.update(region_config) region_api.install_service_plugin( region_name, tenant.tenant_name, service.service_alias, data) except region_api.CallApiError as e: logger.debug(e) build_error_plugin_ids.append(service_plugin.plugin_id) if build_error_plugin_ids: app_plugin_relation_repo.get_service_plugin_relation_by_service_id( service.service_id).filter( plugin_id__in=build_error_plugin_ids).delete() return result
def post(self, request, *args, **kwargs): """ 组件构建 --- parameters: - name: tenantName description: 租户名 required: true type: string paramType: path - name: serviceAlias description: 组件别名 required: true type: string paramType: path """ probe = None is_deploy = request.data.get("is_deploy", True) try: if not check_memory_quota( self.oauth_instance, self.tenant.enterprise_id, self.service.min_memory, self.service.min_node): raise ServiceHandleException(msg="not enough quota", error_code=20002) if self.service.service_source == "third_party": is_deploy = False # create third component from region new_service = app_service.create_third_party_service( self.tenant, self.service, self.user.nick_name) else: # 数据中心创建组件 new_service = app_service.create_region_service( self.tenant, self.service, self.user.nick_name) self.service = new_service if is_deploy: try: app_manage_service.deploy( self.tenant, self.service, self.user, group_version=None, oauth_instance=self.oauth_instance) except ErrInsufficientResource as e: result = general_message(e.error_code, e.msg, e.msg_show) return Response(result, status=e.status_code) except Exception as e: logger.exception(e) err = ErrComponentBuildFailed() result = general_message(err.error_code, e, err.msg_show) return Response(result, status=400) # 添加组件部署关系 deploy_repo.create_deploy_relation_by_service_id( service_id=self.service.service_id) result = general_message(200, "success", "构建成功") return Response(result, status=result["code"]) except HttpClient.CallApiError as e: logger.exception(e) if e.status == 403: result = general_message(10407, "no cloud permission", e.message) status = e.status elif e.status == 400: if "is exist" in e.message.get("body", ""): result = general_message(400, "the service is exist in region", "该组件在数据中心已存在,你可能重复创建?") else: result = general_message(400, "call cloud api failure", e.message) status = e.status else: result = general_message(400, "call cloud api failure", e.message) status = 400 # 删除probe # 删除region端数据 if probe: probe_service.delete_service_probe(self.tenant, self.service, probe.probe_id) if self.service.service_source != "third_party": event_service.delete_service_events(self.service) port_service.delete_region_port(self.tenant, self.service) volume_service.delete_region_volumes(self.tenant, self.service) env_var_service.delete_region_env(self.tenant, self.service) dependency_service.delete_region_dependency( self.tenant, self.service) app_manage_service.delete_region_service(self.tenant, self.service) self.service.create_status = "checked" self.service.save() return Response(result, status=status)