Changeset 153 for trunk/web/templates


Ignore:
Timestamp:
Oct 9, 2007, 8:09:47 AM (17 years ago)
Author:
ecprice
Message:

Improve the error infrastructure. Hopefully this works; I haven't
really tested it.

Location:
trunk/web/templates
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/templates/error.tmpl

    r113 r153  
    88#def body
    99<h1>ERROR</h1>
    10 <p>$errorMessage in operation $op</p>
    1110
     11<p>$errorMessage in operation $op.  This shouldn't happen!  Please
     12email sipb-xen@mit.edu to explain how it happened.  Stderr:</p>
     13<pre>$stderr</pre>
    1214#end def
  • trunk/web/templates/info.tmpl

    r147 r153  
    6363  <input type="hidden" name="machine_id" value="$machine.machine_id"/>
    6464  <table>
    65     <tr><td>Owner:</td><td><input type="text" value="$machine.owner"/></td></tr>
    66     <tr><td>Contact email:</td><td><input type="text" value="$machine.contact"/></td></tr>
     65    <tr><td>Owner:</td><td><input type="text" name="owner", value="$machine.owner"/></td></tr>
     66    <tr><td>Contact email:</td><td><input type="text" name="contact" value="$machine.contact"/></td></tr>
    6767#if $machine.nics
    68     <tr><td>Hostname:</td><td><input type="text" value="$machine.nics[0].hostname"/>.servers.csail.mit.edu</td></tr>
     68    <tr><td>Hostname:</td><td><input type="text" name="hostname" value="$machine.nics[0].hostname"/>.servers.csail.mit.edu</td></tr>
    6969#end if
    7070#if not $on
    71     <tr><td>Ram:</td><td><input type="text" size=3 value="$machine.memory"/>MB (max $max_mem)</td></tr>
    72     <tr><td>Disk:</td><td><input type="text" size=3 value="${machine.disks[0].size/1024.}"/>GB (max $max_disk)</td><td>WARNING: Modifying disk size may corrupt your data.</td></tr>
     71    <tr><td>Ram:</td><td><input type="text" size=3 name="memory" value="$machine.memory"/>MB (max $max_mem)</td></tr>
     72    <tr><td>Disk:</td><td><input type="text" size=3 name="disk" value="${machine.disks[0].size/1024.}"/>GB (max $max_disk)</td><td>WARNING: Modifying disk size may corrupt your data.</td></tr>
    7373#end if
    7474    <tr><td><input type="submit" class="button" name="action" value="Change"/></td></tr>
  • trunk/web/templates/main.py

    r152 r153  
    1313import hmac
    1414import datetime
    15 
    16 sys.stderr = sys.stdout
     15import StringIO
     16
     17sys.stderr = StringIO.StringIO()
    1718sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages')
    1819
     
    3233    the select box).
    3334    """
    34     pass
     35    def __init__(self, err_field, err_value, expl=None):
     36        super(InvalidInput, self).__init__(expl)
     37        self.err_field = err_field
     38        self.err_value = err_value
    3539
    3640class CodeError(MyException):
     
    116120    return machine.owner == user.username
    117121
    118 def error(op, user, fields, err):
     122def error(op, user, fields, err, emsg):
    119123    """Print an error page when a CodeError occurs"""
    120     d = dict(op=op, user=user, errorMessage=str(err))
    121     print Template(file='error.tmpl', searchList=[d, global_dict]);
     124    d = dict(op=op, user=user, errorMessage=str(err),
     125             stderr=emsg)
     126    return Template(file='error.tmpl', searchList=[d, global_dict]);
     127
     128def invalidInput(op, user, fields, err, emsg):
     129    """Print an error page when an InvalidInput exception occurs"""
     130    d = dict(op=op, user=user, err_field=err.err_field,
     131             err_value=str(err.err_value), stderr=emsg,
     132             errorMessage=str(err))
     133    return Template(file='invalid.tmpl', searchList=[d, global_dict]);
    122134
    123135def validMachineName(name):
     
    266278    try:
    267279        if memory > maxMemory(user):
    268             raise InvalidInput("Too much memory requested")
     280            raise InvalidInput('memory', memory,
     281                               "Max %s" % maxMemory(user))
    269282        if disk > maxDisk(user) * 1024:
    270             raise InvalidInput("Too much disk requested")
     283            raise InvalidInput('disk', disk,
     284                               "Max %s" % maxDisk(user))
    271285        if not canAddVm(user):
    272             raise InvalidInput("Too many VMs requested")
     286            raise InvalidInput('create', True, 'Unable to create more VMs')
    273287        res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')')
    274288        id = res.fetchone()[0]
     
    312326            raise ValueError
    313327    except ValueError:
    314         raise InvalidInput("Invalid memory amount; must be at least %s MB" %
    315                           MIN_MEMORY_SINGLE)
     328        raise InvalidInput('memory', memory,
     329                           "Minimum %s MB" % MIN_MEMORY_SINGLE)
    316330    if memory > maxMemory(user, machine):
    317         raise InvalidInput("Too much memory requested")
     331        raise InvalidInput('memory', memory,
     332                           'Maximum %s MB' % maxMemory(user, machine))
    318333    return memory
    319334
     
    323338        disk = float(disk)
    324339        if disk > maxDisk(user, machine):
    325             raise InvalidInput("Too much disk requested")
     340            raise InvalidInput('disk', disk,
     341                               "Maximum %s G" % maxDisk(user, machine))
    326342        disk = int(disk * 1024)
    327343        if disk < MIN_DISK_SINGLE * 1024:
    328344            raise ValueError
    329345    except ValueError:
    330         raise InvalidInput("Invalid disk amount; minimum is %s GB" %
    331                           MIN_DISK_SINGLE)
     346        raise InvalidInput('disk', disk,
     347                           "Minimum %s GB" % MIN_DISK_SINGLE)
    332348    return disk
    333349
     
    336352    name = fields.getfirst('name')
    337353    if not validMachineName(name):
    338         raise InvalidInput("Invalid name '%s'" % name)
     354        raise InvalidInput('name', name)
    339355    name = user.username + '_' + name.lower()
    340356
    341357    if Machine.get_by(name=name):
    342         raise InvalidInput("A machine named '%s' already exists" % name)
     358        raise InvalidInput('name', name,
     359                           "Already exists")
    343360   
    344361    memory = fields.getfirst('memory')
     
    360377    d = dict(user=user,
    361378             machine=machine)
    362     print Template(file='create.tmpl',
     379    return Template(file='create.tmpl',
    363380                   searchList=[d, global_dict]);
    364381
     
    392409             uptimes=uptimes,
    393410             cdroms=CDROM.select())
    394     print Template(file='list.tmpl', searchList=[d, global_dict])
     411    return Template(file='list.tmpl', searchList=[d, global_dict])
    395412
    396413def testMachineId(user, machineId, exists=True):
     
    453470             hostname=os.environ.get('SERVER_NAME', 'localhost'),
    454471             authtoken=token)
    455     print Template(file='vnc.tmpl',
     472    return Template(file='vnc.tmpl',
    456473                   searchList=[d, global_dict])
    457474
     
    530547    elif action == 'Power on':
    531548        if maxMemory(user) < machine.memory:
    532             raise InvalidInput("You don't have enough free RAM quota")
     549            raise InvalidInput('action', 'Power on',
     550                               "You don't have enough free RAM quota to turn on this machine")
    533551        bootMachine(machine, cdrom)
    534552    elif action == 'Power off':
     
    543561             command=action,
    544562             machine=machine)
    545     print Template(file="command.tmpl", searchList=[d, global_dict])
    546        
     563    return Template(file="command.tmpl", searchList=[d, global_dict])
     564
     565def testOwner(user, owner, machine=None):
     566    if owner != user.username:
     567        raise InvalidInput('owner', owner,
     568                           "Invalid")
     569    return owner
     570
     571def testContact(user, contact, machine=None):
     572    if contact != user.email:
     573        raise InvalidInput('contact', contact,
     574                           "Invalid")
     575    return contact
     576
     577def testHostname(user, hostname, machine):
     578    for nic in machine.nics:
     579        if hostname == nic.hostname:
     580            return hostname
     581    raise InvalidInput('hostname', hostname,
     582                       "Different from before")
     583
     584
    547585def modify(user, fields):
    548586    """Handler for modifying attributes of a machine."""
    549587    #XXX not written yet
    550588    machine = testMachineId(user, fields.getfirst('machine_id'))
    551    
     589    owner = testOwner(user, fields.getfirst('owner'), machine)
     590    contact = testContact(user, fields.getfirst('contact'))
     591    hostname = testHostname(user, fields.getfirst('hostname'),
     592                            machine)
     593    ram = fields.getfirst('memory')
     594    if ram is not None:
     595        ram = validMemory(user, ram, machine)
     596    disk = testDisk(user, fields.getfirst('disk'))
     597    if disk is not None:
     598        disk = validDisk(user, disk, machine)
     599
     600   
     601
    552602def help(user, fields):
    553603    """Handler for help messages."""
     
    570620             mapping=mapping)
    571621   
    572     print Template(file="help.tmpl", searchList=[d, global_dict])
     622    return Template(file="help.tmpl", searchList=[d, global_dict])
    573623   
    574624
     
    645695             max_disk=max_disk,
    646696             fields = fields)
    647     print Template(file='info.tmpl',
     697    return Template(file='info.tmpl',
    648698                   searchList=[d, global_dict])
    649699
     
    686736        operation = 'list'
    687737   
    688     fun = mapping.get(operation,
    689                       lambda u, e:
    690                           error(operation, u, e,
    691                                 "Invalid operation '%s'" % operation))
     738    def badOperation(u, e):
     739        raise CodeError("Unknown operation")
     740
     741    fun = mapping.get(operation, badOperation)
    692742    if fun not in (help, ):
    693743        connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
    694744    try:
    695         fun(u, fields)
     745        output = fun(u, fields)
     746        print 'Content-Type: text/html\n'
     747        sys.stderr.seek(0)
     748        e = sys.stderr.read()
     749        if e:
     750            output = output.replace('<body>', '<body><pre>'+e+'</pre>')
     751        print output
    696752    except CodeError, err:
    697         error(operation, u, fields, err)
     753        print 'Content-Type: text/html\n'
     754        sys.stderr.seek(0)
     755        e = sys.stderr.read()
     756        print error(operation, u, fields, err, e)
    698757    except InvalidInput, err:
    699         error(operation, u, fields, err)
     758        print 'Content-Type: text/html\n'
     759        sys.stderr.seek(0)
     760        e = sys.stderr.read()
     761        print invalidInput(operation, u, fields, err, e)
     762    except:
     763        print 'Content-Type: text/plain\n'
     764        sys.stderr.seek(0)
     765        e = sys.stderr.read()
     766        print e
     767        print '----'
     768        raise
  • trunk/web/templates/skeleton.tmpl

    r139 r153  
    1616function helppopup(name){
    1717   closeWin()
    18    helpWin = window.open("help?simple=true&subject="+encodeURIComponent(name), "HMMTHelp",
     18   helpWin = window.open("help?simple=true&subject="+encodeURIComponent(name), "Help",
    1919"status, height = 300, width = 400");
    2020   if (window.focus){helpWin.focus();}
Note: See TracChangeset for help on using the changeset viewer.