source: package_branches/invirt-web/cherrypy/code/main.py @ 2412

Last change on this file since 2412 was 2412, checked in by quentin, 15 years ago

Remove calls to helppopup from outside templates

  • Property svn:executable set to *
File size: 27.1 KB
Line 
1#!/usr/bin/python
2"""Main CGI script for web interface"""
3
4import base64
5import cPickle
6import cgi
7import datetime
8import hmac
9import random
10import sha
11import simplejson
12import sys
13import time
14import urllib
15import socket
16import cherrypy
17from StringIO import StringIO
18def revertStandardError():
19    """Move stderr to stdout, and return the contents of the old stderr."""
20    errio = sys.stderr
21    if not isinstance(errio, StringIO):
22        return ''
23    sys.stderr = sys.stdout
24    errio.seek(0)
25    return errio.read()
26
27def printError():
28    """Revert stderr to stdout, and print the contents of stderr"""
29    if isinstance(sys.stderr, StringIO):
30        print revertStandardError()
31
32if __name__ == '__main__':
33    import atexit
34    atexit.register(printError)
35
36import templates
37from Cheetah.Template import Template
38import validation
39import cache_acls
40from webcommon import State
41import controls
42from getafsgroups import getAfsGroupMembers
43from invirt import database
44from invirt.database import Machine, CDROM, session, connect, MachineAccess, Type, Autoinstall
45from invirt.config import structs as config
46from invirt.common import InvalidInput, CodeError
47
48from view import View
49
50class InvirtWeb(View):
51    def __init__(self):
52        super(self.__class__,self).__init__()
53        connect()
54        self._cp_config['tools.require_login.on'] = True
55        self._cp_config['tools.mako.imports'] = ['from invirt.config import structs as config',
56                                                 'from invirt import database']
57
58
59    @cherrypy.expose
60    @cherrypy.tools.mako(filename="/list.mako")
61    def list(self):
62        """Handler for list requests."""
63        checkpoint.checkpoint('Getting list dict')
64        d = getListDict(cherrypy.request.login, cherrypy.request.state)
65        checkpoint.checkpoint('Got list dict')
66        return d
67    index=list
68
69    @cherrypy.expose
70    @cherrypy.tools.mako(filename="/info.mako")
71    def info(self, machine_id):
72        """Handler for info on a single VM."""
73        machine = validation.Validate(cherrypy.request.login, cherrypy.request.state, machine_id=machine_id).machine
74        d = infoDict(cherrypy.request.login, cherrypy.request.state, machine)
75        checkpoint.checkpoint('Got infodict')
76        return d
77
78    @cherrypy.expose
79    @cherrypy.tools.mako(filename="/help.mako")
80    def help(self, subject=None, simple=False):
81        """Handler for help messages."""
82
83        help_mapping = {
84            'Autoinstalls': """
85The autoinstaller builds a minimal Debian or Ubuntu system to run as a
86ParaVM.  You can access the resulting system by logging into the <a
87href="help?simple=true&subject=ParaVM+Console">serial console server</a>
88with your Kerberos tickets; there is no root password so sshd will
89refuse login.</p>
90
91<p>Under the covers, the autoinstaller uses our own patched version of
92xen-create-image, which is a tool based on debootstrap.  If you log
93into the serial console while the install is running, you can watch
94it.
95""",
96            'ParaVM Console': """
97ParaVM machines do not support local console access over VNC.  To
98access the serial console of these machines, you can SSH with Kerberos
99to %s, using the name of the machine as your
100username.""" % config.console.hostname,
101            'HVM/ParaVM': """
102HVM machines use the virtualization features of the processor, while
103ParaVM machines rely on a modified kernel to communicate directly with
104the hypervisor.  HVMs support boot CDs of any operating system, and
105the VNC console applet.  The three-minute autoinstaller produces
106ParaVMs.  ParaVMs typically are more efficient, and always support the
107<a href="help?subject=ParaVM+Console">console server</a>.</p>
108
109<p>More details are <a
110href="https://xvm.scripts.mit.edu/wiki/Paravirtualization">on the
111wiki</a>, including steps to prepare an HVM guest to boot as a ParaVM
112(which you can skip by using the autoinstaller to begin with.)</p>
113
114<p>We recommend using a ParaVM when possible and an HVM when necessary.
115""",
116            'CPU Weight': """
117Don't ask us!  We're as mystified as you are.""",
118            'Owner': """
119The owner field is used to determine <a
120href="help?subject=Quotas">quotas</a>.  It must be the name of a
121locker that you are an AFS administrator of.  In particular, you or an
122AFS group you are a member of must have AFS rlidwka bits on the
123locker.  You can check who administers the LOCKER locker using the
124commands 'attach LOCKER; fs la /mit/LOCKER' on Athena.)  See also <a
125href="help?subject=Administrator">administrator</a>.""",
126            'Administrator': """
127The administrator field determines who can access the console and
128power on and off the machine.  This can be either a user or a moira
129group.""",
130            'Quotas': """
131Quotas are determined on a per-locker basis.  Each locker may have a
132maximum of 512 mebibytes of active ram, 50 gibibytes of disk, and 4
133active machines.""",
134            'Console': """
135<strong>Framebuffer:</strong> At a Linux boot prompt in your VM, try
136setting <tt>fb=false</tt> to disable the framebuffer.  If you don't,
137your machine will run just fine, but the applet's display of the
138console will suffer artifacts.
139""",
140            'Windows': """
141<strong>Windows Vista:</strong> The Vista image is licensed for all MIT students and will automatically activate off the network; see <a href="/static/msca-email.txt">the licensing confirmation e-mail</a> for details. The installer requires 512 MiB RAM and at least 7.5 GiB disk space (15 GiB or more recommended).<br>
142<strong>Windows XP:</strong> This is the volume license CD image. You will need your own volume license key to complete the install. We do not have these available for the general MIT community; ask your department if they have one.
143"""
144            }
145
146        if not subject:
147            subject = sorted(help_mapping.keys())
148        if not isinstance(subject, list):
149            subject = [subject]
150
151        return dict(simple=simple,
152                    subjects=subject,
153                    mapping=help_mapping)
154    help._cp_config['tools.require_login.on'] = False
155
156    @cherrypy.expose
157    @cherrypy.tools.mako(filename="/helloworld.mako")
158    def helloworld(self, **kwargs):
159        return {'request': cherrypy.request, 'kwargs': kwargs}
160    helloworld._cp_config['tools.require_login.on'] = False
161
162def pathSplit(path):
163    if path.startswith('/'):
164        path = path[1:]
165    i = path.find('/')
166    if i == -1:
167        i = len(path)
168    return path[:i], path[i:]
169
170class Checkpoint:
171    def __init__(self):
172        self.start_time = time.time()
173        self.checkpoints = []
174
175    def checkpoint(self, s):
176        self.checkpoints.append((s, time.time()))
177
178    def __str__(self):
179        return ('Timing info:\n%s\n' %
180                '\n'.join(['%s: %s' % (d, t - self.start_time) for
181                           (d, t) in self.checkpoints]))
182
183checkpoint = Checkpoint()
184
185def makeErrorPre(old, addition):
186    if addition is None:
187        return
188    if old:
189        return old[:-6]  + '\n----\n' + str(addition) + '</pre>'
190    else:
191        return '<p>STDERR:</p><pre>' + str(addition) + '</pre>'
192
193Template.database = database
194Template.config = config
195Template.err = None
196
197class JsonDict:
198    """Class to store a dictionary that will be converted to JSON"""
199    def __init__(self, **kws):
200        self.data = kws
201        if 'err' in kws:
202            err = kws['err']
203            del kws['err']
204            self.addError(err)
205
206    def __str__(self):
207        return simplejson.dumps(self.data)
208
209    def addError(self, text):
210        """Add stderr text to be displayed on the website."""
211        self.data['err'] = \
212            makeErrorPre(self.data.get('err'), text)
213
214class Defaults:
215    """Class to store default values for fields."""
216    memory = 256
217    disk = 4.0
218    cdrom = ''
219    autoinstall = ''
220    name = ''
221    description = ''
222    type = 'linux-hvm'
223
224    def __init__(self, max_memory=None, max_disk=None, **kws):
225        if max_memory is not None:
226            self.memory = min(self.memory, max_memory)
227        if max_disk is not None:
228            self.disk = min(self.disk, max_disk)
229        for key in kws:
230            setattr(self, key, kws[key])
231
232
233
234DEFAULT_HEADERS = {'Content-Type': 'text/html'}
235
236def invalidInput(op, username, fields, err, emsg):
237    """Print an error page when an InvalidInput exception occurs"""
238    d = dict(op=op, user=username, err_field=err.err_field,
239             err_value=str(err.err_value), stderr=emsg,
240             errorMessage=str(err))
241    return templates.invalid(searchList=[d])
242
243def hasVnc(status):
244    """Does the machine with a given status list support VNC?"""
245    if status is None:
246        return False
247    for l in status:
248        if l[0] == 'device' and l[1][0] == 'vfb':
249            d = dict(l[1][1:])
250            return 'location' in d
251    return False
252
253def parseCreate(username, state, fields):
254    kws = dict([(kw, fields.getfirst(kw)) for kw in 'name description owner memory disksize vmtype cdrom autoinstall'.split()])
255    validate = validation.Validate(username, state, strict=True, **kws)
256    return dict(contact=username, name=validate.name, description=validate.description, memory=validate.memory,
257                disksize=validate.disksize, owner=validate.owner, machine_type=getattr(validate, 'vmtype', Defaults.type),
258                cdrom=getattr(validate, 'cdrom', None),
259                autoinstall=getattr(validate, 'autoinstall', None))
260
261def create(username, state, path, fields):
262    """Handler for create requests."""
263    try:
264        parsed_fields = parseCreate(username, state, fields)
265        machine = controls.createVm(username, state, **parsed_fields)
266    except InvalidInput, err:
267        pass
268    else:
269        err = None
270    state.clear() #Changed global state
271    d = getListDict(username, state)
272    d['err'] = err
273    if err:
274        for field in fields.keys():
275            setattr(d['defaults'], field, fields.getfirst(field))
276    else:
277        d['new_machine'] = parsed_fields['name']
278    return templates.list(searchList=[d])
279
280
281def getListDict(username, state):
282    """Gets the list of local variables used by list.tmpl."""
283    checkpoint.checkpoint('Starting')
284    machines = state.machines
285    checkpoint.checkpoint('Got my machines')
286    on = {}
287    has_vnc = {}
288    xmlist = state.xmlist
289    checkpoint.checkpoint('Got uptimes')
290    can_clone = 'ice3' not in state.xmlist_raw
291    for m in machines:
292        if m not in xmlist:
293            has_vnc[m] = 'Off'
294            m.uptime = None
295        else:
296            m.uptime = xmlist[m]['uptime']
297            if xmlist[m]['console']:
298                has_vnc[m] = True
299            elif m.type.hvm:
300                has_vnc[m] = "WTF?"
301            else:
302                has_vnc[m] = "ParaVM"
303    max_memory = validation.maxMemory(username, state)
304    max_disk = validation.maxDisk(username)
305    checkpoint.checkpoint('Got max mem/disk')
306    defaults = Defaults(max_memory=max_memory,
307                        max_disk=max_disk,
308                        owner=username)
309    checkpoint.checkpoint('Got defaults')
310    def sortkey(machine):
311        return (machine.owner != username, machine.owner, machine.name)
312    machines = sorted(machines, key=sortkey)
313    d = dict(user=username,
314             cant_add_vm=validation.cantAddVm(username, state),
315             max_memory=max_memory,
316             max_disk=max_disk,
317             defaults=defaults,
318             machines=machines,
319             has_vnc=has_vnc,
320             can_clone=can_clone)
321    return d
322
323def vnc(username, state, path, fields):
324    """VNC applet page.
325
326    Note that due to same-domain restrictions, the applet connects to
327    the webserver, which needs to forward those requests to the xen
328    server.  The Xen server runs another proxy that (1) authenticates
329    and (2) finds the correct port for the VM.
330
331    You might want iptables like:
332
333    -t nat -A PREROUTING -s ! 18.181.0.60 -i eth1 -p tcp -m tcp \
334      --dport 10003 -j DNAT --to-destination 18.181.0.60:10003
335    -t nat -A POSTROUTING -d 18.181.0.60 -o eth1 -p tcp -m tcp \
336      --dport 10003 -j SNAT --to-source 18.187.7.142
337    -A FORWARD -d 18.181.0.60 -i eth1 -o eth1 -p tcp -m tcp \
338      --dport 10003 -j ACCEPT
339
340    Remember to enable iptables!
341    echo 1 > /proc/sys/net/ipv4/ip_forward
342    """
343    machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
344
345    token = controls.vnctoken(machine)
346    host = controls.listHost(machine)
347    if host:
348        port = 10003 + [h.hostname for h in config.hosts].index(host)
349    else:
350        port = 5900 # dummy
351
352    status = controls.statusInfo(machine)
353    has_vnc = hasVnc(status)
354
355    d = dict(user=username,
356             on=status,
357             has_vnc=has_vnc,
358             machine=machine,
359             hostname=state.environ.get('SERVER_NAME', 'localhost'),
360             port=port,
361             authtoken=token)
362    return templates.vnc(searchList=[d])
363
364def getHostname(nic):
365    """Find the hostname associated with a NIC.
366
367    XXX this should be merged with the similar logic in DNS and DHCP.
368    """
369    if nic.hostname:
370        hostname = nic.hostname
371    elif nic.machine:
372        hostname = nic.machine.name
373    else:
374        return None
375    if '.' in hostname:
376        return hostname
377    else:
378        return hostname + '.' + config.dns.domains[0]
379
380def getNicInfo(data_dict, machine):
381    """Helper function for info, get data on nics for a machine.
382
383    Modifies data_dict to include the relevant data, and returns a list
384    of (key, name) pairs to display "name: data_dict[key]" to the user.
385    """
386    data_dict['num_nics'] = len(machine.nics)
387    nic_fields_template = [('nic%s_hostname', 'NIC %s Hostname'),
388                           ('nic%s_mac', 'NIC %s MAC Addr'),
389                           ('nic%s_ip', 'NIC %s IP'),
390                           ]
391    nic_fields = []
392    for i in range(len(machine.nics)):
393        nic_fields.extend([(x % i, y % i) for x, y in nic_fields_template])
394        data_dict['nic%s_hostname' % i] = getHostname(machine.nics[i])
395        data_dict['nic%s_mac' % i] = machine.nics[i].mac_addr
396        data_dict['nic%s_ip' % i] = machine.nics[i].ip
397    if len(machine.nics) == 1:
398        nic_fields = [(x, y.replace('NIC 0 ', '')) for x, y in nic_fields]
399    return nic_fields
400
401def getDiskInfo(data_dict, machine):
402    """Helper function for info, get data on disks for a machine.
403
404    Modifies data_dict to include the relevant data, and returns a list
405    of (key, name) pairs to display "name: data_dict[key]" to the user.
406    """
407    data_dict['num_disks'] = len(machine.disks)
408    disk_fields_template = [('%s_size', '%s size')]
409    disk_fields = []
410    for disk in machine.disks:
411        name = disk.guest_device_name
412        disk_fields.extend([(x % name, y % name) for x, y in
413                            disk_fields_template])
414        data_dict['%s_size' % name] = "%0.1f GiB" % (disk.size / 1024.)
415    return disk_fields
416
417def command(username, state, path, fields):
418    """Handler for running commands like boot and delete on a VM."""
419    back = fields.getfirst('back')
420    try:
421        d = controls.commandResult(username, state, fields)
422        if d['command'] == 'Delete VM':
423            back = 'list'
424    except InvalidInput, err:
425        if not back:
426            raise
427        print >> sys.stderr, err
428        result = err
429    else:
430        result = 'Success!'
431        if not back:
432            return templates.command(searchList=[d])
433    if back == 'list':
434        state.clear() #Changed global state
435        d = getListDict(username, state)
436        d['result'] = result
437        return templates.list(searchList=[d])
438    elif back == 'info':
439        machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
440        return ({'Status': '303 See Other',
441                 'Location': 'info?machine_id=%d' % machine.machine_id},
442                "You shouldn't see this message.")
443    else:
444        raise InvalidInput('back', back, 'Not a known back page.')
445
446def modifyDict(username, state, fields):
447    """Modify a machine as specified by CGI arguments.
448
449    Return a list of local variables for modify.tmpl.
450    """
451    olddisk = {}
452    session.begin()
453    try:
454        kws = dict([(kw, fields.getfirst(kw)) for kw in 'machine_id owner admin contact name description memory vmtype disksize'.split()])
455        validate = validation.Validate(username, state, **kws)
456        machine = validate.machine
457        oldname = machine.name
458
459        if hasattr(validate, 'memory'):
460            machine.memory = validate.memory
461
462        if hasattr(validate, 'vmtype'):
463            machine.type = validate.vmtype
464
465        if hasattr(validate, 'disksize'):
466            disksize = validate.disksize
467            disk = machine.disks[0]
468            if disk.size != disksize:
469                olddisk[disk.guest_device_name] = disksize
470                disk.size = disksize
471                session.save_or_update(disk)
472
473        update_acl = False
474        if hasattr(validate, 'owner') and validate.owner != machine.owner:
475            machine.owner = validate.owner
476            update_acl = True
477        if hasattr(validate, 'name'):
478            machine.name = validate.name
479            for n in machine.nics:
480                if n.hostname == oldname:
481                    n.hostname = validate.name
482        if hasattr(validate, 'description'):
483            machine.description = validate.description
484        if hasattr(validate, 'admin') and validate.admin != machine.administrator:
485            machine.administrator = validate.admin
486            update_acl = True
487        if hasattr(validate, 'contact'):
488            machine.contact = validate.contact
489
490        session.save_or_update(machine)
491        if update_acl:
492            cache_acls.refreshMachine(machine)
493        session.commit()
494    except:
495        session.rollback()
496        raise
497    for diskname in olddisk:
498        controls.resizeDisk(oldname, diskname, str(olddisk[diskname]))
499    if hasattr(validate, 'name'):
500        controls.renameMachine(machine, oldname, validate.name)
501    return dict(user=username,
502                command="modify",
503                machine=machine)
504
505def modify(username, state, path, fields):
506    """Handler for modifying attributes of a machine."""
507    try:
508        modify_dict = modifyDict(username, state, fields)
509    except InvalidInput, err:
510        result = None
511        machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
512    else:
513        machine = modify_dict['machine']
514        result = 'Success!'
515        err = None
516    info_dict = infoDict(username, state, machine)
517    info_dict['err'] = err
518    if err:
519        for field in fields.keys():
520            setattr(info_dict['defaults'], field, fields.getfirst(field))
521    info_dict['result'] = result
522    return templates.info(searchList=[info_dict])
523
524def badOperation(u, s, p, e):
525    """Function called when accessing an unknown URI."""
526    return ({'Status': '404 Not Found'}, 'Invalid operation.')
527
528def infoDict(username, state, machine):
529    """Get the variables used by info.tmpl."""
530    status = controls.statusInfo(machine)
531    checkpoint.checkpoint('Getting status info')
532    has_vnc = hasVnc(status)
533    if status is None:
534        main_status = dict(name=machine.name,
535                           memory=str(machine.memory))
536        uptime = None
537        cputime = None
538    else:
539        main_status = dict(status[1:])
540        main_status['host'] = controls.listHost(machine)
541        start_time = float(main_status.get('start_time', 0))
542        uptime = datetime.timedelta(seconds=int(time.time()-start_time))
543        cpu_time_float = float(main_status.get('cpu_time', 0))
544        cputime = datetime.timedelta(seconds=int(cpu_time_float))
545    checkpoint.checkpoint('Status')
546    display_fields = [('name', 'Name'),
547                      ('description', 'Description'),
548                      ('owner', 'Owner'),
549                      ('administrator', 'Administrator'),
550                      ('contact', 'Contact'),
551                      ('type', 'Type'),
552                      'NIC_INFO',
553                      ('uptime', 'uptime'),
554                      ('cputime', 'CPU usage'),
555                      ('host', 'Hosted on'),
556                      ('memory', 'RAM'),
557                      'DISK_INFO',
558                      ('state', 'state (xen format)'),
559                      ]
560    fields = []
561    machine_info = {}
562    machine_info['name'] = machine.name
563    machine_info['description'] = machine.description
564    machine_info['type'] = machine.type.hvm and 'HVM' or 'ParaVM'
565    machine_info['owner'] = machine.owner
566    machine_info['administrator'] = machine.administrator
567    machine_info['contact'] = machine.contact
568
569    nic_fields = getNicInfo(machine_info, machine)
570    nic_point = display_fields.index('NIC_INFO')
571    display_fields = (display_fields[:nic_point] + nic_fields +
572                      display_fields[nic_point+1:])
573
574    disk_fields = getDiskInfo(machine_info, machine)
575    disk_point = display_fields.index('DISK_INFO')
576    display_fields = (display_fields[:disk_point] + disk_fields +
577                      display_fields[disk_point+1:])
578
579    main_status['memory'] += ' MiB'
580    for field, disp in display_fields:
581        if field in ('uptime', 'cputime') and locals()[field] is not None:
582            fields.append((disp, locals()[field]))
583        elif field in machine_info:
584            fields.append((disp, machine_info[field]))
585        elif field in main_status:
586            fields.append((disp, main_status[field]))
587        else:
588            pass
589            #fields.append((disp, None))
590
591    checkpoint.checkpoint('Got fields')
592
593
594    max_mem = validation.maxMemory(machine.owner, state, machine, False)
595    checkpoint.checkpoint('Got mem')
596    max_disk = validation.maxDisk(machine.owner, machine)
597    defaults = Defaults()
598    for name in 'machine_id name description administrator owner memory contact'.split():
599        setattr(defaults, name, getattr(machine, name))
600    defaults.type = machine.type.type_id
601    defaults.disk = "%0.2f" % (machine.disks[0].size/1024.)
602    checkpoint.checkpoint('Got defaults')
603    d = dict(user=username,
604             on=status is not None,
605             machine=machine,
606             defaults=defaults,
607             has_vnc=has_vnc,
608             uptime=str(uptime),
609             ram=machine.memory,
610             max_mem=max_mem,
611             max_disk=max_disk,
612             fields = fields)
613    return d
614
615def unauthFront(_, _2, _3, fields):
616    """Information for unauth'd users."""
617    return templates.unauth(searchList=[{'simple' : True, 
618            'hostname' : socket.getfqdn()}])
619
620def admin(username, state, path, fields):
621    if path == '':
622        return ({'Status': '303 See Other',
623                 'Location': 'admin/'},
624                "You shouldn't see this message.")
625    if not username in getAfsGroupMembers(config.adminacl, 'athena.mit.edu'):
626        raise InvalidInput('username', username,
627                           'Not in admin group %s.' % config.adminacl)
628    newstate = State(username, isadmin=True)
629    newstate.environ = state.environ
630    return handler(username, newstate, path, fields)
631
632def throwError(_, __, ___, ____):
633    """Throw an error, to test the error-tracing mechanisms."""
634    raise RuntimeError("test of the emergency broadcast system")
635
636mapping = dict(vnc=vnc,
637               command=command,
638               modify=modify,
639               create=create,
640               unauth=unauthFront,
641               admin=admin,
642               overlord=admin,
643               errortest=throwError)
644
645def printHeaders(headers):
646    """Print a dictionary as HTTP headers."""
647    for key, value in headers.iteritems():
648        print '%s: %s' % (key, value)
649    print
650
651def send_error_mail(subject, body):
652    import subprocess
653
654    to = config.web.errormail
655    mail = """To: %s
656From: root@%s
657Subject: %s
658
659%s
660""" % (to, config.web.hostname, subject, body)
661    p = subprocess.Popen(['/usr/sbin/sendmail', '-f', to, to],
662                         stdin=subprocess.PIPE)
663    p.stdin.write(mail)
664    p.stdin.close()
665    p.wait()
666
667def show_error(op, username, fields, err, emsg, traceback):
668    """Print an error page when an exception occurs"""
669    d = dict(op=op, user=username, fields=fields,
670             errorMessage=str(err), stderr=emsg, traceback=traceback)
671    details = templates.error_raw(searchList=[d])
672    exclude = config.web.errormail_exclude
673    if username not in exclude and '*' not in exclude:
674        send_error_mail('xvm error on %s for %s: %s' % (op, username, err),
675                        details)
676    d['details'] = details
677    return templates.error(searchList=[d])
678
679def handler(username, state, path, fields):
680    operation, path = pathSplit(path)
681    if not operation:
682        operation = 'list'
683    print 'Starting', operation
684    fun = mapping.get(operation, badOperation)
685    return fun(username, state, path, fields)
686
687class App:
688    def __init__(self, environ, start_response):
689        self.environ = environ
690        self.start = start_response
691
692        self.username = getUser(environ)
693        self.state = State(self.username)
694        self.state.environ = environ
695
696        random.seed() #sigh
697
698    def __iter__(self):
699        start_time = time.time()
700        database.clear_cache()
701        sys.stderr = StringIO()
702        fields = cgi.FieldStorage(fp=self.environ['wsgi.input'], environ=self.environ)
703        operation = self.environ.get('PATH_INFO', '')
704        if not operation:
705            self.start("301 Moved Permanently", [('Location', './')])
706            return
707        if self.username is None:
708            operation = 'unauth'
709
710        try:
711            checkpoint.checkpoint('Before')
712            output = handler(self.username, self.state, operation, fields)
713            checkpoint.checkpoint('After')
714
715            headers = dict(DEFAULT_HEADERS)
716            if isinstance(output, tuple):
717                new_headers, output = output
718                headers.update(new_headers)
719            e = revertStandardError()
720            if e:
721                if hasattr(output, 'addError'):
722                    output.addError(e)
723                else:
724                    # This only happens on redirects, so it'd be a pain to get
725                    # the message to the user.  Maybe in the response is useful.
726                    output = output + '\n\nstderr:\n' + e
727            output_string =  str(output)
728            checkpoint.checkpoint('output as a string')
729        except Exception, err:
730            if not fields.has_key('js'):
731                if isinstance(err, InvalidInput):
732                    self.start('200 OK', [('Content-Type', 'text/html')])
733                    e = revertStandardError()
734                    yield str(invalidInput(operation, self.username, fields,
735                                           err, e))
736                    return
737            import traceback
738            self.start('500 Internal Server Error',
739                       [('Content-Type', 'text/html')])
740            e = revertStandardError()
741            s = show_error(operation, self.username, fields,
742                           err, e, traceback.format_exc())
743            yield str(s)
744            return
745        status = headers.setdefault('Status', '200 OK')
746        del headers['Status']
747        self.start(status, headers.items())
748        yield output_string
749        if fields.has_key('timedebug'):
750            yield '<pre>%s</pre>' % cgi.escape(str(checkpoint))
751
752def constructor():
753    connect()
754    return App
755
756def main():
757    from flup.server.fcgi_fork import WSGIServer
758    WSGIServer(constructor()).run()
759
760if __name__ == '__main__':
761    main()
Note: See TracBrowser for help on using the repository browser.