def post(self, request, app_id, *args, **kwargs): ctcs = CreateThirdComponentSerializer(data=request.data) ctcs.is_valid(raise_exception=True) req_date = ctcs.data validate_endpoints_info(req_date["endpoints"]) new_component = console_app_service.create_third_party_app(self.region_name, self.team, self.user, req_date["component_name"], req_date["endpoints"], req_date["endpoints_type"]) # add component to app code, msg_show = group_service.add_service_to_group(self.team, self.region_name, app_id, new_component.service_id) if code != 200: raise ServiceHandleException( msg="add component to app failure", msg_show=msg_show, status_code=code, error_code=code) endpoints_type = req_date["endpoints_type"] bean = new_component.to_dict() if endpoints_type == "api": # 生成秘钥 deploy = deploy_repo.get_deploy_relation_by_service_id(service_id=new_component.service_id) api_secret_key = pickle.loads(base64.b64decode(deploy)).get("secret_key") # 从环境变量中获取域名,没有在从请求中获取 host = os.environ.get('DEFAULT_DOMAIN', "http://" + request.get_host()) api_url = host + "/console/" + "third_party/{0}".format(new_component.service_id) bean["api_service_key"] = api_secret_key bean["url"] = api_url console_app_service.create_third_party_service(self.team, new_component, self.user.nick_name) return Response(bean, status=status.HTTP_200_OK)
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 save_data( self, migrate_tenant, migrate_region, user, changed_service_map, metadata, group_id, same_team, same_region, sync_flag=False, ): from console.services.groupcopy_service import groupapp_copy_service group = group_repo.get_group_by_id(group_id) apps = metadata["apps"] old_new_service_id_map = dict() service_relations_list = [] service_mnt_list = [] # restore component for app in apps: service_base_info = app["service_base"] new_service_id = changed_service_map[ service_base_info["service_id"]]["ServiceID"] new_service_alias = changed_service_map[ service_base_info["service_id"]]["ServiceAlias"] ts = self.__init_app(app["service_base"], new_service_id, new_service_alias, user, migrate_region, migrate_tenant) old_new_service_id_map[app["service_base"] ["service_id"]] = ts.service_id group_service.add_service_to_group(migrate_tenant, migrate_region, group.ID, ts.service_id) self.__save_port(migrate_region, migrate_tenant, ts, app["service_ports"], group.governance_mode, app["service_env_vars"], sync_flag) self.__save_env(migrate_tenant, ts, app["service_env_vars"]) self.__save_volume( migrate_tenant, ts, app["service_volumes"], app["service_config_file"] if 'service_config_file' in app else None) self.__save_compile_env(ts, app["service_compile_env"]) self.__save_service_label(migrate_tenant, ts, migrate_region, app["service_labels"]) if sync_flag: self.__save_service_probes(ts, app["service_probes"]) self.__save_service_source(migrate_tenant, ts, app["service_source"]) self.__save_service_auth(ts, app["service_auths"]) self.__save_third_party_service_endpoints( ts, app.get("third_party_service_endpoints", [])) self.__save_service_monitors(migrate_tenant, ts, app.get("service_monitors")) self.__save_component_graphs(ts, app.get("component_graphs")) if ts.service_source == "third_party": app_service.create_third_party_service(migrate_tenant, ts, user.nick_name) probes = probe_repo.get_service_probe(ts.service_id) # 为组件添加默认探针 if not probes: if groupapp_copy_service.is_need_to_add_default_probe(ts): code, msg, probe = app_service.add_service_default_porbe( migrate_tenant, ts) logger.debug( "add default probe; code: {}; msg: {}".format( code, msg)) else: for probe in probes: prob_data = { "service_id": ts.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( ts.service_region, migrate_tenant.tenant_name, ts.service_alias, prob_data) if res.get("status") != 200: logger.debug(body) probe.delete() except Exception as e: logger.debug("error", e) probe.delete() service_relations = app["service_relation"] service_mnts = app["service_mnts"] if service_relations: service_relations_list[0:0] = list(service_relations) if service_mnts: service_mnt_list[0:0] = list(service_mnts) # 更新状态 ts.create_status = "complete" ts.save() # restore plugin info self.__save_plugins(migrate_region, migrate_tenant, metadata["plugin_info"]["plugins"]) self.__save_plugin_config_items( metadata["plugin_info"]["plugin_config_items"]) self.__save_plugin_config_groups( metadata["plugin_info"]["plugin_config_groups"]) versions = self.__save_plugin_build_versions( migrate_tenant, metadata["plugin_info"]["plugin_build_versions"]) for app in apps: new_service_id = old_new_service_id_map[app["service_base"] ["service_id"]] # plugin if app.get("service_plugin_relation", None): self.__save_plugin_relations(new_service_id, app["service_plugin_relation"], versions) if app.get("service_plugin_config", None): self.__save_service_plugin_config(new_service_id, app["service_plugin_config"]) self.__save_service_relations(migrate_tenant, service_relations_list, old_new_service_id_map, same_team, same_region) self.__save_service_mnt_relation(migrate_tenant, service_mnt_list, old_new_service_id_map, same_team, same_region) # restore application config group self.__save_app_config_groups(metadata.get("app_config_group_info"), migrate_tenant, migrate_region, group_id, changed_service_map)
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 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, is_upgrade=True, group_version=None) # 添加应用部署关系 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) 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=result["code"])
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)