Search in sources :

Example 11 with VMSnapshotVO

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;
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) UpdateBuilder(com.cloud.utils.db.UpdateBuilder) Date(java.util.Date)

Example 12 with VMSnapshotVO

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);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO)

Example 13 with VMSnapshotVO

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());
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) GuestOSVO(com.cloud.storage.GuestOSVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Answer(com.cloud.agent.api.Answer) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) DeleteVMSnapshotCommand(com.cloud.agent.api.DeleteVMSnapshotCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO)

Example 14 with VMSnapshotVO

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;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) RevertToVMSnapshotCommand(com.cloud.agent.api.RevertToVMSnapshotCommand) GuestOSVO(com.cloud.storage.GuestOSVO) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO)

Example 15 with VMSnapshotVO

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);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO)

Aggregations

VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)35 UserVmVO (com.cloud.vm.UserVmVO)27 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)20 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)20 Account (com.cloud.user.Account)17 HostVO (com.cloud.host.HostVO)16 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)15 GuestOSVO (com.cloud.storage.GuestOSVO)15 Test (org.junit.Test)14 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)13 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)13 ArrayList (java.util.ArrayList)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 VolumeVO (com.cloud.storage.VolumeVO)11 ConfigurationException (javax.naming.ConfigurationException)11 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 IOException (java.io.IOException)10