示例#1
0
文件: func.py 项目: znut/maekin-cloud
def getCloudInfo(midIP):
	text = rUrl(midIP+':8080'+'/cloud/getInfo')
	if text.count('403 Forbidden') != 0:
		return 'Host is forbidden'
	elif not text.startswith('error') and  text != '':
		cloud_info = {}
		c = getXML(text,'content')[0]
		cloud_info['UUID'] = str(getXMLVal(c,'UUID'))
		cloud_info['name'] = str(getXMLVal(c,'name'))
		net = getXML(c,'network')[0]
		cloud_info['networkID'] = str(net.attributes['id'].value)
		cloud_info['mask'] = str(net.attributes['mask'].value)
		cloud_info['defaultRoute'] = str(net.attributes['defaultRoute'].value)
		if(str(getXMLVal(c,'dns')) != ''):
			cloud_info['dnsPool'] = str(getXMLVal(c,'dns')).split(',')
		else:
			cloud_info['dnsPool'] = []
		if(str(getXMLVal(c,'guest')) != ''):
			cloud_info['guestPool'] = str(getXMLVal(c,'guest')).split(',')
		else:
			cloud_info['guestPool'] = []
		cloud_info['automode'] = str(getXMLVal(c,'autoMode'))
		return cloud_info
	else:
		return 'Not found Cloud API'
示例#2
0
文件: func.py 项目: znut/maekin-cloud
def getHostInfo(hostID=""):
	hostInfoList = []
	if hostID == "":
		text = cloud_rURL("/host/getInfo")
		if text.startswith('error') or text == '' :
			return hostInfoList
		for a in getXML(text,'host'):
			hostInfo = extractHostInfo(a)
			hostInfoList.append(hostInfo)
	else:
		text = cloud_rURL("/host/getInfo?hostID="+hostID)
		if text.startswith('error') or text == '' :
			return hostInfoList
		hostInfoList.append(extractHostInfo(getXML(text,'host')[0]))
	return hostInfoList
示例#3
0
文件: func.py 项目: znut/maekin-cloud
def getTemplate(temID=''):
	if temID == '':
		text = cloud_rURL("/template/getInfo")
		template_list = []
		if text.startswith('error') or text == '':
			return ''
		for a in getXML(text,'template'):
			template = {}
			template['id'] = str(a.attributes['templateID'].value)
			template['OS'] = str(getXMLVal(a,'OS'))
			if 'windows' in str.lower(template['OS']):
				template['ostype'] = 'windows'
			elif 'centos' in str.lower(template['OS']):
				template['ostype'] = 'centos'
			elif 'ubuntu' in str.lower(template['OS']):
				template['ostype'] = 'ubuntu'
			elif 'linux' in str.lower(template['OS']):
				template['ostype'] = 'linux'
			else:
				template['ostype'] = 'OS'
			template['description'] = str(getXMLVal(a,'description'))
			template['minimumMemory'] = str(getXMLVal(a,'minimumMemory'))
			template['maximumMemory'] = str(getXMLVal(a,'maximumMemory'))
			template_list.append(template)
			if len(Template.objects.filter(templateID=template['id']))==0:
				newTem = Template.objects.create(OS=template['OS'],templateID=template['id'],description=template['description'],minMem =template['minimumMemory'],maxMem=template['maximumMemory'],ostype=template['ostype'])
				VI.objects.get(id=0).template.add(newTem)
		return template_list
	else:
		text = cloud_rURL("/template/getInfo?templateID="+temID)
		a = getXML(text,'template')[0]
		template = {}
		template['id'] = str(a.attributes['templateID'].value)
		template['OS'] = str(getXMLVal(a,'OS'))
		if 'windows' in str.lower(template['OS']):
			template['ostype'] = 'windows'
		elif 'centos' in str.lower(template['OS']):
			template['ostype'] = 'centos'
		elif 'ubuntu' in str.lower(template['OS']):
			template['ostype'] = 'ubuntu'
		elif 'linux' in str.lower(template['OS']):
			template['ostype'] = 'linux'
		else:
			template['ostype'] = 'OS'
		template['description'] = str(getXMLVal(a,'description'))
		template['minimumMemory'] = str(getXMLVal(a,'minimumMemory'))
		template['maximumMemory'] = str(getXMLVal(a,'maximumMemory'))
		return template
示例#4
0
文件: func.py 项目: znut/maekin-cloud
def isHostTask(data):
	xml = getXML(data,'task')[0]
	opcode = xml.attributes['taskID'].value
	if int(opcode) >= 16:
		return True
	else:
		return False
示例#5
0
文件: func.py 项目: znut/maekin-cloud
def extractVMInfo(a):
	imageID = int(a.attributes['guestID'].value)
	name 	= str(a.attributes['guestName'].value)
	hostid 	= str(getXMLVal(a,'lastHostID'))
	uuid 	= str(getXMLVal(a,'lastUUID'))
	MAC 	= str(getXMLVal(a,'MAC'))
	IP 		= str(getXMLVal(a,'IP'))
	template = str(getXMLVal(a,'templateID'))
	memory 	= str(int(getXMLVal(a,'memory'))/1024/1024)
	vCPU 	= str(getXMLVal(a,'vCPU'))
	inbound = str(getXMLVal(a,'inbound'))
	outbound = str(getXMLVal(a,'outbound'))
	vm 		= {'imageID':imageID,'name':name,'hostid':hostid,'uuid':uuid,'MAC':MAC,'IP':IP,'template':template,'vCPU':vCPU,'memory':memory,'inbound':inbound,'outbound':outbound}
	text2 = cloud_rURL('/guest/getState?guestID='+str(imageID))
	guestRaw = getXML(text2,'guest')
	if len(guestRaw) > 0:
		b = guestRaw[0]
		vm['status'] = ['shutoff','on','saved'][int(getXMLVal(b,'status'))]
		vm['activity'] = ['none','cloning','booting','saveing','restoring'][int(getXMLVal(b,'activity'))]
		vm['runningState'] = ['none','running','out of memory','paused','shutting down','shutoff','crashed'][int(getXMLVal(b,'runningState'))]
		vmobj = get_or_none(VM,imageID=imageID)
		if not vmobj == None:
			vm['ownerVI'] = VM.objects.get(imageID=imageID).ownerVI.id
		else:
			vm['ownerVI'] = 0
		return vm
	else:
		return None
示例#6
0
文件: func.py 项目: znut/maekin-cloud
def getVMinfo(vmid=None):
	vm_info_list = []
	if vmid == None:
		text = cloud_rURL('/guest/getInfo')
		if text.startswith('error') or text == '':
			return ''
		for a in getXML(text,'guest'):
			vm_info = extractVMInfo(a)
			if vm_info != None:
				vm_info_list.append(vm_info)
		return vm_info_list
	else:
		text = cloud_rURL('/guest/getInfo?guestID='+str(vmid))
		if text != '' :
			if len(getXML(text,'guest')) > 0:	
				return extractVMInfo(getXML(text,'guest')[0])
		return []
示例#7
0
文件: func.py 项目: znut/maekin-cloud
def setHostLogDetail(data):
	xml = getXML(data,'task')[0]
	taskID = int(xml.attributes['taskID'].value)
	log = HostLog.objects.get(taskID=taskID)
	x = int(getXMLVal(xml,'status'))
	log.status = ['queued','working','finish'][x]
	currentfinishStatus = ['None','success','error'][int(getXMLVal(xml,'finishStatus'))]
	if log.action == 'host_add':
		if log.status == 'finish' and currentfinishStatus == 'success':
			log.hostID = int(getXML(data,'host')[0].attributes['hostID'].value)
	log.startTime = str(getXMLVal(xml,'createTimestamp'))
	log.finishTime = str(getXMLVal(xml,'finishTimestamp'))
	log.finishMessage = str(getXMLVal(xml,'finishMessage'))
	if currentfinishStatus == 'error':
		log.error = True
	elif currentfinishStatus == 'success':
		log.error = False
	log.save()
	return log
示例#8
0
文件: func.py 项目: znut/maekin-cloud
def setVMLogDetail(data):
	xml = getXML(data,'task')[0]
	taskID = int(xml.attributes['taskID'].value)
	log = VMLog.objects.get(taskID=taskID)
	x = int(getXMLVal(xml,'status'))
	log.status = ['queued','working','finish'][x]
	currentfinishStatus = ['None','success','error'][int(getXMLVal(xml,'finishStatus'))]
	if log.action == 'create':
		if log.status == 'finish' and currentfinishStatus == 'success':
			log.vmImageID = int(getXML(data,'guest')[0].attributes['guestID'].value)
			if get_or_none(VM,imageID=int(getXML(data,'guest')[0].attributes['guestID'].value)) == None:
				recordNewVMtoDB(getVMinfo(log.vmImageID),log.viID)
	log.startTime = str(getXMLVal(xml,'createTimestamp'))
	log.finishTime = str(getXMLVal(xml,'finishTimestamp'))
	log.finishMessage = str(getXMLVal(xml,'finishMessage'))
	if currentfinishStatus == 'error':
		log.error = True
	elif currentfinishStatus == 'success':
		log.error = False
	log.save()
	return log
示例#9
0
文件: func.py 项目: znut/maekin-cloud
def getHostStat(targetHostID=''):
	hostListStat = []
	if targetHostID == '':
		alldata = cloud_rURL('/host/getAllCurrentInfo')
	else:
		alldata = cloud_rURL('/host/getAllCurrentInfo?hostID='+targetHostID)
	for data in getXML(alldata,'host'):
		hostID = str(data.attributes['hostID'].value)
		if  str(data.attributes['polling'].value) == 'success':
			hostStat= {}
			hostStat['hostID'] = hostID
			hostStat['cpuAverage'] 	= str(getXMLVal(getXML(data,'cpuInfo')[0],'average'))
			hostStat['memTotal'] 	= str(getXMLVal(getXML(data,'memoryInfo')[0],'memTotal'))
			hostStat['memFree'] 	= str(getXMLVal(getXML(data,'memoryInfo')[0],'memFree'))
			hostStat['netRX'] 	= str(getXMLVal(getXML(data,'networkInfo')[0],'rx'))
			hostStat['netTX'] 	= str(getXMLVal(getXML(data,'networkInfo')[0],'tx'))
			hostStat['storageCap'] 	= str(getXMLVal(getXML(data,'storageInfo')[0],'capacity'))
			hostStat['storageFree'] 	= str(getXMLVal(getXML(data,'storageInfo')[0],'free'))
			hostStat['storageMaekinUsage'] 	= str(getXMLVal(getXML(data,'storageInfo')[0],'maekinUsage'))
			hostStat['storageImageUsage'] 	= str(getXMLVal(getXML(data,'storageInfo')[0],'imageUsage'))
			hostListStat.append(hostStat)
	return hostListStat
示例#10
0
文件: func.py 项目: znut/maekin-cloud
def createHostLog(hostID,user,command,xml):
	if hostID == None:
		log = HostLog.objects.create(hostID=None,username=user.username,action=command,taskID=int(getXML(xml,'task')[0].attributes['taskID'].value))
	else:
		log = HostLog.objects.create(hostID=int(hostID),username=user.username,action=command,taskID=int(getXML(xml,'task')[0].attributes['taskID'].value))
示例#11
0
文件: func.py 项目: znut/maekin-cloud
def createVMLog(vmImageID,name,user,command,xml,viid=None):
	if vmImageID == None:	#for createVM
		log = VMLog.objects.create(vmImageID=None,vmName=name,username=user.username,action=command,viID=viid,taskID=int(getXML(xml,'task')[0].attributes['taskID'].value))
	else:
		log = VMLog.objects.create(vmImageID=int(vmImageID),vmName=name,username=user.username,action=command,viID=viid,taskID=int(getXML(xml,'task')[0].attributes['taskID'].value))
示例#12
0
文件: func.py 项目: znut/maekin-cloud
def getVMStat(vmlist,getCPU,getMEM,getNET,getIO):
	vmListStat = []
	alldata = cloud_rURL('/guest/getCustomizedInfo?cpu='+getCPU+'&memory='+getMEM+'&network='+getNET+'&io='+getIO+'&guestIDs='+vmlist)
	for data in getXML(alldata,'guest'):
		vmID = int(data.attributes['guestID'].value)
		if  str(data.attributes['polling'].value) == 'success':
			vmStat = {}
			vmStat['vmID'] = vmID
			if getCPU == '1':
				vmStat['cpuAverage'] 	= str(getXMLVal(getXML(data,'cpuInfo')[0],'average'))
				vmStat['cpuCpuTime'] 	= str(getXMLVal(getXML(data,'cpuInfo')[0],'cpuTime'))
			if getMEM == '1':
				vmStat['memTotal'] 	= str(getXMLVal(getXML(data,'memoryInfo')[0],'memTotal'))
				vmStat['memUse'] 	= str(getXMLVal(getXML(data,'memoryInfo')[0],'memUse'))
			if getNET == '1':
				vmStat['netRX'] 	= str(getXMLVal(getXML(data,'networkInfo')[0],'rx'))
				vmStat['netTX'] 	= str(getXMLVal(getXML(data,'networkInfo')[0],'tx'))
				vmStat['netSumRx'] 	= str(getXMLVal(getXML(data,'networkInfo')[0],'sumRx'))
				vmStat['netSumTx'] 	= str(getXMLVal(getXML(data,'networkInfo')[0],'sumTx'))
			if getIO == '1':
				vmStat['ioRX'] 	= str(getXMLVal(getXML(data,'ioInfo')[0],'rx'))
				vmStat['ioTX'] 	= str(getXMLVal(getXML(data,'ioInfo')[0],'wx'))
				vmStat['ioSumRx'] 	= str(getXMLVal(getXML(data,'ioInfo')[0],'sumRx'))
				vmStat['ioSumTx'] 	= str(getXMLVal(getXML(data,'ioInfo')[0],'sumWx'))
			vmListStat.append(vmStat)
	return vmListStat