Ignore:
Timestamp:
Oct 11, 2007, 1:55:29 AM (17 years ago)
Author:
ecprice
Message:

Help, admin, links at the top.

File:
1 edited

Legend:

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

    r182 r187  
    1616import getafsgroups
    1717
    18 sys.stderr = StringIO.StringIO()
     18errio = StringIO.StringIO()
     19sys.stderr = errio
    1920sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages')
    2021
     
    8485MAX_VMS_ACTIVE = 4
    8586
    86 def getMachinesByOwner(owner):
    87     """Return the machines owned by a given owner."""
     87def getMachinesByOwner(user, machine=None):
     88    """Return the machines owned by the same as a machine.
     89   
     90    If the machine is None, return the machines owned by the same
     91    user.
     92    """
     93    if machine:
     94        owner = machine.owner
     95    else:
     96        owner = user.username
    8897    return Machine.select_by(owner=owner)
    8998
     
    100109    if not on:
    101110        return MAX_MEMORY_SINGLE
    102     machines = getMachinesByOwner(user.username)
     111    machines = getMachinesByOwner(user, machine)
    103112    active_machines = [x for x in machines if g.uptimes[x]]
    104113    mem_usage = sum([x.memory for x in active_machines if x != machine])
     
    106115
    107116def maxDisk(user, machine=None):
    108     machines = getMachinesByOwner(user.username)
     117    machines = getMachinesByOwner(user, machine)
    109118    disk_usage = sum([sum([y.size for y in x.disks])
    110119                      for x in machines if x != machine])
     
    112121
    113122def canAddVm(user):
    114     machines = getMachinesByOwner(user.username)
     123    machines = getMachinesByOwner(user)
    115124    active_machines = [x for x in machines if g.uptimes[x]]
    116125    return (len(machines) < MAX_VMS_TOTAL and
     
    118127
    119128def haveAccess(user, machine):
    120     """Return whether a user has access to a machine"""
     129    """Return whether a user has adminstrative access to a machine"""
     130    if user.username == 'moo':
     131        return True
     132    if user.username in (machine.administrator, machine.owner):
     133        return True
     134    if checkAfsGroup(user, machine.administrator, 'athena.mit.edu'): #XXX Cell?
     135        return True
     136    return owns(user, machine)
     137
     138def owns(user, machine):
     139    """Return whether a user owns a machine"""
    121140    if user.username == 'moo':
    122141        return True
     
    573592    return Template(file="command.tmpl", searchList=[d, global_dict])
    574593
    575 def testOwner(user, owner, machine=None):
    576     if owner == machine.owner:   #XXX What do we do when you lose access to the locker?
    577         return owner
     594def testAdmin(user, admin, machine):
     595    if admin in (None, machine.administrator):
     596        return None
     597    if admin == user.username:
     598        return admin
     599    if getafsgroups.checkAfsGroup(user, admin, 'athena.mit.edu'):
     600        return admin
     601    if getafsgroups.checkAfsGroup(user, 'system:'+admin, 'athena.mit.edu'):
     602        return 'system:'+admin
     603    raise InvalidInput('admin', admin,
     604                       'You must control the group you move it to')
     605   
     606def testOwner(user, owner, machine):
     607    if owner in (None, machine.owner):
     608        return None
     609    #XXX should you be able to transfer ownership if you don't already own it?
     610    #if not owns(user, machine):
     611    #    raise InvalidInput('owner', owner, "You don't own this machine, so you can't  transfer ownership")
    578612    value = getafsgroups.checkLockerOwner(user.username, owner, verbose=True)
    579613    if value == True:
     
    582616
    583617def testContact(user, contact, machine=None):
     618    if contact in (None, machine.contact):
     619        return None
    584620    if not re.match("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", contact, re.I):
    585621        raise InvalidInput('contact', contact, "Not a valid email")
     
    590626
    591627def testName(user, name, machine=None):
    592     if name is None:
     628    if name in (None, machine.name):
    593629        return None
    594630    if not Machine.select_by(name=name):
    595         return name
    596     if name == machine.name:
    597631        return name
    598632    raise InvalidInput('name', name, "Already taken")
     
    618652        machine = testMachineId(user, fields.getfirst('machine_id'))
    619653        owner = testOwner(user, fields.getfirst('owner'), machine)
    620         contact = testContact(user, fields.getfirst('contact'))
    621         hostname = testHostname(owner, fields.getfirst('hostname'),
    622                                 machine)
     654        admin = testAdmin(user, fields.getfirst('administrator'), machine)
     655        contact = testContact(user, fields.getfirst('contact'), machine)
     656        hostname = testHostname(owner, fields.getfirst('hostname'), machine)
    623657        name = testName(user, fields.getfirst('name'), machine)
    624658        oldname = machine.name
     
    644678            ctx.current.save(nic)
    645679
    646         if owner is not None and owner != machine.owner:
     680        if owner is not None:
    647681            machine.owner = owner
    648         if name is not None and name != machine.name:
     682        if name is not None:
    649683            machine.name = name
     684        if admin is not None:
     685            machine.administrator = admin
     686        if contact is not None:
     687            machine.contact = contact
    650688           
    651689        ctx.current.save(machine)
     
    656694    for diskname in olddisk:
    657695        remctl("web", "lvresize", oldname, diskname, str(olddisk[diskname]))
    658     if name is not None and name != oldname:
     696    if name is not None:
    659697        for disk in machine.disks:
    660             if oldname != name:
    661                 remctl("web", "lvrename", oldname, disk.guest_device_name, name)
     698            remctl("web", "lvrename", oldname, disk.guest_device_name, name)
    662699        remctl("web", "moveregister", oldname, name)
    663700    d = dict(user=user,
     
    681718want an HVM virtualized machine.""",
    682719                   cpu_weight="""Don't ask us!  We're as mystified as you are.""",
    683                    owner="""The Owner must be the name of a locker that you are an AFS
    684 administrator of.  In particular, you or an AFS group you are a member
    685 of must have AFS rlidwka bits on the locker.  You can check see who
    686 administers the LOCKER locker using the command 'fs la /mit/LOCKER' on
    687 Athena.)""")
    688    
     720                   owner="""The owner field is used to determine <a href="help?subject=quotas">quotas</a>.  It must be the name
     721of a locker that you are an AFS administrator of.  In particular, you
     722or an AFS group you are a member of must have AFS rlidwka bits on the
     723locker.  You can check see who administers the LOCKER locker using the
     724command 'fs la /mit/LOCKER' on Athena.)  See also <a href="help?subject=administrator">administrator</a>.""",
     725                   administrator="""The administrator field determines who can access the console and power on and off the machine.  This can be either a user or a moira group.""",
     726                   quotas="""Quotas are determined on a per-locker basis.  Each
     727quota may have a maximum of 512 megabytes of active ram, 50 gigabytes of disk, and 4 active machines."""
     728
     729                   )
     730   
     731    if not subjects:
     732        subjects = sorted(mapping.keys())
     733       
    689734    d = dict(user=user,
    690735             simple=simple,
     
    715760    display_fields = [('name', 'Name'),
    716761                      ('owner', 'Owner'),
     762                      ('administrator', 'Administrator'),
    717763                      ('contact', 'Contact'),
    718764                      ('type', 'Type'),
     
    736782    machine_info['type'] = machine.type.hvm and 'HVM' or 'ParaVM'
    737783    machine_info['owner'] = machine.owner
     784    machine_info['administrator'] = machine.administrator
    738785    machine_info['contact'] = machine.contact
    739786
     
    817864        output = fun(u, fields)
    818865        print 'Content-Type: text/html\n'
    819         sys.stderr.seek(0)
    820         e = sys.stderr.read()
    821866        sys.stderr=sys.stdout
     867        errio.seek(0)
     868        e = errio.read()
    822869        if e:
    823870            output = str(output)
     
    826873    except CodeError, err:
    827874        print 'Content-Type: text/html\n'
    828         sys.stderr.seek(0)
    829         e = sys.stderr.read()
    830875        sys.stderr=sys.stdout
     876        errio.seek(0)
     877        e = errio.read()
    831878        print error(operation, u, fields, err, e)
    832879    except InvalidInput, err:
    833880        print 'Content-Type: text/html\n'
    834         sys.stderr.seek(0)
    835         e = sys.stderr.read()
    836881        sys.stderr=sys.stdout
     882        errio.seek(0)
     883        e = errio.read()
    837884        print invalidInput(operation, u, fields, err, e)
    838885    except:
    839886        print 'Content-Type: text/plain\n'
    840         sys.stderr.seek(0)
    841         e = sys.stderr.read()
     887        sys.stderr=sys.stdout
     888        errio.seek(0)
     889        e = errio.read()
    842890        print e
    843891        print '----'
    844         sys.stderr = sys.stdout
    845892        raise
Note: See TracChangeset for help on using the changeset viewer.