<s> from django . db import models <EOL> from django . contrib import auth <EOL> from threading import Lock <EOL> import re <EOL> import uuid <EOL> import inspect <EOL> from django . core . exceptions import ValidationError <EOL> from vt_manager . models . NetworkInterface import NetworkInterface <EOL> from vt_manager . communication . XmlRpcClient import XmlRpcClient <EOL> from vt_manager . models . utils . Choices import VirtTechClass , OSDistClass , OSVersionClass , OSTypeClass <EOL> from vt_manager . utils . MutexStore import MutexStore <EOL> from vt_manager . models . MacRange import MacRange <EOL> from vt_manager . models . Ip4Range import Ip4Range <EOL> from vt_manager . common . utils import validators <EOL> def validateAgentURLwrapper ( url ) : <EOL> VTServer . validateAgentURL ( url ) <EOL> class VTServer ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> app_label = '<STR_LIT>' <EOL> __childClasses = ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> '''<STR_LIT>''' <EOL> available = models . BooleanField ( default = <NUM_LIT:1> , verbose_name = "<STR_LIT>" , editable = False ) <EOL> enabled = models . BooleanField ( default = <NUM_LIT:1> , verbose_name = "<STR_LIT>" , editable = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT> , default = "<STR_LIT>" , verbose_name = "<STR_LIT:Name>" , validators = [ validators . resourceNameValidator ] ) <EOL> uuid = models . CharField ( max_length = <NUM_LIT> , default = uuid . uuid4 ( ) , editable = False ) <EOL> '''<STR_LIT>''' <EOL> operatingSystemType = models . CharField ( choices = OSTypeClass . OS_TYPE_CHOICES , max_length = <NUM_LIT> , verbose_name = "<STR_LIT>" , validators = [ OSTypeClass . validateOSType ] ) <EOL> operatingSystemDistribution = models . CharField ( choices = OSDistClass . OS_DIST_CHOICES , max_length = <NUM_LIT> , verbose_name = "<STR_LIT>" , validators = [ OSDistClass . validateOSDist ] ) <EOL> operatingSystemVersion = models . CharField ( choices = OSVersionClass . OS_VERSION_CHOICES , max_length = <NUM_LIT> , verbose_name = "<STR_LIT>" , validators = [ OSVersionClass . validateOSVersion ] ) <EOL> '''<STR_LIT>''' <EOL> virtTech = models . CharField ( choices = VirtTechClass . VIRT_TECH_CHOICES , max_length = <NUM_LIT> , verbose_name = "<STR_LIT>" , validators = [ VirtTechClass . validateVirtTech ] ) <EOL> '''<STR_LIT>''' <EOL> numberOfCPUs = models . IntegerField ( blank = True , null = True , verbose_name = "<STR_LIT>" ) <EOL> CPUFrequency = models . IntegerField ( blank = True , null = True , verbose_name = "<STR_LIT>" ) <EOL> memory = models . IntegerField ( blank = True , null = True , verbose_name = "<STR_LIT>" ) <EOL> discSpaceGB = models . FloatField ( blank = True , null = True , verbose_name = "<STR_LIT>" ) <EOL> '''<STR_LIT>''' <EOL> agentURL = models . URLField ( verify_exists = False , verbose_name = "<STR_LIT>" , validators = [ validateAgentURLwrapper ] , help_text = "<STR_LIT>" ) <EOL> agentPassword = models . CharField ( blank = True , null = True , max_length = <NUM_LIT> , verbose_name = "<STR_LIT>" , validators = [ validators . resourceNameValidator ] ) <EOL> url = models . URLField ( verify_exists = False , verbose_name = "<STR_LIT>" , editable = False , blank = True ) <EOL> '''<STR_LIT>''' <EOL> networkInterfaces = models . ManyToManyField ( '<STR_LIT>' , blank = True , null = False , editable = False ) <EOL> '''<STR_LIT>''' <EOL> subscribedMacRanges = models . ManyToManyField ( '<STR_LIT>' , blank = True , null = True , editable = False ) <EOL> subscribedIp4Ranges = models . ManyToManyField ( '<STR_LIT>' , blank = True , null = True , editable = False ) <EOL> '''<STR_LIT>''' <EOL> mutex = None <EOL> '''<STR_LIT>''' <EOL> doSave = True <EOL> def getChildObject ( self ) : <EOL> for childClass in self . __childClasses : <EOL> try : <EOL> return self . __getattribute__ ( childClass . lower ( ) ) <EOL> except Exception : <EOL> pass <EOL> def __tupleContainsKey ( tu , key ) : <EOL> for val in tu : <EOL> if val [ <NUM_LIT:0> ] == key : <EOL> return True <EOL> return False <EOL> def autoSave ( self ) : <EOL> if self . doSave : <EOL> self . save ( ) <EOL> def getLockIdentifier ( self ) : <EOL> return inspect . currentframe ( ) . f_code . co_filename + str ( self ) + str ( self . id ) <EOL> '''<STR_LIT>''' <EOL> def setName ( self , name ) : <EOL> self . name = name <EOL> self . autoSave ( ) <EOL> def getName ( self ) : <EOL> return self . name <EOL> def setUUID ( self , uuid ) : <EOL> self . uuid = uuid <EOL> self . autoSave ( ) <EOL> def getUUID ( self ) : <EOL> return self . uuid <EOL> def setNumberOfCPUs ( self , cpus ) : <EOL> self . numberOfCPUs = cpus <EOL> self . autoSave ( ) <EOL> def getNumberOfCPUs ( self ) : <EOL> return self . numberOfCPUs <EOL> def setMemory ( self , memory ) : <EOL> self . memory = memory <EOL> self . autoSave ( ) <EOL> def getCPUFrequency ( self ) : <EOL> return self . CPUFrequency <EOL> def setCPUFrequency ( self , frequency ) : <EOL> self . CPUFrequency = frequency <EOL> self . autoSave ( ) <EOL> def getMemory ( self ) : <EOL> return self . memory <EOL> def setDiscSpaceGB ( self , space ) : <EOL> self . discSpaceGB = space <EOL> self . autoSave ( ) <EOL> def getDiscSpaceGB ( self ) : <EOL> return self . discSpaceGB <EOL> @ staticmethod <EOL> def validateAgentURL ( url ) : <EOL> if url == "<STR_LIT>" : <EOL> return <EOL> try : <EOL> XmlRpcClient . callRPCMethod ( url , "<STR_LIT>" , "<STR_LIT>" ) <EOL> except Exception as e : <EOL> raise ValidationError ( "<STR_LIT>" + str ( e ) ) <EOL> def setAgentURL ( self , url ) : <EOL> with MutexStore . getObjectLock ( self . getLockIdentifier ( ) ) : <EOL> VTServer . validateAgentURL ( url ) <EOL> self . agentURL = url <EOL> self . autoSave ( ) <EOL> def getAgentURL ( self ) : <EOL> return self . agentURL <EOL> def setURL ( self , url ) : <EOL> self . url = url <EOL> self . autoSave ( ) <EOL> def getURL ( self ) : <EOL> return self . url <EOL> def setVirtTech ( self , virtTech ) : <EOL> VirtTechClass . validateVirtTech ( virtTech ) <EOL> self . virtTech = virtTech <EOL> self . autoSave ( ) <EOL> def getVirtTech ( self ) : <EOL> return self . virtTech <EOL> def setOSType ( self , osType ) : <EOL> OSTypeClass . validateOSType ( osType ) <EOL> self . operatingSystemType = osType <EOL> self . autoSave ( ) <EOL> def getOSType ( self ) : <EOL> return self . operatingSystemType <EOL> def setOSVersion ( self , version ) : <EOL> OSVersionClass . validateOSVersion ( version ) <EOL> self . operatingSystemVersion = version <EOL> self . autoSave ( ) <EOL> def getOSVersion ( self ) : <EOL> return self . operatingSystemVersion <EOL> def setOSDistribution ( self , dist ) : <EOL> OSDistClass . validateOSDist ( dist ) <EOL> self . operatingSystemDistribution = dist <EOL> self . autoSave ( ) <EOL> def getOSDistribution ( self ) : <EOL> return self . operatingSystemDistribution <EOL> def setAvailable ( self , av ) : <EOL> self . available = av <EOL> self . autoSave ( ) <EOL> def getAvailable ( self ) : <EOL> return self . available <EOL> def setEnabled ( self , en ) : <EOL> self . enabled = en <EOL> self . autoSave ( ) <EOL> def getEnabled ( self ) : <EOL> return self . enabled <EOL> def getAgentPassword ( self ) : <EOL> return self . agentPassword <EOL> def setAgentPassword ( self , password ) : <EOL> self . agentPassword = password <EOL> self . autoSave ( ) <EOL> def destroy ( self ) : <EOL> with MutexStore . getObjectLock ( self . getLockIdentifier ( ) ) : <EOL> if self . vms . all ( ) . count ( ) > <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> for interface in self . networkInterfaces . all ( ) : <EOL> interface . destroy ( ) <EOL> self . delete ( ) <EOL> '''<STR_LIT>''' <EOL> def subscribeToMacRange ( self , newRange ) : <EOL> if not isinstance ( newRange , MacRange ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if newRange in self . subscribedMacRanges . all ( ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . subscribedMacRanges . add ( newRange ) <EOL> self . autoSave ( ) <EOL> def unsubscribeToMacRange ( self , oRange ) : <EOL> if not isinstance ( oRange , MacRange ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . subscribedMacRanges . remove ( oRange ) <EOL> self . autoSave ( ) <EOL> def getSubscribedMacRangesNoGlobal ( self ) : <EOL> return self . subscribedMacRanges . all ( ) <EOL> def getSubscribedMacRanges ( self ) : <EOL> if self . subscribedMacRanges . all ( ) . count ( ) > <NUM_LIT:0> : <EOL> return self . subscribedMacRanges . all ( ) <EOL> else : <EOL> return MacRange . objects . filter ( isGlobal = True ) <EOL> def subscribeToIp4Range ( self , newRange ) : <EOL> if not isinstance ( newRange , Ip4Range ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if newRange in self . subscribedIp4Ranges . all ( ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . subscribedIp4Ranges . add ( newRange ) <EOL> self . autoSave ( ) <EOL> def unsubscribeToIp4Range ( self , oRange ) : <EOL> if not isinstance ( oRange , Ip4Range ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . subscribedIp4Ranges . remove ( oRange ) <EOL> self . autoSave ( ) <EOL> def getSubscribedIp4RangesNoGlobal ( self ) : <EOL> return self . subscribedIp4Ranges . all ( ) <EOL> def getSubscribedIp4Ranges ( self ) : <EOL> if self . subscribedIp4Ranges . all ( ) . count ( ) > <NUM_LIT:0> : <EOL> return self . subscribedIp4Ranges . all ( ) <EOL> else : <EOL> return Ip4Range . objects . filter ( isGlobal = True ) <EOL> def __allocateMacFromSubscribedRanges ( self ) : <EOL> macObj = None <EOL> for macRange in self . getSubscribedMacRanges ( ) : <EOL> try : <EOL> macObj = macRange . allocateMac ( ) <EOL> except Exception as e : <EOL> continue <EOL> break <EOL> if macObj == None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return macObj <EOL> def __allocateIpFromSubscribedRanges ( self ) : <EOL> ipObj = None <EOL> for ipRange in self . getSubscribedIp4Ranges ( ) : <EOL> try : <EOL> ipObj = ipRange . allocateIp ( ) <EOL> except Exception as e : <EOL> continue <EOL> break <EOL> if ipObj == None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return ipObj <EOL> '''<STR_LIT>''' <EOL> def __createEnslavedDataVMNetworkInterface ( self , serverInterface ) : <EOL> macObj = self . __allocateMacFromSubscribedRanges ( ) <EOL> interface = NetworkInterface . createVMDataInterface ( serverInterface . getName ( ) + "<STR_LIT>" , macObj ) <EOL> serverInterface . attachInterfaceToBridge ( interface ) <EOL> return interface <EOL> def __createEnslavedMgmtVMNetworkInterface ( self , serverInterface ) : <EOL> macObj = self . __allocateMacFromSubscribedRanges ( ) <EOL> ipObj = self . __allocateIpFromSubscribedRanges ( ) <EOL> interface = NetworkInterface . createVMMgmtInterface ( serverInterface . getName ( ) + "<STR_LIT>" , macObj , ipObj ) <EOL> serverInterface . attachInterfaceToBridge ( interface ) <EOL> return interface <EOL> def createEnslavedVMInterfaces ( self ) : <EOL> vmInterfaces = set ( ) <EOL> try : <EOL> for serverInterface in self . networkInterfaces . all ( ) : <EOL> if serverInterface . isMgmt : <EOL> vmInterfaces . add ( self . __createEnslavedMgmtVMNetworkInterface ( serverInterface ) ) <EOL> else : <EOL> vmInterfaces . add ( self . __createEnslavedDataVMNetworkInterface ( serverInterface ) ) <EOL> except Exception as e : <EOL> for interface in vmInterfaces : <EOL> interface . destroy ( ) <EOL> raise e <EOL> return vmInterfaces <EOL> '''<STR_LIT>''' <EOL> def setMgmtBridge ( self , name , macStr ) : <EOL> with MutexStore . getObjectLock ( self . getLockIdentifier ( ) ) : <EOL> nInter = self . networkInterfaces . filter ( isMgmt = True , isBridge = True ) <EOL> if nInter . count ( ) == <NUM_LIT:1> : <EOL> mgmt = nInter . get ( ) <EOL> mgmt . setName ( name ) <EOL> mgmt . setMacStr ( macStr ) <EOL> elif nInter . count ( ) == <NUM_LIT:0> : <EOL> mgmt = NetworkInterface . createServerMgmtBridge ( name , macStr ) <EOL> self . networkInterfaces . add ( mgmt ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . autoSave ( ) <EOL> def addDataBridge ( self , name , macStr , switchId , port ) : <EOL> with MutexStore . getObjectLock ( self . getLockIdentifier ( ) ) : <EOL> if self . networkInterfaces . filter ( name = name , isBridge = True ) . count ( ) > <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> netInt = NetworkInterface . createServerDataBridge ( name , macStr , switchId , port ) <EOL> self . networkInterfaces . add ( netInt ) <EOL> self . autoSave ( ) <EOL> def updateDataBridge ( self , interface ) : <EOL> with MutexStore . getObjectLock ( self . getLockIdentifier ( ) ) : <EOL> if self . networkInterfaces . filter ( id = interface . id ) . count ( ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> NetworkInterface . updateServerDataBridge ( interface . id , interface . getName ( ) , interface . getMacStr ( ) , interface . getSwitchID ( ) , interface . getPort ( ) ) <EOL> def deleteDataBridge ( self , netInt ) : <EOL> with MutexStore . getObjectLock ( self . getLockIdentifier ( ) ) : <EOL> if not isinstance ( netInt , NetworkInterface ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if netInt . getNumberOfConnections ( ) > <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . networkInterfaces . remove ( netInt ) <EOL> netInt . destroy ( ) <EOL> self . autoSave ( ) <EOL> def getNetworkInterfaces ( self ) : <EOL> return self . networkInterfaces . all ( ) . order_by ( '<STR_LIT>' , '<STR_LIT:id>' ) </s>
