+def getNicInfo(data_dict, machine):
+ 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):
+ 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):
+ 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):
+ 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 MyException("Invalid cdrom type '%s'" % cdrom)
+ if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown', 'Delete VM'):
+ raise MyException("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':
+ 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)
+
+def modify(user, fields):
+ machine = testMachineId(user, fields.getfirst('machine_id'))
+
+