Search in sources :

Example 1 with VMSnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy in project cloudstack by apache.

the class VMSnapshotManagerImpl method orchestrateDeleteAllVMSnapshots.

private boolean orchestrateDeleteAllVMSnapshots(long vmId, VMSnapshot.Type type) {
    boolean result = true;
    List<VMSnapshotVO> listVmSnapshots = _vmSnapshotDao.findByVm(vmId);
    if (listVmSnapshots == null || listVmSnapshots.isEmpty()) {
        return true;
    }
    for (VMSnapshotVO snapshot : listVmSnapshots) {
        VMSnapshotVO target = _vmSnapshotDao.findById(snapshot.getId());
        if (type != null && target.getType() != type)
            continue;
        VMSnapshotStrategy strategy = findVMSnapshotStrategy(target);
        if (!strategy.deleteVMSnapshot(target)) {
            result = false;
            break;
        }
    }
    return result;
}
Also used : VMSnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy)

Example 2 with VMSnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy in project cloudstack by apache.

the class VMSnapshotManagerImpl method syncVMSnapshot.

@Override
public boolean syncVMSnapshot(VMInstanceVO vm, Long hostId) {
    try {
        UserVmVO userVm = _userVMDao.findById(vm.getId());
        if (userVm == null)
            return false;
        List<VMSnapshotVO> vmSnapshotsInExpungingStates = _vmSnapshotDao.listByInstanceId(vm.getId(), VMSnapshot.State.Expunging, VMSnapshot.State.Reverting, VMSnapshot.State.Creating);
        for (VMSnapshotVO vmSnapshotVO : vmSnapshotsInExpungingStates) {
            VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshotVO);
            if (vmSnapshotVO.getState() == VMSnapshot.State.Expunging) {
                return strategy.deleteVMSnapshot(vmSnapshotVO);
            } else if (vmSnapshotVO.getState() == VMSnapshot.State.Creating) {
                return strategy.takeVMSnapshot(vmSnapshotVO) != null;
            } else if (vmSnapshotVO.getState() == VMSnapshot.State.Reverting) {
                return strategy.revertVMSnapshot(vmSnapshotVO);
            }
        }
    } catch (Exception e) {
        s_logger.error(e.getMessage(), e);
        if (_vmSnapshotDao.listByInstanceId(vm.getId(), VMSnapshot.State.Expunging).size() == 0)
            return true;
        else
            return false;
    }
    return false;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VMSnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ManagementServerException(com.cloud.exception.ManagementServerException)

Example 3 with VMSnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy in project cloudstack by apache.

the class VMSnapshotManagerImpl method orchestrateDeleteVMSnapshot.

private boolean orchestrateDeleteVMSnapshot(Long vmSnapshotId) {
    Account caller = getCaller();
    VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new InvalidParameterValueException("unable to find the vm snapshot with id " + vmSnapshotId);
    }
    _accountMgr.checkAccess(caller, null, true, vmSnapshot);
    // check VM snapshot states, only allow to delete vm snapshots in created and error state
    if (VMSnapshot.State.Ready != vmSnapshot.getState() && VMSnapshot.State.Expunging != vmSnapshot.getState() && VMSnapshot.State.Error != vmSnapshot.getState()) {
        throw new InvalidParameterValueException("Can't delete the vm snapshotshot " + vmSnapshotId + " due to it is not in Created or Error, or Expunging State");
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(vmSnapshot.getVmId())) {
        List<VMSnapshotVO> expungingSnapshots = _vmSnapshotDao.listByInstanceId(vmSnapshot.getVmId(), VMSnapshot.State.Expunging);
        if (expungingSnapshots.size() > 0 && expungingSnapshots.get(0).getId() == vmSnapshot.getId())
            s_logger.debug("Target VM snapshot already in expunging state, go on deleting it: " + vmSnapshot.getDisplayName());
        else
            throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
    }
    if (vmSnapshot.getState() == VMSnapshot.State.Allocated) {
        return _vmSnapshotDao.remove(vmSnapshot.getId());
    } else {
        try {
            VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshot);
            return strategy.deleteVMSnapshot(vmSnapshot);
        } catch (Exception e) {
            s_logger.debug("Failed to delete vm snapshot: " + vmSnapshotId, e);
            return false;
        }
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMSnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ManagementServerException(com.cloud.exception.ManagementServerException)

Example 4 with VMSnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy in project cloudstack by apache.

the class VMSnapshotManagerImpl method orchestrateRevertToVMSnapshot.

private UserVm orchestrateRevertToVMSnapshot(Long vmSnapshotId) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException {
    // check if VM snapshot exists in DB
    final VMSnapshotVO vmSnapshotVo = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshotVo == null) {
        throw new InvalidParameterValueException("unable to find the vm snapshot with id " + vmSnapshotId);
    }
    Long vmId = vmSnapshotVo.getVmId();
    final UserVmVO userVm = _userVMDao.findById(vmId);
    // check if VM exists
    if (userVm == null) {
        throw new InvalidParameterValueException("Revert vm to snapshot: " + vmSnapshotId + " failed due to vm: " + vmId + " is not found");
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(vmId)) {
        throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
    }
    Account caller = getCaller();
    _accountMgr.checkAccess(caller, null, true, vmSnapshotVo);
    // VM should be in running or stopped states
    if (userVm.getState() != VirtualMachine.State.Running && userVm.getState() != VirtualMachine.State.Stopped) {
        throw new InvalidParameterValueException("VM Snapshot reverting failed due to vm is not in the state of Running or Stopped.");
    }
    // if snapshot is not created, error out
    if (vmSnapshotVo.getState() != VMSnapshot.State.Ready) {
        throw new InvalidParameterValueException("VM Snapshot reverting failed due to vm snapshot is not in the state of Created.");
    }
    UserVmVO vm = null;
    Long hostId = null;
    // start or stop VM first, if revert from stopped state to running state, or from running to stopped
    if (userVm.getState() == VirtualMachine.State.Stopped && vmSnapshotVo.getType() == VMSnapshot.Type.DiskAndMemory) {
        try {
            _itMgr.advanceStart(userVm.getUuid(), new HashMap<VirtualMachineProfile.Param, Object>(), null);
            vm = _userVMDao.findById(userVm.getId());
            hostId = vm.getHostId();
        } catch (Exception e) {
            s_logger.error("Start VM " + userVm.getInstanceName() + " before reverting failed due to " + e.getMessage());
            throw new CloudRuntimeException(e.getMessage());
        }
    } else {
        if (userVm.getState() == VirtualMachine.State.Running && vmSnapshotVo.getType() == VMSnapshot.Type.Disk) {
            try {
                _itMgr.advanceStop(userVm.getUuid(), true);
            } catch (Exception e) {
                s_logger.error("Stop VM " + userVm.getInstanceName() + " before reverting failed due to " + e.getMessage());
                throw new CloudRuntimeException(e.getMessage());
            }
        }
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(userVm.getId())) {
        throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
    }
    try {
        VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshotVo);
        strategy.revertVMSnapshot(vmSnapshotVo);
        Transaction.execute(new TransactionCallbackWithExceptionNoReturn<CloudRuntimeException>() {

            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) throws CloudRuntimeException {
                revertUserVmDetailsFromVmSnapshot(userVm, vmSnapshotVo);
                updateUserVmServiceOffering(userVm, vmSnapshotVo);
            }
        });
        return userVm;
    } catch (Exception e) {
        s_logger.debug("Failed to revert vmsnapshot: " + vmSnapshotId, e);
        throw new CloudRuntimeException(e.getMessage());
    }
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VMSnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ManagementServerException(com.cloud.exception.ManagementServerException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 5 with VMSnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy in project cloudstack by apache.

the class VMSnapshotManagerImpl method orchestrateCreateVMSnapshot.

private VMSnapshot orchestrateCreateVMSnapshot(Long vmId, Long vmSnapshotId, Boolean quiescevm) {
    UserVmVO userVm = _userVMDao.findById(vmId);
    if (userVm == null) {
        throw new InvalidParameterValueException("Create vm to snapshot failed due to vm: " + vmId + " is not found");
    }
    List<VolumeVO> volumeVos = _volumeDao.findByInstanceAndType(vmId, Type.ROOT);
    if (volumeVos == null || volumeVos.isEmpty()) {
        throw new CloudRuntimeException("Create vm to snapshot failed due to no root disk found");
    }
    VolumeVO rootVolume = volumeVos.get(0);
    if (!rootVolume.getState().equals(Volume.State.Ready)) {
        throw new CloudRuntimeException("Create vm to snapshot failed due to vm: " + vmId + " has root disk in " + rootVolume.getState() + " state");
    }
    VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new CloudRuntimeException("VM snapshot id: " + vmSnapshotId + " can not be found");
    }
    VMSnapshotOptions options = new VMSnapshotOptions(quiescevm);
    vmSnapshot.setOptions(options);
    try {
        VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshot);
        VMSnapshot snapshot = strategy.takeVMSnapshot(vmSnapshot);
        return snapshot;
    } catch (Exception e) {
        s_logger.debug("Failed to create vm snapshot: " + vmSnapshotId, e);
        return null;
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshotOptions(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions) VMSnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ManagementServerException(com.cloud.exception.ManagementServerException)

Aggregations

VMSnapshotStrategy (org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy)5 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 ManagementServerException (com.cloud.exception.ManagementServerException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)4 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 ConfigurationException (javax.naming.ConfigurationException)4 UserVmVO (com.cloud.vm.UserVmVO)3 Account (com.cloud.user.Account)2 VolumeVO (com.cloud.storage.VolumeVO)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 VMSnapshotOptions (org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions)1