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