Search in sources :

Example 1 with DeleteVMSnapshotAnswer

use of com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer in project cosmic by MissionCriticalCloud.

the class DefaultVMSnapshotStrategy method processAnswer.

@DB
protected void processAnswer(final VMSnapshotVO vmSnapshot, final UserVm userVm, final Answer as, final Long hostId) {
    try {
        Transaction.execute(new TransactionCallbackWithExceptionNoReturn<NoTransitionException>() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) throws NoTransitionException {
                if (as instanceof CreateVMSnapshotAnswer) {
                    final CreateVMSnapshotAnswer answer = (CreateVMSnapshotAnswer) as;
                    finalizeCreate(vmSnapshot, answer.getVolumeTOs());
                    vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded);
                } else if (as instanceof RevertToVMSnapshotAnswer) {
                    final RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) as;
                    finalizeRevert(vmSnapshot, answer.getVolumeTOs());
                    vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded);
                } else if (as instanceof DeleteVMSnapshotAnswer) {
                    final DeleteVMSnapshotAnswer answer = (DeleteVMSnapshotAnswer) as;
                    finalizeDelete(vmSnapshot, answer.getVolumeTOs());
                    vmSnapshotDao.remove(vmSnapshot.getId());
                }
            }
        });
    } catch (final Exception e) {
        final String errMsg = "Error while process answer: " + as.getClass() + " due to " + e.getMessage();
        s_logger.error(errMsg, e);
        throw new CloudRuntimeException(errMsg);
    }
}
Also used : CreateVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.CreateVMSnapshotAnswer) RevertToVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) TransactionStatus(com.cloud.utils.db.TransactionStatus) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) ConfigurationException(javax.naming.ConfigurationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) DB(com.cloud.utils.db.DB)

Example 2 with DeleteVMSnapshotAnswer

use of com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testDeleteVMSnapshot.

@Test
public void testDeleteVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    final Long hostId = 1L;
    final Long vmId = 1L;
    final Long guestOsId = 1L;
    final HypervisorType hypervisorType = HypervisorType.Any;
    final String hypervisorVersion = "default";
    final String guestOsName = "Other";
    final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
    final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
    Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    Mockito.when(vmSnapshot.getId()).thenReturn(1L);
    Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    final HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final DeleteVMSnapshotAnswer answer = Mockito.mock(DeleteVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    final boolean result = vmSnapshotStrategy.deleteVMSnapshot(vmSnapshot);
    assertTrue(result);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) HypervisorType(com.cloud.model.enumeration.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.legacymodel.to.VMSnapshotTO) Command(com.cloud.legacymodel.communication.command.Command) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) Test(org.junit.Test)

Example 3 with DeleteVMSnapshotAnswer

use of com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer in project cosmic by MissionCriticalCloud.

the class CitrixDeleteVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final DeleteVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String snapshotName = command.getTarget().getSnapshotName();
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final List<VDI> vdiList = new ArrayList<>();
        final Set<VM> snapshots = VM.getByNameLabel(conn, snapshotName);
        if (snapshots == null || snapshots.size() == 0) {
            s_logger.warn("VM snapshot with name " + snapshotName + " does not exist, assume it is already deleted");
            return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());
        }
        final VM snapshot = snapshots.iterator().next();
        final Set<VBD> vbds = snapshot.getVBDs(conn);
        for (final VBD vbd : vbds) {
            if (vbd.getType(conn) == Types.VbdType.DISK) {
                final VDI vdi = vbd.getVDI(conn);
                vdiList.add(vdi);
            }
        }
        if (command.getTarget().getType() == VMSnapshot.Type.DiskAndMemory) {
            vdiList.add(snapshot.getSuspendVDI(conn));
        }
        snapshot.destroy(conn);
        for (final VDI vdi : vdiList) {
            vdi.destroy(conn);
        }
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException ex) {
        }
        // re-calculate used capacify for this VM snapshot
        for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
            final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName());
            volumeTo.setSize(size);
        }
        return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());
    } catch (final Exception e) {
        s_logger.warn("Catch Exception: " + e.getClass().toString() + " due to " + e.toString(), e);
        return new DeleteVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer)

Example 4 with DeleteVMSnapshotAnswer

use of com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtDeleteVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final DeleteVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    Domain dm = null;
    DomainSnapshot snapshot = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
        // only remove this snapshot, not children
        snapshot.delete(0);
        return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
    } catch (final LibvirtException e) {
        final String msg = " Delete VM snapshot failed due to " + e.toString();
        if (dm == null) {
            s_logger.debug("Can not find running vm: " + vmName + ", now we are trying to delete the vm snapshot using qemu-img if the format of root volume is QCOW2");
            VolumeObjectTO rootVolume = null;
            for (final VolumeObjectTO volume : cmd.getVolumeTOs()) {
                if (volume.getVolumeType() == VolumeType.ROOT) {
                    rootVolume = volume;
                    break;
                }
            }
            if (rootVolume != null && ImageFormat.QCOW2.equals(rootVolume.getFormat())) {
                final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) rootVolume.getDataStore();
                final KvmPhysicalDisk rootDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), rootVolume.getPath());
                final String command = "qemu-img snapshot -l " + rootDisk.getPath() + " | tail -n +3 | awk -F ' ' '{print $2}' | grep ^" + cmd.getTarget().getSnapshotName() + "$";
                final String qemu_img_snapshot = Script.runSimpleBashScript(command);
                if (qemu_img_snapshot == null) {
                    s_logger.info("Cannot find snapshot " + cmd.getTarget().getSnapshotName() + " in file " + rootDisk.getPath() + ", return true");
                    return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
                }
                final int result = Script.runSimpleBashScriptForExitValue("qemu-img snapshot -d " + cmd.getTarget().getSnapshotName() + " " + rootDisk.getPath());
                if (result != 0) {
                    return new DeleteVMSnapshotAnswer(cmd, false, "Delete VM Snapshot Failed due to can not remove snapshot from image file " + rootDisk.getPath() + " : " + result);
                } else {
                    return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
                }
            }
        } else if (snapshot == null) {
            s_logger.debug("Can not find vm snapshot " + cmd.getTarget().getSnapshotName() + " on vm: " + vmName + ", return true");
            return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
        }
        s_logger.warn(msg, e);
        return new DeleteVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) Domain(org.libvirt.Domain) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

Aggregations

DeleteVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer)4 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)3 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)2 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)2 ArrayList (java.util.ArrayList)2 KvmPhysicalDisk (com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)1 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)1 HostVO (com.cloud.host.HostVO)1 CreateVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.CreateVMSnapshotAnswer)1 RevertToVMSnapshotAnswer (com.cloud.legacymodel.communication.answer.RevertToVMSnapshotAnswer)1 Command (com.cloud.legacymodel.communication.command.Command)1 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)1 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)1 VMSnapshotTO (com.cloud.legacymodel.to.VMSnapshotTO)1 HypervisorType (com.cloud.model.enumeration.HypervisorType)1 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)1 GuestOSVO (com.cloud.storage.GuestOSVO)1 DB (com.cloud.utils.db.DB)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1