use of com.cloud.engine.subsystem.api.storage.VMSnapshotStrategy in project cosmic by MissionCriticalCloud.
the class VMSnapshotManagerImpl method orchestrateCreateVMSnapshot.
private VMSnapshot orchestrateCreateVMSnapshot(final Long vmId, final Long vmSnapshotId, final Boolean quiescevm) {
final UserVmVO userVm = _userVMDao.findById(vmId);
if (userVm == null) {
throw new InvalidParameterValueException("Create vm to snapshot failed due to vm: " + vmId + " is not found");
}
final 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");
}
final 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");
}
final VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
if (vmSnapshot == null) {
throw new CloudRuntimeException("VM snapshot id: " + vmSnapshotId + " can not be found");
}
final VMSnapshotOptions options = new VMSnapshotOptions(quiescevm);
vmSnapshot.setOptions(options);
try {
final VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshot);
return strategy.takeVMSnapshot(vmSnapshot);
} catch (final Exception e) {
s_logger.debug("Failed to create vm snapshot: " + vmSnapshotId, e);
return null;
}
}
use of com.cloud.engine.subsystem.api.storage.VMSnapshotStrategy in project cosmic by MissionCriticalCloud.
the class VMSnapshotManagerImpl method syncVMSnapshot.
@Override
public boolean syncVMSnapshot(final VMInstanceVO vm, final Long hostId) {
try {
final UserVmVO userVm = _userVMDao.findById(vm.getId());
if (userVm == null) {
return false;
}
final List<VMSnapshotVO> vmSnapshotsInExpungingStates = _vmSnapshotDao.listByInstanceId(vm.getId(), VMSnapshot.State.Expunging, VMSnapshot.State.Reverting, VMSnapshot.State.Creating);
for (final VMSnapshotVO vmSnapshotVO : vmSnapshotsInExpungingStates) {
final 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 (final 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;
}
use of com.cloud.engine.subsystem.api.storage.VMSnapshotStrategy in project cosmic by MissionCriticalCloud.
the class VMSnapshotManagerImpl method orchestrateDeleteAllVMSnapshots.
private boolean orchestrateDeleteAllVMSnapshots(final long vmId, final VMSnapshot.Type type) {
boolean result = true;
final List<VMSnapshotVO> listVmSnapshots = _vmSnapshotDao.findByVm(vmId);
if (listVmSnapshots == null || listVmSnapshots.isEmpty()) {
return true;
}
for (final VMSnapshotVO snapshot : listVmSnapshots) {
final VMSnapshotVO target = _vmSnapshotDao.findById(snapshot.getId());
if (type != null && target.getType() != type) {
continue;
}
final VMSnapshotStrategy strategy = findVMSnapshotStrategy(target);
if (!strategy.deleteVMSnapshot(target)) {
result = false;
break;
}
}
return result;
}
use of com.cloud.engine.subsystem.api.storage.VMSnapshotStrategy in project cosmic by MissionCriticalCloud.
the class VMSnapshotManagerImpl method orchestrateRevertToVMSnapshot.
private UserVm orchestrateRevertToVMSnapshot(final 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);
}
final 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");
}
final 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.");
}
final UserVmVO vm;
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<>(), null);
vm = _userVMDao.findById(userVm.getId());
hostId = vm.getHostId();
} catch (final 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 (final 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 {
final VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshotVo);
strategy.revertVMSnapshot(vmSnapshotVo);
return userVm;
} catch (final Exception e) {
s_logger.debug("Failed to revert vmsnapshot: " + vmSnapshotId, e);
throw new CloudRuntimeException(e.getMessage());
}
}
use of com.cloud.engine.subsystem.api.storage.VMSnapshotStrategy in project cosmic by MissionCriticalCloud.
the class VMSnapshotManagerImpl method orchestrateDeleteVMSnapshot.
private boolean orchestrateDeleteVMSnapshot(final Long vmSnapshotId) {
final Account caller = getCaller();
final 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())) {
final 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 {
final VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshot);
return strategy.deleteVMSnapshot(vmSnapshot);
} catch (final Exception e) {
s_logger.debug("Failed to delete vm snapshot: " + vmSnapshotId, e);
return false;
}
}
}
Aggregations