- return '<span class="helplink"><a href="help?subject='+subj+'&simple=true" target="_blank" onclick="return helppopup(\''+subj+'\')">(?)</a></span>'
-
-
-global_dict = {}
-global_dict['helppopup'] = helppopup
-
-
-# ... and stolen from xend/uuid.py
-def randomUUID():
- """Generate a random UUID."""
-
- return [ random.randint(0, 255) for _ in range(0, 16) ]
-
-def uuidToString(u):
- """Turn a numeric UUID to a hyphen-seperated one."""
- return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
- "%02x" * 6]) % tuple(u)
-
-MAX_MEMORY_TOTAL = 512
-MAX_MEMORY_SINGLE = 256
-MIN_MEMORY_SINGLE = 16
-MAX_DISK_TOTAL = 50
-MAX_DISK_SINGLE = 50
-MIN_DISK_SINGLE = 0.1
-MAX_VMS_TOTAL = 10
-MAX_VMS_ACTIVE = 4
-
-def getMachinesByOwner(owner):
- """Return the machines owned by a given owner."""
- return Machine.select_by(owner=owner)
-
-def maxMemory(user, machine=None):
- """Return the maximum memory for a machine or a user.
-
- If machine is None, return the memory available for a new
- machine. Else, return the maximum that machine can have.
-
- on is a dictionary from machines to booleans, whether a machine is
- on. If None, it is recomputed. XXX make this global?
- """
-
- machines = getMachinesByOwner(user.username)
- active_machines = [x for x in machines if g.uptimes[x]]
- mem_usage = sum([x.memory for x in active_machines if x != machine])
- return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage)
-
-def maxDisk(user, machine=None):
- machines = getMachinesByOwner(user.username)
- disk_usage = sum([sum([y.size for y in x.disks])
- for x in machines if x != machine])
- return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.)
-
-def canAddVm(user):
- machines = getMachinesByOwner(user.username)
- active_machines = [x for x in machines if g.uptimes[x]]
- return (len(machines) < MAX_VMS_TOTAL and
- len(active_machines) < MAX_VMS_ACTIVE)
-
-def haveAccess(user, machine):
- """Return whether a user has access to a machine"""
- if user.username == 'moo':
- return True
- return machine.owner == user.username
-
-def error(op, user, fields, err):
- """Print an error page when a CodeError occurs"""
- d = dict(op=op, user=user, errorMessage=str(err))
- print Template(file='error.tmpl', searchList=[d, global_dict]);
-
-def validMachineName(name):
- """Check that name is valid for a machine name"""
- if not name:
- return False
- charset = string.ascii_letters + string.digits + '-_'
- if name[0] in '-_' or len(name) > 22:
- return False
- for x in name:
- if x not in charset:
- return False
- return True
-
-def kinit(username = 'tabbott/extra', keytab = '/etc/tabbott.keytab'):
- """Kinit with a given username and keytab"""
-
- p = subprocess.Popen(['kinit', "-k", "-t", keytab, username],
- stderr=subprocess.PIPE)
- e = p.wait()
- if e:
- raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
-
-def checkKinit():
- """If we lack tickets, kinit."""
- p = subprocess.Popen(['klist', '-s'])
- if p.wait():
- kinit()
-
-def remctl(*args, **kws):
- """Perform a remctl and return the output.
-
- kinits if necessary, and outputs errors to stderr.
- """
- checkKinit()
- p = subprocess.Popen(['remctl', 'black-mesa.mit.edu']
- + list(args),
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- if kws.get('err'):
- p.wait()
- return p.stdout.read(), p.stderr.read()
- if p.wait():
- raise CodeError('ERROR on remctl %s: %s' %
- (args, p.stderr.read()))
- return p.stdout.read()
-
-def lvcreate(machine, disk):
- """Create a single disk for a machine"""
- remctl('web', 'lvcreate', machine.name,
- disk.guest_device_name, str(disk.size))
-
-def makeDisks(machine):
- """Update the lvm partitions to add a disk."""
- for disk in machine.disks:
- lvcreate(machine, disk)
-
-def bootMachine(machine, cdtype):
- """Boot a machine with a given boot CD.
-
- If cdtype is None, give no boot cd. Otherwise, it is the string
- id of the CD (e.g. 'gutsy_i386')
- """
- if cdtype is not None:
- remctl('web', 'vmboot', machine.name,
- cdtype)