Changeset 572 for trunk/packages/sipb-xen-www/code/validation.py
- Timestamp:
- Jun 2, 2008, 11:25:47 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/sipb-xen-www/code/validation.py
r537 r572 6 6 import string 7 7 from sipb_xen_database import Machine, NIC, Type, Disk 8 from webcommon import InvalidInput , g8 from webcommon import InvalidInput 9 9 10 10 MAX_MEMORY_TOTAL = 512 … … 17 17 MAX_VMS_ACTIVE = 4 18 18 19 def getMachinesByOwner(user, machine=None): 19 class Validate: 20 def __init__(self, username, state, machine_id=None, name=None, owner=None, 21 admin=None, contact=None, memory=None, disksize=None, 22 vmtype=None, cdrom=None, clone_from=None): 23 # XXX Successive quota checks aren't a good idea, since you 24 # can't necessarily change the locker and disk size at the 25 # same time. 26 created_new = (machine_id is None) 27 28 if machine_id is not None: 29 self.machine = testMachineId(username, machine_id) 30 machine = getattr(self, 'machine', None) 31 32 owner = testOwner(username, owner, machine) 33 if owner is not None: 34 self.owner = owner 35 admin = testAdmin(username, admin, machine) 36 if admin is not None: 37 self.admin = admin 38 contact = testContact(username, contact, machine) 39 if contact is not None: 40 self.contact = contact 41 name = testName(username, name, machine) 42 if name is not None: 43 self.name = name 44 if memory is not None: 45 self.memory = validMemory(self.owner, state, memory, machine, 46 on=not created_new) 47 if disksize is not None: 48 self.disksize = validDisk(self.owner, disksize, machine) 49 if vmtype is not None: 50 self.vmtype = validVmType(vmtype) 51 if cdrom is not None: 52 if not CDROM.get(cdrom): 53 raise CodeError("Invalid cdrom type '%s'" % cdrom) 54 self.cdrom = cdrom 55 if clone_from is not None: 56 if clone_from not in ('ice3', ): 57 raise CodeError("Invalid clone image '%s'" % clone_from) 58 self.clone_from = clone_from 59 60 61 def getMachinesByOwner(owner, machine=None): 20 62 """Return the machines owned by the same as a machine. 21 63 … … 25 67 if machine: 26 68 owner = machine.owner 27 else:28 owner = user29 69 return Machine.select_by(owner=owner) 30 70 31 def maxMemory( user, machine=None, on=True):71 def maxMemory(owner, g, machine=None, on=True): 32 72 """Return the maximum memory for a machine or a user. 33 73 … … 44 84 if not on: 45 85 return MAX_MEMORY_SINGLE 46 machines = getMachinesByOwner( user, machine)86 machines = getMachinesByOwner(owner, machine) 47 87 active_machines = [x for x in machines if g.xmlist.get(x)] 48 88 mem_usage = sum([x.memory for x in active_machines if x != machine]) 49 89 return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage) 50 90 51 def maxDisk( user, machine=None):91 def maxDisk(owner, machine=None): 52 92 """Return the maximum disk that a machine can reach. 53 93 … … 60 100 machine_id = None 61 101 disk_usage = Disk.query().filter_by(Disk.c.machine_id != machine_id, 62 owner= user).sum(Disk.c.size) or 0102 owner=owner).sum(Disk.c.size) or 0 63 103 return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.) 64 104 65 def cantAddVm( user):66 machines = getMachinesByOwner( user)105 def cantAddVm(owner, g): 106 machines = getMachinesByOwner(owner) 67 107 active_machines = [x for x in machines if g.xmlist.get(x)] 68 108 if len(machines) >= MAX_VMS_TOTAL: … … 72 112 'To create more, turn one off.') 73 113 return False 74 75 def validAddVm(user):76 reason = cantAddVm(user)77 if reason:78 raise InvalidInput('create', True, reason)79 return True80 114 81 115 def haveAccess(user, machine): … … 99 133 return True 100 134 101 def validMemory( user, memory, machine=None, on=True):102 """Parse and validate limits for memory for a given user and machine.135 def validMemory(owner, g, memory, machine=None, on=True): 136 """Parse and validate limits for memory for a given owner and machine. 103 137 104 138 on is whether the memory must be valid after the machine is … … 112 146 raise InvalidInput('memory', memory, 113 147 "Minimum %s MiB" % MIN_MEMORY_SINGLE) 114 if memory > maxMemory(user, machine, on): 148 max_val = maxMemory(owner, g, machine, on) 149 if memory > max_val: 115 150 raise InvalidInput('memory', memory, 116 'Maximum %s MiB for %s' % (maxMemory(user, machine), 117 user)) 151 'Maximum %s MiB for %s' % (max_val, owner)) 118 152 return memory 119 153 120 def validDisk( user, disk, machine=None):121 """Parse and validate limits for disk for a given user and machine."""154 def validDisk(owner, disk, machine=None): 155 """Parse and validate limits for disk for a given owner and machine.""" 122 156 try: 123 157 disk = float(disk) 124 if disk > maxDisk( user, machine):158 if disk > maxDisk(owner, machine): 125 159 raise InvalidInput('disk', disk, 126 "Maximum %s G" % maxDisk( user, machine))160 "Maximum %s G" % maxDisk(owner, machine)) 127 161 disk = int(disk * 1024) 128 162 if disk < MIN_DISK_SINGLE * 1024: … … 191 225 If machine is None, this is the owner of a new machine. 192 226 """ 193 if owner == user or machine is not None and owner == machine.owner:227 if owner == user: 194 228 return owner 229 if machine is not None and owner in (machine.owner, None): 230 return None 195 231 if owner is None: 196 232 raise InvalidInput('owner', owner, "Owner must be specified") … … 214 250 215 251 def testName(user, name, machine=None): 216 if name in (None, machine.name): 252 if name is None: 253 return None 254 if machine is not None and name == machine.name: 217 255 return None 218 256 if not Machine.select_by(name=name): 257 if not validMachineName(name): 258 raise InvalidInput('name', name, 'You must provide a machine name. Max 22 chars, alnum plus \'-\' and \'_\'.') 219 259 return name 220 260 raise InvalidInput('name', name, "Name is already taken.")
Note: See TracChangeset
for help on using the changeset viewer.