def EchStart(self): EchList = LvsInfo.objects.get(EchId=self.params['EchId']) VlanId = EchList.VlanId TapUUID = EchList.TapUUID Tap = 'tap%s' % TapUUID[:11] operateinfo = { 'action': 'start', 'parame': { 'EchId': self.params['EchId'], 'VlanId': VlanId, 'Tap': Tap } } print operateinfo KvmOperateOut = NcloudLib.HttpRequest(operateinfo, EchList['EchVncIp'], 35796) print KvmOperateOut if KvmOperateOut['code'] == 0: return_data = KvmOperateOut else: return_data = { 'code': 100001, 'msg': '对不起,暂时无法为您启动当前主机,谢谢。', } return return_data
def AddEchToSwitch(self): #判断 提交的switch 是否存在 try: VpcSwDetails = VpcSwitch.objects.get(VswId=self.params['vswid']) VpcDetails = VpcInfo.objects.get(TenantId=VpcSwDetails['TenantId']) EchDetails = EchInfo.objects.get(EchId=self.params['echid']) EchMacs = EchDetails['EchMacs'] EchName = EchDetails['EchName'] EchKvmIp = EchDetails['EchVncIp'] TapUUID = EchDetails['TapUUID'] Tags = VpcDetails['SegmentationId'] DnsmasqxIp = VpcSwDetails['DnsmasqxIp'] UsableIp = VpcSwDetails['UsableIp'] Qdhcp = VpcSwDetails['Qdhcp'] operateinfo = { 'action': 'AddEchToSwitch', 'parame': { 'EchMacs': EchMacs, 'Qdhcp': Qdhcp, 'DnsmasqxIp': DnsmasqxIp, 'UsableIp': UsableIp, 'EchName': EchName, 'Type': 1, }, } Status = NcloudLib.HttpRequest(operateinfo, '127.0.0.1', 35796) if Status['code'] == 0: EchIp = Status['EchIp'] operateinfos = { 'action': 'SetEchTapTags', 'parame': { 'Tags': Tags, 'TapUUID': TapUUID, }, } #Status = NcloudLib.HttpRequest(operateinfos,EchKvmIp,35796) Status = { 'code': 0, } if Status['code'] == 0: EchDetails.EchIp = EchIp EchDetails.TenantId = VpcSwDetails['TenantId'] EchDetails.VlanId = Tags EchDetails.save() return_data = { "code": 0, "msg": '成功将云主机添加到当前交换机。', } else: return_data = {"code": 1, "msg": "对不起,无法将云主机添加到当前交换机。"} except: return_data = {"code": 1, "msg": "对不起,无法将云主机添加到当前交换机。"} return return_data
def EchDestroy(self): EchList = LbsInfo.objects.get(EchId=self.params['EchId']) operateinfo = {'action':'destroy','parame':{'EchId':self.params['EchId'],}} KvmOperateOut = NcloudLib.HttpRequest(operateinfo,EchList['EchVncIp'],35796) if KvmOperateOut['code'] == 0: return_data = KvmOperateOut else: return_data = { 'code':100001, 'msg': '对不起,暂时无法为您强制关闭当前主机,谢谢。', } return return_data
def EchStatus(self): EchList = LbsInfo.objects.get(EchId=self.params['EchId']) operateinfo = {'action':'ech_status','parame':{'EchId':self.params['EchId'],}} KvmOperateOut = NcloudLib.HttpRequest(operateinfo,EchList['EchVncIp'],35796) if KvmOperateOut['code'] == 0: return_data = KvmOperateOut else: return_data = { 'code':100001, 'Status': 'shutdown', } return return_data
def CreateVPC(self): #创建VPC Create Network #获取数据库最后一条 VpcDetail = VpcInfo.objects.filter(UserId=self.Data['UserId']) if len(VpcDetail) == 0: VpcId = 'vpc-%s' % str(uuid.uuid1())[:8] VpcLish = VpcInfo.objects.all().order_by('-CreateTime')[0:1] if VpcLish: for VpcDetails in VpcLish: VpcRawId = VpcDetails.SegmentationId SegmentationId = VpcRawId + 1 else: SegmentationId = 1 #根据当前time.time()生成md5 TenantId = NcloudLib.md5Encrypt(str(time.time())) Status = self.__CreateRouter(VpcId, TenantId) if Status['code'] == 0: print Status inVpc = VpcInfo(VpcId=VpcId, VpcName=self.params['vpcname'], NetworkType='vlan', SegmentationId=SegmentationId, Shared='False', Status='ACTIVE', Subnets=self.params['vpcsegmentation'], TenantId=TenantId, VpcBewrite='-', Zone=self.Data['Zone'], UserId=self.Data['UserId']) inVpc.save() return_data = { "code": 0, "msg": "恭喜您,创建专有网络成功", } else: return_data = { "code": 0, "msg": "创建专有网络失败.", } else: return_data = { "code": 0, "msg": "创建专有网络失败,用户名下的 VPC 数量达到配额上限.", } return return_data
def CreateEch(self): KVMList = KvmInfo.objects.filter(KVMArea=self.params['zone']).filter(KVMSurplusCpu__gte=int(self.params['cpu'])).filter(KVMSurplusMem__gte=int(self.params['memory'])).order_by('-KVMSurplusCpu')[0:1] if KVMList: for kvmDetails in KVMList: kvmDetail = KvmInfo.objects.get(id=kvmDetails.id) kvmDetail.KVMSurplusCpu = kvmDetails.KVMSurplusCpu - int(self.params['cpu']) kvmDetail.KVMSurplusMem = kvmDetails.KVMSurplusMem - int(self.params['memory']) #kvmDetail.save() ImageList = ImagesInfo.objects.get(ImagesId=self.params['image_id']) ImagesPlatform = ImageList.ImagesPlatform if ImagesPlatform == 'linux': SystemVolume = 20 else: SystemVolume = 40 #get image list print self.params['image_id'] GetImageList = ImagesList.objects.filter(ImagesId=self.params['image_id']).filter(ImagesStatus=1)[0:1] if GetImageList: for imageDetails in GetImageList: imageDetail = ImagesList.objects.get(id=imageDetails.id) ImagesId = imageDetail.ImagesName SYSPosition = imageDetail.SYSPosition NetWorkId = imageDetail.NetWorkId DiskPosition = imageDetail.SYSPosition ImagesPosition = imageDetail.ImagesPosition PoolId = imageDetails.PoolId imageDetail.ImagesStatus = 0 #生成ECH数据 #生成EchIp 获取当前 SwitchDetails = VpcSwitch.objects.get(VswId=self.params['vswid']) TenantId = SwitchDetails.TenantId DnsmasqxIp = SwitchDetails.DnsmasqxIp UsableIp = SwitchDetails.UsableIp Qdhcp = SwitchDetails.Qdhcp VpcDetails = VpcInfo.objects.get(TenantId=TenantId) SegmentationId = VpcDetails.SegmentationId NetEchNumList = EchInfo.objects.filter(TenantId=TenantId) NetEchNum = len(NetEchNumList) if NetEchNum < UsableIp: EchIp = '%s.%s.%s.%s' % (DnsmasqxIp.split('.')[0],DnsmasqxIp.split('.')[1],DnsmasqxIp.split('.')[2],int(DnsmasqxIp.split('.')[3])+NetEchNum+1) #IpList = IpInfo.objects.filter(IpArea=self.params['zone']).filter(IpType=0).filter(IpUseNum__lte=254).order_by('IpUseNum')[0:1] #if IpList: ''' for ipDetails in IpList: ipDetail = IpInfo.objects.get(id=ipDetails.id) IpNet = ipDetail.IpAddress EchRawIpNum = ipDetail.IpUseNum + 1 EchIp = '%s.%s' % (IpNet,EchRawIpNum) EchMask = ipDetail.NetMask EchGW = ipDetail.GateWay ipDetail.IpUseNum = EchRawIpNum ''' #生成ECHUUID EchUUID = str(uuid.uuid1()) #生成 tapUUID TapUUID = str(uuid.uuid1()) #获取 VNC 端口 #更具端口序列插叙 最大一个 + 1 EchLish = EchInfo.objects.filter(EchZone=self.Data['Zone']).order_by('-EchVncPort')[0:1] if EchLish: for Ech in EchLish: VNCPort = Ech.EchVncPort +1 else: VNCPort = 59001 #生成ECHID EchId = 'i-%s' % EchUUID[:8] #生成ECHNAME EchName = 'iZ%sZ' % EchId.split('-')[1] #生成ECHNETID Tap = 'tap%s' % TapUUID[:11] #生成MAC地址 EchMac = NcloudLib.buildMac() MvEchInfo = { 'action':'MvEchInfo', 'parame':{ 'EchId':EchId, 'ImagesPosition':ImagesPosition, 'ImagesId':ImagesId, 'EchUUID':EchUUID, } } DeleteEchImages = { 'action':'DeleteEchImages', 'parame':{ 'EchId':EchId, 'ImagesPosition':ImagesPosition, 'ImagesId':ImagesId, 'EchUUID':EchUUID, } } operateinfo = { 'action':'Create_ech', 'parame':{ 'EchId':EchId, 'EchName':EchName, 'EchCpu':int(self.params['cpu']), 'EchMem':int(self.params['memory']), 'EchUUID':EchUUID, 'TapUUID':TapUUID, 'Tap':Tap, 'EchVNCPort':VNCPort, 'EchIp':EchIp, 'EchPlatform':ImageList.ImagesPlatform, 'ImagesId':ImagesId, 'ImagesPosition':SYSPosition, 'SYSPosition':SYSPosition, 'EchMac':EchMac, }, } #读取本地 存储list地址 如果地址为等于二 则 for 循环判断 GfsList = GfsInfo.objects.filter(Zone=self.Data['Zone']).filter(GfsId=PoolId) GfsIps = '' GfsIpList = [] if GfsList: for g in xrange(len(GfsList)): GfsIp = GfsList[g].GfsIp MvEchStatus = NcloudLib.HttpRequest(MvEchInfo,GfsIp,35796) if MvEchStatus['code'] == 0: GfsIps = GfsIp GfsIpList.append(GfsIps) #设置状态为成功 0 else: #判断当前 g值 如果为0 则直接跳出 如果为1 则删除0中的镜像 if g == 0: break else: #删除上一次移动的镜像 MvEchStatus = NcloudLib.HttpRequest(DeleteEchImages,GfsIps,35796) GfsIps = '' if GfsIps != '': #创建云主机 #print kvmDetail.KVMIp #发送至KVM主机创建 Status = NcloudLib.HttpRequest(operateinfo,kvmDetail.KVMIp,35796) #print Status #如果创建成功 则生成disk磁盘数据 if Status['code'] == 0: imageDetail.save() #生成DISKUUID = DiskUUID = str(uuid.uuid1()) #DiskLish = DiskInfo.objects.all().order_by('-CreateTime')[0:1] ''' #if DiskLish: for Disk in DiskLish: DiskRawId = Disk.DiskId DiskRawNum = DiskRawId.split('-') #生成EId DiskNum = int(DiskRawNum[1]) + 1 #生成DiskID DiskId = 'd-%s' % DiskNum #生成DiskNAME DiskName = 'DZ%sZ' % DiskNum else: DiskNum = 1000001 #生成ECHID DiskId = 'd-%s' % DiskNum #生成DiskNAME DiskName = 'DZ%sZ' % DiskNum ''' DiskId = 'd-%s' % DiskUUID[:8] DiskName = 'DZ%sZ' % DiskUUID[:8] inDisk = DiskInfo(DiskId=DiskId,DiskName=DiskName,DiskVolume=SystemVolume,DiskZone=PoolId,DiskPosition=DiskPosition,DiskMount='vda',DiskAttr='system',EchId=EchId,UserId=self.Data['UserId']) inDisk.save() ''' for datavposition in params['data']: if datavposition == '0': pass else: #生成存储空间ID diskindex = params['data'].index(datavposition) DiskdataId = '%s-%s' % (DiskId,diskindex) DiskdataName = DiskdataId.replace('-','') DiskdataName = DiskdataName.upper() DiskdataPosition = diskdata[diskindex] if diskindex == 0: DiskdataMount = 'vdb' elif diskindex == 1: DiskdataMount = 'vdc' elif diskindex == 2: DiskdataMount = 'vdd' elif diskindex == 3: DiskdataMount = 'vde' inDiskdata = DiskInfo(DiskId=DiskdataId,DiskName=DiskdataName,DiskVolume=int(datavposition),DiskZone='SHJ1',DiskPosition=DiskdataPosition,DiskMount=DiskdataMount,DiskAttr='data',EchId=EchId,UserId=str(UserVncId.id)) inDiskdata.save() ''' #获取MAC地址 ''' operateinfo = {'action':'ech_mac','parame':{'EchId':EchId,}} EchMacOperateOut = NcloudLib.HttpRequest(operateinfo,kvmDetail.KVMIp,35796) #print EchMacOperateOut EchMac = EchMacOperateOut['macs'] ''' #print EchMac inech = EchInfo(EchId=EchId,EchName=EchName,EchBewrite='',EchZone=self.params['zone'],EchCpu=int(self.params['cpu']),EchMemory=int(self.params['memory']),ImageId=self.params['image_id'],EchIp=EchIp,EchVncIp=kvmDetail.KVMIp,EchVncPort=VNCPort,EchPaymentType=0,EchPaymentDate=0,EchMacs=EchMac,TapUUID=TapUUID,EchUUID=EchUUID,TenantId=TenantId,VlanId=SegmentationId,UserId=self.Data['UserId']) kvmDetail.save() #d.save() #ipDetail.save() inech.save() UserVncId = UserInfo.objects.get(id=self.Data['UserId']) vncpasstoken = "%s%s%s" %(UserVncId.VNCPasswd,'NDZ',VNCPort) #print vncpasstoken vnchash = md5.new() vnchash.update(vncpasstoken) token = '%s: %s:%s\n' % (vnchash.hexdigest(),kvmDetail.KVMIp,VNCPort) f = open("vnc_tokens",'a') f.write(token) f.close() #添加MAC及IP至DHCP服务器 #EchMac、EchIp 、EchId operateinfo = { 'action':'AddEchToSwitch', 'parame':{ 'EchName':EchName, 'EchMacs':EchMac, 'EchIp':EchIp, 'Qdhcp':Qdhcp, 'Type':0, } } HostOperateOut = NcloudLib.HttpRequest(operateinfo,'192.168.1.186',35796) #print HostOperateOut if HostOperateOut == 'done': return HttpResponse('1') else: return HttpResponse('0') return_data = { 'code':0, 'Status': '恭喜,创建云主机成功。', 'msg' : '恭喜,创建云主机成功。', 'Zone':self.Data['Zone'], } #创建EHC云主机结束 else: #增加存储容量 storageInfo for Gfsip in GfsIpList: MvEchStatus = NcloudLib.HttpRequest(DeleteEchImages,Gfsip,35796) return_data = { 'code':100002, 'Status': '无法创建移动用主机镜像文件', 'msg' : '对不起,暂时无法为您创建主机,请稍后再试,谢谢', } else: return_data = { 'code':100002, 'Status': '无法创建移动用主机镜像文件', 'msg' : '对不起,暂时无法为您创建主机,请稍后再试,谢谢', } ''' DeleteEchImages = { 'action':'DeleteEchImages', 'parame':{ 'EchId':EchId, 'ImagesPosition':ImagesPosition, } } MvEchStatus = NcloudLib.HttpRequest(DeleteEchImages,GfsIp,35796) ''' #如果成功继续 否则返回失败结果 else: return_data = { 'code':100002, 'Status': '当前区域存储区域不能满足需求', 'msg' : '对不起,暂时无法为您创建主机,请稍后再试,谢谢', } else: return_data = { 'code':100002, 'Status': '内网ip不能满足需求', 'msg' : '对不起,暂时无法为您创建主机,请稍后再试,谢谢', } else: return_data = { 'code':100002, 'Status': 'system data no系统空间不足', 'msg' : '对不起,暂时无法为您创建主机,请稍后再试,谢谢', } else: return_data = { 'code':100002, 'Status': 'KVM剩余CPU或MEM不足', 'msg' : '对不起,暂时无法为您创建主机,请稍后再试,谢谢', } return return_data
def CreateLBMonitor(self): #验证白名单地址格式是否正常 正确输入 1 HandleVisitPower = NcloudNlbLib.HandleVisitPlace(self.params) if HandleVisitPower['VisitPlaceFruit'] == 1: #提交域名地址 Domain = self.params['listeners'][0]['loadbalancer_listener_name'] LinstenDomain = LbMonitorInfo.objects.filter(LbDomain=Domain) #提交负载端口 listener_port = self.params['listeners'][0]['listener_port'] #判断当前提交的域名是否已存在 if LinstenDomain: #存在域名判断端口 存在则域名端口存在 返回结果 否则域名域名端口不存在可创建 ListenPort = LbMonitorInfo.objects.filter( LbDomain=Domain).filter(LbPort=listener_port) if ListenPort: #请重新输入域名或端口 code = 1 msg = '请重新输入域名或端口' else: #端口不存在创建 code = 1 msg = '端口不存在创建' else: #不存在域名创建 LbList = LbInfo.objects.get(LbId=self.params['loadbalancerid']) EipList = EipInfo.objects.get(id=LbList.EipId) EipGroup = EipList.EipGroup DomainName = Domain.split('.')[len(Domain.split('.')) - 2] UpstreamName = Domain.replace('.', '') #判断轮询类型 balance_mode = self.params['listeners'][0]['balance_mode'] if balance_mode == 'roundrobin': balance_mode = '' else: balance_mode = 'ip_hash;' #判断session类型 session_sticky = self.params['listeners'][0]['session_sticky'] if session_sticky: session_sticky = 'session_sticky;' else: session_sticky = '' #判断检测类型 healthy_check_method = self.params['listeners'][0][ 'healthy_check_method'] checkinterval = self.params['listeners'][0][ 'healthy_check_option'].split('|')[0] checktimeout = self.params['listeners'][0][ 'healthy_check_option'].split('|')[1] checkfall = self.params['listeners'][0][ 'healthy_check_option'].split('|')[2] checkrise = self.params['listeners'][0][ 'healthy_check_option'].split('|')[3] forwardfor = self.params['listeners'][0]['forwardfor'].split( '|') listener_option = self.params['listeners'][0][ 'listener_option'].split('|') checkinterval = int(checkinterval) * 1000 checktimeout = int(checktimeout) * 1000 if healthy_check_method == 'tcp': check = 'check interval=%s rise=%s fall=%s timeout=%s type=tcp;' % ( checkinterval, checkrise, checkfall, checktimeout) check_http_send = '' check_http_expect_alive = 'check_http_expect_alive http_2xx http_3xx http_4xx;' else: check = 'check interval=%s rise=%s fall=%s timeout=%s type=http;' % ( checkinterval, checkrise, checkfall, checktimeout) check_http_send = 'check_http_send "GET %s HTTP/1.0\r\n\r\n";' check_http_expect_alive = 'check_http_expect_alive http_2xx http_3xx http_4xx;' #判断是否显示客户端 if forwardfor[0] == '1': ClientIP = "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" else: ClientIP = '' if forwardfor[1] == '1': LBID = "proxy_set_header QC-LB-ID $server_name;" else: LBID = '' if forwardfor[2] == '1': LBIP = "proxy_set_header QC-LB-IP $server_addr;" else: LBIP = '' if forwardfor[3] == '1': ClientProto = "proxy_set_header X-Forwarded-Proto http;" else: ClientProto = '' if listener_option[1] == '1': RealIP = """ proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr;""" else: RealIP = '' if listener_option[2] == '1': gzip = 'gzip on;' else: gzip = '' timeout = self.params['listeners'][0]['timeout'] connecttimeout = """ proxy_connect_timeout %s; proxy_read_timeout %s; proxy_send_timeout %s; """ % (timeout, timeout, timeout) Upstream = { "UpstreamName": UpstreamName, "check": check, "check_http_send": check_http_send, "check_http_expect_alive": check_http_expect_alive, "session": session_sticky, } if self.params['listeners'][0]['balance_mode'] == 'roundrobin': LbMode = 0 LbsList = LbsInfo.objects.filter( LbsZone=self.Data['Zone']).filter(IpsId=LbList.IpsId) if LbsList: #获取负载集群状态 NLBHandleInfo = [] NLBStatus = 0 #通过LbInfo中LbsId 查询LbsInfo中LbsWeight #LbsWeightDetail = LbsInfo.objects.get(id=LbList.LbsId) for LbsDetail in LbsList: LbsHandleInfo = { 'LbsIp': LbsDetail['LbsIp'], 'LbsWeight': LbsDetail['LbsWeight'], 'id': str(LbsDetail['id']), 'EchIp': LbsDetail['EchIp'], } #获取相对主备ip LBOperateOut = NcloudLib.HttpRequest( {'action': 'NLBStatus'}, LbsDetail['LbsIp'], 35796) NLBStatus = int(LBOperateOut['code']) NLBHandleInfo.append(LbsHandleInfo) #如果用户类型不等于3则判断当前LB下是否存在同IP,同端口 如果存在不执行LVS添加Real 如不存在需添加LVS Real配置 print NLBHandleInfo LbIp = LbList.LbIp Tip = LbIp.split(',')[0] Uip = LbIp.split(',')[1] for NLBHandle in NLBHandleInfo: server = """ server { listen %s:%s; server_name %s; location / { include server/%s/*.conf; } }""" % (NLBHandle['EchIp'], listener_port, Domain, UpstreamName + listener_port) if NLBHandle['LbsWeight'] == 1: ServerMASTER = server else: ServerBACKUP = server if self.Data['UserType'] != 3: #获取Eip地址 LbMonitorList = LbMonitorInfo.objects.filter( LbId=self.params['loadbalancerid']).filter( LbPort=listener_port) if LbMonitorList: CreateLVSVirtualStatus = 1 pass else: #获取Eips #LBDetails = LbInfo.objects.get(LbId=self.params['loadbalancerid']) #EipDetails = EipInfo.objects.get(AssignId=LBDetails['LbId']) RealIps = [] for LbsDetails in NLBHandleInfo: RealIps.append(LbsDetails['EchIp']) #获取状态 LvsList = LvsInfo.objects.filter( GroupId=LbList.LvsGroupId) if LvsList: LVSHandleInfo = [] LVSStatus = 0 for LvsDetail in LvsList: LvsHandle = { 'LvsIp': LvsDetail['LvsIp'], } #获取相对主备ip NcloudLib.HttpRequest({'action':'NLBStatus'},LbsDetail['LbsIp'],35796) #获取LVS状态 LVSOperateOut = NcloudLib.HttpRequest( {'action': 'LVSStatus'}, LvsDetail['LvsIp'], 35796) LVSStatus = int(LVSOperateOut['code']) LVSHandleInfo.append(LvsHandle) if LVSStatus == 0: #获取LVS Real Info parame = { 'Tip': Tip, 'Uip': Uip, 'listener_port': listener_port, 'RealIps': RealIps, } LvsVirtualInfo = NcloudNlbLib.GetDoubletLvsVirtualCfg( parame) Lvsoperateinfo = { 'action': 'CreateLVSVirtual', 'parame': { 'LvsVirtualInfo': LvsVirtualInfo['LVSVirtualInfo'], 'VirtualFileName': LbIp + '.' + listener_port, 'Eips': [Tip, Uip], 'listener_port': listener_port, }, } print Lvsoperateinfo for LvsHandles in LVSHandleInfo: LVSOperateOut = NcloudLib.HttpRequest( Lvsoperateinfo, LvsHandles['LvsIp'], 35796) if LVSOperateOut['code'] == 0: CreateLVSVirtualStatus = 1 else: CreateLVSVirtualStatus = 0 else: CreateLVSVirtualStatus = 1 if NLBStatus == 0: proxy_pass = '******' % UpstreamName servers = { "proxy_pass": proxy_pass, "ClientIP": ClientIP, "LBID": LBID, "LBIP": LBIP, "ClientProto": ClientProto, "RealIP": RealIP, "gzip": gzip, "connecttimeout": connecttimeout, "VisitPlaceList": HandleVisitPower['VisitPlaceList'], } for LbsDetail in LbsList: if LbsDetail['LbsWeight'] == 1: Lbsoperateinfo = { 'action': 'CreateLBMonitor', 'parame': { 'Upstream': Upstream, 'server': ServerMASTER, 'servers': servers, 'FileName': UpstreamName + listener_port, }, } else: Lbsoperateinfo = { 'action': 'CreateLBMonitor', 'parame': { 'Upstream': Upstream, 'server': ServerBACKUP, 'servers': servers, 'FileName': UpstreamName + listener_port, }, } LbsId = LbsDetail.id LbsIp = LbsDetail.LbsIp LbsZone = LbsDetail.LbsZone LBOperateOut = NcloudLib.HttpRequest( Lbsoperateinfo, LbsIp, 35796) print LBOperateOut if LBOperateOut['code'] == 0: CreateLBMonitorStatus = 1 else: CreateLBMonitorStatus = 0 if CreateLBMonitorStatus == 1 and CreateLVSVirtualStatus == 1: inLbMonitor = LbMonitorInfo( LbDomain=Domain, LbProtocol=self.params['listeners'][0] ['listener_protocol'], LbPort=listener_port, LbMode=LbMode, SessionSticky=self.params['listeners'][0] ['session_sticky'], HealthyCheckMethod=self.params['listeners'][0] ['healthy_check_method'], HealthyCheckOption=self.params['listeners'][0] ['healthy_check_option'], Forwardfor=self.params['listeners'][0] ['forwardfor'], Timeout=timeout, LbOption=self.params['listeners'][0] ['listener_option'], LbId=self.params['loadbalancerid']) inLbMonitor.save() #print LBOperateOut code = 0 msg = '创建成功,您添加域名解析就可以通过域名访问了哦。' else: code = 1 msg = '添加监听器失败,请联系客服' else: code = 1 msg = '当前负载集群中有一个或多个存在故障,等待恢复后再重新操作。' else: code = 1 msg = '负载集群不存在,请联系客服' else: code = 1 msg = '您输入的白名单有问题,请您确认是否存在错误。' return_data = { 'code': code, 'msg': msg, } return_data = json.dumps(return_data) return return_data
def CreateForwardRule(self): ''' #params {"action":"CreateForwardRule","forwardname":"test","serverport":"80","clientport":"80","forwardprotocol" #:"tcp","echid":"instances-227a3fe6","vrtid":"vrt-f7542f4e"} ''' #根据 echid 查询 Ip 地址 #验证同一个外网ip服务端口是否被转发存在则 返回失败, ForwardRuleDetails = VpcForwardRule.objects.filter( ServerPort=self.params['serverport']).filter( Zone=self.Data['Zone']).filter(UserId=self.Data['UserId']) if not ForwardRuleDetails: RouterDetails = VpcRouter.objects.get(VrtId=self.params['vrtid']) RouterEips = RouterDetails.RouterEips Qrouter = RouterDetails.Qrouter TenantId = RouterDetails.TenantId EchDetails = EchInfo.objects.get(EchId=self.params['echid']) EchIp = EchDetails.EchIp operateinfo = { 'action': 'CreateForwardRule', 'parame': { 'RouterEips': RouterEips, 'ForwardProtocol': self.params['forwardprotocol'], 'ServerPort': self.params['serverport'], 'EchIp': EchIp, 'Clientport': self.params['clientport'], 'Qrouter': Qrouter, }, } Status = NcloudLib.HttpRequest(operateinfo, '127.0.0.1', 35796) #ip netns exec qroute iptables -t nat -I PREROUTING --dst RouterEips -p forwardprotocol --dport serverport -j DNAT --to-destination EchIp:clientport if Status['code'] == 0: #将数据插入数据库 VpcForwardRuleint = VpcForwardRule( ForwardName=self.params['forwardname'], ServerPort=self.params['serverport'], Clientport=self.params['clientport'], EchId=self.params['echid'], VrtId=self.params['vrtid'], RouterEips=RouterEips, EchIp=EchIp, ForwardProtocol=self.params['forwardprotocol'], Status='ACTIVE', TenantId=TenantId, Zone=self.Data['Zone'], UserId=self.Data['UserId']) VpcForwardRuleint.save() return_data = { "code": 0, "msg": "成功创建端口转发规则。", } else: return_data = { "code": 10202, "msg": "对不起,无法为您创建端口转发规则。", } else: return_data = { "code": 10201, "msg": "对不起,无法为您创建端口转发规则。", } return return_data
def CreateSwitch(self): print self.params if self.__verifySubnets()['code'] == 0: RouterDetail = VpcRouter.objects.get(VpcId=self.params['vpcid']) Qrouter = RouterDetail['Qrouter'] QrUUID = RouterDetail['QrUUID'] TenantId = RouterDetail['TenantId'] RouterEips = RouterDetail['RouterEips'] VpcDetail = VpcInfo.objects.get(TenantId=TenantId) SegmentationId = VpcDetail['SegmentationId'] Tags = SegmentationId TunId = hex(SegmentationId) VswId = 'vsw-%s' % str(uuid.uuid1())[:8] VswName = self.params['switchname'] Qdhcp = 'qdhcp-%s' % str(uuid.uuid1()) TapUUID = str(uuid.uuid1()) TapMac = NcloudLib.buildMac() Cidr = self.params['subnets'] UsableIp = pow( 2, (32 - int(self.params['subnets'].split('/')[1]))) - 4 GatewayIp = "%s.%s.%s.1" % ( self.params['subnets'].split('/')[0].split('.')[0], self.params['subnets'].split('/')[0].split('.')[1], self.params['subnets'].split('/')[0].split('.')[2]) DnsmasqxIp = "%s.%s.%s.2" % ( self.params['subnets'].split('/')[0].split('.')[0], self.params['subnets'].split('/')[0].split('.')[1], self.params['subnets'].split('/')[0].split('.')[2]) DhcpStartIp = "%s.%s.%s.3" % ( self.params['subnets'].split('/')[0].split('.')[0], self.params['subnets'].split('/')[0].split('.')[1], self.params['subnets'].split('/')[0].split('.')[2]) Mask = self.params['subnets'].split('/')[1] if self.params['switchdescribe'] == '': SwitchDescribe = '-' else: SwitchDescribe = self.params['switchdescribe'] operateinfo = { 'action': 'Create_Switch', 'parame': { 'Qdhcp': Qdhcp, 'TapUUID': TapUUID, 'TapMac': TapMac, 'Mask': Mask, 'GatewayIp': GatewayIp, 'DnsmasqxIp': DnsmasqxIp, 'Qrouter': Qrouter, 'QrUUID': QrUUID, 'Tags': Tags, 'TunId': TunId, 'Cidr': Cidr, 'RouterEips': RouterEips, 'DhcpStartIp': DhcpStartIp, 'UsableIp': UsableIp, }, } Status = NcloudLib.HttpRequest(operateinfo, '127.0.0.1', 35796) if Status['code'] == 0: operateinfos = { 'action': 'Create_TunFlow', 'parame': { 'Tags': Tags, 'TunId': TunId, }, } KvmList = KvmInfo.objects.filter(KVMArea=self.Data['Zone']) KvmTunStatus = 1 for KvmDetails in KvmList: Status = NcloudLib.HttpRequest(operateinfos, KvmDetails.KVMIp, 35796) if Status['code'] == 0: KvmTunStatus = 0 else: KvmTunStatus = 1 break if KvmTunStatus == 0: intSwitch = VpcSwitch(VswId=VswId, VswName=VswName, Status='ACTIVE', VpcId=self.params['vpcid'], TenantId=TenantId, Qdhcp=Qdhcp, TapUUID=TapUUID, TapMac=TapMac, Cidr=Cidr, GatewayIp=GatewayIp, DnsmasqxIp=DnsmasqxIp, UsableIp=UsableIp, SwitchDescribe=SwitchDescribe, Zone=self.Data['Zone'], UserId=self.Data['UserId']) intSwitch.save() return_data = { "code": 0, "msg": "创建交换机成功。", } else: return_data = { "code": 100013, "msg": "创建交换机失败。", } else: return_data = { "code": 100013, "msg": "创建交换机失败。", } else: return_data = {"code": 10012, "msg": "对不起,您输入的网段不正确。"} return return_data
def __CreateRouter(self, VpcId, TenantId): #生成UUID #print '-------------------------------------' #VrtId = 'vrt-' % str(uuid.uuid1())[:8] Qrouter = 'qrouter-%s' % str(uuid.uuid1()) #Qrouter = 'qrouter-' % str(uuid.uuid1())[:8] QrUUID = str(uuid.uuid1()) #QrUUID = 'qr-%s' % str(uuid.uuid1())[:11] QRMac = NcloudLib.buildMac() QgUUID = str(uuid.uuid1()) #QgUUID = 'qg-%s' % str(uuid.uuid1())[:11] QGMac = NcloudLib.buildMac() #print '-------------------------------------' #RouterEips = StringField(max_length=50) #RouterEipsId = StringField(max_length=50) VpcIpList = VpcIpInfo.objects.filter(IpArea=self.Data['Zone']).filter( Status=0)[0:1] for VpcIpDetails in VpcIpList: RouterEips = VpcIpDetails.IpAddress EipsNetMask = VpcIpDetails.NetMask RouterEipsId = str(VpcIpDetails.id) EipsGateWay = VpcIpDetails.GateWay VpcIpDetails.Status = 1 operateinfo = { 'action': 'Create_Router', 'parame': { 'Qrouter': Qrouter, 'QrUUID': QrUUID, 'QRMac': QRMac, 'QgUUID': QgUUID, 'QGMac': QGMac, 'RouterEips': RouterEips, 'EipsNetMask': EipsNetMask, 'EipsGateWay': EipsGateWay, }, } Status = NcloudLib.HttpRequest(operateinfo, '127.0.0.1', 35796) #print Status if Status['code'] == 0: #print 'ssaaaaaaaaaaaaa' #network 执行成功 插入数据 VrtId = 'vrt-%s' % str(uuid.uuid1())[:8] VpcRouterInt = VpcRouter(VrtId=VrtId, VrtName='-', Status='ACTIVE', VpcId=VpcId, TenantId=TenantId, Zone=self.Data['Zone'], Qrouter=Qrouter, QrUUID=QrUUID, QRMac=QRMac, QgUUID=QgUUID, QGMac=QGMac, RouterEips=RouterEips, RouterEipsId=RouterEipsId, UserId=self.Data['UserId']) VtbId = 'vtb-%s' % str(uuid.uuid1())[:8] TargetSubnets = '11.11.0.0/20' VpcRouterTableInt = VpcRouterTable(VtbId=VtbId, Status='ACTIVE', TargetSubnets=TargetSubnets, NextHop='-', NextHopType='-', Type=0, VpcId=VpcId, TenantId=TenantId, VrtId=VrtId, Zone=self.Data['Zone'], UserId=self.Data['UserId']) VpcRouterInt.save() VpcRouterTableInt.save() VpcIpDetails.save() return_data = { "code": 0, } else: return_data = { "coed": 1, } return return_data
def DeleteBankend(self): #根据条件查询列表 存在继续执行 不存在返回结果 try: BackendDetails = BackendInfo.objects.get( id=self.params['BackendId']) if BackendDetails['BackendStatus'] == 1: ServerBackend = 'server %s:%s weight=%s;' % ( BackendDetails['BackendIp'], BackendDetails['BackendPort'], BackendDetails['BackendWeight']) else: ServerBackend = '#server %s:%s weight=%s;' % ( BackendDetails['BackendIp'], BackendDetails['BackendPort'], BackendDetails['BackendWeight']) BackendDetail = BackendInfo.objects.filter( LbMonitorId=BackendDetails['LbMonitorId']) BackendNum = len(BackendDetail) LbMonitorDetail = LbMonitorInfo.objects.get( id=BackendDetails['LbMonitorId']) UpstreamName = LbMonitorDetail['LbDomain'].replace('.', '') operateinfo = { 'action': 'DeleteBankend', 'parame': { 'FileName': UpstreamName + str(LbMonitorDetail['LbPort']), 'ServerBackend': ServerBackend, "BackendNum": BackendNum, }, } if BackendDetails: #获取LbsGroupId LbDetails = LbInfo.objects.get(LbId=BackendDetails['LbId']) #获取LbsIp组 LbsDetails = LbsInfo.objects.filter(IpsId=LbDetails['IpsId']) #获取Lbs组状态 NLBHandleInfo = [] NLBStatus = 0 for LbsDetail in LbsDetails: LbsHandleInfo = { 'LbsWeight': LbsDetail['LbsWeight'], 'LbsIp': LbsDetail['LbsIp'], } #获取相对主备ip LBOperateOut = NcloudLib.HttpRequest( {'action': 'NLBStatus'}, LbsDetail['LbsIp'], 35796) NLBStatus = int(LBOperateOut['code']) NLBHandleInfo.append(LbsHandleInfo) if NLBStatus == 0: for LbsDetail in LbsDetails: LbsIp = LbsDetail.LbsIp BackendOperateOut = NcloudLib.HttpRequest( operateinfo, LbsDetail['LbsIp'], 35796) print BackendOperateOut if BackendOperateOut['code'] == 0: BackendStatus = 1 else: BackendStatus = 0 if BackendStatus == 1: BackendDetails.delete() code = 0 msg = '操作成功。' else: code = 0 msg = '操作失败' else: code = 1012 msg = '当前后端服务器不存在' except: code = 1011 msg = '当前后端服务器不存在' return_data = { 'code': code, 'msg': msg, } return_data = json.dumps(return_data) return return_data
def CreateLbBackend(self): VswDetails = VpcSwitch.objects.get(VswId=self.params['vswid']) VrtDetails = VpcRouter.objects.get(TenantId=VswDetails.TenantId) BackendList = BackendInfo.objects.filter( LbMonitorId=self.params['LbUpstreamId']).filter( TenantId=VswDetails.TenantId).filter( BackendPort=self.params['BackendPort']) if BackendList: code = 1 msg = '您提交的已提交,请修改后重新提交' else: #获取ECH IPAddress EchList = EchInfo.objects.get(EchId=self.params['EchId']) EchIp = EchList.EchIp BackendPort = self.params['BackendPort'] BackendWeight = self.params['BackendWeight'] #获取LBMonitor domain,port,LbProtocol LbMonitorList = LbMonitorInfo.objects.get( id=self.params['LbUpstreamId']) LbDomain = LbMonitorList.LbDomain UpstreamName = LbDomain.replace('.', '') listener_port = LbMonitorList.LbPort LbProtocol = LbMonitorList.LbProtocol RouterEips = VrtDetails.RouterEips BackendDetails = BackendInfo.objects.filter( LbMonitorId=self.params['LbUpstreamId']) BackendNum = len(BackendDetails) #创建Backend ServerBackend = """server %s:%s weight=%s;\n""" % ( RouterEips, BackendPort, BackendWeight) operateinfo = { 'action': 'CreateLbBackend', 'parame': { 'FileName': UpstreamName + str(listener_port), 'ServerBackend': ServerBackend, "BackendNum": BackendNum, }, } LbDetails = LbInfo.objects.get(LbId=self.params['loadbalancerid']) LbsList = LbsInfo.objects.filter(IpsId=LbDetails['IpsId']) if LbsList: #获取负载集群状态 NLBHandleInfo = [] NLBStatus = 0 for LbsDetail in LbsList: LbsHandleInfo = { 'LbsWeight': LbsDetail['LbsWeight'], 'LbsIp': LbsDetail['LbsIp'], } #获取相对主备ip LBOperateOut = NcloudLib.HttpRequest( {'action': 'NLBStatus'}, LbsDetail['LbsIp'], 35796) NLBStatus = int(LBOperateOut['code']) NLBHandleInfo.append(LbsHandleInfo) if NLBStatus == 0: for LbsDetail in LbsList: LbsIp = LbsDetail.LbsIp print operateinfo BackendOperateOut = NcloudLib.HttpRequest( operateinfo, LbsDetail['LbsIp'], 35796) print BackendOperateOut if BackendOperateOut['code'] == 0: BackendStatus = 1 else: BackendStatus = 0 if BackendStatus == 1: inBackend = BackendInfo( BackendName=self.params['BackendName'], BackendIp=EchIp, BackendPort=BackendPort, LbProtocol=LbProtocol, BackendWeight=BackendWeight, EchId=self.params['EchId'], TenantId=VswDetails.TenantId, RouterEips=RouterEips, LbMonitorId=self.params['LbUpstreamId'], LbId=self.params['loadbalancerid']) inBackend.save() code = 0 msg = '创建成功。' else: code = 0 msg = '暂时无法为您添加后端负载,请您稍后提交或联系客服。' else: code = 10010 msg = '暂时无法为您添加后端负载,请您稍后提交或联系客服。' return_data = { 'code': code, 'msg': msg, } return_data = json.dumps(return_data) return return_data
def CreateLB(self): #计算LbId LbUUID = str(uuid.uuid1()) LbId = 'lb-' + LbUUID[:8] eipgroup = EipInfo.objects.get(id=self.params['eip']) eipgroup.AssignType = 1 eipgroup.AssignId = LbUUID EipId = eipgroup.EipId EipGroup = eipgroup.EipGroup IpsId = eipgroup.EipAddressId #如果用户类型等于3,则直接插入数据 if self.Data['UserType'] != 3: #查询Lvsinfo表 #查询当前区域是否有LVS集群 有继续 无返回结果 Lvs_List = LvsInfo.objects.filter(LvsZone=self.Data['Zone']) if Lvs_List: #print '存在' LVSHandles = [] LVSStatus = 0 for LvsDetails in Lvs_List: LvsHandleInfo = { 'id': str(LvsDetails['id']), 'LvsWeight': LvsDetails['LvsWeight'], 'LvsIp': LvsDetails['LvsIp'], 'GroupId': LvsDetails['GroupId'], } LVOperateOut = NcloudLib.HttpRequest( {'action': 'LVSStatus'}, LvsDetails['LvsIp'], 35796) print LVOperateOut LVSStatus = int(LVOperateOut['code']) VrrpNum = LvsDetails['VrrpNum'] LVSHandles.append(LvsHandleInfo) #判断LVS集群组状态是否正常 正常继续 不正常返回结果 if LVSStatus == 0: print IpsId Lbs_List = LbsInfo.objects.filter(IpsId=IpsId) if Lbs_List: print '存在Lbs' #存在负载 则为查询结果中lbsweight 1为主 0为备 NLBHandleInfo = [] NLBStatus = 0 NLBIPs = [] for LbsDetail in Lbs_List: LbsHandleInfo = { 'LbsWeight': LbsDetail['LbsWeight'], 'LbsIp': LbsDetail['LbsIp'], 'id': LbsDetail['id'], 'GroupId': LbsDetail['GroupId'], } #获取相对主备ip #获取当前负载主机状态 LBOperateOut = NcloudLib.HttpRequest( {'action': 'NLBStatus'}, LbsDetail['LbsIp'], 35796) NLBStatus = int(LBOperateOut['code']) NLBIPs.append(LbsDetail['LbsIp']) NLBHandleInfo.append(LbsHandleInfo) #如果NLBStatus=0 负载集群组存活 if NLBStatus == 0: if EipGroup == 1: #双线IP #获取VID和Rid #根据VrrpNum 计算ViD和Rid 当前值 T+1 U+2 更新VrrpNum+2 TVrrpNum = VrrpNum + 1 UVrrpNum = VrrpNum + 2 KVrrpNum = VrrpNum + 2 Tip = self.params['eips'].split(',')[0] Uip = self.params['eips'].split(',')[1] parame = { 'TVrrpNum': TVrrpNum, 'UVrrpNum': UVrrpNum, 'Tip': Tip, 'Uip': Uip, } #获取KEEPLIVED配置文件 LvsCfg = NcloudNlbLib.GetDoubletLvsCfg(parame) #获取NLB-Server配置文件 #LbCfg = NcloudNlbLib.GetDoubletCreateLBCfg(NLBHandleInfo) for LVSHandle in LVSHandles: if LVSHandle['LvsWeight'] == 1: operateinfo = { 'action': 'CreateLVS', 'parame': { 'Keepalived': LvsCfg['keepalivedM'], 'LBroute': LvsCfg['LBroute'], 'FileLvsName': self.params['eips'], 'Eips': [Tip, Uip], } } LvsId = LVSHandle['id'] LvsGroupId = LVSHandle['GroupId'] else: operateinfo = { 'action': 'CreateLVS', 'parame': { 'Keepalived': LvsCfg['keepalivedB'], 'LBroute': LvsCfg['LBroute'], 'FileLvsName': self.params['eips'], 'Eips': [Tip, Uip], } } print operateinfo LVSOperateOut = NcloudLib.HttpRequest( operateinfo, LVSHandle['LvsIp'], 35796) print LVSOperateOut if LVSOperateOut['code'] == 0: CreateLvsStatus = 1 else: CreateLvsStatus = 0 CreateLBStatus = 1 ''' for NLBHandle in NLBHandleInfo: operateinfo = { 'action':'CreateLB', 'parame': { 'LBserver' : LbCfg['LBserver'], 'FileServerName' : NLBHandle['LbsIp'], }, } print operateinfo #LBOperateOut = Operate(NLBHandle['LbsIp'],operateinfo) print LBOperateOut if 'done' == 'done': CreateLBStatus = 1 else: CreateLBStatus = 0 ''' if CreateLBStatus == 1 and CreateLvsStatus == 1: #更新LVS相对主备 for LvsDetails in Lvs_List: if LvsDetails['LvsWeight'] == 1: LvsDetails.LvsWeight = 2 else: LvsDetails.LvsWeight = 1 LvsDetails.VrrpNum = KVrrpNum LvsDetails.save() inLb = LbInfo(LbId=LbId, LbName=self.params['LBName'], LbType=self.params['LBType'], LbIp=self.params['eips'], EipId=self.params['eip'], LbContNum=5000, LbZone=self.Data['Zone'], LvsId=LvsId, LvsGroupId=LvsGroupId, LbPaymentType=0, LbPaymentDate=0, IpsId=IpsId, LbUUID=LbUUID, UserId=self.Data['UserId']) inLb.save() eipgroup.save() return_data = { 'code': 0, 'msg': '您已提交成功,稍等片刻,8号就能为您服务了。', } else: return_data = { 'code': 1001, 'msg': '创建失败,请您重新在多戳几次鼠标或联系客服妹子。', } else: return_data = { 'code': 1, 'msg': '创建失败,请您重新在多戳几次鼠标或联系客服妹子1。', } else: ''' print '不存在Lbs' #不存在更改预备负载集群状态 #获取是否存在预备负载集群 存在 获取LbsWeight = 0 为主 并变更状态为1 不存在则返回 Lbs_ready_List = LbsInfo.objects.filter(LbsStatus=0).filter(LbsZone=self.Data['Zone']).filter(GroupId=0) if Lbs_ready_List: NLBHandleInfo = [] NLBStatus = 0 #计算LBSGroupID = LbsGroupIdList = LbsInfo.objects.filter() LbsGroupId = len(LbsGroupIdList)/2 for LbsDetail in Lbs_ready_List: LbsHandleInfo = { 'LbsWeight' : LbsDetail['LbsWeight'], 'LbsIp' : LbsDetail['LbsIp'], 'id' : LbsDetail['id'], } if LbsDetail['LbsWeight'] == 1: LbsId = str(LbsDetail['id']) #获取相对主备ip LBOperateOut = Operate(LbsDetail['LbsIp'],{'action':'NLBStatus'}) NLBStatus = int(LBOperateOut) NLBHandleInfo.append(LbsHandleInfo) if NLBStatus == 0: if EipGroup == 1: #双线IP #获取VID和Rid #根据VrrpNum 计算ViD和Rid 当前值 T+1 U+2 更新VrrpNum+2 TVrrpNum = VrrpNum + 1 UVrrpNum = VrrpNum + 2 KVrrpNum = VrrpNum + 2 Tip = self.params['eips'].split(',')[0] Uip = self.params['eips'].split(',')[1] parame = { 'TVrrpNum' : TVrrpNum, 'UVrrpNum' : UVrrpNum, 'Tip' : Tip, 'Uip' : Uip, } #获取KEEPLIVED配置文件 LvsCfg = NcloudNlbLib.GetDoubletLvsCfg(parame) #获取NLB-Server配置文件 LbCfg = NcloudNlbLib.GetDoubletCreateLBCfg(NLBHandleInfo) for LVSHandle in LVSHandles: if LVSHandle['LvsWeight'] == 1: operateinfo = { 'action':'CreateLVS', 'parame' : { 'Keepalived' : LvsCfg['keepalivedM'], 'LBroute' : LvsCfg['LBroute'], 'FileLvsName' : self.params['eips'], 'Eips' : [Tip,Uip], } } LvsId = LVSHandle['id'] LvsGroupId = LVSHandle['GroupId'] else: operateinfo = { 'action':'CreateLVS', 'parame' : { 'Keepalived' : LvsCfg['keepalivedB'], 'LBroute' : LvsCfg['LBroute'], 'FileLvsName' : self.params['eips'], 'Eips' : [Tip,Uip], } } print operateinfo LVSOperateOut = Operate(LVSHandle['LvsIp'],operateinfo) print LVSOperateOut if LVSOperateOut == 'done': CreateLvsStatus = 1 else: CreateLvsStatus = 0 for NLBHandle in NLBHandleInfo: operateinfo = { 'action':'CreateLB', 'parame': { 'LBserver' : LbCfg['LBserver'], 'FileServerName' : self.params['eips'], 'NetLo' : NetLo, }, } print operateinfo #LBOperateOut = Operate(NLBHandle['LbsIp'],operateinfo) #print LBOperateOut if 'done' == 'done': CreateLBStatus = 1 else: CreateLBStatus = 0 if CreateLBStatus == 1 and CreateLvsStatus == 1: #更新当前相对主的状态 for LbsStatusInfo in Lbs_ready_List: LbsStatusInfo.GroupId = LbsGroupId LbsStatusInfo.save() for LvsDetails in Lvs_List: if LvsDetails['LvsWeight'] == 1: LvsDetails.LvsWeight = 2 else: LvsDetails.LvsWeight = 1 LvsDetails.VrrpNum = KVrrpNum LvsDetails.save() LbsStatusEdit = LbsInfo.objects.get(id=LbsId) LbsStatusEdit.LbsStatus = 1 LbsStatusEdit.save() inLb = LbInfo(LbId=LbId,LbName=self.params['LBName'],LbType=self.params['LBType'],LbIp=self.params['eips'],EipId=self.params['eip'],LbContNum=5000,LbsZone=self.Data['Zone'],LbsId=LbsId,LbsGroupId=LbsGroupId,LvsId=LvsId,LvsGroupId=LvsGroupId,LbPaymentType=0,LbPaymentDate=0,UserId=self.Data['UserId']) inLb.save() eipgroup.save() return_data = { 'code' : 0, 'msg' : '您已提交成功,稍等片刻,8号就能为您服务了。', } else: return_data = { 'code' : 10010, 'msg' : '创建失败,请您重新在多戳几次鼠标或联系客服妹子1。', } else: return_data = { 'code' : 1001, 'msg' : '创建失败,请您重新在多戳几次鼠标或联系客服妹子1。', } ''' return_data = { 'code': 1001, 'msg': '创建失败,请您重新在多戳几次鼠标或联系客服妹子。', } else: print '暂时无法操作' return_data = { 'code': 1, 'msg': '创建失败,请您重新在多戳几次鼠标或联系客服妹子。', } else: #print '不存在' return_data = { 'code': 10010, 'msg': '对不起,您提交的区域暂时无法为您创建负载均衡。', } else: #用户类型==3 调用统一的NLB 直接插入数据 inLb = LbInfo(LbId=LbId, LbName=self.params['LBName'], LbType=self.params['LBType'], LbIp=self.params['eips'], EipId=self.params['eip'], LbContNum=5000, LbZone=self.Data['Zone'], LvsId='', LvsGroupId=1, LbPaymentType=0, LbPaymentDate=0, IpsId=IpsId, LbUUID=LbUUID, UserId=self.Data['UserId']) inLb.save() eipgroup.save() return_data = { 'code': 0, 'msg': '您已提交成功,稍等片刻,8号就能为您服务了。', } return_data = json.dumps(return_data) return return_data