+ searchList=[d, global_dict])
+
+def getNicInfo(data_dict, machine):
+ """Helper function for info, get data on nics for a machine.
+
+ Modifies data_dict to include the relevant data, and returns a list
+ of (key, name) pairs to display "name: data_dict[key]" to the user.
+ """
+ data_dict['num_nics'] = len(machine.nics)
+ nic_fields_template = [('nic%s_hostname', 'NIC %s hostname'),
+ ('nic%s_mac', 'NIC %s MAC Addr'),
+ ('nic%s_ip', 'NIC %s IP'),
+ ]
+ nic_fields = []
+ for i in range(len(machine.nics)):
+ nic_fields.extend([(x % i, y % i) for x, y in nic_fields_template])
+ data_dict['nic%s_hostname' % i] = machine.nics[i].hostname + '.servers.csail.mit.edu'
+ data_dict['nic%s_mac' % i] = machine.nics[i].mac_addr
+ data_dict['nic%s_ip' % i] = machine.nics[i].ip
+ if len(machine.nics) == 1:
+ nic_fields = [(x, y.replace('NIC 0 ', '')) for x, y in nic_fields]
+ return nic_fields
+
+def getDiskInfo(data_dict, machine):
+ """Helper function for info, get data on disks for a machine.
+
+ Modifies data_dict to include the relevant data, and returns a list
+ of (key, name) pairs to display "name: data_dict[key]" to the user.
+ """
+ data_dict['num_disks'] = len(machine.disks)
+ disk_fields_template = [('%s_size', '%s size')]
+ disk_fields = []
+ for disk in machine.disks:
+ name = disk.guest_device_name
+ disk_fields.extend([(x % name, y % name) for x, y in disk_fields_template])
+ data_dict['%s_size' % name] = "%0.1f GB" % (disk.size / 1024.)
+ return disk_fields
+
+def deleteVM(machine):
+ """Delete a VM."""
+ transaction = ctx.current.create_transaction()
+ delete_disk_pairs = [(machine.name, d.guest_device_name) for d in machine.disks]
+ try:
+ for nic in machine.nics:
+ nic.machine_id = None
+ nic.hostname = None
+ ctx.current.save(nic)
+ for disk in machine.disks:
+ ctx.current.delete(disk)
+ ctx.current.delete(machine)
+ transaction.commit()
+ except:
+ transaction.rollback()
+ raise
+ for mname, dname in delete_disk_pairs:
+ remctl('web', 'lvremove', mname, dname)
+ unregisterMachine(machine)
+
+def command(user, fields):
+ """Handler for running commands like boot and delete on a VM."""
+ print time.time()-start_time
+ machine = testMachineId(user, fields.getfirst('machine_id'))
+ action = fields.getfirst('action')
+ cdrom = fields.getfirst('cdrom')
+ print time.time()-start_time
+ if cdrom is not None and not CDROM.get(cdrom):
+ raise CodeError("Invalid cdrom type '%s'" % cdrom)
+ if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown', 'Delete VM'):
+ raise CodeError("Invalid action '%s'" % action)
+ if action == 'Reboot':
+ if cdrom is not None:
+ remctl('reboot', machine.name, cdrom)
+ else:
+ remctl('reboot', machine.name)
+ elif action == 'Power on':
+ if maxMemory(user) < machine.memory:
+ raise InvalidInput("You don't have enough free RAM quota")
+ bootMachine(machine, cdrom)
+ elif action == 'Power off':
+ remctl('destroy', machine.name)
+ elif action == 'Shutdown':
+ remctl('shutdown', machine.name)
+ elif action == 'Delete VM':
+ deleteVM(machine)
+ print time.time()-start_time
+
+ d = dict(user=user,
+ command=action,
+ machine=machine)
+ print Template(file="command.tmpl", searchList=[d, global_dict])
+
+def modify(user, fields):
+ """Handler for modifying attributes of a machine."""
+ #XXX not written yet
+ machine = testMachineId(user, fields.getfirst('machine_id'))
+
+def help(user, fields):
+ """Handler for help messages."""
+ simple = fields.getfirst('simple')
+ subjects = fields.getlist('subject')
+
+ mapping = dict(paravm_console="""
+ParaVM machines do not support console access over VNC. To access
+these machines, you either need to boot with a liveCD and ssh in or
+hope that the sipb-xen maintainers add support for serial consoles.""",
+ hvm_paravm="""
+HVM machines use the virtualization features of the processor, while
+ParaVM machines use Xen's emulation of virtualization features. You
+want an HVM virtualized machine.""",
+ cpu_weight="""Don't ask us! We're as mystified as you are.""")
+
+ d = dict(user=user,
+ simple=simple,
+ subjects=subjects,
+ mapping=mapping)
+
+ print Template(file="help.tmpl", searchList=[d, global_dict])
+