1 | #!/usr/bin/python |
---|
2 | """Main CGI script for web interface""" |
---|
3 | |
---|
4 | import base64 |
---|
5 | import cPickle |
---|
6 | import cgi |
---|
7 | import datetime |
---|
8 | import hmac |
---|
9 | import random |
---|
10 | import sha |
---|
11 | import simplejson |
---|
12 | import sys |
---|
13 | import time |
---|
14 | import urllib |
---|
15 | import socket |
---|
16 | import cherrypy |
---|
17 | from StringIO import StringIO |
---|
18 | def 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 | |
---|
27 | def printError(): |
---|
28 | """Revert stderr to stdout, and print the contents of stderr""" |
---|
29 | if isinstance(sys.stderr, StringIO): |
---|
30 | print revertStandardError() |
---|
31 | |
---|
32 | if __name__ == '__main__': |
---|
33 | import atexit |
---|
34 | atexit.register(printError) |
---|
35 | |
---|
36 | import templates |
---|
37 | from Cheetah.Template import Template |
---|
38 | import validation |
---|
39 | import cache_acls |
---|
40 | from webcommon import State |
---|
41 | import controls |
---|
42 | from getafsgroups import getAfsGroupMembers |
---|
43 | from invirt import database |
---|
44 | from invirt.database import Machine, CDROM, session, connect, MachineAccess, Type, Autoinstall |
---|
45 | from invirt.config import structs as config |
---|
46 | from invirt.common import InvalidInput, CodeError |
---|
47 | |
---|
48 | from view import View |
---|
49 | |
---|
50 | class 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': """ |
---|
85 | The autoinstaller builds a minimal Debian or Ubuntu system to run as a |
---|
86 | ParaVM. You can access the resulting system by logging into the <a |
---|
87 | href="help?simple=true&subject=ParaVM+Console">serial console server</a> |
---|
88 | with your Kerberos tickets; there is no root password so sshd will |
---|
89 | refuse login.</p> |
---|
90 | |
---|
91 | <p>Under the covers, the autoinstaller uses our own patched version of |
---|
92 | xen-create-image, which is a tool based on debootstrap. If you log |
---|
93 | into the serial console while the install is running, you can watch |
---|
94 | it. |
---|
95 | """, |
---|
96 | 'ParaVM Console': """ |
---|
97 | ParaVM machines do not support local console access over VNC. To |
---|
98 | access the serial console of these machines, you can SSH with Kerberos |
---|
99 | to %s, using the name of the machine as your |
---|
100 | username.""" % config.console.hostname, |
---|
101 | 'HVM/ParaVM': """ |
---|
102 | HVM machines use the virtualization features of the processor, while |
---|
103 | ParaVM machines rely on a modified kernel to communicate directly with |
---|
104 | the hypervisor. HVMs support boot CDs of any operating system, and |
---|
105 | the VNC console applet. The three-minute autoinstaller produces |
---|
106 | ParaVMs. 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 |
---|
110 | href="https://xvm.scripts.mit.edu/wiki/Paravirtualization">on the |
---|
111 | wiki</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': """ |
---|
117 | Don't ask us! We're as mystified as you are.""", |
---|
118 | 'Owner': """ |
---|
119 | The owner field is used to determine <a |
---|
120 | href="help?subject=Quotas">quotas</a>. It must be the name of a |
---|
121 | locker that you are an AFS administrator of. In particular, you or an |
---|
122 | AFS group you are a member of must have AFS rlidwka bits on the |
---|
123 | locker. You can check who administers the LOCKER locker using the |
---|
124 | commands 'attach LOCKER; fs la /mit/LOCKER' on Athena.) See also <a |
---|
125 | href="help?subject=Administrator">administrator</a>.""", |
---|
126 | 'Administrator': """ |
---|
127 | The administrator field determines who can access the console and |
---|
128 | power on and off the machine. This can be either a user or a moira |
---|
129 | group.""", |
---|
130 | 'Quotas': """ |
---|
131 | Quotas are determined on a per-locker basis. Each locker may have a |
---|
132 | maximum of 512 mebibytes of active ram, 50 gibibytes of disk, and 4 |
---|
133 | active machines.""", |
---|
134 | 'Console': """ |
---|
135 | <strong>Framebuffer:</strong> At a Linux boot prompt in your VM, try |
---|
136 | setting <tt>fb=false</tt> to disable the framebuffer. If you don't, |
---|
137 | your machine will run just fine, but the applet's display of the |
---|
138 | console 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 | |
---|
162 | def 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 | |
---|
170 | class 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 | |
---|
183 | checkpoint = Checkpoint() |
---|
184 | |
---|
185 | def 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 | |
---|
193 | Template.database = database |
---|
194 | Template.config = config |
---|
195 | Template.err = None |
---|
196 | |
---|
197 | class 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 | |
---|
214 | class 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 | |
---|
234 | DEFAULT_HEADERS = {'Content-Type': 'text/html'} |
---|
235 | |
---|
236 | def 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 | |
---|
243 | def 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 | |
---|
253 | def 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 | |
---|
261 | def 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 | |
---|
281 | def 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"+helppopup("ParaVM Console") |
---|
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 | |
---|
323 | def 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 | |
---|
364 | def 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 | |
---|
380 | def 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 | |
---|
401 | def 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 | |
---|
417 | def 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 | |
---|
446 | def 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 | |
---|
505 | def 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 | |
---|
524 | def badOperation(u, s, p, e): |
---|
525 | """Function called when accessing an unknown URI.""" |
---|
526 | return ({'Status': '404 Not Found'}, 'Invalid operation.') |
---|
527 | |
---|
528 | def 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 | ('cpu_weight', 'CPU weight'+helppopup('CPU Weight')), |
---|
560 | ] |
---|
561 | fields = [] |
---|
562 | machine_info = {} |
---|
563 | machine_info['name'] = machine.name |
---|
564 | machine_info['description'] = machine.description |
---|
565 | machine_info['type'] = machine.type.hvm and 'HVM' or 'ParaVM' |
---|
566 | machine_info['owner'] = machine.owner |
---|
567 | machine_info['administrator'] = machine.administrator |
---|
568 | machine_info['contact'] = machine.contact |
---|
569 | |
---|
570 | nic_fields = getNicInfo(machine_info, machine) |
---|
571 | nic_point = display_fields.index('NIC_INFO') |
---|
572 | display_fields = (display_fields[:nic_point] + nic_fields + |
---|
573 | display_fields[nic_point+1:]) |
---|
574 | |
---|
575 | disk_fields = getDiskInfo(machine_info, machine) |
---|
576 | disk_point = display_fields.index('DISK_INFO') |
---|
577 | display_fields = (display_fields[:disk_point] + disk_fields + |
---|
578 | display_fields[disk_point+1:]) |
---|
579 | |
---|
580 | main_status['memory'] += ' MiB' |
---|
581 | for field, disp in display_fields: |
---|
582 | if field in ('uptime', 'cputime') and locals()[field] is not None: |
---|
583 | fields.append((disp, locals()[field])) |
---|
584 | elif field in machine_info: |
---|
585 | fields.append((disp, machine_info[field])) |
---|
586 | elif field in main_status: |
---|
587 | fields.append((disp, main_status[field])) |
---|
588 | else: |
---|
589 | pass |
---|
590 | #fields.append((disp, None)) |
---|
591 | |
---|
592 | checkpoint.checkpoint('Got fields') |
---|
593 | |
---|
594 | |
---|
595 | max_mem = validation.maxMemory(machine.owner, state, machine, False) |
---|
596 | checkpoint.checkpoint('Got mem') |
---|
597 | max_disk = validation.maxDisk(machine.owner, machine) |
---|
598 | defaults = Defaults() |
---|
599 | for name in 'machine_id name description administrator owner memory contact'.split(): |
---|
600 | setattr(defaults, name, getattr(machine, name)) |
---|
601 | defaults.type = machine.type.type_id |
---|
602 | defaults.disk = "%0.2f" % (machine.disks[0].size/1024.) |
---|
603 | checkpoint.checkpoint('Got defaults') |
---|
604 | d = dict(user=username, |
---|
605 | on=status is not None, |
---|
606 | machine=machine, |
---|
607 | defaults=defaults, |
---|
608 | has_vnc=has_vnc, |
---|
609 | uptime=str(uptime), |
---|
610 | ram=machine.memory, |
---|
611 | max_mem=max_mem, |
---|
612 | max_disk=max_disk, |
---|
613 | owner_help=helppopup("Owner"), |
---|
614 | fields = fields) |
---|
615 | return d |
---|
616 | |
---|
617 | def unauthFront(_, _2, _3, fields): |
---|
618 | """Information for unauth'd users.""" |
---|
619 | return templates.unauth(searchList=[{'simple' : True, |
---|
620 | 'hostname' : socket.getfqdn()}]) |
---|
621 | |
---|
622 | def admin(username, state, path, fields): |
---|
623 | if path == '': |
---|
624 | return ({'Status': '303 See Other', |
---|
625 | 'Location': 'admin/'}, |
---|
626 | "You shouldn't see this message.") |
---|
627 | if not username in getAfsGroupMembers(config.adminacl, 'athena.mit.edu'): |
---|
628 | raise InvalidInput('username', username, |
---|
629 | 'Not in admin group %s.' % config.adminacl) |
---|
630 | newstate = State(username, isadmin=True) |
---|
631 | newstate.environ = state.environ |
---|
632 | return handler(username, newstate, path, fields) |
---|
633 | |
---|
634 | def throwError(_, __, ___, ____): |
---|
635 | """Throw an error, to test the error-tracing mechanisms.""" |
---|
636 | raise RuntimeError("test of the emergency broadcast system") |
---|
637 | |
---|
638 | mapping = dict(vnc=vnc, |
---|
639 | command=command, |
---|
640 | modify=modify, |
---|
641 | create=create, |
---|
642 | unauth=unauthFront, |
---|
643 | admin=admin, |
---|
644 | overlord=admin, |
---|
645 | errortest=throwError) |
---|
646 | |
---|
647 | def printHeaders(headers): |
---|
648 | """Print a dictionary as HTTP headers.""" |
---|
649 | for key, value in headers.iteritems(): |
---|
650 | print '%s: %s' % (key, value) |
---|
651 | print |
---|
652 | |
---|
653 | def send_error_mail(subject, body): |
---|
654 | import subprocess |
---|
655 | |
---|
656 | to = config.web.errormail |
---|
657 | mail = """To: %s |
---|
658 | From: root@%s |
---|
659 | Subject: %s |
---|
660 | |
---|
661 | %s |
---|
662 | """ % (to, config.web.hostname, subject, body) |
---|
663 | p = subprocess.Popen(['/usr/sbin/sendmail', '-f', to, to], |
---|
664 | stdin=subprocess.PIPE) |
---|
665 | p.stdin.write(mail) |
---|
666 | p.stdin.close() |
---|
667 | p.wait() |
---|
668 | |
---|
669 | def show_error(op, username, fields, err, emsg, traceback): |
---|
670 | """Print an error page when an exception occurs""" |
---|
671 | d = dict(op=op, user=username, fields=fields, |
---|
672 | errorMessage=str(err), stderr=emsg, traceback=traceback) |
---|
673 | details = templates.error_raw(searchList=[d]) |
---|
674 | exclude = config.web.errormail_exclude |
---|
675 | if username not in exclude and '*' not in exclude: |
---|
676 | send_error_mail('xvm error on %s for %s: %s' % (op, username, err), |
---|
677 | details) |
---|
678 | d['details'] = details |
---|
679 | return templates.error(searchList=[d]) |
---|
680 | |
---|
681 | def handler(username, state, path, fields): |
---|
682 | operation, path = pathSplit(path) |
---|
683 | if not operation: |
---|
684 | operation = 'list' |
---|
685 | print 'Starting', operation |
---|
686 | fun = mapping.get(operation, badOperation) |
---|
687 | return fun(username, state, path, fields) |
---|
688 | |
---|
689 | class App: |
---|
690 | def __init__(self, environ, start_response): |
---|
691 | self.environ = environ |
---|
692 | self.start = start_response |
---|
693 | |
---|
694 | self.username = getUser(environ) |
---|
695 | self.state = State(self.username) |
---|
696 | self.state.environ = environ |
---|
697 | |
---|
698 | random.seed() #sigh |
---|
699 | |
---|
700 | def __iter__(self): |
---|
701 | start_time = time.time() |
---|
702 | database.clear_cache() |
---|
703 | sys.stderr = StringIO() |
---|
704 | fields = cgi.FieldStorage(fp=self.environ['wsgi.input'], environ=self.environ) |
---|
705 | operation = self.environ.get('PATH_INFO', '') |
---|
706 | if not operation: |
---|
707 | self.start("301 Moved Permanently", [('Location', './')]) |
---|
708 | return |
---|
709 | if self.username is None: |
---|
710 | operation = 'unauth' |
---|
711 | |
---|
712 | try: |
---|
713 | checkpoint.checkpoint('Before') |
---|
714 | output = handler(self.username, self.state, operation, fields) |
---|
715 | checkpoint.checkpoint('After') |
---|
716 | |
---|
717 | headers = dict(DEFAULT_HEADERS) |
---|
718 | if isinstance(output, tuple): |
---|
719 | new_headers, output = output |
---|
720 | headers.update(new_headers) |
---|
721 | e = revertStandardError() |
---|
722 | if e: |
---|
723 | if hasattr(output, 'addError'): |
---|
724 | output.addError(e) |
---|
725 | else: |
---|
726 | # This only happens on redirects, so it'd be a pain to get |
---|
727 | # the message to the user. Maybe in the response is useful. |
---|
728 | output = output + '\n\nstderr:\n' + e |
---|
729 | output_string = str(output) |
---|
730 | checkpoint.checkpoint('output as a string') |
---|
731 | except Exception, err: |
---|
732 | if not fields.has_key('js'): |
---|
733 | if isinstance(err, InvalidInput): |
---|
734 | self.start('200 OK', [('Content-Type', 'text/html')]) |
---|
735 | e = revertStandardError() |
---|
736 | yield str(invalidInput(operation, self.username, fields, |
---|
737 | err, e)) |
---|
738 | return |
---|
739 | import traceback |
---|
740 | self.start('500 Internal Server Error', |
---|
741 | [('Content-Type', 'text/html')]) |
---|
742 | e = revertStandardError() |
---|
743 | s = show_error(operation, self.username, fields, |
---|
744 | err, e, traceback.format_exc()) |
---|
745 | yield str(s) |
---|
746 | return |
---|
747 | status = headers.setdefault('Status', '200 OK') |
---|
748 | del headers['Status'] |
---|
749 | self.start(status, headers.items()) |
---|
750 | yield output_string |
---|
751 | if fields.has_key('timedebug'): |
---|
752 | yield '<pre>%s</pre>' % cgi.escape(str(checkpoint)) |
---|
753 | |
---|
754 | def constructor(): |
---|
755 | connect() |
---|
756 | return App |
---|
757 | |
---|
758 | def main(): |
---|
759 | from flup.server.fcgi_fork import WSGIServer |
---|
760 | WSGIServer(constructor()).run() |
---|
761 | |
---|
762 | if __name__ == '__main__': |
---|
763 | main() |
---|