def post(self, request, groupId, *args, **kwargs): data = {} tenant_id = self.tenant.tenant_id try: service_group_id = request.POST.get("service_group_id", None) services_json = request.POST.get("services") services = json.loads(services_json) success = tenant_svc.init_for_region(self.response_region, self.tenantName, self.tenant.tenant_id, self.user) if not success: data["status"] = "failure" return JsonResponse(data, status=200) need_create_service, is_pass, result = self.preprocess(services) logger.debug("need_create_service: {}".format(need_create_service)) if not is_pass: return JsonResponse(result, status=200) is_success = True GroupCreateTemp.objects.filter(share_group_id=groupId).delete() for ncs in need_create_service: service_id = make_uuid(tenant_id) service_cname = ncs.service_cname try: temp_data = {} temp_data["tenant_id"] = tenant_id temp_data["service_id"] = service_id temp_data["service_key"] = ncs.service_key temp_data["service_cname"] = service_cname temp_data["share_group_id"] = groupId temp_data["service_group_id"] = service_group_id GroupCreateTemp.objects.create(**temp_data) except Exception as ex: logger.exception(ex) GroupCreateTemp.objects.filter( share_group_id=groupId).delete() is_success = False if is_success: next_url = "/apps/{0}/group-deploy/{1}/step3/?group_id={2}".format( self.tenantName, groupId, service_group_id) data.update({ "success": True, "status": "success", "next_url": next_url }) else: data.update({"success": False, "status": "failure"}) except Exception as e: data.update({"success": False, "code": 500}) logger.exception(e) return JsonResponse(data, status=200)
def init_region_tenant(self, region, tenant_name, tenant_id, nick_name): user = Users(nick_name=nick_name) for num in range(0, 3): result = tenant_svc.init_for_region(region, tenant_name, tenant_id, user) if result: logger.debug("openapi.cloudservice", "init tenant region success!") return result else: logger.error( "openapi.cloudservice", "init tenant region failed! try again!num:{0}".format(num)) return False
def post(self, request, *args, **kwargs): result = {} service_id = "" try: tenant_id = self.tenant.tenant_id service_id = request.POST.get("service_id", "") image_input = request.POST.get("image_url", "") service_cname = request.POST.get("create_app_name", "") _is, list_args, run_execs = ImageAnalyst.analystImage(image_input) if not run_execs: run_execs = "" image_url = list_args[-1] result["image_url"] = image_url if image_url != "": imagesr = None if service_id != "": try: imagesr = ImageServiceRelation.objects.get( service_id=service_id) except Exception: pass if imagesr is None: imagesr = ImageServiceRelation() service_id = make_uuid(self.tenant.tenant_id) imagesr.tenant_id = self.tenant.tenant_id imagesr.service_id = service_id imagesr.image_url = image_url imagesr.service_cname = service_cname imagesr.save() logger.debug("setp2 image url is {}".format(image_url)) # save service attach info # min_memory = int(request.POST.get("service_min_memory", 128)) # # 将G转换为M # if min_memory < 128: # min_memory *= 1024 # min_node = int(request.POST.get("service_min_node", 1)) # judge region tenant is init success = tenant_svc.init_for_region(self.response_region, self.tenantName, tenant_id, self.user) if not success: result["status"] = "failure" return JsonResponse(result, status=200) result["status"] = "success" result["id"] = service_id group_id = request.POST.get("select_group_id", "") # 创建关系 if group_id != "": group_id = int(group_id) if group_id > 0: ServiceGroupRelation.objects.create( service_id=service_id, group_id=group_id, tenant_id=self.tenant.tenant_id, region_name=self.response_region) if _is == "is_docker": args = "" for mm in list_args[:-1]: if args: args = "{0}^_^{1}=={2}".format(args, mm[0], mm[1]) else: args = "{0}=={1}".format(mm[0], mm[1]) if args: args += "^_^{0}=={1}^_^{2}=={3}".format( "image", image_url, "run_exec", run_execs) else: args = "{0}=={1}^_^{2}=={3}".format( "image", image_url, "run_exec", run_execs) logger.debug(args) result["params"] = base64.b64encode(args) else: result["status"] = "no_image_url" return JsonResponse(result, status=500) except Exception as e: logger.exception(e) if service_id != "" and service_id is not None: ImageServiceRelation.objects.filter( service_id=service_id).delete() ServiceAttachInfo.objects.filter( service_id=service_id).delete() result["status"] = "failure" result["fail_info"] = 900 return JsonResponse(result, status=500) return JsonResponse(result, status=200)
def post(self, request, *args, **kwargs): tenant_id = self.tenant.tenant_id service_id = make_uuid(tenant_id) service_alias = "gr" + service_id[-6:] result = {} try: success = tenant_svc.init_for_region(self.response_region, self.tenantName, tenant_id, self.user) if not success: result["status"] = "failure" return JsonResponse(result, status=200) service_key = request.POST.get("service_key", None) if service_key is None: result["status"] = "notexist" return JsonResponse(result, status=200) app_version = request.POST.get("app_version", None) if app_version is None: result["status"] = "notexist" return JsonResponse(result, status=200) service_cname = request.POST.get("create_app_name", None) if service_cname is None: result["status"] = "empty" return JsonResponse(result, status=200) service = None if app_version: try: service = ServiceInfo.objects.get(service_key=service_key, version=app_version) except ServiceInfo.DoesNotExist: pass else: service_list = ServiceInfo.objects.filter( service_key=service_key) if len(service_list) > 0: service = list(service_list)[0] app_version = service.version if service is None: result["status"] = "notexist" return JsonResponse(result, status=200) # calculate resource min_cpu = baseService.calculate_service_cpu( self.response_region, service.min_memory) service.min_cpu = min_cpu tempService = TenantServiceInfo() tempService.min_memory = service.min_memory tempService.service_region = self.response_region tempService.min_node = service.min_node diffMemory = service.min_node * service.min_memory rt_type, flag = tenantUsedResource.predict_next_memory( self.tenant, tempService, diffMemory, False) if not flag: if rt_type == "memory": result["status"] = "over_memory" result["tenant_type"] = self.tenant.pay_type else: result["status"] = "over_money" return JsonResponse(result, status=200) pre_paid_period = int(request.POST.get("pre_paid_period", 0)) disk = int(request.POST.get("disk_num", 0)) * 1024 # create console service newTenantService = baseService.create_service( service_id, tenant_id, service_alias, service_cname, service, self.user.pk, region=self.response_region) sai = attach_info_mamage.create_service_attach_info( newTenantService, newTenantService.min_memory * newTenantService.min_node, 0) create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 创建预付费订单 if sai.pre_paid_money > 0: ServiceFeeBill.objects.create( tenant_id=tenant_id, service_id=service_id, prepaid_money=sai.pre_paid_money, pay_status="unpayed", cost_type="first_create", node_memory=newTenantService.min_memory, node_num=newTenantService.min_node, disk=disk, buy_period=pre_paid_period * 24 * 30, create_time=create_time, pay_time=create_time) group_id = request.POST.get("select_group_id", "") # 创建关系 if group_id != "": group_id = int(group_id) if group_id > 0: ServiceGroupRelation.objects.create( service_id=service_id, group_id=group_id, tenant_id=self.tenant.tenant_id, region_name=self.response_region) monitorhook.serviceMonitor(self.user.nick_name, newTenantService, 'create_service', True) result["status"] = "success" result["service_id"] = service_id result["service_alias"] = service_alias # 添加安装步骤记录 ServiceCreateStep.objects.create(tenant_id=tenant_id, service_id=service_id, app_step=11) except Exception as e: logger.exception(e) # tempTenantService = TenantServiceInfo.objects.filter(service_id=service_id)[0] TenantServiceInfo.objects.filter(service_id=service_id).delete() TenantServiceAuth.objects.filter(service_id=service_id).delete() TenantServiceRelation.objects.filter( service_id=service_id).delete() ServiceGroupRelation.objects.filter(service_id=service_id).delete() ServiceAttachInfo.objects.filter(service_id=service_id).delete() # monitorhook.serviceMonitor(self.user.nick_name, tempTenantService, 'create_service_error', False) result["status"] = "failure" return JsonResponse(result, status=200)
def post(self, request, *args, **kwargs): service_alias = "" service_code_from = "" tenant_id = self.tenant.tenant_id service_id = make_uuid(tenant_id) data = {} try: success = tenant_svc.init_for_region(self.response_region, self.tenantName, tenant_id, self.user) if not success: data["status"] = "failure" return JsonResponse(data, status=200) service_desc = "" service_cname = request.POST.get("create_app_name", "") service_code_from = request.POST.get("service_code_from", "") if service_code_from is None or service_code_from == "": data["status"] = "code_from" return JsonResponse(data, status=200) service_cname = service_cname.rstrip().lstrip() if service_cname is None or service_cname == "": data["status"] = "empty" return JsonResponse(data, status=200) # get base service service = ServiceInfo.objects.get(service_key="application") # 根据页面参数获取节点数和每个节点的内存大小 # min_memory = int(request.POST.get("service_min_memory", 128)) # # 将G转换为M # if min_memory < 128: # min_memory *= 1024 # min_node = int(request.POST.get("service_min_node", 1)) # service.min_memory = min_memory # service.min_node = min_node service.min_cpu = baseService.calculate_service_cpu( self.response_region, service.min_memory) service_alias = "gr" + service_id[-6:] # calculate resource tempService = TenantServiceInfo() tempService.min_memory = service.min_memory tempService.service_region = self.response_region tempService.min_node = service.min_node tempService.service_alias = service_alias diffMemory = service.min_node * service.min_memory rt_type, flag = tenantUsedResource.predict_next_memory( self.tenant, tempService, diffMemory, False) if not flag: if rt_type == "memory": data["status"] = "over_memory" data["tenant_type"] = self.tenant.pay_type else: data["status"] = "over_money" return JsonResponse(data, status=200) # save service attach info pre_paid_period = int(request.POST.get("pre_paid_period", 0)) disk = int(request.POST.get("disk_num", 0)) * 1024 create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # create console service service.desc = service_desc newTenantService = baseService.create_service( service_id, tenant_id, service_alias, service_cname, service, self.user.pk, region=self.response_region) monitorhook.serviceMonitor(self.user.nick_name, newTenantService, 'create_service', True) baseService.addServicePort(newTenantService, False, container_port=5000, protocol='http', port_alias=service_alias.upper() + str(5000), is_inner_service=False, is_outer_service=True) sai = attach_info_mamage.create_service_attach_info( newTenantService, newTenantService.min_memory * newTenantService.min_node, disk) if sai.pre_paid_money > 0: ServiceFeeBill.objects.create(tenant_id=tenant_id, service_id=service_id, prepaid_money=sai.pre_paid_money, pay_status="unpayed", cost_type="first_create", node_memory=service.min_memory, node_num=service.min_node, disk=disk, buy_period=pre_paid_period * 24 * 30, create_time=create_time, pay_time=create_time) # code repos # 自建git (gitlab_self)与gitlab_manual一样 if service_code_from == "gitlab_self": service_code_from = "gitlab_manual" if service_code_from == "gitlab_new": codeRepositoriesService.initRepositories( self.tenant, self.user, newTenantService, service_code_from, "", "", "") elif service_code_from == "gitlab_exit": code_clone_url = request.POST.get("service_code_clone_url", "") code_id = request.POST.get("service_code_id", "") code_version = request.POST.get("service_code_version", "master") if code_id == "" or code_clone_url == "" or code_version == "": data["status"] = "code_repos" TenantServiceInfo.objects.get( service_id=service_id).delete() return JsonResponse(data, status=200) codeRepositoriesService.initRepositories( self.tenant, self.user, newTenantService, service_code_from, code_clone_url, code_id, code_version) elif service_code_from == "gitlab_manual": code_clone_url = request.POST.get("service_code_clone_url", "") code_version = request.POST.get("service_code_version", "master") code_id = 0 if code_clone_url == "" or code_version == "": data["status"] = "code_repos" TenantServiceInfo.objects.get( service_id=service_id).delete() return JsonResponse(data, status=200) codeRepositoriesService.initRepositories( self.tenant, self.user, newTenantService, service_code_from, code_clone_url, code_id, code_version) elif service_code_from == "github": code_id = request.POST.get("service_code_id", "") code_clone_url = request.POST.get("service_code_clone_url", "") code_version = request.POST.get("service_code_version", "master") if code_id == "" or code_clone_url == "" or code_version == "": data["status"] = "code_repos" TenantServiceInfo.objects.get( service_id=service_id).delete() return JsonResponse(data, status=200) codeRepositoriesService.initRepositories( self.tenant, self.user, newTenantService, service_code_from, code_clone_url, code_id, code_version) group_id = request.POST.get("select_group_id", "") # 创建关系 if group_id != "": group_id = int(group_id) if group_id > 0: ServiceGroupRelation.objects.create( service_id=service_id, group_id=group_id, tenant_id=self.tenant.tenant_id, region_name=self.response_region) monitorhook.serviceMonitor(self.user.nick_name, newTenantService, 'init_region_service', True) data["status"] = "success" data["service_alias"] = service_alias data["service_id"] = service_id except Exception as e: logger.exception("create console service failed!") logger.exception(e) tempTenantService = TenantServiceInfo.objects.get( service_id=service_id) codeRepositoriesService.deleteProject(tempTenantService) TenantServiceInfo.objects.filter(service_id=service_id).delete() TenantServiceAuth.objects.filter(service_id=service_id).delete() TenantServiceRelation.objects.filter( service_id=service_id).delete() ServiceGroupRelation.objects.filter(service_id=service_id) ServiceAttachInfo.objects.filter(service_id=service_id) monitorhook.serviceMonitor(self.user.nick_name, tempTenantService, 'create_service_error', False) data["status"] = "failure" return JsonResponse(data, status=200)
def post(self, request, *args, **kwargs): result = {} tenant_id = self.tenant.tenant_id compose_file_id = request.POST.get("compose_file_id", "") group_id = request.POST.get("group_id", "") try: # judge region tenant is init success = tenant_svc.init_for_region(self.response_region, self.tenantName, tenant_id, self.user) if not success: result["status"] = "failure" return JsonResponse(result, status=200) # if tenantAccountService.isOwnedMoney(self.tenant, self.response_region): # result["status"] = "owed" # return JsonResponse(result, status=200) if group_id == "": result["status"] = "no_group" return JsonResponse(result, status=200) services_attach_infos = request.POST.get("services_attach_infos", "") services_attach_infos = self.json_loads(services_attach_infos) deps = {} services_list = [] for service_attach_info in services_attach_infos: service_cname = service_attach_info.get("app_name") service_id = service_attach_info.get("service_id") deps[service_cname] = service_id ts = TenantServiceInfo() min_memory = int( service_attach_info.get("service_min_memory", 128)) # 将G转换为M if min_memory < 128: min_memory *= 1024 min_node = int(service_attach_info.get("service_min_node", 1)) ts.min_memory = min_memory ts.min_node = min_node services_list.append(ts) res = tenantUsedResource.predict_batch_services_memory( self.tenant, services_list, self.response_region) if not res: result["status"] = "over_memory" result["tenant_type"] = self.tenant.pay_type return JsonResponse(result, status=200) for service_attach_info in services_attach_infos: service_cname = service_attach_info.get("app_name", "") if service_cname == "": result["status"] = "empty" return JsonResponse(result, status=200) min_memory = int( service_attach_info.get("service_min_memory", 128)) # 将G转换为M if min_memory < 128: min_memory *= 1024 min_node = int(service_attach_info.get("service_min_node", 1)) # calculate resource tempService = TenantServiceInfo() tempService.min_memory = min_memory tempService.service_region = self.response_region tempService.min_node = int(min_node) diffMemory = min_memory # 判断是否超出资源 rt_type, flag = tenantUsedResource.predict_next_memory( self.tenant, tempService, diffMemory, False) if not flag: if rt_type == "memory": result["status"] = "over_memory" result["tenant_type"] = self.tenant.pay_type else: result["status"] = "over_money" return JsonResponse(result, status=200) service_id = service_attach_info.get("service_id", None) if service_id is None: result["status"] = "no_service" return JsonResponse(result, status=200) service_alias = "gr" + service_id[-6:] service_image = service_attach_info.get("service_image") version = "" if ":" in service_image: index = service_image.index(":") version = service_image[index + 1:] else: version = "latest" service = ServiceInfo() service.service_key = "0000" service.desc = "" service.category = "app_publish" service.image = service_image service.cmd = "" service.setting = "" service.extend_method = "stateless" service.env = "," service.min_node = min_node cm = min_memory if min_memory != "": cm = int(min_memory) service.min_memory = cm service.min_cpu = baseService.calculate_service_cpu( self.response_region, cm) service.inner_port = 0 service.volume_mount_path = "" service.version = version service.namespace = "goodrain" service.update_version = 1 service.volume_mount_path = "" service.service_type = "application" # service host_path service.host_path = "/grdata/tenant/" + self.tenant.tenant_id + "/service/" + service_id # 创建服务 newTenantService = baseService.create_service( service_id, tenant_id, service_alias, service_cname, service, self.user.pk, region=self.response_region) newTenantService.code_from = "image_manual" newTenantService.language = "docker-compose" newTenantService.save() sai = attach_info_mamage.create_service_attach_info( newTenantService, newTenantService.min_memory * newTenantService.min_node, 0) monitorhook.serviceMonitor(self.user.nick_name, newTenantService, 'create_service', True) ServiceGroupRelation.objects.create( service_id=service_id, group_id=int(group_id), tenant_id=tenant_id, region_name=self.response_region) result[service_id] = { "service_id": service_id, "service_cname": service_cname, "service_alias": service_alias } result["status"] = "success" result["group_id"] = group_id result["compose_file_id"] = compose_file_id except Exception as e: ServiceGroupRelation.objects.filter( group_id=int(group_id)).delete() logger.exception(e) return JsonResponse(result, status=200)