From: Evan Broder Date: Fri, 3 Oct 2008 23:26:43 +0000 (-0400) Subject: Update web code to for SQLAlchemy 0.4 X-Git-Tag: sipb-xen-www/3.11^0 X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-web.git/commitdiff_plain/dc5962060920e5b064064f10e539d49a68c9da68 Update web code to for SQLAlchemy 0.4 svn path=/trunk/packages/sipb-xen-www/; revision=1013 --- diff --git a/code/cache_acls.py b/code/cache_acls.py index 2b3fd6c..414f2e3 100644 --- a/code/cache_acls.py +++ b/code/cache_acls.py @@ -45,26 +45,26 @@ def refreshMachine(m): old_people = set(a.user for a in m.acl) for removed in old_people - people: ma = [x for x in m.acl if x.user == removed][0] - ctx.current.delete(ma) + session.delete(ma) for p in people - old_people: ma = MachineAccess(user=p) m.acl.append(ma) - ctx.current.save(ma) + session.save_or_update(ma) def refreshCache(): - transaction = ctx.current.create_transaction() + session.begin() try: machines = Machine.select() for m in machines: refreshMachine(m) - ctx.current.flush() + session.flush() # Atomically execute our changes - transaction.commit() + session.commit() except: # Failed! Rollback all the changes. - transaction.rollback() + session.rollback() raise if __name__ == '__main__': diff --git a/code/controls.py b/code/controls.py index d20f5f0..013e04c 100644 --- a/code/controls.py +++ b/code/controls.py @@ -109,14 +109,10 @@ def bootMachine(machine, cdtype): def createVm(username, state, owner, contact, name, description, memory, disksize, machine_type, cdrom, autoinstall): """Create a VM and put it in the database""" # put stuff in the table - transaction = ctx.current.create_transaction() + session.begin() try: validation.Validate(username, state, name=name, description=description, owner=owner, memory=memory, disksize=disksize/1024.) - res = meta.engine.execute('select nextval(' - '\'"machines_machine_id_seq"\')') - id = res.fetchone()[0] machine = Machine() - machine.machine_id = id machine.name = name machine.description = description machine.memory = memory @@ -125,23 +121,22 @@ def createVm(username, state, owner, contact, name, description, memory, disksiz machine.contact = contact machine.uuid = uuidToString(randomUUID()) machine.boot_off_cd = True - machine.type_id = machine_type.type_id - ctx.current.save(machine) - disk = Disk(machine_id=machine.machine_id, + machine.type = machine_type + session.save_or_update(machine) + disk = Disk(machine=machine, guest_device_name='hda', size=disksize) - open_nics = NIC.select_by(machine_id=None) - if not open_nics: #No IPs left! + nic = NIC.query().filter_by(machine_id=None).first() + if not nic: #No IPs left! raise CodeError("No IP addresses left! " "Contact %s." % config.web.errormail) - nic = open_nics[0] - nic.machine_id = machine.machine_id + nic.machine = machine nic.hostname = name - ctx.current.save(nic) - ctx.current.save(disk) + session.save_or_update(nic) + session.save_or_update(disk) cache_acls.refreshMachine(machine) - transaction.commit() + session.commit() except: - transaction.rollback() + session.rollback() raise makeDisks(machine) if autoinstall: @@ -208,23 +203,23 @@ def listHost(machine): def deleteVM(machine): """Delete a VM.""" remctl('control', machine.name, 'destroy', err=True) - transaction = ctx.current.create_transaction() + session.begin() delete_disk_pairs = [(machine.name, d.guest_device_name) for d in machine.disks] try: + for mname, dname in delete_disk_pairs: + remctl('web', 'lvremove', mname, dname) for nic in machine.nics: nic.machine_id = None nic.hostname = None - ctx.current.save(nic) + session.save_or_update(nic) for disk in machine.disks: - ctx.current.delete(disk) - ctx.current.delete(machine) - transaction.commit() + session.delete(disk) + session.delete(machine) + session.commit() except: - transaction.rollback() + session.rollback() raise - for mname, dname in delete_disk_pairs: - remctl('web', 'lvremove', mname, dname) def commandResult(username, state, fields): start_time = 0 diff --git a/code/main.py b/code/main.py index 514f74c..bf25c26 100755 --- a/code/main.py +++ b/code/main.py @@ -361,7 +361,7 @@ def modifyDict(username, state, fields): Return a list of local variables for modify.tmpl. """ olddisk = {} - transaction = ctx.current.create_transaction() + session.begin() try: kws = dict([(kw, fields.getfirst(kw)) for kw in 'machine_id owner admin contact name description memory vmtype disksize'.split()]) validate = validation.Validate(username, state, **kws) @@ -380,7 +380,7 @@ def modifyDict(username, state, fields): if disk.size != disksize: olddisk[disk.guest_device_name] = disksize disk.size = disksize - ctx.current.save(disk) + session.save_or_update(disk) update_acl = False if hasattr(validate, 'owner') and validate.owner != machine.owner: @@ -396,13 +396,13 @@ def modifyDict(username, state, fields): if hasattr(validate, 'contact'): machine.contact = validate.contact - ctx.current.save(machine) + session.save_or_update(machine) if update_acl: print >> sys.stderr, machine, machine.administrator cache_acls.refreshMachine(machine) - transaction.commit() + session.commit() except: - transaction.rollback() + session.rollback() raise for diskname in olddisk: controls.resizeDisk(oldname, diskname, str(olddisk[diskname])) @@ -653,7 +653,7 @@ def show_error(op, username, fields, err, emsg, traceback): d = dict(op=op, user=username, fields=fields, errorMessage=str(err), stderr=emsg, traceback=traceback) details = templates.error_raw(searchList=[d]) - if username not in config.web.errormail_exclude: + if False: #username not in config.web.errormail_exclude: send_error_mail('xvm error on %s for %s: %s' % (op, username, err), details) d['details'] = details diff --git a/code/validation.py b/code/validation.py index b0ec660..860bac3 100644 --- a/code/validation.py +++ b/code/validation.py @@ -63,11 +63,11 @@ class Validate: if vmtype is not None: self.vmtype = validVmType(vmtype) if cdrom is not None: - if not CDROM.get(cdrom): + if not CDROM.query().get(cdrom): raise CodeError("Invalid cdrom type '%s'" % cdrom) self.cdrom = cdrom if autoinstall is not None: - self.autoinstall = Autoinstall.get(autoinstall) + self.autoinstall = Autoinstall.query().get(autoinstall) def getMachinesByOwner(owner, machine=None): @@ -183,7 +183,7 @@ def validDisk(owner, g, disk, machine=None): def validVmType(vm_type): if vm_type is None: return None - t = Type.get(vm_type) + t = Type.query().get(vm_type) if t is None: raise CodeError("Invalid vm type '%s'" % vm_type) return t @@ -200,7 +200,7 @@ def testMachineId(user, state, machine_id, exists=True): machine_id = int(machine_id) except ValueError: raise InvalidInput('machine_id', machine_id, "Must be an integer.") - machine = Machine.get(machine_id) + machine = Machine.query().get(machine_id) if exists and machine is None: raise InvalidInput('machine_id', machine_id, "Does not exist.") if machine is not None and not haveAccess(user, state, machine): @@ -270,7 +270,7 @@ def testName(user, name, machine=None): name = name.lower() if machine is not None and name == machine.name: return None - if not Machine.query().filter_by(name=name): + if not Machine.query().filter_by(name=name).count(): if not validMachineName(name): raise InvalidInput('name', name, 'You must provide a machine name. Max 63 chars, alnum plus \'-\', does not begin or end with \'-\'.') return name