示例#1
0
    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
示例#2
0
	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
示例#3
0
    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