Search in sources :

Example 6 with DeleteVMSnapshotAnswer

use of com.cloud.agent.api.DeleteVMSnapshotAnswer in project cloudstack by apache.

the class VmwareStorageManagerImpl method execute.

@Override
public DeleteVMSnapshotAnswer execute(VmwareHostService hostService, DeleteVMSnapshotCommand cmd) {
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VirtualMachineMO vmMo = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    String vmName = cmd.getVmName();
    String vmSnapshotName = cmd.getTarget().getSnapshotName();
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        }
        if (vmMo == null) {
            String msg = "Unable to find VM for RevertToVMSnapshotCommand";
            s_logger.debug(msg);
            return new DeleteVMSnapshotAnswer(cmd, false, msg);
        } else {
            if (vmMo.getSnapshotMor(vmSnapshotName) == null) {
                s_logger.debug("can not find the snapshot " + vmSnapshotName + ", assume it is already removed");
            } else {
                if (!vmMo.removeSnapshot(vmSnapshotName, false)) {
                    String msg = "delete vm snapshot " + vmSnapshotName + " due to error occured in vmware";
                    s_logger.error(msg);
                    return new DeleteVMSnapshotAnswer(cmd, false, msg);
                }
            }
            s_logger.debug("snapshot: " + vmSnapshotName + " is removed");
            // after removed snapshot, the volumes' paths have been changed for the VM, needs to report new paths to manager
            Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
            setVolumeToPathAndSize(listVolumeTo, mapNewDisk, context, hyperHost, cmd.getVmName());
            return new DeleteVMSnapshotAnswer(cmd, listVolumeTo);
        }
    } catch (Exception e) {
        String msg = e.getMessage();
        s_logger.error("failed to delete vm snapshot " + vmSnapshotName + " of vm " + vmName + " due to " + msg);
        return new DeleteVMSnapshotAnswer(cmd, false, msg);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 7 with DeleteVMSnapshotAnswer

use of com.cloud.agent.api.DeleteVMSnapshotAnswer in project cloudstack by apache.

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<VDI>();
        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(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer)

Aggregations

DeleteVMSnapshotAnswer (com.cloud.agent.api.DeleteVMSnapshotAnswer)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)5 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)3 CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)2 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)2 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)2 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)2 GuestOSVO (com.cloud.storage.GuestOSVO)2 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)2 UserVmVO (com.cloud.vm.UserVmVO)2 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)2 IOException (java.io.IOException)2 RemoteException (java.rmi.RemoteException)2 ArrayList (java.util.ArrayList)2 ConfigurationException (javax.naming.ConfigurationException)2 Answer (com.cloud.agent.api.Answer)1 Command (com.cloud.agent.api.Command)1 DeleteVMSnapshotCommand (com.cloud.agent.api.DeleteVMSnapshotCommand)1