Search in sources :

Example 11 with RevertToVMSnapshotAnswer

use of com.cloud.agent.api.RevertToVMSnapshotAnswer in project cosmic by MissionCriticalCloud.

the class CitrixRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = command.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    final Connection conn = citrixResourceBase.getConnection();
    PowerState vmState = null;
    VM vm = null;
    try {
        final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
        if (vmSnapshots == null || vmSnapshots.size() == 0) {
            return new RevertToVMSnapshotAnswer(command, false, "Cannot find vmSnapshot with name: " + command.getTarget().getSnapshotName());
        }
        final VM vmSnapshot = vmSnapshots.iterator().next();
        // find target VM or creating a work VM
        try {
            vm = citrixResourceBase.getVM(conn, vmName);
        } catch (final Exception e) {
            vm = citrixResourceBase.createWorkingVM(conn, vmName, command.getGuestOSType(), command.getPlatformEmulator(), listVolumeTo);
        }
        if (vm == null) {
            return new RevertToVMSnapshotAnswer(command, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        // call plugin to execute revert
        citrixResourceBase.revertToSnapshot(conn, vmSnapshot, vmName, vm.getUuid(conn), snapshotMemory, citrixResourceBase.getHost().getUuid());
        vm = citrixResourceBase.getVM(conn, vmName);
        final Set<VBD> vbds = vm.getVBDs(conn);
        final Map<String, VDI> vdiMap = new HashMap<>();
        // get vdi:vbdr to a map
        for (final VBD vbd : vbds) {
            final VBD.Record vbdr = vbd.getRecord(conn);
            if (vbdr.type == Types.VbdType.DISK) {
                final VDI vdi = vbdr.VDI;
                vdiMap.put(vbdr.userdevice, vdi);
            }
        }
        if (!snapshotMemory) {
            vm.destroy(conn);
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        // after revert, VM's volumes path have been changed, need to report to manager
        for (final VolumeObjectTO volumeTo : listVolumeTo) {
            final Long deviceId = volumeTo.getDeviceId();
            final VDI vdi = vdiMap.get(deviceId.toString());
            volumeTo.setPath(vdi.getUuid(conn));
        }
        return new RevertToVMSnapshotAnswer(command, listVolumeTo, vmState);
    } catch (final Exception e) {
        s_logger.error("revert vm " + vmName + " to snapshot " + command.getTarget().getSnapshotName() + " failed due to " + e.getMessage());
        return new RevertToVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VM(com.xensource.xenapi.VM) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 12 with RevertToVMSnapshotAnswer

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

the class CitrixRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    final List<VolumeObjectTO> listVolumeTo = command.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = command.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    final Connection conn = citrixResourceBase.getConnection();
    PowerState vmState = null;
    VM vm = null;
    try {
        final Set<VM> vmSnapshots = VM.getByNameLabel(conn, command.getTarget().getSnapshotName());
        if (vmSnapshots == null || vmSnapshots.size() == 0) {
            return new RevertToVMSnapshotAnswer(command, false, "Cannot find vmSnapshot with name: " + command.getTarget().getSnapshotName());
        }
        final VM vmSnapshot = vmSnapshots.iterator().next();
        // find target VM or creating a work VM
        try {
            vm = citrixResourceBase.getVM(conn, vmName);
        } catch (final Exception e) {
            vm = citrixResourceBase.createWorkingVM(conn, vmName, command.getGuestOSType(), command.getPlatformEmulator(), listVolumeTo);
        }
        if (vm == null) {
            return new RevertToVMSnapshotAnswer(command, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        // call plugin to execute revert
        citrixResourceBase.revertToSnapshot(conn, vmSnapshot, vmName, vm.getUuid(conn), snapshotMemory, citrixResourceBase.getHost().getUuid());
        vm = citrixResourceBase.getVM(conn, vmName);
        final Set<VBD> vbds = vm.getVBDs(conn);
        final Map<String, VDI> vdiMap = new HashMap<String, VDI>();
        // get vdi:vbdr to a map
        for (final VBD vbd : vbds) {
            final VBD.Record vbdr = vbd.getRecord(conn);
            if (vbdr.type == Types.VbdType.DISK) {
                final VDI vdi = vbdr.VDI;
                vdiMap.put(vbdr.userdevice, vdi);
            }
        }
        if (!snapshotMemory) {
            vm.destroy(conn);
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        // after revert, VM's volumes path have been changed, need to report to manager
        for (final VolumeObjectTO volumeTo : listVolumeTo) {
            final Long deviceId = volumeTo.getDeviceId();
            final VDI vdi = vdiMap.get(deviceId.toString());
            volumeTo.setPath(vdi.getUuid(conn));
        }
        return new RevertToVMSnapshotAnswer(command, listVolumeTo, vmState);
    } catch (final Exception e) {
        s_logger.error("revert vm " + vmName + " to snapshot " + command.getTarget().getSnapshotName() + " failed due to " + e.getMessage());
        return new RevertToVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VM(com.xensource.xenapi.VM) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 13 with RevertToVMSnapshotAnswer

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

the class VmwareStorageManagerImpl method execute.

@Override
public RevertToVMSnapshotAnswer execute(VmwareHostService hostService, RevertToVMSnapshotCommand cmd) {
    String snapshotName = cmd.getTarget().getSnapshotName();
    String vmName = cmd.getVmName();
    Boolean snapshotMemory = cmd.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
    VirtualMachineMO vmMo = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        // wait if there are already VM revert task running
        ManagedObjectReference taskmgr = context.getServiceContent().getTaskManager();
        List<ManagedObjectReference> tasks = context.getVimClient().getDynamicProperty(taskmgr, "recentTask");
        for (ManagedObjectReference taskMor : tasks) {
            TaskInfo info = (TaskInfo) (context.getVimClient().getDynamicProperty(taskMor, "info"));
            if (info.getEntityName().equals(cmd.getVmName()) && org.apache.commons.lang3.StringUtils.isNotBlank(info.getName()) && info.getName().equalsIgnoreCase("RevertToSnapshot_Task")) {
                s_logger.debug("There is already a VM snapshot task running, wait for it");
                context.getVimClient().waitForTask(taskMor);
            }
        }
        HostMO hostMo = (HostMO) hyperHost;
        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 RevertToVMSnapshotAnswer(cmd, false, msg);
        } else {
            if (cmd.isReloadVm()) {
                vmMo.reload();
            }
            boolean result = false;
            if (snapshotName != null) {
                ManagedObjectReference morSnapshot = vmMo.getSnapshotMor(snapshotName);
                result = hostMo.revertToSnapshot(morSnapshot);
            } else {
                return new RevertToVMSnapshotAnswer(cmd, false, "Unable to find the snapshot by name " + snapshotName);
            }
            if (result) {
                Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
                setVolumeToPathAndSize(listVolumeTo, mapNewDisk, context, hyperHost, cmd.getVmName());
                if (!snapshotMemory) {
                    vmState = VirtualMachine.PowerState.PowerOff;
                }
                return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
            } else {
                return new RevertToVMSnapshotAnswer(cmd, false, "Error while reverting to snapshot due to execute in ESXi");
            }
        }
    } catch (Exception e) {
        String msg = "revert vm " + vmName + " to snapshot " + snapshotName + " failed due to " + e.getMessage();
        s_logger.error(msg);
        return new RevertToVMSnapshotAnswer(cmd, false, msg);
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TaskInfo(com.vmware.vim25.TaskInfo) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VirtualMachine(com.cloud.vm.VirtualMachine) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 14 with RevertToVMSnapshotAnswer

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

the class DefaultVMSnapshotStrategy method revertVMSnapshot.

@Override
public boolean revertVMSnapshot(VMSnapshot vmSnapshot) {
    VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.RevertRequested);
    } catch (NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    boolean result = false;
    try {
        VMSnapshotVO snapshot = vmSnapshotDao.findById(vmSnapshotVO.getId());
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        String vmInstanceName = userVm.getInstanceName();
        VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(snapshot).getParent();
        VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, true);
        Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
        GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
        RevertToVMSnapshotCommand revertToSnapshotCommand = new RevertToVMSnapshotCommand(vmInstanceName, userVm.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
        HostVO host = hostDao.findById(hostId);
        GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
        if (guestOsMapping == null) {
            revertToSnapshotCommand.setPlatformEmulator(null);
        } else {
            revertToSnapshotCommand.setPlatformEmulator(guestOsMapping.getGuestOsName());
        }
        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 (OperationTimedoutException e) {
        s_logger.debug("Failed to revert vm snapshot", e);
        throw new CloudRuntimeException(e.getMessage());
    } catch (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 (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(org.apache.cloudstack.storage.to.VolumeObjectTO)

Aggregations

RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)7 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)7 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)6 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)5 HostVO (com.cloud.host.HostVO)5 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)5 GuestOSVO (com.cloud.storage.GuestOSVO)5 UserVmVO (com.cloud.vm.UserVmVO)5 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)5 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)4 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)4 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)4 IOException (java.io.IOException)4 Command (com.cloud.agent.api.Command)3 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)3 PowerState (com.cloud.vm.VirtualMachine.PowerState)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3