-def createVm(user, name, memory, disk, is_hvm, cdrom):
- """Create a VM and put it in the database"""
- # put stuff in the table
- transaction = ctx.current.create_transaction()
- try:
- res = meta.engine.execute('select nextval(\'"machines_machine_id_seq"\')')
- id = res.fetchone()[0]
- machine = Machine()
- machine.machine_id = id
- machine.name = name
- machine.memory = memory
- machine.owner = user.username
- machine.contact = user.email
- machine.uuid = uuidToString(randomUUID())
- machine.boot_off_cd = True
- machine_type = Type.get_by(hvm=is_hvm)
- machine.type_id = machine_type.type_id
- ctx.current.save(machine)
- disk = Disk(machine.machine_id,
- 'hda', disk)
- open = NIC.select_by(machine_id=None)
- if not open: #No IPs left!
- return "No IP addresses left! Contact sipb-xen-dev@mit.edu"
- nic = open[0]
- nic.machine_id = machine.machine_id
- nic.hostname = name
- ctx.current.save(nic)
- ctx.current.save(disk)
- transaction.commit()
- except:
- transaction.rollback()
- raise
- makeDisks()
- registerMachine(machine)
- # tell it to boot with cdrom
- bootMachine(machine, cdrom)
-
- return machine
-
-def validMemory(user, memory, machine=None):
- try:
- memory = int(memory)
- if memory <= 0:
- raise ValueError
- except ValueError:
- raise MyException("Invalid memory amount")
- if memory > maxMemory(user, machine):
- raise MyException("Too much memory requested")
- return memory
-
-def validDisk(user, disk, machine=None):
- try:
- disk = float(disk)
- if disk > maxDisk(user, machine):
- raise MyException("Too much disk requested")
- disk = int(disk * 1024)
- if disk <= 0:
- raise ValueError
- except ValueError:
- raise MyException("Invalid disk amount")
- return disk
-
-def create(user, fields):