from invirt.config import structs as config
from invirt.database import Machine, Disk, Type, NIC, CDROM, session, meta
from invirt.config import structs as config
from invirt.database import Machine, Disk, Type, NIC, CDROM, session, meta
+def remctl(*args, **kwargs):
+ return gen_remctl(config.remote.hostname,
+ principal='daemon/'+config.web.hostname,
+ *args, **kwargs)
+
def lvcreate(machine, disk):
"""Create a single disk for a machine"""
remctl('web', 'lvcreate', machine.name,
def lvcreate(machine, disk):
"""Create a single disk for a machine"""
remctl('web', 'lvcreate', machine.name,
remctl('control', machine.name, 'install',
'dist=%s' % autoinstall.distribution,
'mirror=%s' % autoinstall.mirror,
remctl('control', machine.name, 'install',
'dist=%s' % autoinstall.distribution,
'mirror=%s' % autoinstall.mirror,
'imagesize=%s' % imagesize)
def lvcopy(machine_orig_name, machine, rootpw):
'imagesize=%s' % imagesize)
def lvcopy(machine_orig_name, machine, rootpw):
machine.contact = contact
machine.uuid = uuidToString(randomUUID())
machine.boot_off_cd = True
machine.contact = contact
machine.uuid = uuidToString(randomUUID())
machine.boot_off_cd = True
session.save_or_update(machine)
disk = Disk(machine=machine,
guest_device_name='hda', size=disksize)
session.save_or_update(machine)
disk = Disk(machine=machine,
guest_device_name='hda', size=disksize)
if not nic: #No IPs left!
raise CodeError("No IP addresses left! "
"Contact %s." % config.web.errormail)
if not nic: #No IPs left!
raise CodeError("No IP addresses left! "
"Contact %s." % config.web.errormail)
session.save_or_update(nic)
session.save_or_update(disk)
cache_acls.refreshMachine(machine)
session.save_or_update(nic)
session.save_or_update(disk)
cache_acls.refreshMachine(machine)
- makeDisks(machine)
- if autoinstall:
- lvinstall(machine, autoinstall)
- else:
- # tell it to boot with cdrom
- bootMachine(machine, cdrom)
+ try:
+ if autoinstall:
+ lvinstall(machine, autoinstall)
+ else:
+ # tell it to boot with cdrom
+ bootMachine(machine, cdrom)
+ except CodeError, e:
+ deleteVM(machine)
+ raise
def deleteVM(machine):
"""Delete a VM."""
remctl('control', machine.name, 'destroy', err=True)
def deleteVM(machine):
"""Delete a VM."""
remctl('control', machine.name, 'destroy', err=True)
-def commandResult(username, state, fields):
+def commandResult(username, state, command_name, machine_id, fields):
- machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
- action = fields.getfirst('action')
- cdrom = fields.getfirst('cdrom')
+ machine = validation.Validate(username, state, machine_id=machine_id).machine
+ action = command_name
+ cdrom = fields.get('cdrom') or None
if cdrom is not None and not CDROM.query().filter_by(cdrom_id=cdrom).one():
raise CodeError("Invalid cdrom type '%s'" % cdrom)
if cdrom is not None and not CDROM.query().filter_by(cdrom_id=cdrom).one():
raise CodeError("Invalid cdrom type '%s'" % cdrom)
if cdrom is not None:
out, err = remctl('control', machine.name, 'reboot', cdrom,
err=True)
if cdrom is not None:
out, err = remctl('control', machine.name, 'reboot', cdrom,
err=True)
if validation.maxMemory(username, state, machine) < machine.memory:
raise InvalidInput('action', 'Power on',
"You don't have enough free RAM quota "
"to turn on this machine.")
bootMachine(machine, cdrom)
if validation.maxMemory(username, state, machine) < machine.memory:
raise InvalidInput('action', 'Power on',
"You don't have enough free RAM quota "
"to turn on this machine.")
bootMachine(machine, cdrom)
out, err = remctl('control', machine.name, 'destroy', err=True)
if err:
if re.match("machine '.*' is not on", err):
out, err = remctl('control', machine.name, 'destroy', err=True)
if err:
if re.match("machine '.*' is not on", err):
print >> sys.stderr, 'Error on power off:'
print >> sys.stderr, err
raise CodeError('ERROR on remctl')
print >> sys.stderr, 'Error on power off:'
print >> sys.stderr, err
raise CodeError('ERROR on remctl')
out, err = remctl('control', machine.name, 'shutdown', err=True)
if err:
if re.match("machine '.*' is not on", err):
out, err = remctl('control', machine.name, 'shutdown', err=True)
if err:
if re.match("machine '.*' is not on", err):
print >> sys.stderr, 'Error on Shutdown:'
print >> sys.stderr, err
raise CodeError('ERROR on remctl')
print >> sys.stderr, 'Error on Shutdown:'
print >> sys.stderr, err
raise CodeError('ERROR on remctl')