2 Functions to perform remctls.
5 from sipb_xen_database import Machine, Disk, Type, NIC, CDROM, ctx, meta
7 from webcommon import CodeError, InvalidInput
14 # ... and stolen from xend/uuid.py
16 """Generate a random UUID."""
18 return [ random.randint(0, 255) for _ in range(0, 16) ]
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)
26 def kinit(username = 'daemon/sipb-xen.mit.edu', keytab = '/etc/sipb-xen.keytab'):
27 """Kinit with a given username and keytab"""
29 p = subprocess.Popen(['kinit', "-k", "-t", keytab, username],
30 stderr=subprocess.PIPE)
33 raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
36 """If we lack tickets, kinit."""
37 p = subprocess.Popen(['klist', '-s'])
41 def remctl(*args, **kws):
42 """Perform a remctl and return the output.
44 kinits if necessary, and outputs errors to stderr.
47 p = subprocess.Popen(['remctl', 'black-mesa.mit.edu']
49 stdout=subprocess.PIPE,
50 stderr=subprocess.PIPE)
53 return p.stdout.read(), p.stderr.read()
55 print >> sys.stderr, 'Error', v, 'on remctl', args, ':'
56 print >> sys.stderr, p.stderr.read()
57 raise CodeError('ERROR on remctl')
58 return p.stdout.read()
60 def lvcreate(machine, disk):
61 """Create a single disk for a machine"""
62 remctl('web', 'lvcreate', machine.name,
63 disk.guest_device_name, str(disk.size))
65 def makeDisks(machine):
66 """Update the lvm partitions to add a disk."""
67 for disk in machine.disks:
68 lvcreate(machine, disk)
70 def bootMachine(machine, cdtype):
71 """Boot a machine with a given boot CD.
73 If cdtype is None, give no boot cd. Otherwise, it is the string
74 id of the CD (e.g. 'gutsy_i386')
76 if cdtype is not None:
77 remctl('control', machine.name, 'create',
80 remctl('control', machine.name, 'create')
82 def registerMachine(machine):
83 """Register a machine to be controlled by the web interface"""
84 remctl('web', 'register', machine.name)
86 def unregisterMachine(machine):
87 """Unregister a machine to not be controlled by the web interface"""
88 remctl('web', 'unregister', machine.name)
90 def createVm(owner, contact, name, memory, disk, is_hvm, cdrom):
91 """Create a VM and put it in the database"""
92 # put stuff in the table
93 transaction = ctx.current.create_transaction()
95 validation.validMemory(owner, memory)
96 validation.validDisk(owner, disk * 1. / 1024)
97 validation.validAddVm(owner)
98 res = meta.engine.execute('select nextval('
99 '\'"machines_machine_id_seq"\')')
100 id = res.fetchone()[0]
102 machine.machine_id = id
104 machine.memory = memory
105 machine.owner = owner
106 machine.administrator = owner
107 machine.contact = contact
108 machine.uuid = uuidToString(randomUUID())
109 machine.boot_off_cd = True
110 machine_type = Type.get_by(hvm=is_hvm)
111 machine.type_id = machine_type.type_id
112 ctx.current.save(machine)
113 disk = Disk(machine.machine_id,
115 open_nics = NIC.select_by(machine_id=None)
116 if not open_nics: #No IPs left!
117 raise CodeError("No IP addresses left! "
118 "Contact sipb-xen-dev@mit.edu")
120 nic.machine_id = machine.machine_id
122 ctx.current.save(nic)
123 ctx.current.save(disk)
126 transaction.rollback()
128 registerMachine(machine)
130 # tell it to boot with cdrom
131 bootMachine(machine, cdrom)
134 def getUptimes(machines=None):
135 """Return a dictionary mapping machine names to uptime strings"""
136 value_string = remctl('web', 'listvms')
137 lines = value_string.splitlines()
142 uptime = ' '.join(lst[2:])
146 ans[m] = d.get(m.name)
150 """Parse a status string into nested tuples of strings.
152 s = output of xm list --long <machine_name>
154 values = re.split('([()])', s)
156 for v in values[2:-2]: #remove initial and final '()'
163 if len(stack[-1]) == 1:
165 stack[-2].append(stack[-1])
170 stack[-1].extend(v.split())
173 def statusInfo(machine):
174 """Return the status list for a given machine.
176 Gets and parses xm list --long
178 value_string, err_string = remctl('control', machine.name, 'list-long',
180 if 'Unknown command' in err_string:
181 raise CodeError("ERROR in remctl list-long %s is not registered" %
183 elif 'does not exist' in err_string:
186 raise CodeError("ERROR in remctl list-long %s: %s" %
187 (machine.name, err_string))
188 status = parseStatus(value_string)
191 def deleteVM(machine):
193 remctl('control', machine.name, 'destroy', err=True)
194 transaction = ctx.current.create_transaction()
195 delete_disk_pairs = [(machine.name, d.guest_device_name)
196 for d in machine.disks]
198 for nic in machine.nics:
199 nic.machine_id = None
201 ctx.current.save(nic)
202 for disk in machine.disks:
203 ctx.current.delete(disk)
204 ctx.current.delete(machine)
207 transaction.rollback()
209 for mname, dname in delete_disk_pairs:
210 remctl('web', 'lvremove', mname, dname)
211 unregisterMachine(machine)
213 def commandResult(user, fields):
215 print >> sys.stderr, time.time()-start_time
216 machine = validation.testMachineId(user, fields.getfirst('machine_id'))
217 action = fields.getfirst('action')
218 cdrom = fields.getfirst('cdrom')
219 print >> sys.stderr, time.time()-start_time
220 if cdrom is not None and not CDROM.get(cdrom):
221 raise CodeError("Invalid cdrom type '%s'" % cdrom)
222 if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown',
224 raise CodeError("Invalid action '%s'" % action)
225 if action == 'Reboot':
226 if cdrom is not None:
227 out, err = remctl('control', machine.name, 'reboot', cdrom,
230 out, err = remctl('control', machine.name, 'reboot',
233 if re.match("Error: Domain '.*' does not exist.", err):
234 raise InvalidInput("action", "reboot",
237 print >> sys.stderr, 'Error on reboot:'
238 print >> sys.stderr, err
239 raise CodeError('ERROR on remctl')
241 elif action == 'Power on':
242 if validation.maxMemory(user, machine) < machine.memory:
243 raise InvalidInput('action', 'Power on',
244 "You don't have enough free RAM quota "
245 "to turn on this machine.")
246 bootMachine(machine, cdrom)
247 elif action == 'Power off':
248 out, err = remctl('control', machine.name, 'destroy', err=True)
250 if re.match("Error: Domain '.*' does not exist.", err):
251 raise InvalidInput("action", "Power off",
252 "Machine is not on.")
254 print >> sys.stderr, 'Error on power off:'
255 print >> sys.stderr, err
256 raise CodeError('ERROR on remctl')
257 elif action == 'Shutdown':
258 out, err = remctl('control', machine.name, 'shutdown', err=True)
260 if re.match("Error: Domain '.*' does not exist.", err):
261 raise InvalidInput("action", "Shutdown",
262 "Machine is not on.")
264 print >> sys.stderr, 'Error on Shutdown:'
265 print >> sys.stderr, err
266 raise CodeError('ERROR on remctl')
267 elif action == 'Delete VM':
269 print >> sys.stderr, time.time()-start_time
276 def resizeDisk(machine_name, disk_name, new_size):
277 remctl("web", "lvresize", machine_name, disk_name, new_size)
279 def renameMachine(machine, old_name, new_name):
280 for disk in machine.disks:
281 remctl("web", "lvrename", old_name,
282 disk.guest_device_name, new_name)
283 remctl("web", "moveregister", old_name, new_name)