source: trunk/packages/sipb-xen-www/code/controls.py @ 543

Last change on this file since 543 was 543, checked in by broder, 16 years ago

Are we xvm now? Really? Make it so

File size: 9.6 KB
Line 
1"""
2Functions to perform remctls.
3"""
4
5from sipb_xen_database import Machine, Disk, Type, NIC, CDROM, ctx, meta
6import validation
7from webcommon import CodeError, InvalidInput
8import random
9import subprocess
10import sys
11import time
12import re
13import cache_acls
14import cPickle
15
16# ... and stolen from xend/uuid.py
17def randomUUID():
18    """Generate a random UUID."""
19
20    return [ random.randint(0, 255) for _ in range(0, 16) ]
21
22def uuidToString(u):
23    """Turn a numeric UUID to a hyphen-seperated one."""
24    return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
25                     "%02x" * 6]) % tuple(u)
26# end stolen code
27
28def kinit(username = 'daemon/sipb-xen.mit.edu', keytab = '/etc/sipb-xen.keytab'):
29    """Kinit with a given username and keytab"""
30
31    p = subprocess.Popen(['kinit', "-k", "-t", keytab, username],
32                         stderr=subprocess.PIPE)
33    e = p.wait()
34    if e:
35        raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
36
37def checkKinit():
38    """If we lack tickets, kinit."""
39    p = subprocess.Popen(['klist', '-s'])
40    if p.wait():
41        kinit()
42
43def remctl(*args, **kws):
44    """Perform a remctl and return the output.
45
46    kinits if necessary, and outputs errors to stderr.
47    """
48    checkKinit()
49    p = subprocess.Popen(['remctl', 'remote.mit.edu']
50                         + list(args),
51                         stdout=subprocess.PIPE,
52                         stderr=subprocess.PIPE)
53    v = p.wait()
54    if kws.get('err'):
55        return p.stdout.read(), p.stderr.read()
56    if v:
57        print >> sys.stderr, 'Error', v, 'on remctl', args, ':'
58        print >> sys.stderr, p.stderr.read()
59        raise CodeError('ERROR on remctl')
60    return p.stdout.read()
61
62def lvcreate(machine, disk):
63    """Create a single disk for a machine"""
64    remctl('web', 'lvcreate', machine.name,
65           disk.guest_device_name, str(disk.size))
66   
67def makeDisks(machine):
68    """Update the lvm partitions to add a disk."""
69    for disk in machine.disks:
70        lvcreate(machine, disk)
71
72def lvcopy(machine_orig_name, machine, rootpw):
73    """Copy a golden image onto a machine's disk"""
74    remctl('web', 'lvcopy', machine_orig_name, machine.name, rootpw)
75
76def bootMachine(machine, cdtype):
77    """Boot a machine with a given boot CD.
78
79    If cdtype is None, give no boot cd.  Otherwise, it is the string
80    id of the CD (e.g. 'gutsy_i386')
81    """
82    if cdtype is not None:
83        out, err = remctl('control', machine.name, 'create', 
84                          cdtype, err=True)
85    else:
86        out, err = remctl('control', machine.name, 'create',
87                          err=True)
88    if 'already exists' in out:
89        raise InvalidInput('action', 'create',
90                           'VM %s is already on' % machine.name)
91    elif err:
92        raise CodeError('"%s" on "control %s create %s' 
93                        % (err, machine.name, cdtype))
94
95def createVm(owner, contact, name, memory, disk_size, machine_type, cdrom, clone_from):
96    """Create a VM and put it in the database"""
97    # put stuff in the table
98    transaction = ctx.current.create_transaction()
99    try:
100        validation.validMemory(owner, memory)
101        validation.validDisk(owner, disk_size  * 1. / 1024)
102        validation.validAddVm(owner)
103        res = meta.engine.execute('select nextval('
104                                  '\'"machines_machine_id_seq"\')')
105        id = res.fetchone()[0]
106        machine = Machine()
107        machine.machine_id = id
108        machine.name = name
109        machine.memory = memory
110        machine.owner = owner
111        machine.administrator = owner
112        machine.contact = contact
113        machine.uuid = uuidToString(randomUUID())
114        machine.boot_off_cd = True
115        machine.type_id = machine_type.type_id
116        ctx.current.save(machine)
117        disk = Disk(machine_id=machine.machine_id,
118                    guest_device_name='hda', size=disk_size)
119        open_nics = NIC.select_by(machine_id=None)
120        if not open_nics: #No IPs left!
121            raise CodeError("No IP addresses left!  "
122                            "Contact xvm@mit.edu.")
123        nic = open_nics[0]
124        nic.machine_id = machine.machine_id
125        nic.hostname = name
126        ctx.current.save(nic)
127        ctx.current.save(disk)
128        cache_acls.refreshMachine(machine)
129        transaction.commit()
130    except:
131        transaction.rollback()
132        raise
133    makeDisks(machine)
134    if clone_from:
135        lvcopy(clone_from, machine, 'password')
136    # tell it to boot with cdrom
137    bootMachine(machine, cdrom)
138    return machine
139
140def getList(machines):
141    """Return a dictionary mapping machine  to dicts."""
142    value_string = remctl('web', 'listvms', '--pickle')
143    value_dict = cPickle.loads(value_string)
144
145    d = dict((m, value_dict[m.name]) for m in machines if m.name in value_dict)
146    return d
147
148def parseStatus(s):
149    """Parse a status string into nested tuples of strings.
150
151    s = output of xm list --long <machine_name>
152    """
153    values = re.split('([()])', s)
154    stack = [[]]
155    for v in values[2:-2]: #remove initial and final '()'
156        if not v:
157            continue
158        v = v.strip()
159        if v == '(':
160            stack.append([])
161        elif v == ')':
162            if len(stack[-1]) == 1:
163                stack[-1].append('')
164            stack[-2].append(stack[-1])
165            stack.pop()
166        else:
167            if not v:
168                continue
169            stack[-1].extend(v.split())
170    return stack[-1]
171
172def statusInfo(machine):
173    """Return the status list for a given machine.
174
175    Gets and parses xm list --long
176    """
177    value_string, err_string = remctl('control', machine.name, 'list-long', 
178                                      err=True)
179    if 'Unknown command' in err_string:
180        raise CodeError("ERROR in remctl list-long %s is not registered" % 
181                        (machine.name,))
182    elif 'does not exist' in err_string:
183        return None
184    elif err_string:
185        raise CodeError("ERROR in remctl list-long %s%s" % 
186                        (machine.name, err_string))
187    status = parseStatus(value_string)
188    return status
189
190def deleteVM(machine):
191    """Delete a VM."""
192    remctl('control', machine.name, 'destroy', err=True)
193    transaction = ctx.current.create_transaction()
194    delete_disk_pairs = [(machine.name, d.guest_device_name) 
195                         for d in machine.disks]
196    try:
197        for nic in machine.nics:
198            nic.machine_id = None
199            nic.hostname = None
200            ctx.current.save(nic)
201        for disk in machine.disks:
202            ctx.current.delete(disk)
203        for access in machine.acl:
204            ctx.current.delete(access)
205        ctx.current.delete(machine)
206        transaction.commit()
207    except:
208        transaction.rollback()
209        raise
210    for mname, dname in delete_disk_pairs:
211        remctl('web', 'lvremove', mname, dname)
212
213def commandResult(user, fields):
214    start_time = 0
215    machine = validation.testMachineId(user, fields.getfirst('machine_id'))
216    action = fields.getfirst('action')
217    cdrom = fields.getfirst('cdrom')
218    if cdrom is not None and not CDROM.get(cdrom):
219        raise CodeError("Invalid cdrom type '%s'" % cdrom)   
220    if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown', 
221                      'Delete VM'):
222        raise CodeError("Invalid action '%s'" % action)
223    if action == 'Reboot':
224        if cdrom is not None:
225            out, err = remctl('control', machine.name, 'reboot', cdrom,
226                              err=True)
227        else:
228            out, err = remctl('control', machine.name, 'reboot',
229                              err=True)
230        if err:
231            if re.match("Error: Domain '.*' does not exist.", err):
232                raise InvalidInput("action", "reboot", 
233                                   "Machine is not on")
234            else:
235                print >> sys.stderr, 'Error on reboot:'
236                print >> sys.stderr, err
237                raise CodeError('ERROR on remctl')
238               
239    elif action == 'Power on':
240        if validation.maxMemory(user, machine) < machine.memory:
241            raise InvalidInput('action', 'Power on',
242                               "You don't have enough free RAM quota "
243                               "to turn on this machine.")
244        bootMachine(machine, cdrom)
245    elif action == 'Power off':
246        out, err = remctl('control', machine.name, 'destroy', err=True)
247        if err:
248            if re.match("Error: Domain '.*' does not exist.", err):
249                raise InvalidInput("action", "Power off", 
250                                   "Machine is not on.")
251            else:
252                print >> sys.stderr, 'Error on power off:'
253                print >> sys.stderr, err
254                raise CodeError('ERROR on remctl')
255    elif action == 'Shutdown':
256        out, err = remctl('control', machine.name, 'shutdown', err=True)
257        if err:
258            if re.match("Error: Domain '.*' does not exist.", err):
259                raise InvalidInput("action", "Shutdown", 
260                                   "Machine is not on.")
261            else:
262                print >> sys.stderr, 'Error on Shutdown:'
263                print >> sys.stderr, err
264                raise CodeError('ERROR on remctl')
265    elif action == 'Delete VM':
266        deleteVM(machine)
267
268    d = dict(user=user,
269             command=action,
270             machine=machine)
271    return d
272
273def resizeDisk(machine_name, disk_name, new_size):
274    remctl("web", "lvresize", machine_name, disk_name, new_size)
275
276def renameMachine(machine, old_name, new_name):
277    for disk in machine.disks:
278        remctl("web", "lvrename", old_name, 
279               disk.guest_device_name, new_name)
280   
Note: See TracBrowser for help on using the repository browser.