def cleanupVServer(id):
    '''
    Cleans up any unused files and network devices
    '''
    name = _getVMName(id)
    network.removeVServerTaps(id)

    _cleanupRunFiles(name)
def cleanupVServer(id):
    '''
    Cleans up any unused files and network devices
    '''
    name = _getVMName(id)
    network.removeVServerTaps(id)

    _cleanupRunFiles(name)
def startVServer(id, ramMiB, disks, networkCards):
    if isVServerRunning(id):
        _logger.warning(
            'Tried to start vserver "vm{0}" even though it was already running. Doing nothing instead.'
            .format(id))
        return

    cleanupVServer(id)

    name = _getVMName(id)
    try:
        kvm = KVMCommand()

        kvm.setDaemon(_getPIDFilename(name))
        kvm.enableMonitor(_getMonitorFilename(name))
        kvm.setName(name)
        kvm.setRAM(ramMiB)
        kvm.setCPUCount(1)
        kvm.disableVGAAndSetupSerialConsole(_getSerialFilename(name))
        #kvm.enableVNC(_getVNCFilename(name))

        for i, x in enumerate(disks):
            if i == 0:
                boot = True
            else:
                boot = False
            kvm.addDisk(x['path'], i, boot, x.get('model', 'ide'))

        networks = [x['network'] for x in networkCards]
        taps = network.configureVServerTaps(id, networks)
        for i, x in enumerate(taps):
            kvm.addNIC(x, i, networkCards[i].get('model', 'e1000'),
                       _createLocalUniqueMACAddress(id, i))

        checkedSystemCall(kvm.getCommand())
        _logger.info('Started vserver "{0}".'.format(name))
    except:
        _logger.error('Could not start vserver "{0}".'.format(name))
        network.removeVServerTaps(name)
        raise
def startVServer(id, ramMiB, disks, networkCards):
    if isVServerRunning(id):
        _logger.warning('Tried to start vserver "vm{0}" even though it was already running. Doing nothing instead.'.format(id))
        return

    cleanupVServer(id)

    name = _getVMName(id)
    try:
        kvm = KVMCommand()

        kvm.setDaemon(_getPIDFilename(name))
        kvm.enableMonitor(_getMonitorFilename(name))
        kvm.setName(name)
        kvm.setRAM(ramMiB)
        kvm.setCPUCount(1)
        kvm.disableVGAAndSetupSerialConsole(_getSerialFilename(name))
        #kvm.enableVNC(_getVNCFilename(name))

        for i, x in enumerate(disks):
            if i == 0:
                boot = True
            else:
                boot = False
            kvm.addDisk(x['path'], i, boot, x.get('model', 'ide'))

        networks = [x['network'] for x in networkCards]
        taps = network.configureVServerTaps(id, networks)
        for i, x in enumerate(taps):
            kvm.addNIC(x, i, networkCards[i].get('model', 'e1000'), _createLocalUniqueMACAddress(id, i))

        checkedSystemCall(kvm.getCommand())
        _logger.info('Started vserver "{0}".'.format(name))
    except:
        _logger.error('Could not start vserver "{0}".'.format(name))
        network.removeVServerTaps(name)
        raise