Changeset 177 for trunk


Ignore:
Timestamp:
Oct 10, 2007, 1:56:37 AM (17 years ago)
Author:
ecprice
Message:

Several modifications.

Location:
trunk/web/templates
Files:
3 edited

Legend:

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

    r161 r177  
    2929    checkAfsGroup(user, group) returns True if and only if user is in AFS group group in cell cell
    3030    """
    31     print user, group
    32     p = subprocess.Popen(["pts", "membership", group, '-c', cell], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    33     p2 = subprocess.Popen(["grep", "-v", "^Members"], stdin=p.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    34     if p2.wait():
     31    p = subprocess.Popen(["pts", "membership", group, '-c', cell],
     32                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     33    if p.wait():
    3534        return False
    36     for member in p2.stdout.read().split():
    37         if member == user:
     35    for line in p.stdout.readlines()[1:]:
     36        if line.strip() == user:
    3837            return True
    3938    return False
    4039
    41 def checkLockerOwner(user, locker):
     40def checkLockerOwner(user, locker, verbose=False):
    4241    """
    4342    checkLockerOwner(user, locker) returns True if and only if user administers locker
    4443    """
    45     p = subprocess.Popen(["fs", "whichcell", "/mit/" + locker], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    46     if (p.wait()):
     44    p = subprocess.Popen(["fs", "whichcell", "/mit/" + locker],
     45                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     46    if p.wait():
     47        if verbose:
     48            return p.stderr.read()
    4749        return False
    4850    cell = p.stdout.read().split()[-1][1:-1]
    49     p = subprocess.Popen(["fs", "listacl", "/mit/" + locker], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    50     p2 = subprocess.Popen(["grep", "^  .* rlidwka$"], stdin=p.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    51     if (p2.wait()):
     51    p = subprocess.Popen(["fs", "listacl", "/mit/" + locker],
     52                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     53    if p.wait():
     54        if verbose:
     55            return p.stderr.read()
    5256        return False
    53     for line in p2.stdout.read().split('\n'):
     57    for line in p.stdout.readlines()[1:]:
    5458        entry = line.split()
    55         if entry == [] or entry[0] == "Negative":
     59        if not entry or entry[0] == "Negative":
    5660            break
    5761        if entry[1] == "rlidwka":
    58             if entry[0] == user or (entry[0][0:6] == "system" and checkAfsGroup(user, entry[0], cell)):
     62            if entry[0] == user or (entry[0][0:6] == "system" and
     63                                    checkAfsGroup(user, entry[0], cell)):
    5964                return True
     65    if verbose:
     66        return "You don't have admin bits on /mit/" + locker
    6067    return False
    6168
  • trunk/web/templates/info.tmpl

    r166 r177  
    6363  <input type="hidden" name="machine_id" value="$machine.machine_id"/>
    6464  <table>
    65     <tr><td>Owner$owner_help:</td><td><input type="text" name="owner", value="$machine.owner"/></td></tr>
     65    <tr><td>Owner${helppopup("owner")}:</td><td><input type="text" name="owner", value="$machine.owner"/></td></tr>
    6666    <tr><td>Contact email:</td><td><input type="text" name="contact" value="$machine.contact"/></td></tr>
    6767#if $machine.nics
  • trunk/web/templates/main.py

    r176 r177  
    8888    return Machine.select_by(owner=owner)
    8989
    90 def maxMemory(user, machine=None):
     90def maxMemory(user, machine=None, on=True):
    9191    """Return the maximum memory for a machine or a user.
    9292
     
    9494    machine.  Else, return the maximum that machine can have.
    9595
    96     on is a dictionary from machines to booleans, whether a machine is
    97     on.  If None, it is recomputed. XXX make this global?
    98     """
    99 
     96    on is whether the machine should be turned on.  If false, the max
     97    memory for the machine to change to, if it is left off, is
     98    returned.
     99    """
     100    if not on:
     101        return MAX_MEMORY_SINGLE
    100102    machines = getMachinesByOwner(user.username)
    101103    active_machines = [x for x in machines if g.uptimes[x]]
     
    119121    if user.username == 'moo':
    120122        return True
    121     return getafsgroups.checkLockerOwner(user.username,machine.owner)
     123    return getafsgroups.checkLockerOwner(user.username, machine.owner)
    122124
    123125def error(op, user, fields, err, emsg):
     
    175177        return p.stdout.read(), p.stderr.read()
    176178    if p.wait():
    177         raise CodeError('ERROR on remctl %s: %s' %
    178                           (args, p.stderr.read()))
     179        print >> sys.stderr, 'Error on remctl %s:' % args
     180        print >> sys.stderr, p.stderr.read()
     181        raise CodeError('ERROR on remctl')
    179182    return p.stdout.read()
    180183
     
    320323    return machine
    321324
    322 def validMemory(user, memory, machine=None):
    323     """Parse and validate limits for memory for a given user and machine."""
     325def validMemory(user, memory, machine=None, on=True):
     326    """Parse and validate limits for memory for a given user and machine.
     327
     328    on is whether the memory must be valid after the machine is
     329    switched on.
     330    """
    324331    try:
    325332        memory = int(memory)
     
    329336        raise InvalidInput('memory', memory,
    330337                           "Minimum %s MB" % MIN_MEMORY_SINGLE)
    331     if memory > maxMemory(user, machine):
     338    if memory > maxMemory(user, machine, on):
    332339        raise InvalidInput('memory', memory,
    333340                           'Maximum %s MB' % maxMemory(user, machine))
     
    361368   
    362369    memory = fields.getfirst('memory')
    363     memory = validMemory(user, memory)
     370    memory = validMemory(user, memory, on=True)
    364371   
    365372    disk = fields.getfirst('disk')
     
    566573
    567574def testOwner(user, owner, machine=None):
    568     if not getafsgroups.checkLockerOwner(user.username, owner):
    569         raise InvalidInput('owner', owner,
    570                            "Invalid")
    571     return owner
     575    if owner == machine.owner:   #XXX What do we do when you lose access to the locker?
     576        return owner
     577    value = getafsgroups.checkLockerOwner(user.username, owner, verbose=True)
     578    if value == True:
     579        return owner
     580    raise InvalidInput('owner', owner, value)
    572581
    573582def testContact(user, contact, machine=None):
    574     if contact != user.email:
    575         raise InvalidInput('contact', contact,
    576                            "Invalid")
     583    if not re.match("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", contact, re.I):
     584        raise InvalidInput('contact', contact, "Not a valid email")
    577585    return contact
    578586
     
    581589
    582590def testName(user, name, machine=None):
    583     if Machine.select_by(name=name) == []:
     591    if name is None:
     592        return None
     593    if not Machine.select_by(name=name):
    584594        return name
    585595    if name == machine.name:
    586596        return name
    587     raise InvalidInput('name', name,
    588                        "Already taken")
     597    raise InvalidInput('name', name, "Already taken")
    589598
    590599def testHostname(user, hostname, machine):
     
    593602            return hostname
    594603    # check if doesn't already exist
    595     if NIC.select_by(hostname=hostname) == []:
    596         return hostname
    597     raise InvalidInput('hostname', hostname,
    598                        "Different from before")
    599 
     604    if NIC.select_by(hostname=hostname):
     605        raise InvalidInput('hostname', hostname,
     606                           "Already exists")
     607    if not re.match("^[A-Z0-9-]{1,22}$", hostname, re.I):
     608        raise InvalidInput('hostname', hostname, "Not a valid hostname; must only use number, letters, and dashes.")
     609    return hostname
    600610
    601611def modify(user, fields):
    602612    """Handler for modifying attributes of a machine."""
    603     #XXX not written yet
    604 
     613
     614    olddisk = {}
    605615    transaction = ctx.current.create_transaction()
    606616    try:
     
    609619        contact = testContact(user, fields.getfirst('contact'))
    610620        hostname = testHostname(owner, fields.getfirst('hostname'),
    611                             machine)
     621                                machine)
    612622        name = testName(user, fields.getfirst('name'), machine)
    613623        oldname = machine.name
    614624        command="modify"
    615         olddisk = {}
    616625
    617626        memory = fields.getfirst('memory')
    618627        if memory is not None:
    619             memory = validMemory(user, memory, machine)
    620         else:
    621             memory = machine.memory
    622         if memory != machine.memory:
     628            memory = validMemory(user, memory, machine, on=False)
    623629            machine.memory = memory
    624 
     630 
    625631        disksize = testDisk(user, fields.getfirst('disk'))
    626632        if disksize is not None:
    627633            disksize = validDisk(user, disksize, machine)
    628         else:
    629             disksize = machine.disks[0].size
    630         for disk in machine.disks:
    631             olddisk[disk.guest_device_name] = disk.size
    632             disk.size = disksize
    633             ctx.current.save(disk)
     634            disk = machine.disks[0]
     635            if disk.size != disksize:
     636                olddisk[disk.guest_device_name] = disksize
     637                disk.size = disksize
     638                ctx.current.save(disk)
    634639       
    635         # XXX all NICs get same hostname on change?  Interface doesn't support more.
    636         for nic in machine.nics:
     640        # XXX first NIC gets hostname on change?  Interface doesn't support more.
     641        for nic in machine.nics[:1]:
    637642            nic.hostname = hostname
    638643            ctx.current.save(nic)
    639644
    640         if owner != machine.owner:
     645        if owner is not None and owner != machine.owner:
    641646            machine.owner = owner
    642         if name != machine.name:
     647        if name is not None and name != machine.name:
    643648            machine.name = name
    644649           
     
    648653        transaction.rollback()
    649654        raise
    650     remctl("web", "moveregister", oldname, name)
    651     for disk in machine.disks:
    652         # XXX all disks get the same size on change?  Interface doesn't support more.
    653         if disk.size != olddisk[disk.guest_device_name]:
    654             remctl("web", "lvresize", oldname, disk.guest_device_name, str(disk.size))
    655         if oldname != name:
    656             remctl("web", "lvrename", oldname, disk.guest_device_name, name)
     655    for diskname in olddisk:
     656        remctl("web", "lvresize", oldname, diskname, str(olddisk[diskname]))
     657    if name is not None and name != oldname:
     658        for disk in machine.disks:
     659            if oldname != name:
     660                remctl("web", "lvrename", oldname, disk.guest_device_name, name)
     661        remctl("web", "moveregister", oldname, name)
    657662    d = dict(user=user,
    658663             command=command,
     
    815820        sys.stderr.seek(0)
    816821        e = sys.stderr.read()
     822        sys.stderr=sys.stdout
    817823        if e:
    818824            output = str(output)
Note: See TracChangeset for help on using the changeset viewer.