Ignore:
Timestamp:
Oct 8, 2007, 4:44:14 AM (17 years ago)
Author:
ecprice
Message:

More updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/templates/main.py

    r140 r144  
    4242                     "%02x" * 6]) % tuple(u)
    4343
    44 def maxMemory(user, machine=None):
    45     return 256
     44MAX_MEMORY_TOTAL = 512
     45MAX_MEMORY_SINGLE = 256
     46MIN_MEMORY_SINGLE = 16
     47MAX_DISK_TOTAL = 50
     48MAX_DISK_SINGLE = 50
     49MIN_DISK_SINGLE = 0.1
     50MAX_VMS_TOTAL = 10
     51MAX_VMS_ACTIVE = 4
     52
     53def getMachinesOwner(owner):
     54    return Machine.select_by(owner=owner)
     55
     56def 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)
    4663
    4764def 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
     70def 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)
    4977
    5078def haveAccess(user, machine):
     
    95123                         stderr=subprocess.PIPE)
    96124    if kws.get('err'):
     125        p.wait()
    97126        return p.stdout.read(), p.stderr.read()
    98127    if p.wait():
    99         print >> sys.stderr, 'ERROR on remctl ', args
    100         print >> sys.stderr, p.stderr.read()
     128        raise MyException('ERROR on remctl %s: %s' %
     129                          (args, p.stderr.read()))
    101130    return p.stdout.read()
    102131
     
    159188        uptime = ' '.join(lst[2:])
    160189        d[name] = uptime
    161     return d
     190    ans = {}
     191    for m in machines:
     192        ans[m] = d.get(m.name)
     193    return ans
    162194
    163195def statusInfo(machine):
     
    191223    transaction = ctx.current.create_transaction()
    192224    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")
    193231        res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')')
    194232        id = res.fetchone()[0]
     
    218256        transaction.rollback()
    219257        raise
     258    registerMachine(machine)
    220259    makeDisks()
    221     registerMachine(machine)
    222260    # tell it to boot with cdrom
    223261    bootMachine(machine, cdrom)
     
    228266    try:
    229267        memory = int(memory)
    230         if memory <= 0:
     268        if memory < MIN_MEMORY_SINGLE:
    231269            raise ValueError
    232270    except ValueError:
    233         raise MyException("Invalid memory amount")
     271        raise MyException("Invalid memory amount; must be at least %s MB" %
     272                          MIN_MEMORY_SINGLE)
    234273    if memory > maxMemory(user, machine):
    235274        raise MyException("Too much memory requested")
     
    242281            raise MyException("Too much disk requested")
    243282        disk = int(disk * 1024)
    244         if disk <= 0:
     283        if disk < MIN_DISK_SINGLE * 1024:
    245284            raise ValueError
    246285    except ValueError:
    247         raise MyException("Invalid disk amount")
     286        raise MyException("Invalid disk amount; minimum is %s GB" %
     287                          MIN_DISK_SINGLE)
    248288    return disk
    249289
     
    287327    on = uptimes
    288328    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'
    291331        elif m.type.hvm:
    292             has_vnc[m.name] = True
     332            has_vnc[m] = True
    293333        else:
    294             has_vnc[m.name] = "ParaVM"+helppopup("paravm_console")
     334            has_vnc[m] = "ParaVM"+helppopup("paravm_console")
    295335    #     for m in machines:
    296336    #         status = statusInfo(m)
    297337    #         on[m.name] = status is not None
    298338    #         has_vnc[m.name] = hasVnc(status)
     339    max_mem=maxMemory(user, on=on)
     340    max_disk=maxDisk(user)
    299341    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),
    302347             machines=machines,
    303348             has_vnc=has_vnc,
     
    418463            remctl('reboot', machine.name)
    419464    elif action == 'Power on':
     465        if maxMemory(user) < machine.memory:
     466            raise MyException("You don't have enough free RAM quota")
    420467        bootMachine(machine, cdrom)
    421468    elif action == 'Power off':
     
    515562            pass
    516563            #fields.append((disp, None))
    517 
     564    max_mem = maxMemory(user, machine)
     565    max_disk = maxDisk(user, machine)
    518566    d = dict(user=user,
    519567             cdroms=CDROM.select(),
     
    523571             uptime=str(uptime),
    524572             ram=machine.memory,
    525              maxmem=maxMemory(user, machine),
    526              maxdisk=maxDisk(user, machine),
     573             max_mem=max_mem,
     574             max_disk=max_disk,
    527575             fields = fields)
    528576    print Template(file='info.tmpl',
     
    549597        u.email = os.environ[ 'SSL_CLIENT_S_DN_Email']
    550598    else:
    551         u.username = 'nobody'
    552         u.email = None
     599        u.username = 'moo'
     600        u.email = 'nobody'
    553601    connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
    554602    operation = os.environ.get('PATH_INFO', '')
Note: See TracChangeset for help on using the changeset viewer.