def __install_plugin(self, region_name, user, tenant, plugin_template): image = None image_tag = None if plugin_template["share_image"]: image_and_tag = plugin_template["share_image"].rsplit(":", 1) if len(image_and_tag) > 1: image = image_and_tag[0] image_tag = image_and_tag[1] else: image = image_and_tag[0] image_tag = "latest" status, msg, plugin_base_info = plugin_service.create_tenant_plugin( tenant, user.user_id, region_name, plugin_template["desc"], plugin_template["plugin_alias"], plugin_template["category"], "image", image, plugin_template["code_repo"]) if status != 200: return status, msg plugin_base_info.origin = 'local_market' plugin_base_info.origin_share_id = plugin_template.get("plugin_key") plugin_base_info.save() build_version = plugin_template.get('build_version') min_memory = plugin_template.get('min_memory', 128) plugin_build_version = plugin_version_service.create_build_version( region_name, plugin_base_info.plugin_id, tenant.tenant_id, user.user_id, "", "unbuild", min_memory, image_tag=image_tag, code_version="", build_version=build_version) share_config_groups = plugin_template.get('config_groups', []) plugin_config_service.create_config_groups(plugin_base_info.plugin_id, build_version, share_config_groups) event_id = make_uuid() plugin_build_version.event_id = event_id plugin_build_version.plugin_version_status = "fixed" plugin_service.create_region_plugin(region_name, tenant, plugin_base_info, image_tag=image_tag) ret = plugin_service.build_plugin( region_name, plugin_base_info, plugin_build_version, user, tenant, event_id, plugin_template.get("plugin_image", None)) plugin_build_version.build_status = ret.get('bean').get('status') plugin_build_version.save() return 200, "success"
def install_plugin(self, user, tenant, region_name, market_plugin): plugin_template = json.loads(market_plugin.plugin_template) share_plugin_info = plugin_template.get("share_plugin_info") sid = transaction.savepoint() try: image = None image_tag = None if plugin_template["share_image"]: image_and_tag = plugin_template["share_image"].rsplit(":", 1) if len(image_and_tag) > 1: image = image_and_tag[0] image_tag = image_and_tag[1] else: image = image_and_tag[0] image_tag = "latest" status, msg, plugin_base_info = plugin_service.create_tenant_plugin( tenant, user.user_id, region_name, share_plugin_info.get("desc"), plugin_template["plugin_name"], plugin_template["category"], "image", image, plugin_template["code_repo"]) if status != 200: return status, msg plugin_base_info.origin = 'local_market' if market_plugin.source == 'local' else market_plugin.source plugin_base_info.origin_share_id = share_plugin_info.get( "plugin_key") plugin_base_info.save() build_version = plugin_template.get('build_version') min_memory = build_version.get('min_memory') plugin_build_version = plugin_version_service.create_build_version( region_name, plugin_base_info.plugin_id, tenant.tenant_id, user.user_id, "", "unbuild", min_memory, image_tag=image_tag, code_version="") config_groups, config_items = [], [] share_config_groups = share_plugin_info.get('config_groups', []) for group in share_config_groups: plugin_config_group = PluginConfigGroup( plugin_id=plugin_base_info.plugin_id, build_version=plugin_build_version.build_version, config_name=group.get("config_name"), service_meta_type=group.get("service_meta_type"), injection=group.get("injection")) config_groups.append(plugin_config_group) share_config_items = group.get('config_items', []) for item in share_config_items: plugin_config_item = PluginConfigItems( plugin_id=plugin_base_info.plugin_id, build_version=plugin_build_version.build_version, service_meta_type=item.get("service_meta_type"), attr_name=item.get("attr_name"), attr_alt_value=item.get("attr_alt_value"), attr_type=item.get("attr_type", "string"), attr_default_value=item.get("attr_default_value", None), is_change=item.get("is_change", False), attr_info=item.get("attr_info", ""), protocol=item.get("protocol", "")) config_items.append(plugin_config_item) PluginConfigGroup.objects.bulk_create(config_groups) PluginConfigItems.objects.bulk_create(config_items) event_id = make_uuid() plugin_build_version.event_id = event_id plugin_build_version.plugin_version_status = "fixed" plugin_service.create_region_plugin(region_name, tenant, plugin_base_info, image_tag=image_tag) ret = plugin_service.build_plugin( region_name, plugin_base_info, plugin_build_version, user, tenant, event_id, share_plugin_info.get("plugin_image", None)) plugin_build_version.build_status = ret.get('bean').get('status') plugin_build_version.save() return 200, '安装成功' except Exception as e: logger.exception(e) if sid: transaction.savepoint_rollback(sid) return 500, '插件安装失败'
def post(self, request, *args, **kwargs): """ 插件创建 --- parameters: - name: tenantName description: 团队名 required: true type: string paramType: path - name: plugin_alias description: 插件名称 required: true type: string paramType: form - name: build_source description: 构建来源 dockerfile | image required: true type: string paramType: form - name: min_memory description: 最小内存 required: true type: integer paramType: form - name: category description: 插件类别 net-plugin:down|net-plugin:up|net-plugin:in-and-out| analyst-plugin:perf|init-plugin|general-plugin required: false type: string paramType: form - name: build_cmd description: 构建命令 required: false type: string paramType: form - name: code_repo description: dockerfile构建代码仓库地址,选择dockerfile时必须 required: false type: string paramType: form - name: code_version description: 代码版本,默认master required: false type: string paramType: form - name: image description: 镜像构建时镜像名称 required: false type: string paramType: form - name: desc description: 镜像说明 required: true type: string paramType: form """ # 必要参数 plugin_alias = request.data.get("plugin_alias", None) build_source = request.data.get("build_source", None) min_memory = request.data.get("min_memory", None) category = request.data.get("category", None) desc = request.data.get("desc", None) # 非必要参数 build_cmd = request.data.get("build_cmd", None) code_repo = request.data.get("code_repo", None) code_version = request.data.get("code_version", None) image = request.data.get("image", None) # username and password is used for private docker hub or private git repo username = request.data.get("username", None) password = request.data.get("password", None) tenant_plugin = None plugin_build_version = None try: if not plugin_alias: return Response(general_message(400, "plugin alias is null", "插件名称未指明"), status=400) if not build_source: return Response(general_message(400, "build source is null", "构建来源未指明"), status=400) if not min_memory: return Response(general_message(400, "plugin min_memroy is null", "插件内存大小未指明"), status=400) if not category: return Response(general_message(400, "plugin category is null", "插件类别未指明"), status=400) else: if category not in allow_plugins: return Response(general_message( 400, "plugin category is wrong", "插件类别参数错误,详情请参数API说明"), status=400) if not desc: return Response(general_message(400, "plugin desc is null", "请填写插件描述"), status=400) image_tag = "" if image and build_source == "image": image_and_tag = image.rsplit(":", 1) if len(image_and_tag) > 1: image = image_and_tag[0] image_tag = image_and_tag[1] else: image = image_and_tag[0] image_tag = "latest" # 创建基本信息 plugin_params = { "tenant_id": self.tenant.tenant_id, "region": self.response_region, "create_user": self.user.user_id, "desc": desc, "plugin_alias": plugin_alias, "category": category, "build_source": build_source, "image": image, "code_repo": code_repo, "username": username, "password": password } code, msg, tenant_plugin = plugin_service.create_tenant_plugin( plugin_params) if code != 200: return Response(general_message(code, "create plugin error", msg), status=code) # 创建插件版本信息 plugin_build_version = plugin_version_service.create_build_version( self.response_region, tenant_plugin.plugin_id, self.tenant.tenant_id, self.user.user_id, "", "unbuild", min_memory, build_cmd, image_tag, code_version) # 数据中心创建插件 code, msg = plugin_service.create_region_plugin( self.response_region, self.tenant, tenant_plugin, image_tag) if code != 200: plugin_service.delete_console_tenant_plugin( tenant_plugin.plugin_id) plugin_version_service.delete_build_version_by_id_and_version( tenant_plugin.plugin_id, plugin_build_version.build_version, True) return Response(general_message(code, "create plugin error", msg), status=code) bean = tenant_plugin.to_dict() bean["build_version"] = plugin_build_version.build_version bean["code_version"] = plugin_build_version.code_version bean["build_status"] = plugin_build_version.build_status bean["update_info"] = plugin_build_version.update_info bean["image_tag"] = plugin_build_version.image_tag result = general_message(200, "success", "创建成功", bean=bean) except Exception as e: logger.exception(e) result = error_message(e.message) if tenant_plugin: plugin_service.delete_console_tenant_plugin( tenant_plugin.plugin_id) if plugin_build_version: plugin_version_service.delete_build_version_by_id_and_version( tenant_plugin.plugin_id, plugin_build_version.build_version, True) 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 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