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