source: trunk/packages/invirt-web/code/controls.py @ 2804

Last change on this file since 2804 was 2803, checked in by broder, 14 years ago

Add more user-friendly error handling for common errors, so they don't
send us e-mail. (LP: #307296)

File size: 10.5 KB
RevLine 
[209]1import validation
[1612]2from invirt.common import CodeError, InvalidInput
[209]3import random
4import sys
5import time
6import re
[265]7import cache_acls
[550]8import yaml
[209]9
[863]10from invirt.config import structs as config
[1001]11from invirt.database import Machine, Disk, Type, NIC, CDROM, session, meta
[1614]12from invirt.remctl import remctl as gen_remctl
[863]13
[209]14# ... and stolen from xend/uuid.py
15def randomUUID():
16    """Generate a random UUID."""
17
18    return [ random.randint(0, 255) for _ in range(0, 16) ]
19
20def uuidToString(u):
21    """Turn a numeric UUID to a hyphen-seperated one."""
22    return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
23                     "%02x" * 6]) % tuple(u)
24# end stolen code
25
[1614]26def remctl(*args, **kwargs):
[1618]27    return gen_remctl(config.remote.hostname,
[1614]28                      principal='daemon/'+config.web.hostname,
[1618]29                      *args, **kwargs)
[1614]30
[209]31def lvcreate(machine, disk):
32    """Create a single disk for a machine"""
33    remctl('web', 'lvcreate', machine.name,
34           disk.guest_device_name, str(disk.size))
35   
36def makeDisks(machine):
37    """Update the lvm partitions to add a disk."""
38    for disk in machine.disks:
39        lvcreate(machine, disk)
40
[629]41def getswap(disksize, memsize):
42    """Returns the recommended swap partition size."""
43    return int(min(disksize / 4, memsize * 1.5))
44
45def lvinstall(machine, autoinstall):
46    disksize = machine.disks[0].size
47    memsize = machine.memory
[1096]48    swapsize = getswap(disksize, memsize)
49    imagesize = disksize - swapsize
[629]50    ip = machine.nics[0].ip
[1096]51    remctl('control', machine.name, 'install', 
52           'dist=%s' % autoinstall.distribution,
53           'mirror=%s' % autoinstall.mirror,
[1695]54           'arch=%s' % autoinstall.arch,
[1096]55           'imagesize=%s' % imagesize)
[629]56
[340]57def lvcopy(machine_orig_name, machine, rootpw):
58    """Copy a golden image onto a machine's disk"""
59    remctl('web', 'lvcopy', machine_orig_name, machine.name, rootpw)
60
[209]61def bootMachine(machine, cdtype):
62    """Boot a machine with a given boot CD.
63
64    If cdtype is None, give no boot cd.  Otherwise, it is the string
65    id of the CD (e.g. 'gutsy_i386')
66    """
[2097]67    if cdtype is not None:
68        out, err = remctl('control', machine.name, 'create', 
69                          cdtype, err=True)
70    else:
71        out, err = remctl('control', machine.name, 'create',
72                          err=True)
73    if 'already running' in err:
74        raise InvalidInput('action', 'create',
75                           'VM %s is already on' % machine.name)
[2803]76    elif 'I need' in err and 'but dom0_min_mem is' in err:
77        raise InvalidInput('action', 'create',
78                           "We're really sorry, but our servers don't have enough capacity to create your VM right now. Try creating a VM with less RAM, or shutting down another VM of yours. Feel free to ask %s if you would like to know when we plan to have more resources." % (config.contact))
79    elif ('Booting VMs is temporarily disabled for maintenance, sorry' in err or
80          'LVM operations are temporarily disabled for maintenance, sorry' in err):
81        raise InvalidInput('action', 'create',
82                           err)
83    elif "Boot loader didn't return any data!" in err:
84        raise InvalidInput('action', 'create',
85                           "The ParaVM bootloader was unable to find an operating system to boot. Do you have GRUB configured correctly?")
86    elif 'xc_dom_find_loader: no loader found' in err:
87        raise InvalidInput('action', 'create',
88                           "The ParaVM bootloader was unable to boot the kernel you have configured. Are you sure this kernel is capable of running as a Xen ParaVM guest?")
[2097]89    elif err:
[261]90        raise CodeError('"%s" on "control %s create %s' 
91                        % (err, machine.name, cdtype))
[209]92
[629]93def createVm(username, state, owner, contact, name, description, memory, disksize, machine_type, cdrom, autoinstall):
[209]94    """Create a VM and put it in the database"""
95    # put stuff in the table
[1013]96    session.begin()
[209]97    try:
[609]98        validation.Validate(username, state, name=name, description=description, owner=owner, memory=memory, disksize=disksize/1024.)
[209]99        machine = Machine()
100        machine.name = name
[609]101        machine.description = description
[209]102        machine.memory = memory
[228]103        machine.owner = owner
[1709]104        machine.administrator = None
[228]105        machine.contact = contact
[209]106        machine.uuid = uuidToString(randomUUID())
107        machine.boot_off_cd = True
[1013]108        machine.type = machine_type
109        session.save_or_update(machine)
110        disk = Disk(machine=machine,
[572]111                    guest_device_name='hda', size=disksize)
[2211]112        nic = NIC.query().filter_by(machine_id=None).filter_by(reusable=True).first()
[1013]113        if not nic: #No IPs left!
[209]114            raise CodeError("No IP addresses left!  "
[879]115                            "Contact %s." % config.web.errormail)
[1013]116        nic.machine = machine
[209]117        nic.hostname = name
[1013]118        session.save_or_update(nic)
119        session.save_or_update(disk)
[265]120        cache_acls.refreshMachine(machine)
[2295]121        makeDisks(machine)
[1013]122        session.commit()
[209]123    except:
[1013]124        session.rollback()
[209]125        raise
[2020]126    try:
127        if autoinstall:
128            lvinstall(machine, autoinstall)
129        else:
130            # tell it to boot with cdrom
131            bootMachine(machine, cdrom)
132    except CodeError, e:
133        deleteVM(machine)
134        raise
[209]135    return machine
136
[554]137def getList():
138    """Return a dictionary mapping machine names to dicts."""
[550]139    value_string = remctl('web', 'listvms')
[574]140    value_dict = yaml.load(value_string, yaml.CSafeLoader)
[554]141    return value_dict
[209]142
143def parseStatus(s):
144    """Parse a status string into nested tuples of strings.
145
146    s = output of xm list --long <machine_name>
147    """
148    values = re.split('([()])', s)
149    stack = [[]]
150    for v in values[2:-2]: #remove initial and final '()'
151        if not v:
152            continue
153        v = v.strip()
154        if v == '(':
155            stack.append([])
156        elif v == ')':
157            if len(stack[-1]) == 1:
158                stack[-1].append('')
159            stack[-2].append(stack[-1])
160            stack.pop()
161        else:
162            if not v:
163                continue
164            stack[-1].extend(v.split())
165    return stack[-1]
166
167def statusInfo(machine):
168    """Return the status list for a given machine.
169
170    Gets and parses xm list --long
171    """
[2097]172    value_string, err_string = remctl('control', machine.name, 'list-long', 
173                                      err=True)
174    if 'Unknown command' in err_string:
175        raise CodeError("ERROR in remctl list-long %s is not registered" % 
176                        (machine.name,))
177    elif 'is not on' in err_string:
178        return None
179    elif err_string:
180        raise CodeError("ERROR in remctl list-long %s%s" % 
181                        (machine.name, err_string))
[209]182    status = parseStatus(value_string)
183    return status
184
[662]185def listHost(machine):
186    """Return the host a machine is running on"""
[2097]187    out, err = remctl('control', machine.name, 'listhost', err=True)
188    if err:
[662]189        return None
[666]190    return out.strip()
[662]191
[1615]192def vnctoken(machine):
193    """Return a time-stamped VNC token"""
[2097]194    out, err = remctl('control', machine.name, 'vnctoken', err=True)
195    if err:
[1615]196        return None
197    return out.strip()
198
[209]199def deleteVM(machine):
200    """Delete a VM."""
[2097]201    remctl('control', machine.name, 'destroy', err=True)
[1013]202    session.begin()
[209]203    delete_disk_pairs = [(machine.name, d.guest_device_name) 
204                         for d in machine.disks]
205    try:
[1013]206        for mname, dname in delete_disk_pairs:
207            remctl('web', 'lvremove', mname, dname)
[209]208        for nic in machine.nics:
209            nic.machine_id = None
210            nic.hostname = None
[1013]211            session.save_or_update(nic)
[209]212        for disk in machine.disks:
[1013]213            session.delete(disk)
214        session.delete(machine)
215        session.commit()
[209]216    except:
[1013]217        session.rollback()
[209]218        raise
219
[2737]220def commandResult(username, state, command_name, machine_id, fields):
[209]221    start_time = 0
[2737]222    machine = validation.Validate(username, state, machine_id=machine_id).machine
223    action = command_name
224    cdrom = fields.get('cdrom') or None
[1074]225    if cdrom is not None and not CDROM.query().filter_by(cdrom_id=cdrom).one():
[209]226        raise CodeError("Invalid cdrom type '%s'" % cdrom)   
[2737]227    if action not in "reboot create destroy shutdown delete".split(" "):
[209]228        raise CodeError("Invalid action '%s'" % action)
[2737]229    if action == 'reboot':
[2097]230        if cdrom is not None:
231            out, err = remctl('control', machine.name, 'reboot', cdrom,
232                              err=True)
233        else:
234            out, err = remctl('control', machine.name, 'reboot',
235                              err=True)
236        if err:
237            if re.match("machine '.*' is not on", err):
[209]238                raise InvalidInput("action", "reboot", 
239                                   "Machine is not on")
240            else:
[2097]241                print >> sys.stderr, 'Error on reboot:'
242                print >> sys.stderr, err
243                raise CodeError('ERROR on remctl')
[209]244               
[2737]245    elif action == 'create':
[572]246        if validation.maxMemory(username, state, machine) < machine.memory:
[209]247            raise InvalidInput('action', 'Power on',
248                               "You don't have enough free RAM quota "
249                               "to turn on this machine.")
250        bootMachine(machine, cdrom)
[2737]251    elif action == 'destroy':
[2097]252        out, err = remctl('control', machine.name, 'destroy', err=True)
253        if err:
254            if re.match("machine '.*' is not on", err):
[209]255                raise InvalidInput("action", "Power off", 
256                                   "Machine is not on.")
257            else:
[2097]258                print >> sys.stderr, 'Error on power off:'
259                print >> sys.stderr, err
260                raise CodeError('ERROR on remctl')
[2737]261    elif action == 'shutdown':
[2097]262        out, err = remctl('control', machine.name, 'shutdown', err=True)
263        if err:
264            if re.match("machine '.*' is not on", err):
[209]265                raise InvalidInput("action", "Shutdown", 
266                                   "Machine is not on.")
267            else:
[2097]268                print >> sys.stderr, 'Error on Shutdown:'
269                print >> sys.stderr, err
270                raise CodeError('ERROR on remctl')
[2737]271    elif action == 'delete':
[209]272        deleteVM(machine)
273
[572]274    d = dict(user=username,
[209]275             command=action,
276             machine=machine)
277    return d
278
279def resizeDisk(machine_name, disk_name, new_size):
280    remctl("web", "lvresize", machine_name, disk_name, new_size)
281
282def renameMachine(machine, old_name, new_name):
283    for disk in machine.disks:
284        remctl("web", "lvrename", old_name, 
285               disk.guest_device_name, new_name)
286   
Note: See TracBrowser for help on using the repository browser.