def apply(self, request, project_id_or_code): project_id = request.project.project_id data = self._request_data(request, project_id=project_id) serializer = TemplateReleaseSLZ(data=data, context={"request": request}) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data self.can_use_template(request, validated_data["template"]) # 在数据平台创建项目信息 username = request.user.username cc_app_id = request.project.cc_app_id english_name = request.project.english_name create_data_project(username, project_id, cc_app_id, english_name) # 创建/启动标准日志采集任务 create_and_start_standard_data_flow(username, project_id, cc_app_id) validated_data = serializer.validated_data processor = ReleaseDataProcessor( user=self.request.user, raw_release_data=ReleaseData( project_id=project_id, namespace_info=validated_data["namespace_info"], show_version=validated_data["show_version"], template_files=validated_data["template_files"], template_variables=validated_data["template_variables"], ), ) controller = DeployController(user=self.request.user, release_data=processor.release_data()) controller.apply() return Response()
def preview_or_apply(self, request, project_id, template_id, show_version_id): """ request.data = { 'is_preview': True, 'namespace_id': 'test', 'template_files': [{ 'resource_name': 'Deployment', 'files': [{'name': 'nginx.yaml', 'id': 3}] }], 'template_variables': {} } """ data = self._request_data(request, project_id=project_id, template_id=template_id, show_version_id=show_version_id) serializer = serializers.TemplateReleaseSLZ(data=data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data template = validated_data["show_version"]["template"] self.can_use_template(request, template) # 在数据平台创建项目信息 username = request.user.username cc_app_id = request.project.cc_app_id english_name = request.project.english_name create_data_project(username, project_id, cc_app_id, english_name) # 创建/启动标准日志采集任务 create_and_start_standard_data_flow(username, project_id, cc_app_id) processor = ReleaseDataProcessor( user=self.request.user, raw_release_data=self._raw_release_data(project_id, validated_data)) release_data = processor.release_data() if validated_data["is_preview"]: return Response(release_data.template_files) controller = DeployController(user=self.request.user, release_data=release_data) controller.apply() return Response()
def post(self, request, project_id): """实例化模板 """ # 参数验证 self.project_id = project_id version_id = request.data.get('version_id') show_version_id = request.data.get('show_version_id') template, version_entity = validate_version_id( project_id, version_id, is_return_all=True, show_version_id=show_version_id) # 验证用户是否有使用权限 perm = bcs_perm.Templates(request, project_id, template.id, template.name) perm.can_use(raise_exception=True) self.template_id = version_entity.template_id tem_instance_entity = version_entity.get_version_instance_resource_ids project_kind = request.project.kind self.slz = VersionInstanceCreateOrUpdateSLZ( data=request.data, context={'project_kind': project_kind}) self.slz.is_valid(raise_exception=True) slz_data = self.slz.data # 验证前端传过了的预览资源是否在该版本的资源 req_instance_entity = slz_data.get('instance_entity') or {} self.instance_entity = validate_instance_entity( req_instance_entity, tem_instance_entity) namespaces = slz_data['namespaces'] ns_list = namespaces.split(',') if namespaces else [] access_token = self.request.user.token.access_token username = self.request.user.username # 验证关联lb情况下,lb 是否都已经选中 service_id_list = self.instance_entity.get('service') or [] v_res, err_list, err_msg = validate_lb_info_by_version_id( access_token, project_id, version_entity, ns_list, slz_data.get('lb_info', {}), service_id_list) if not v_res: return Response({ "code": 400, "message": err_msg, "data": err_list }) # 判断 template 下 前台传过来的 namespace 是否已经实例化过 res, ns_name_list, namespace_dict = validate_ns_by_tempalte_id( self.template_id, ns_list, access_token, project_id, req_instance_entity) if not res: return Response({ "code": 400, "message": "以下命名空间已经实例化过,不能再实例化\n%s" % "\n".join(ns_name_list), "data": ns_name_list }) # 在数据平台创建项目信息 cc_app_id = request.project.cc_app_id english_name = request.project.english_name create_data_project(request.user.username, project_id, cc_app_id, english_name) # 创建/启动标准日志采集任务 create_and_start_standard_data_flow(username, project_id, cc_app_id) slz_data['ns_list'] = ns_list slz_data['instance_entity'] = self.instance_entity slz_data['template_id'] = self.template_id slz_data['project_id'] = project_id slz_data['version_id'] = version_id slz_data['show_version_id'] = show_version_id result = handle_all_config(slz_data, access_token, username) instance_entity = slz_data.get("instance_entity") all_tmpl_name_dict = self.get_tmpl_name(instance_entity) # 添加操作记录 temp_name = version_entity.get_template_name() for i in result['success']: client.ContextActivityLogClient( project_id=project_id, user=username, resource_type="template", resource=temp_name, resource_id=self.template_id, extra=json.dumps(self.instance_entity), description=u"实例化模板集[%s]命名空间[%s]" % (temp_name, i['ns_name'])).log_add(activity_status="succeed") failed_ns_name_list = [] failed_msg = [] is_show_failed_msg = False # 针对createError的触发后台任务轮训 if result.get('failed'): check_instance_status.delay(request.user.token.access_token, project_id, request.project.get("kind"), all_tmpl_name_dict, result['failed']) for i in result['failed']: if i['res_type']: description = "实例化模板集[%s]命名空间[%s],在实例化%s时失败,错误消息:%s" % ( temp_name, i['ns_name'], i['res_type'], i['err_msg']) failed_ns_name_list.append("%s(实例化%s时)" % (i['ns_name'], i['res_type'])) else: description = "实例化模板集[%s]命名空间[%s]失败,错误消息:%s" % ( temp_name, i['ns_name'], i['err_msg']) failed_ns_name_list.append(i['ns_name']) if i.get('show_err_msg'): failed_msg.append(i['err_msg']) is_show_failed_msg = True client.ContextActivityLogClient( project_id=project_id, user=username, resource_type="template", resource=temp_name, resource_id=self.template_id, extra=json.dumps(self.instance_entity), description=description).log_add(activity_status="failed") if is_show_failed_msg: msg = '\n'.join(failed_msg) else: msg = "以下命名空间实例化失败,\n%s,请联系集群管理员解决" % "\n".join( failed_ns_name_list) if failed_ns_name_list: return Response({ "code": 400, "message": msg, "data": failed_ns_name_list }) return Response({ "code": 0, "message": "OK", "data": { "version_id": version_id, "template_id": self.template_id, } })