+def testOwner(user, owner, machine):
+ if owner in (None, machine.owner):
+ return None
+ #XXX should you be able to transfer ownership if you don't already own it?
+ #if not owns(user, machine):
+ # raise InvalidInput('owner', owner, "You don't own this machine, so you can't transfer ownership")
+ 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, "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 modify(user, fields):
+ """Handler for modifying attributes of a machine."""
+
+ 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)
+
+ # XXX first NIC gets hostname on change? Interface doesn't support more.
+ for nic in machine.nics[:1]:
+ nic.hostname = hostname
+ ctx.current.save(nic)
+
+ if owner is not None:
+ machine.owner = owner
+ if name is not None:
+ machine.name = name
+ if admin is not None:
+ machine.administrator = admin
+ if contact is not None:
+ machine.contact = contact
+
+ ctx.current.save(machine)
+ transaction.commit()
+ except:
+ transaction.rollback()
+ raise
+ for diskname in olddisk:
+ remctl("web", "lvresize", oldname, diskname, str(olddisk[diskname]))
+ if name is not None:
+ for disk in machine.disks:
+ remctl("web", "lvrename", oldname, disk.guest_device_name, name)
+ remctl("web", "moveregister", oldname, name)
+ d = dict(user=user,
+ command=command,
+ machine=machine)
+ return Template(file="command.tmpl", searchList=[d, global_dict])
+
+