source: trunk/packages/invirt-web/code/main.py @ 2167

Last change on this file since 2167 was 2161, checked in by andersk, 16 years ago

Don't confuse {KB, MB, GB} with {KiB, MiB, GiB}.

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