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__':
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
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:
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
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)
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:
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]))
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
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):
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
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):
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