Changeset 144 for trunk/web/templates/main.py
- Timestamp:
- Oct 8, 2007, 4:44:14 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/web/templates/main.py
r140 r144 42 42 "%02x" * 6]) % tuple(u) 43 43 44 def maxMemory(user, machine=None): 45 return 256 44 MAX_MEMORY_TOTAL = 512 45 MAX_MEMORY_SINGLE = 256 46 MIN_MEMORY_SINGLE = 16 47 MAX_DISK_TOTAL = 50 48 MAX_DISK_SINGLE = 50 49 MIN_DISK_SINGLE = 0.1 50 MAX_VMS_TOTAL = 10 51 MAX_VMS_ACTIVE = 4 52 53 def getMachinesOwner(owner): 54 return Machine.select_by(owner=owner) 55 56 def maxMemory(user, machine=None, on=None): 57 machines = getMachinesOwner(user.username) 58 if on is None: 59 on = getUptimes(machines) 60 active_machines = [x for x in machines if on[x]] 61 mem_usage = sum([x.memory for x in active_machines if x != machine]) 62 return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage) 46 63 47 64 def maxDisk(user, machine=None): 48 return 10.0 65 machines = getMachinesOwner(user.username) 66 disk_usage = sum([sum([y.size for y in x.disks]) 67 for x in machines if x != machine]) 68 return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.) 69 70 def canAddVm(user, on=None): 71 machines = getMachinesOwner(user.username) 72 if on is None: 73 on = getUptimes(machines) 74 active_machines = [x for x in machines if on[x]] 75 return (len(machines) < MAX_VMS_TOTAL and 76 len(active_machines) < MAX_VMS_ACTIVE) 49 77 50 78 def haveAccess(user, machine): … … 95 123 stderr=subprocess.PIPE) 96 124 if kws.get('err'): 125 p.wait() 97 126 return p.stdout.read(), p.stderr.read() 98 127 if p.wait(): 99 print >> sys.stderr, 'ERROR on remctl ', args100 print >> sys.stderr, p.stderr.read()128 raise MyException('ERROR on remctl %s: %s' % 129 (args, p.stderr.read())) 101 130 return p.stdout.read() 102 131 … … 159 188 uptime = ' '.join(lst[2:]) 160 189 d[name] = uptime 161 return d 190 ans = {} 191 for m in machines: 192 ans[m] = d.get(m.name) 193 return ans 162 194 163 195 def statusInfo(machine): … … 191 223 transaction = ctx.current.create_transaction() 192 224 try: 225 if memory > maxMemory(user): 226 raise MyException("Too much memory requested") 227 if disk > maxDisk(user) * 1024: 228 raise MyException("Too much disk requested") 229 if not canAddVm(user): 230 raise MyException("Too many VMs requested") 193 231 res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')') 194 232 id = res.fetchone()[0] … … 218 256 transaction.rollback() 219 257 raise 258 registerMachine(machine) 220 259 makeDisks() 221 registerMachine(machine)222 260 # tell it to boot with cdrom 223 261 bootMachine(machine, cdrom) … … 228 266 try: 229 267 memory = int(memory) 230 if memory < = 0:268 if memory < MIN_MEMORY_SINGLE: 231 269 raise ValueError 232 270 except ValueError: 233 raise MyException("Invalid memory amount") 271 raise MyException("Invalid memory amount; must be at least %s MB" % 272 MIN_MEMORY_SINGLE) 234 273 if memory > maxMemory(user, machine): 235 274 raise MyException("Too much memory requested") … … 242 281 raise MyException("Too much disk requested") 243 282 disk = int(disk * 1024) 244 if disk < = 0:283 if disk < MIN_DISK_SINGLE * 1024: 245 284 raise ValueError 246 285 except ValueError: 247 raise MyException("Invalid disk amount") 286 raise MyException("Invalid disk amount; minimum is %s GB" % 287 MIN_DISK_SINGLE) 248 288 return disk 249 289 … … 287 327 on = uptimes 288 328 for m in machines: 289 if not on .get(m.name):290 has_vnc[m .name] = 'Off'329 if not on[m]: 330 has_vnc[m] = 'Off' 291 331 elif m.type.hvm: 292 has_vnc[m .name] = True332 has_vnc[m] = True 293 333 else: 294 has_vnc[m .name] = "ParaVM"+helppopup("paravm_console")334 has_vnc[m] = "ParaVM"+helppopup("paravm_console") 295 335 # for m in machines: 296 336 # status = statusInfo(m) 297 337 # on[m.name] = status is not None 298 338 # has_vnc[m.name] = hasVnc(status) 339 max_mem=maxMemory(user, on=on) 340 max_disk=maxDisk(user) 299 341 d = dict(user=user, 300 maxmem=maxMemory(user), 301 maxdisk=maxDisk(user), 342 can_add_vm=canAddVm(user, on=on), 343 max_mem=max_mem, 344 max_disk=max_disk, 345 default_mem=max_mem, 346 default_disk=min(4.0, max_disk), 302 347 machines=machines, 303 348 has_vnc=has_vnc, … … 418 463 remctl('reboot', machine.name) 419 464 elif action == 'Power on': 465 if maxMemory(user) < machine.memory: 466 raise MyException("You don't have enough free RAM quota") 420 467 bootMachine(machine, cdrom) 421 468 elif action == 'Power off': … … 515 562 pass 516 563 #fields.append((disp, None)) 517 564 max_mem = maxMemory(user, machine) 565 max_disk = maxDisk(user, machine) 518 566 d = dict(user=user, 519 567 cdroms=CDROM.select(), … … 523 571 uptime=str(uptime), 524 572 ram=machine.memory, 525 max mem=maxMemory(user, machine),526 max disk=maxDisk(user, machine),573 max_mem=max_mem, 574 max_disk=max_disk, 527 575 fields = fields) 528 576 print Template(file='info.tmpl', … … 549 597 u.email = os.environ[ 'SSL_CLIENT_S_DN_Email'] 550 598 else: 551 u.username = ' nobody'552 u.email = None599 u.username = 'moo' 600 u.email = 'nobody' 553 601 connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen') 554 602 operation = os.environ.get('PATH_INFO', '')
Note: See TracChangeset
for help on using the changeset viewer.