+ return d
+
+def command(user, fields):
+ """Handler for running commands like boot and delete on a VM."""
+ js = fields.getfirst('js')
+ try:
+ d = commandResult(user, fields)
+ except InvalidInput, err:
+ if not js:
+ raise
+ result = None
+ else:
+ err = None
+ result = 'Success!'
+ if not js:
+ return Template(file='command.tmpl', searchList=[d])
+ if js == 'list':
+ g.clear() #Changed global state
+ d = getListDict(user)
+ t = Template(file='list.tmpl', searchList=[d])
+ return JsonDict(createtable=t.createTable(),
+ machinelist=t.machineList(d['machines']),
+ result=result,
+ err=err)
+ elif js == 'info':
+ machine = testMachineId(user, fields.getfirst('machine_id'))
+ d = infoDict(user, machine)
+ t = Template(file='info.tmpl', searchList=[d])
+ return JsonDict(info=t.infoTable(),
+ commands=t.commands(),
+ modify=t.modifyForm(),
+ result=result,
+ err=err)
+ else:
+ raise InvalidInput('js', js, 'Not a known js type.')
+
+def testAdmin(user, admin, machine):
+ if admin in (None, machine.administrator):
+ return None
+ if admin == user.username:
+ return admin
+ if getafsgroups.checkAfsGroup(user.username, admin, 'athena.mit.edu'):
+ return admin
+ if getafsgroups.checkAfsGroup(user.username, 'system:'+admin,
+ 'athena.mit.edu'):
+ return 'system:'+admin
+ raise InvalidInput('administrator', admin,
+ 'You must control the group you move it to.')
+
+def testOwner(user, owner, machine):
+ if owner in (None, machine.owner):
+ return None
+ value = getafsgroups.checkLockerOwner(user.username, owner, verbose=True)
+ if value == True:
+ return owner
+ raise InvalidInput('owner', owner, value)
+
+def testContact(user, contact, machine=None):
+ if contact in (None, machine.contact):
+ return None
+ if not re.match("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", contact, re.I):
+ raise InvalidInput('contact', contact, "Not a valid email.")
+ return contact
+
+def testDisk(user, disksize, machine=None):
+ return disksize
+
+def testName(user, name, machine=None):
+ if name in (None, machine.name):
+ return None
+ if not Machine.select_by(name=name):
+ return name
+ raise InvalidInput('name', name, "Name is already taken.")
+
+def testHostname(user, hostname, machine):
+ for nic in machine.nics:
+ if hostname == nic.hostname:
+ return hostname
+ # check if doesn't already exist
+ if NIC.select_by(hostname=hostname):
+ raise InvalidInput('hostname', hostname,
+ "Already exists")
+ if not re.match("^[A-Z0-9-]{1,22}$", hostname, re.I):
+ raise InvalidInput('hostname', hostname, "Not a valid hostname; "
+ "must only use number, letters, and dashes.")
+ return hostname
+
+def modifyDict(user, fields):
+ olddisk = {}
+ transaction = ctx.current.create_transaction()
+ try:
+ machine = testMachineId(user, fields.getfirst('machine_id'))
+ owner = testOwner(user, fields.getfirst('owner'), machine)
+ admin = testAdmin(user, fields.getfirst('administrator'), machine)
+ contact = testContact(user, fields.getfirst('contact'), machine)
+ hostname = testHostname(owner, fields.getfirst('hostname'), machine)
+ name = testName(user, fields.getfirst('name'), machine)
+ oldname = machine.name
+ command = "modify"
+
+ memory = fields.getfirst('memory')
+ if memory is not None:
+ memory = validMemory(user, memory, machine, on=False)
+ machine.memory = memory
+
+ disksize = testDisk(user, fields.getfirst('disk'))
+ if disksize is not None:
+ disksize = validDisk(user, disksize, machine)
+ disk = machine.disks[0]
+ if disk.size != disksize:
+ olddisk[disk.guest_device_name] = disksize
+ disk.size = disksize
+ ctx.current.save(disk)