fix a different errortext-matching mismatch
[invirt/packages/invirt-web.git] / code / controls.py
1 """
2 Functions to perform remctls.
3 """
4
5 from sipb_xen_database import Machine, Disk, Type, NIC, CDROM, ctx, meta
6 import validation
7 from webcommon import CodeError, InvalidInput
8 import random
9 import subprocess
10 import sys
11 import time
12 import re
13 import cache_acls
14 import yaml
15
16 # ... and stolen from xend/uuid.py
17 def randomUUID():
18     """Generate a random UUID."""
19
20     return [ random.randint(0, 255) for _ in range(0, 16) ]
21
22 def 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
28 def 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
37 def checkKinit():
38     """If we lack tickets, kinit."""
39     p = subprocess.Popen(['klist', '-s'])
40     if p.wait():
41         kinit()
42
43 def 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
62 def 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     
67 def makeDisks(machine):
68     """Update the lvm partitions to add a disk."""
69     for disk in machine.disks:
70         lvcreate(machine, disk)
71
72 def getswap(disksize, memsize):
73     """Returns the recommended swap partition size."""
74     return int(min(disksize / 4, memsize * 1.5))
75
76 def lvinstall(machine, autoinstall):
77     disksize = machine.disks[0].size
78     memsize = machine.memory
79     imagesize = disksize - getswap(disksize, memsize)
80     ip = machine.nics[0].ip
81     remctl('web', 'install', machine.name, autoinstall.distribution,
82            autoinstall.mirror, str(imagesize), ip)
83
84 def lvcopy(machine_orig_name, machine, rootpw):
85     """Copy a golden image onto a machine's disk"""
86     remctl('web', 'lvcopy', machine_orig_name, machine.name, rootpw)
87
88 def bootMachine(machine, cdtype):
89     """Boot a machine with a given boot CD.
90
91     If cdtype is None, give no boot cd.  Otherwise, it is the string
92     id of the CD (e.g. 'gutsy_i386')
93     """
94     if cdtype is not None:
95         out, err = remctl('control', machine.name, 'create', 
96                           cdtype, err=True)
97     else:
98         out, err = remctl('control', machine.name, 'create',
99                           err=True)
100     if 'already running' in err:
101         raise InvalidInput('action', 'create',
102                            'VM %s is already on' % machine.name)
103     elif err:
104         raise CodeError('"%s" on "control %s create %s' 
105                         % (err, machine.name, cdtype))
106
107 def createVm(username, state, owner, contact, name, description, memory, disksize, machine_type, cdrom, autoinstall):
108     """Create a VM and put it in the database"""
109     # put stuff in the table
110     transaction = ctx.current.create_transaction()
111     try:
112         validation.Validate(username, state, name=name, description=description, owner=owner, memory=memory, disksize=disksize/1024.)
113         res = meta.engine.execute('select nextval('
114                                   '\'"machines_machine_id_seq"\')')
115         id = res.fetchone()[0]
116         machine = Machine()
117         machine.machine_id = id
118         machine.name = name
119         machine.description = description
120         machine.memory = memory
121         machine.owner = owner
122         machine.administrator = owner
123         machine.contact = contact
124         machine.uuid = uuidToString(randomUUID())
125         machine.boot_off_cd = True
126         machine.type_id = machine_type.type_id
127         ctx.current.save(machine)
128         disk = Disk(machine_id=machine.machine_id,
129                     guest_device_name='hda', size=disksize)
130         open_nics = NIC.select_by(machine_id=None)
131         if not open_nics: #No IPs left!
132             raise CodeError("No IP addresses left!  "
133                             "Contact xvm@mit.edu.")
134         nic = open_nics[0]
135         nic.machine_id = machine.machine_id
136         nic.hostname = name
137         ctx.current.save(nic)
138         ctx.current.save(disk)
139         cache_acls.refreshMachine(machine)
140         transaction.commit()
141     except:
142         transaction.rollback()
143         raise
144     makeDisks(machine)
145     if autoinstall:
146         lvinstall(machine, autoinstall)
147     # tell it to boot with cdrom
148     bootMachine(machine, cdrom)
149     return machine
150
151 def getList():
152     """Return a dictionary mapping machine names to dicts."""
153     value_string = remctl('web', 'listvms')
154     value_dict = yaml.load(value_string, yaml.CSafeLoader)
155     return value_dict
156
157 def parseStatus(s):
158     """Parse a status string into nested tuples of strings.
159
160     s = output of xm list --long <machine_name>
161     """
162     values = re.split('([()])', s)
163     stack = [[]]
164     for v in values[2:-2]: #remove initial and final '()'
165         if not v:
166             continue
167         v = v.strip()
168         if v == '(':
169             stack.append([])
170         elif v == ')':
171             if len(stack[-1]) == 1:
172                 stack[-1].append('')
173             stack[-2].append(stack[-1])
174             stack.pop()
175         else:
176             if not v:
177                 continue
178             stack[-1].extend(v.split())
179     return stack[-1]
180
181 def statusInfo(machine):
182     """Return the status list for a given machine.
183
184     Gets and parses xm list --long
185     """
186     value_string, err_string = remctl('control', machine.name, 'list-long', 
187                                       err=True)
188     if 'Unknown command' in err_string:
189         raise CodeError("ERROR in remctl list-long %s is not registered" % 
190                         (machine.name,))
191     elif 'is not on' in err_string:
192         return None
193     elif err_string:
194         raise CodeError("ERROR in remctl list-long %s:  %s" % 
195                         (machine.name, err_string))
196     status = parseStatus(value_string)
197     return status
198
199 def listHost(machine):
200     """Return the host a machine is running on"""
201     out, err = remctl('control', machine.name, 'listhost', err=True)
202     if err:
203         return None
204     return out.strip()
205
206 def deleteVM(machine):
207     """Delete a VM."""
208     remctl('control', machine.name, 'destroy', err=True)
209     transaction = ctx.current.create_transaction()
210     delete_disk_pairs = [(machine.name, d.guest_device_name) 
211                          for d in machine.disks]
212     try:
213         for nic in machine.nics:
214             nic.machine_id = None
215             nic.hostname = None
216             ctx.current.save(nic)
217         for disk in machine.disks:
218             ctx.current.delete(disk)
219         ctx.current.delete(machine)
220         transaction.commit()
221     except:
222         transaction.rollback()
223         raise
224     for mname, dname in delete_disk_pairs:
225         remctl('web', 'lvremove', mname, dname)
226
227 def commandResult(username, state, fields):
228     start_time = 0
229     machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
230     action = fields.getfirst('action')
231     cdrom = fields.getfirst('cdrom')
232     if cdrom is not None and not CDROM.get(cdrom):
233         raise CodeError("Invalid cdrom type '%s'" % cdrom)    
234     if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown', 
235                       'Delete VM'):
236         raise CodeError("Invalid action '%s'" % action)
237     if action == 'Reboot':
238         if cdrom is not None:
239             out, err = remctl('control', machine.name, 'reboot', cdrom,
240                               err=True)
241         else:
242             out, err = remctl('control', machine.name, 'reboot',
243                               err=True)
244         if err:
245             if re.match("machine '.*' is not on", err):
246                 raise InvalidInput("action", "reboot", 
247                                    "Machine is not on")
248             else:
249                 print >> sys.stderr, 'Error on reboot:'
250                 print >> sys.stderr, err
251                 raise CodeError('ERROR on remctl')
252                 
253     elif action == 'Power on':
254         if validation.maxMemory(username, state, machine) < machine.memory:
255             raise InvalidInput('action', 'Power on',
256                                "You don't have enough free RAM quota "
257                                "to turn on this machine.")
258         bootMachine(machine, cdrom)
259     elif action == 'Power off':
260         out, err = remctl('control', machine.name, 'destroy', err=True)
261         if err:
262             if re.match("machine '.*' is not on", err):
263                 raise InvalidInput("action", "Power off", 
264                                    "Machine is not on.")
265             else:
266                 print >> sys.stderr, 'Error on power off:'
267                 print >> sys.stderr, err
268                 raise CodeError('ERROR on remctl')
269     elif action == 'Shutdown':
270         out, err = remctl('control', machine.name, 'shutdown', err=True)
271         if err:
272             if re.match("machine '.*' is not on", err):
273                 raise InvalidInput("action", "Shutdown", 
274                                    "Machine is not on.")
275             else:
276                 print >> sys.stderr, 'Error on Shutdown:'
277                 print >> sys.stderr, err
278                 raise CodeError('ERROR on remctl')
279     elif action == 'Delete VM':
280         deleteVM(machine)
281
282     d = dict(user=username,
283              command=action,
284              machine=machine)
285     return d
286
287 def resizeDisk(machine_name, disk_name, new_size):
288     remctl("web", "lvresize", machine_name, disk_name, new_size)
289
290 def renameMachine(machine, old_name, new_name):
291     for disk in machine.disks:
292         remctl("web", "lvrename", old_name, 
293                disk.guest_device_name, new_name)
294