use of com.cloud.vm.snapshot.VMSnapshotVO in project cosmic by MissionCriticalCloud.
the class VMSnapshotDaoImpl method updateState.
@Override
public boolean updateState(final State currentState, final Event event, final State nextState, final VMSnapshot vo, final Object data) {
final Long oldUpdated = vo.getUpdatedCount();
final Date oldUpdatedTime = vo.getUpdated();
final SearchCriteria<VMSnapshotVO> sc = AllFieldsSearch.create();
sc.setParameters("id", vo.getId());
sc.setParameters("state", currentState);
sc.setParameters("updatedCount", vo.getUpdatedCount());
vo.incrUpdatedCount();
final UpdateBuilder builder = getUpdateBuilder(vo);
builder.set(vo, "state", nextState);
builder.set(vo, "updated", new Date());
final int rows = update((VMSnapshotVO) vo, sc);
if (rows == 0 && s_logger.isDebugEnabled()) {
final VMSnapshotVO dbVol = findByIdIncludingRemoved(vo.getId());
if (dbVol != null) {
final StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString());
str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()).append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=").append(dbVol.getUpdated());
str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()).append("; updatedTime=").append(vo.getUpdated());
str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated).append("; updatedTime=").append(oldUpdatedTime);
} else {
s_logger.debug("Unable to update VM snapshot: id=" + vo.getId() + ", as there is no such snapshot exists in the database anymore");
}
}
return rows > 0;
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method finalizeDelete.
protected void finalizeDelete(final VMSnapshotVO vmSnapshot, final List<VolumeObjectTO> volumeTOs) {
// update volumes path
updateVolumePath(volumeTOs);
// update children's parent snapshots
final List<VMSnapshotVO> children = vmSnapshotDao.listByParent(vmSnapshot.getId());
for (final VMSnapshotVO child : children) {
child.setParent(vmSnapshot.getParent());
vmSnapshotDao.persist(child);
}
// update current snapshot
final VMSnapshotVO current = vmSnapshotDao.findCurrentSnapshotByVmId(vmSnapshot.getVmId());
if (current != null && current.getId() == vmSnapshot.getId() && vmSnapshot.getParent() != null) {
final VMSnapshotVO parent = vmSnapshotDao.findById(vmSnapshot.getParent());
parent.setCurrent(true);
vmSnapshotDao.persist(parent);
}
vmSnapshot.setCurrent(false);
vmSnapshotDao.persist(vmSnapshot);
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method deleteVMSnapshot.
@Override
public boolean deleteVMSnapshot(final VMSnapshot vmSnapshot) {
final UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.ExpungeRequested);
} catch (final NoTransitionException e) {
s_logger.debug("Failed to change vm snapshot state with event ExpungeRequested");
throw new CloudRuntimeException("Failed to change vm snapshot state with event ExpungeRequested: " + e.getMessage());
}
try {
final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(vmSnapshot.getVmId());
final String vmInstanceName = userVm.getInstanceName();
final VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(vmSnapshotVO).getParent();
final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(vmSnapshot.getId(), vmSnapshot.getName(), vmSnapshot.getType(), vmSnapshot.getCreated().getTime(), vmSnapshot.getDescription(), vmSnapshot.getCurrent(), parent, true);
final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
final DeleteVMSnapshotCommand deleteSnapshotCommand = new DeleteVMSnapshotCommand(vmInstanceName, vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
final Answer answer = agentMgr.send(hostId, deleteSnapshotCommand);
if (answer != null && answer.getResult()) {
processAnswer(vmSnapshotVO, userVm, answer, hostId);
return true;
} else {
final String errMsg = (answer == null) ? null : answer.getDetails();
s_logger.error("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + errMsg);
throw new CloudRuntimeException("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + errMsg);
}
} catch (final OperationTimedoutException | AgentUnavailableException e) {
throw new CloudRuntimeException("Delete vm snapshot " + vmSnapshot.getName() + " of vm " + userVm.getInstanceName() + " failed due to " + e.getMessage());
}
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method revertVMSnapshot.
@Override
public boolean revertVMSnapshot(final VMSnapshot vmSnapshot) {
final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
final UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.RevertRequested);
} catch (final NoTransitionException e) {
throw new CloudRuntimeException(e.getMessage());
}
boolean result = false;
try {
final VMSnapshotVO snapshot = vmSnapshotDao.findById(vmSnapshotVO.getId());
final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
final String vmInstanceName = userVm.getInstanceName();
final VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(snapshot).getParent();
final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, true);
final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
final RevertToVMSnapshotCommand revertToSnapshotCommand = new RevertToVMSnapshotCommand(vmInstanceName, userVm.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
final HostVO host = hostDao.findById(hostId);
final GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
if (guestOsMapping == null) {
revertToSnapshotCommand.setPlatformEmulator(null);
} else {
revertToSnapshotCommand.setPlatformEmulator(guestOsMapping.getGuestOsName());
}
final RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) agentMgr.send(hostId, revertToSnapshotCommand);
if (answer != null && answer.getResult()) {
processAnswer(vmSnapshotVO, userVm, answer, hostId);
result = true;
} else {
String errMsg = "Revert VM: " + userVm.getInstanceName() + " to snapshot: " + vmSnapshotVO.getName() + " failed";
if (answer != null && answer.getDetails() != null) {
errMsg = errMsg + " due to " + answer.getDetails();
}
s_logger.error(errMsg);
throw new CloudRuntimeException(errMsg);
}
} catch (final OperationTimedoutException e) {
s_logger.debug("Failed to revert vm snapshot", e);
throw new CloudRuntimeException(e.getMessage());
} catch (final AgentUnavailableException e) {
s_logger.debug("Failed to revert vm snapshot", e);
throw new CloudRuntimeException(e.getMessage());
} finally {
if (!result) {
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
} catch (final NoTransitionException e1) {
s_logger.error("Cannot set vm snapshot state due to: " + e1.getMessage());
}
}
}
return result;
}
use of com.cloud.vm.snapshot.VMSnapshotVO in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method finalizeCreate.
protected void finalizeCreate(final VMSnapshotVO vmSnapshot, final List<VolumeObjectTO> volumeTOs) {
// update volumes path
updateVolumePath(volumeTOs);
vmSnapshot.setCurrent(true);
// change current snapshot
if (vmSnapshot.getParent() != null) {
final VMSnapshotVO previousCurrent = vmSnapshotDao.findById(vmSnapshot.getParent());
previousCurrent.setCurrent(false);
vmSnapshotDao.persist(previousCurrent);
}
vmSnapshotDao.persist(vmSnapshot);
}
Aggregations