source: trunk/web/templates/main.py @ 150

Last change on this file since 150 was 147, checked in by ecprice, 17 years ago

For new naming scheme.

  • Property svn:executable set to *
File size: 23.0 KB
RevLine 
[113]1#!/usr/bin/python
2
3import sys
4import cgi
5import os
6import string
7import subprocess
[119]8import re
[118]9import time
10import cPickle
11import base64
[119]12import sha
13import hmac
[133]14import datetime
[113]15
16sys.stderr = sys.stdout
17sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages')
18
19from Cheetah.Template import Template
20from sipb_xen_database import *
21import random
22
[119]23class MyException(Exception):
[145]24    """Base class for my exceptions"""
[119]25    pass
26
[145]27class InvalidInput(MyException):
28    """Exception for user-provided input is invalid but maybe in good faith.
29
30    This would include setting memory to negative (which might be a
31    typo) but not setting an invalid boot CD (which requires bypassing
32    the select box).
33    """
34    pass
35
36class CodeError(MyException):
37    """Exception for internal errors or bad faith input."""
38    pass
39
40
41
[139]42def helppopup(subj):
[145]43    """Return HTML code for a (?) link to a specified help topic"""
[139]44    return '<span class="helplink"><a href="help?subject='+subj+'&amp;simple=true" target="_blank" onclick="return helppopup(\''+subj+'\')">(?)</a></span>'
45
46
47global_dict = {}
48global_dict['helppopup'] = helppopup
49
50
[113]51# ... and stolen from xend/uuid.py
52def randomUUID():
53    """Generate a random UUID."""
54
55    return [ random.randint(0, 255) for _ in range(0, 16) ]
56
57def uuidToString(u):
[145]58    """Turn a numeric UUID to a hyphen-seperated one."""
[113]59    return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
60                     "%02x" * 6]) % tuple(u)
61
[144]62MAX_MEMORY_TOTAL = 512
63MAX_MEMORY_SINGLE = 256
64MIN_MEMORY_SINGLE = 16
65MAX_DISK_TOTAL = 50
66MAX_DISK_SINGLE = 50
67MIN_DISK_SINGLE = 0.1
68MAX_VMS_TOTAL = 10
69MAX_VMS_ACTIVE = 4
[113]70
[145]71def getMachinesByOwner(owner):
72    """Return the machines owned by a given owner."""
[144]73    return Machine.select_by(owner=owner)
74
75def maxMemory(user, machine=None, on=None):
[145]76    """Return the maximum memory for a machine or a user.
77
78    If machine is None, return the memory available for a new
79    machine.  Else, return the maximum that machine can have.
80
81    on is a dictionary from machines to booleans, whether a machine is
82    on.  If None, it is recomputed. XXX make this global?
83    """
84
85    machines = getMachinesByOwner(user.username)
[144]86    if on is None:
87        on = getUptimes(machines)
88    active_machines = [x for x in machines if on[x]]
89    mem_usage = sum([x.memory for x in active_machines if x != machine])
90    return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage)
91
[133]92def maxDisk(user, machine=None):
[145]93    machines = getMachinesByOwner(user.username)
[144]94    disk_usage = sum([sum([y.size for y in x.disks])
95                      for x in machines if x != machine])
96    return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.)
[119]97
[144]98def canAddVm(user, on=None):
[145]99    machines = getMachinesByOwner(user.username)
[144]100    if on is None:
101        on = getUptimes(machines)
102    active_machines = [x for x in machines if on[x]]
103    return (len(machines) < MAX_VMS_TOTAL and
104            len(active_machines) < MAX_VMS_ACTIVE)
105
[113]106def haveAccess(user, machine):
[145]107    """Return whether a user has access to a machine"""
[138]108    if user.username == 'moo':
[135]109        return True
110    return machine.owner == user.username
[113]111
[119]112def error(op, user, fields, err):
[145]113    """Print an error page when a CodeError occurs"""
[119]114    d = dict(op=op, user=user, errorMessage=str(err))
[139]115    print Template(file='error.tmpl', searchList=[d, global_dict]);
[113]116
117def validMachineName(name):
[119]118    """Check that name is valid for a machine name"""
[113]119    if not name:
120        return False
[119]121    charset = string.ascii_letters + string.digits + '-_'
122    if name[0] in '-_' or len(name) > 22:
[113]123        return False
[140]124    for x in name:
125        if x not in charset:
126            return False
127    return True
[113]128
[119]129def kinit(username = 'tabbott/extra', keytab = '/etc/tabbott.keytab'):
130    """Kinit with a given username and keytab"""
[113]131
[133]132    p = subprocess.Popen(['kinit', "-k", "-t", keytab, username],
133                         stderr=subprocess.PIPE)
[119]134    e = p.wait()
135    if e:
[145]136        raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
[119]137
[113]138def checkKinit():
[119]139    """If we lack tickets, kinit."""
[113]140    p = subprocess.Popen(['klist', '-s'])
141    if p.wait():
142        kinit()
143
[119]144def remctl(*args, **kws):
145    """Perform a remctl and return the output.
146
147    kinits if necessary, and outputs errors to stderr.
148    """
[113]149    checkKinit()
150    p = subprocess.Popen(['remctl', 'black-mesa.mit.edu']
151                         + list(args),
152                         stdout=subprocess.PIPE,
153                         stderr=subprocess.PIPE)
[119]154    if kws.get('err'):
[144]155        p.wait()
[119]156        return p.stdout.read(), p.stderr.read()
[113]157    if p.wait():
[145]158        raise CodeError('ERROR on remctl %s: %s' %
[144]159                          (args, p.stderr.read()))
[119]160    return p.stdout.read()
[113]161
162def makeDisks():
[119]163    """Update the lvm partitions to include all disks in the database."""
164    remctl('web', 'lvcreate')
[113]165
166def bootMachine(machine, cdtype):
[119]167    """Boot a machine with a given boot CD.
168
169    If cdtype is None, give no boot cd.  Otherwise, it is the string
170    id of the CD (e.g. 'gutsy_i386')
171    """
[113]172    if cdtype is not None:
[119]173        remctl('web', 'vmboot', machine.name,
[113]174               cdtype)
175    else:
[119]176        remctl('web', 'vmboot', machine.name)
[113]177
[119]178def registerMachine(machine):
179    """Register a machine to be controlled by the web interface"""
180    remctl('web', 'register', machine.name)
181
[133]182def unregisterMachine(machine):
183    """Unregister a machine to not be controlled by the web interface"""
184    remctl('web', 'unregister', machine.name)
185
[119]186def parseStatus(s):
187    """Parse a status string into nested tuples of strings.
188
189    s = output of xm list --long <machine_name>
190    """
191    values = re.split('([()])', s)
192    stack = [[]]
193    for v in values[2:-2]: #remove initial and final '()'
194        if not v:
195            continue
196        v = v.strip()
197        if v == '(':
198            stack.append([])
199        elif v == ')':
[133]200            if len(stack[-1]) == 1:
201                stack[-1].append('')
[119]202            stack[-2].append(stack[-1])
203            stack.pop()
204        else:
205            if not v:
206                continue
207            stack[-1].extend(v.split())
208    return stack[-1]
209
[133]210def getUptimes(machines):
211    """Return a dictionary mapping machine names to uptime strings"""
212    value_string = remctl('web', 'listvms')
213    lines = value_string.splitlines()
214    d = {}
[147]215    for line in lines:
[133]216        lst = line.split()
217        name, id = lst[:2]
218        uptime = ' '.join(lst[2:])
219        d[name] = uptime
[144]220    ans = {}
221    for m in machines:
222        ans[m] = d.get(m.name)
223    return ans
[133]224
[119]225def statusInfo(machine):
[133]226    """Return the status list for a given machine.
227
228    Gets and parses xm list --long
229    """
[119]230    value_string, err_string = remctl('list-long', machine.name, err=True)
231    if 'Unknown command' in err_string:
[145]232        raise CodeError("ERROR in remctl list-long %s is not registered" % (machine.name,))
[119]233    elif 'does not exist' in err_string:
234        return None
235    elif err_string:
[145]236        raise CodeError("ERROR in remctl list-long %s%s" % (machine.name, err_string))
[119]237    status = parseStatus(value_string)
238    return status
239
240def hasVnc(status):
[133]241    """Does the machine with a given status list support VNC?"""
[119]242    if status is None:
243        return False
244    for l in status:
245        if l[0] == 'device' and l[1][0] == 'vfb':
246            d = dict(l[1][1:])
247            return 'location' in d
248    return False
249
[113]250def createVm(user, name, memory, disk, is_hvm, cdrom):
[133]251    """Create a VM and put it in the database"""
[113]252    # put stuff in the table
253    transaction = ctx.current.create_transaction()
254    try:
[144]255        if memory > maxMemory(user):
[145]256            raise InvalidInput("Too much memory requested")
[144]257        if disk > maxDisk(user) * 1024:
[145]258            raise InvalidInput("Too much disk requested")
[144]259        if not canAddVm(user):
[145]260            raise InvalidInput("Too many VMs requested")
[113]261        res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')')
262        id = res.fetchone()[0]
263        machine = Machine()
264        machine.machine_id = id
265        machine.name = name
266        machine.memory = memory
267        machine.owner = user.username
268        machine.contact = user.email
269        machine.uuid = uuidToString(randomUUID())
270        machine.boot_off_cd = True
271        machine_type = Type.get_by(hvm=is_hvm)
272        machine.type_id = machine_type.type_id
273        ctx.current.save(machine)
274        disk = Disk(machine.machine_id, 
275                    'hda', disk)
276        open = NIC.select_by(machine_id=None)
277        if not open: #No IPs left!
[145]278            raise CodeError("No IP addresses left!  Contact sipb-xen-dev@mit.edu")
[113]279        nic = open[0]
280        nic.machine_id = machine.machine_id
281        nic.hostname = name
282        ctx.current.save(nic)   
283        ctx.current.save(disk)
284        transaction.commit()
285    except:
286        transaction.rollback()
287        raise
[144]288    registerMachine(machine)
[113]289    makeDisks()
290    # tell it to boot with cdrom
291    bootMachine(machine, cdrom)
292
293    return machine
294
[134]295def validMemory(user, memory, machine=None):
[145]296    """Parse and validate limits for memory for a given user and machine."""
[113]297    try:
298        memory = int(memory)
[144]299        if memory < MIN_MEMORY_SINGLE:
[113]300            raise ValueError
301    except ValueError:
[145]302        raise InvalidInput("Invalid memory amount; must be at least %s MB" %
[144]303                          MIN_MEMORY_SINGLE)
[134]304    if memory > maxMemory(user, machine):
[145]305        raise InvalidInput("Too much memory requested")
[134]306    return memory
307
308def validDisk(user, disk, machine=None):
[145]309    """Parse and validate limits for disk for a given user and machine."""
[113]310    try:
311        disk = float(disk)
[134]312        if disk > maxDisk(user, machine):
[145]313            raise InvalidInput("Too much disk requested")
[113]314        disk = int(disk * 1024)
[144]315        if disk < MIN_DISK_SINGLE * 1024:
[113]316            raise ValueError
317    except ValueError:
[145]318        raise InvalidInput("Invalid disk amount; minimum is %s GB" %
[144]319                          MIN_DISK_SINGLE)
[134]320    return disk
321
322def create(user, fields):
[145]323    """Handler for create requests."""
[134]324    name = fields.getfirst('name')
325    if not validMachineName(name):
[145]326        raise InvalidInput("Invalid name '%s'" % name)
[134]327    name = user.username + '_' + name.lower()
328
329    if Machine.get_by(name=name):
[145]330        raise InvalidInput("A machine named '%s' already exists" % name)
[113]331   
[134]332    memory = fields.getfirst('memory')
333    memory = validMemory(user, memory)
334   
335    disk = fields.getfirst('disk')
336    disk = validDisk(user, disk)
337
[113]338    vm_type = fields.getfirst('vmtype')
339    if vm_type not in ('hvm', 'paravm'):
[145]340        raise CodeError("Invalid vm type '%s'"  % vm_type)   
[113]341    is_hvm = (vm_type == 'hvm')
342
343    cdrom = fields.getfirst('cdrom')
344    if cdrom is not None and not CDROM.get(cdrom):
[145]345        raise CodeError("Invalid cdrom type '%s'" % cdrom)   
[113]346   
347    machine = createVm(user, name, memory, disk, is_hvm, cdrom)
348    d = dict(user=user,
349             machine=machine)
350    print Template(file='create.tmpl',
[139]351                   searchList=[d, global_dict]);
[113]352
353def listVms(user, fields):
[145]354    """Handler for list requests."""
[135]355    machines = [m for m in Machine.select() if haveAccess(user, m)]   
[133]356    on = {}
[119]357    has_vnc = {}
[133]358    uptimes = getUptimes(machines)
[136]359    on = uptimes
360    for m in machines:
[144]361        if not on[m]:
362            has_vnc[m] = 'Off'
[138]363        elif m.type.hvm:
[144]364            has_vnc[m] = True
[136]365        else:
[144]366            has_vnc[m] = "ParaVM"+helppopup("paravm_console")
[133]367    #     for m in machines:
368    #         status = statusInfo(m)
369    #         on[m.name] = status is not None
370    #         has_vnc[m.name] = hasVnc(status)
[144]371    max_mem=maxMemory(user, on=on)
372    max_disk=maxDisk(user)
[113]373    d = dict(user=user,
[144]374             can_add_vm=canAddVm(user, on=on),
375             max_mem=max_mem,
376             max_disk=max_disk,
377             default_mem=max_mem,
378             default_disk=min(4.0, max_disk),
[113]379             machines=machines,
[119]380             has_vnc=has_vnc,
[133]381             uptimes=uptimes,
[113]382             cdroms=CDROM.select())
[139]383    print Template(file='list.tmpl', searchList=[d, global_dict])
[113]384
385def testMachineId(user, machineId, exists=True):
[145]386    """Parse, validate and check authorization for a given machineId.
387
388    If exists is False, don't check that it exists.
389    """
[113]390    if machineId is None:
[145]391        raise CodeError("No machine ID specified")
[113]392    try:
393        machineId = int(machineId)
394    except ValueError:
[145]395        raise CodeError("Invalid machine ID '%s'" % machineId)
[113]396    machine = Machine.get(machineId)
397    if exists and machine is None:
[145]398        raise CodeError("No such machine ID '%s'" % machineId)
399    if machine is not None and not haveAccess(user, machine):
400        raise CodeError("No access to machine ID '%s'" % machineId)
[113]401    return machine
402
403def vnc(user, fields):
[119]404    """VNC applet page.
405
406    Note that due to same-domain restrictions, the applet connects to
407    the webserver, which needs to forward those requests to the xen
408    server.  The Xen server runs another proxy that (1) authenticates
409    and (2) finds the correct port for the VM.
410
411    You might want iptables like:
412
413    -t nat -A PREROUTING -s ! 18.181.0.60 -i eth1 -p tcp -m tcp --dport 10003 -j DNAT --to-destination 18.181.0.60:10003
414    -t nat -A POSTROUTING -d 18.181.0.60 -o eth1 -p tcp -m tcp --dport 10003 -j SNAT --to-source 18.187.7.142
415    -A FORWARD -d 18.181.0.60 -i eth1 -o eth1 -p tcp -m tcp --dport 10003 -j ACCEPT
[145]416
417    Remember to enable iptables!
418    echo 1 > /proc/sys/net/ipv4/ip_forward
[119]419    """
[113]420    machine = testMachineId(user, fields.getfirst('machine_id'))
[118]421   
422    TOKEN_KEY = "0M6W0U1IXexThi5idy8mnkqPKEq1LtEnlK/pZSn0cDrN"
423
424    data = {}
[133]425    data["user"] = user.username
[120]426    data["machine"]=machine.name
[118]427    data["expires"]=time.time()+(5*60)
428    pickledData = cPickle.dumps(data)
429    m = hmac.new(TOKEN_KEY, digestmod=sha)
430    m.update(pickledData)
431    token = {'data': pickledData, 'digest': m.digest()}
432    token = cPickle.dumps(token)
433    token = base64.urlsafe_b64encode(token)
434   
[113]435    d = dict(user=user,
436             machine=machine,
[119]437             hostname=os.environ.get('SERVER_NAME', 'localhost'),
[113]438             authtoken=token)
439    print Template(file='vnc.tmpl',
[139]440                   searchList=[d, global_dict])
[113]441
[133]442def getNicInfo(data_dict, machine):
[145]443    """Helper function for info, get data on nics for a machine.
444
445    Modifies data_dict to include the relevant data, and returns a list
446    of (key, name) pairs to display "name: data_dict[key]" to the user.
447    """
[133]448    data_dict['num_nics'] = len(machine.nics)
449    nic_fields_template = [('nic%s_hostname', 'NIC %s hostname'),
450                           ('nic%s_mac', 'NIC %s MAC Addr'),
451                           ('nic%s_ip', 'NIC %s IP'),
452                           ]
453    nic_fields = []
454    for i in range(len(machine.nics)):
455        nic_fields.extend([(x % i, y % i) for x, y in nic_fields_template])
456        data_dict['nic%s_hostname' % i] = machine.nics[i].hostname + '.servers.csail.mit.edu'
457        data_dict['nic%s_mac' % i] = machine.nics[i].mac_addr
458        data_dict['nic%s_ip' % i] = machine.nics[i].ip
459    if len(machine.nics) == 1:
460        nic_fields = [(x, y.replace('NIC 0 ', '')) for x, y in nic_fields]
461    return nic_fields
462
463def getDiskInfo(data_dict, machine):
[145]464    """Helper function for info, get data on disks for a machine.
465
466    Modifies data_dict to include the relevant data, and returns a list
467    of (key, name) pairs to display "name: data_dict[key]" to the user.
468    """
[133]469    data_dict['num_disks'] = len(machine.disks)
470    disk_fields_template = [('%s_size', '%s size')]
471    disk_fields = []
472    for disk in machine.disks:
473        name = disk.guest_device_name
474        disk_fields.extend([(x % name, y % name) for x, y in disk_fields_template])
475        data_dict['%s_size' % name] = "%0.1f GB" % (disk.size / 1024.)
476    return disk_fields
477
478def deleteVM(machine):
[145]479    """Delete a VM."""
[133]480    transaction = ctx.current.create_transaction()
481    delete_disk_pairs = [(machine.name, d.guest_device_name) for d in machine.disks]
482    try:
483        for nic in machine.nics:
484            nic.machine_id = None
485            nic.hostname = None
486            ctx.current.save(nic)
487        for disk in machine.disks:
488            ctx.current.delete(disk)
489        ctx.current.delete(machine)
490        transaction.commit()
491    except:
492        transaction.rollback()
493        raise
494    for mname, dname in delete_disk_pairs:
495        remctl('web', 'lvremove', mname, dname)
496    unregisterMachine(machine)
497
498def command(user, fields):
[145]499    """Handler for running commands like boot and delete on a VM."""
[133]500    print time.time()-start_time
501    machine = testMachineId(user, fields.getfirst('machine_id'))
502    action = fields.getfirst('action')
503    cdrom = fields.getfirst('cdrom')
504    print time.time()-start_time
505    if cdrom is not None and not CDROM.get(cdrom):
[145]506        raise CodeError("Invalid cdrom type '%s'" % cdrom)   
[133]507    if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown', 'Delete VM'):
[145]508        raise CodeError("Invalid action '%s'" % action)
[133]509    if action == 'Reboot':
510        if cdrom is not None:
511            remctl('reboot', machine.name, cdrom)
512        else:
513            remctl('reboot', machine.name)
514    elif action == 'Power on':
[144]515        if maxMemory(user) < machine.memory:
[145]516            raise InvalidInput("You don't have enough free RAM quota")
[133]517        bootMachine(machine, cdrom)
518    elif action == 'Power off':
519        remctl('destroy', machine.name)
520    elif action == 'Shutdown':
521        remctl('shutdown', machine.name)
522    elif action == 'Delete VM':
523        deleteVM(machine)
524    print time.time()-start_time
525
526    d = dict(user=user,
527             command=action,
528             machine=machine)
[139]529    print Template(file="command.tmpl", searchList=[d, global_dict])
[133]530       
531def modify(user, fields):
[145]532    """Handler for modifying attributes of a machine."""
533    #XXX not written yet
[133]534    machine = testMachineId(user, fields.getfirst('machine_id'))
535   
[139]536def help(user, fields):
[145]537    """Handler for help messages."""
[139]538    simple = fields.getfirst('simple')
539    subjects = fields.getlist('subject')
540   
541    mapping = dict(paravm_console="""
542ParaVM machines do not support console access over VNC.  To access
543these machines, you either need to boot with a liveCD and ssh in or
544hope that the sipb-xen maintainers add support for serial consoles.""",
545                   hvm_paravm="""
546HVM machines use the virtualization features of the processor, while
547ParaVM machines use Xen's emulation of virtualization features.  You
548want an HVM virtualized machine.""",
549                   cpu_weight="""Don't ask us!  We're as mystified as you are.""")
550   
551    d = dict(user=user,
552             simple=simple,
553             subjects=subjects,
554             mapping=mapping)
555   
556    print Template(file="help.tmpl", searchList=[d, global_dict])
557   
[133]558
[113]559def info(user, fields):
[145]560    """Handler for info on a single VM."""
[113]561    machine = testMachineId(user, fields.getfirst('machine_id'))
[133]562    status = statusInfo(machine)
563    has_vnc = hasVnc(status)
564    if status is None:
565        main_status = dict(name=machine.name,
566                           memory=str(machine.memory))
567    else:
568        main_status = dict(status[1:])
569    start_time = float(main_status.get('start_time', 0))
570    uptime = datetime.timedelta(seconds=int(time.time()-start_time))
571    cpu_time_float = float(main_status.get('cpu_time', 0))
572    cputime = datetime.timedelta(seconds=int(cpu_time_float))
573    display_fields = """name uptime memory state cpu_weight on_reboot
574     on_poweroff on_crash on_xend_start on_xend_stop bootloader""".split()
575    display_fields = [('name', 'Name'),
576                      ('owner', 'Owner'),
577                      ('contact', 'Contact'),
[136]578                      ('type', 'Type'),
[133]579                      'NIC_INFO',
580                      ('uptime', 'uptime'),
581                      ('cputime', 'CPU usage'),
582                      ('memory', 'RAM'),
583                      'DISK_INFO',
584                      ('state', 'state (xen format)'),
[139]585                      ('cpu_weight', 'CPU weight'+helppopup('cpu_weight')),
[133]586                      ('on_reboot', 'Action on VM reboot'),
587                      ('on_poweroff', 'Action on VM poweroff'),
588                      ('on_crash', 'Action on VM crash'),
589                      ('on_xend_start', 'Action on Xen start'),
590                      ('on_xend_stop', 'Action on Xen stop'),
591                      ('bootloader', 'Bootloader options'),
592                      ]
593    fields = []
594    machine_info = {}
[147]595    machine_info['name'] = machine.name
[136]596    machine_info['type'] = machine.type.hvm and 'HVM' or 'ParaVM'
[133]597    machine_info['owner'] = machine.owner
598    machine_info['contact'] = machine.contact
599
600    nic_fields = getNicInfo(machine_info, machine)
601    nic_point = display_fields.index('NIC_INFO')
602    display_fields = display_fields[:nic_point] + nic_fields + display_fields[nic_point+1:]
603
604    disk_fields = getDiskInfo(machine_info, machine)
605    disk_point = display_fields.index('DISK_INFO')
606    display_fields = display_fields[:disk_point] + disk_fields + display_fields[disk_point+1:]
607   
608    main_status['memory'] += ' MB'
609    for field, disp in display_fields:
610        if field in ('uptime', 'cputime'):
611            fields.append((disp, locals()[field]))
[147]612        elif field in machine_info:
613            fields.append((disp, machine_info[field]))
[133]614        elif field in main_status:
615            fields.append((disp, main_status[field]))
616        else:
617            pass
618            #fields.append((disp, None))
[144]619    max_mem = maxMemory(user, machine)
620    max_disk = maxDisk(user, machine)
[113]621    d = dict(user=user,
[133]622             cdroms=CDROM.select(),
623             on=status is not None,
624             machine=machine,
625             has_vnc=has_vnc,
626             uptime=str(uptime),
627             ram=machine.memory,
[144]628             max_mem=max_mem,
629             max_disk=max_disk,
[133]630             fields = fields)
[113]631    print Template(file='info.tmpl',
[139]632                   searchList=[d, global_dict])
[113]633
634mapping = dict(list=listVms,
635               vnc=vnc,
[133]636               command=command,
637               modify=modify,
[113]638               info=info,
[139]639               create=create,
640               help=help)
[113]641
642if __name__ == '__main__':
[133]643    start_time = time.time()
[113]644    fields = cgi.FieldStorage()
[133]645    class User:
[113]646        username = "moo"
647        email = 'moo@cow.com'
[133]648    u = User()
[140]649    if 'SSL_CLIENT_S_DN_Email' in os.environ:
650        username = os.environ[ 'SSL_CLIENT_S_DN_Email'].split("@")[0]
651        u.username = username
652        u.email = os.environ[ 'SSL_CLIENT_S_DN_Email']
653    else:
[144]654        u.username = 'moo'
655        u.email = 'nobody'
[140]656    connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
[113]657    operation = os.environ.get('PATH_INFO', '')
[140]658    #print 'Content-Type: text/plain\n'
659    #print operation
[119]660    if not operation:
[140]661        print "Status: 301 Moved Permanently"
662        print 'Location: ' + os.environ['SCRIPT_NAME']+'/\n'
663        sys.exit(0)
664    print 'Content-Type: text/html\n'
[119]665
[113]666    if operation.startswith('/'):
667        operation = operation[1:]
668    if not operation:
669        operation = 'list'
670   
671    fun = mapping.get(operation, 
672                      lambda u, e:
673                          error(operation, u, e,
[133]674                                "Invalid operation '%s'" % operation))
[139]675    if fun not in (help, ):
676        connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
[119]677    try:
678        fun(u, fields)
[145]679    except CodeError, err:
[119]680        error(operation, u, fields, err)
[145]681    except InvalidInput, err:
682        error(operation, u, fields, err)
Note: See TracBrowser for help on using the repository browser.