def allocateIp(self): ''' Allocates an IP address of the range ''' with MutexStore.getObjectLock(self.getLockIdentifier()): #Implements first fit algorithm if self.nextAvailableIp == None: raise Exception("Could not allocate any IP") newIp = Ip4Slot.ipFactory(self,self.nextAvailableIp) self.ips.add(newIp) #Try to find new slot try: it= IP4Utils.getIpIterator(self.nextAvailableIp,self.endIp,self.netMask) while True: ip = it.getNextIp() if self.__isIpAvailable(ip): break self.nextAvailableIp = ip except Exception as e: self.nextAvailableIp = None self.autoSave() return newIp
def allocateMac(self): ''' Allocates an MAC address of the range ''' with MutexStore.getObjectLock(self.getLockIdentifier()): #Implements first fit algorithm if self.nextAvailableMac == None: raise Exception("Could not allocate any Mac") newMac = MacSlot.macFactory(self,self.nextAvailableMac) self.macs.add(newMac) #Try to find new slot try: it= EthernetUtils.getMacIterator(self.nextAvailableMac,self.endMac) while True: mac = it.getNextMac() if self.__isMacAvailable(mac): break self.nextAvailableMac = mac except Exception as e: self.nextAvailableMac = None self.autoSave() return newMac
def addExcludedMac(self,macStr,comment=""): ''' Add an MAC to the exclusion list ''' with MutexStore.getObjectLock(self.getLockIdentifier()): #Check is not already allocated if not self.__isMacAvailable(macStr): raise Exception("Mac already allocated or marked as excluded") #then forbidd if not EthernetUtils.isMacInRange(macStr,self.startMac,self.endMac): raise Exception("Mac is not in range") newMac = MacSlot.excludedMacFactory(self,macStr,comment) self.macs.add(newMac) #if was nextSlot shift if self.nextAvailableMac == macStr: try: it= EthernetUtils.getMacIterator(self.nextAvailableMac,self.endMac) while True: mac = it.getNextMac() if self.__isMacAvailable(mac): break self.nextAvailableMac= mac except Exception as e: self.nextAvailableMac = None self.autoSave()
def addExcludedIp(self,ipStr,comment): ''' Add an IP to the exclusion list ''' with MutexStore.getObjectLock(self.getLockIdentifier()): #Check is not already allocated if not self.__isIpAvailable(ipStr): raise Exception("Ip already allocated or marked as excluded") #then forbidd if not IP4Utils.isIpInRange(ipStr,self.startIp,self.endIp): raise Exception("Ip is not in range") newIp = Ip4Slot.excludedIpFactory(self,ipStr,comment) self.ips.add(newIp) #if was nextSlot shift if self.nextAvailableIp == ipStr: try: it= IP4Utils.getIpIterator(self.nextAvailableIp,self.endIp,self.netMask) while True: ip = it.getNextIp() if self.__isIpAvailable(ip): break self.nextAvailableIp = ip except Exception as e: self.nextAvailableIp = None self.autoSave()
def removeExcludedMac(self,macObj): ''' Deletes an Mac from the exclusion list (but it does not destroy the object!!) ''' with MutexStore.getObjectLock(self.getLockIdentifier()): macStr = macObj.getMac() if (not self.macs.get(mac=macStr).isExcludedMac()): raise Exception("Cannot release Mac. Reason may be is unallocated or is not excluded Mac") self.macs.remove(macObj) #Determine new available Mac if not self.nextAvailableMac == None: if EthernetUtils.compareMacs(macStr,self.nextAvailableMac) > 0: #Do nothing pass else: self.nextAvailableMac = macStr else: #No more gaps self.nextAvailableMac = macStr self.autoSave()
def addDataBridge(self,name,macStr,switchId,port): with MutexStore.getObjectLock(self.getLockIdentifier()): if self.networkInterfaces.filter(name=name, isBridge=True).count()> 0: raise Exception("Another data bridge with the same name already exists in this Server") netInt = NetworkInterface.createServerDataBridge(name,macStr,switchId,port) self.networkInterfaces.add(netInt) self.autoSave()
def destroy(self): with MutexStore.getObjectLock(self.getLockIdentifier()): if self.getNumberOfConnections(): raise Exception("Cannot destroy a bridge which has enslaved interfaces") for ip in self.ip4s.all(): ip.destroy() self.mac.destroy() self.delete()
def destroy(self): with MutexStore.getObjectLock(self.getLockIdentifier()): if self.macs.filter(isExcluded=False).count() > 0: raise Exception("Cannot delete MacRange. Range still contains allocated Macs") for mac in self.macs.all(): #Delete excluded macs mac.delete() self.delete()
def destroy(self): with MutexStore.getObjectLock(self.getLockIdentifier()): if self.ips.filter(isExcluded=False).count() > 0: raise Exception("Cannot delete Ip4Range. Range still contains allocated IPs") for ip in self.ips.all(): #Delete excluded ips ip.delete() self.delete()
def deleteVM(self,vm): with MutexStore.getObjectLock(self.getLockIdentifier()): if vm not in self.vms.all(): raise Exception("Cannot delete a VM from pool if it is not already in") self.vms.remove(vm) # Delete related db entry Action.objects.all().filter(objectUUID=vm.uuid).delete() # Keep actions table up-to-date after each deletion #vm_uuids = [ x.uuid for x in VirtualMachine.objects.all() ] #Action.objects.all().exclude(objectUUID__in = vm_uuids).delete() vm.destroy() self.autoSave()
def deleteDataBridge(self,netInt): with MutexStore.getObjectLock(self.getLockIdentifier()): if not isinstance(netInt,NetworkInterface): raise Exception("Can only delete Data Bridge that are instances of NetworkInterace") #TODO: delete interfaces from VMs, for the moment forbid if netInt.getNumberOfConnections() > 0: raise Exception("Cannot delete a Data bridge from a server if this bridge has VM's interfaces ensalved.") self.networkInterfaces.remove(netInt) netInt.destroy() self.autoSave()
def destroy(self): with MutexStore.getObjectLock(self.getLockIdentifier()): if self.vms.all().count()>0: raise Exception("Cannot destroy a server which hosts VMs. Delete VMs first") #Delete associated interfaces for interface in self.networkInterfaces.all(): interface.destroy() #Delete instance self.delete()
def createVM(self,name,uuid,projectId,projectName,sliceId,sliceName,osType,osVersion,osDist,memory,discSpaceGB,numberOfCPUs,callBackUrl,hdSetupType,hdOriginPath,virtSetupType,save=True): with MutexStore.getObjectLock(self.getLockIdentifier()): if KVMVM.objects.filter(uuid=uuid).count() > 0: raise Exception("Cannot create a Virtual Machine with the same UUID as an existing one") #Allocate interfaces for the VM interfaces = self.createEnslavedVMInterfaces() #Call factory vm = KVMVM.create(name,uuid,projectId,projectName,sliceId,sliceName,osType,osVersion,osDist,memory,discSpaceGB,numberOfCPUs,callBackUrl,interfaces,hdSetupType,hdOriginPath,virtSetupType,save) self.vms.add(vm) self.autoSave() return vm
def setMgmtBridge(self,name,macStr): with MutexStore.getObjectLock(self.getLockIdentifier()): nInter = self.networkInterfaces.filter(isMgmt=True,isBridge=True) if nInter.count() == 1: mgmt = nInter.get() mgmt.setName(name) mgmt.setMacStr(macStr) elif nInter.count() == 0: mgmt = NetworkInterface.createServerMgmtBridge(name,macStr) self.networkInterfaces.add(mgmt) else: raise Exception("Unexpected length of management NetworkInterface query") self.autoSave()
def rebasePointer(self): '''Used when pointer has lost track mostly due to bug #''' with MutexStore.getObjectLock(self.getLockIdentifier()): print "Rebasing pointer of range: "+str(self.id) print "Current pointer point to: "+self.nextAvailableIp try: it= IP4Utils.getIpIterator(self.startIp,self.endIp,self.netMask) while True: ip = it.getNextIp() if self.__isIpAvailable(ip): break self.nextAvailableIp = ip except Exception as e: self.nextAvailableIp = None print "Pointer will be rebased to: "+self.nextAvailableIp self.save()
def rebasePointer(self): '''Used when pointer has lost track mostly due to bug #''' with MutexStore.getObjectLock(self.getLockIdentifier()): print "Rebasing pointer of range: "+str(self.id) print "Current pointer point to: "+self.nextAvailableMac try: it= EthernetUtils.getMacIterator(self.startMac,self.endMac) while True: mac = it.getNextMac() if self.__isMacAvailable(mac): break self.nextAvailableMac= mac except Exception as e: self.nextAvailableMac = None print "Pointer will be rebased to: "+self.nextAvailableMac self.save()
def releaseIp(self,ipObj): ''' Releases an IP address of the range (but it does not destroy the object!!) ''' with MutexStore.getObjectLock(self.getLockIdentifier()): ipStr = ipObj.getIp() if not self.ips.filter(ip=ipStr,isExcluded=False).count() > 0: raise Exception("Cannot release Ip %s. Reason may be is unallocated or is an excluded Ip",ipStr) self.ips.remove(ipObj) #Determine new available Ip if not self.nextAvailableIp == None: if IP4Utils.compareIps(ipStr,self.nextAvailableIp) > 0: #Do nothing pass else: self.nextAvailableIp = ipStr else: #No more gaps self.nextAvailableIp = ipStr self.autoSave()
def releaseMac(self,macObj): ''' Releases an MAC address of the range (but it does not destroy the object!!) ''' with MutexStore.getObjectLock(self.getLockIdentifier()): macStr = macObj.getMac() if not self.macs.filter(mac=macStr).count() > 0: raise Exception("Cannot release Mac %s. Reason may be is unallocated or is an excluded Mac",macStr) self.macs.remove(macObj) #Determine new available Mac if not self.nextAvailableMac == None: if EthernetUtils.compareMacs(macStr,self.nextAvailableMac) > 0: #Do nothing pass else: self.nextAvailableMac = macStr else: #No more gaps self.nextAvailableMac = macStr self.autoSave()
def removeExcludedIp(self,ipObj): ''' Deletes an IP from the exclusion list (but it does not destroy the object!!) ''' with MutexStore.getObjectLock(self.getLockIdentifier()): ipStr = ipObj.getIp() if not self.ips.get(ip=ipStr).isExcludedIp(): raise Exception("Cannot release Ip. Reason may be is unallocated or is not excluded Ip") self.ips.remove(ipObj) #Determine new available Ip if not self.nextAvailableIp == None: if IP4Utils.compareIps(ipStr,self.nextAvailableIp) > 0: #Do nothing pass else: self.nextAvailableIp = ipStr else: #No more gaps self.nextAvailableIp = ipStr self.autoSave()
def updateDataBridge(self,interface): with MutexStore.getObjectLock(self.getLockIdentifier()): if self.networkInterfaces.filter(id = interface.id).count()!= 1: raise Exception("Can not update bridge interface because it does not exist or id is duplicated") NetworkInterface.updateServerDataBridge(interface.id,interface.getName(),interface.getMacStr(),interface.getSwitchID(),interface.getPort())
def setAgentURL(self, url): with MutexStore.getObjectLock(self.getLockIdentifier()): VTServer.validateAgentURL(url) self.agentURL = url self.autoSave()
def destroy(self): with MutexStore.getObjectLock(self.getLockIdentifier()): #destroy interfaces for inter in self.networkInterfaces.all(): inter.destroy() self.delete()