Search in sources :

Example 1 with TaskInfo

use of com.vmware.vim25.TaskInfo in project cloudstack by apache.

the class VmwareStorageManagerImpl method execute.

@Override
public CreateVMSnapshotAnswer execute(VmwareHostService hostService, CreateVMSnapshotCommand cmd) {
    List<VolumeObjectTO> volumeTOs = cmd.getVolumeTOs();
    String vmName = cmd.getVmName();
    String vmSnapshotName = cmd.getTarget().getSnapshotName();
    String vmSnapshotDesc = cmd.getTarget().getDescription();
    boolean snapshotMemory = cmd.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
    boolean quiescevm = cmd.getTarget().getQuiescevm();
    VirtualMachineMO vmMo = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        // wait if there are already VM snapshot 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()) && StringUtils.isNotBlank(info.getName()) && info.getName().equalsIgnoreCase("CreateSnapshot_Task")) {
                if (!(info.getState().equals(TaskInfoState.SUCCESS) || info.getState().equals(TaskInfoState.ERROR))) {
                    s_logger.debug("There is already a VM snapshot task running, wait for it");
                    context.getVimClient().waitForTask(taskMor);
                }
            }
        }
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        }
        if (vmMo == null) {
            String msg = "Unable to find VM for CreateVMSnapshotCommand";
            s_logger.info(msg);
            return new CreateVMSnapshotAnswer(cmd, false, msg);
        } else {
            if (vmMo.getSnapshotMor(vmSnapshotName) != null) {
                s_logger.info("VM snapshot " + vmSnapshotName + " already exists");
            } else if (!vmMo.createSnapshot(vmSnapshotName, vmSnapshotDesc, snapshotMemory, quiescevm)) {
                return new CreateVMSnapshotAnswer(cmd, false, "Unable to create snapshot due to esxi internal failed");
            }
            Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
            setVolumeToPathAndSize(volumeTOs, mapNewDisk, context, hyperHost, cmd.getVmName());
            return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), volumeTOs);
        }
    } catch (Exception e) {
        String msg = e.getMessage();
        s_logger.error("failed to create snapshot for vm:" + vmName + " due to " + msg);
        try {
            if (vmMo.getSnapshotMor(vmSnapshotName) != null) {
                vmMo.removeSnapshot(vmSnapshotName, false);
            }
        } catch (Exception e1) {
            s_logger.info("[ignored]" + "error during snapshot remove: " + e1.getLocalizedMessage());
        }
        return new CreateVMSnapshotAnswer(cmd, false, e.getMessage());
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TaskInfo(com.vmware.vim25.TaskInfo) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with TaskInfo

use of com.vmware.vim25.TaskInfo in project cloudstack by apache.

the class VmwareContext method waitForTaskProgressDone.

public void waitForTaskProgressDone(ManagedObjectReference morTask) throws Exception {
    while (true) {
        TaskInfo tinfo = (TaskInfo) _vimClient.getDynamicProperty(morTask, "info");
        Integer progress = tinfo.getProgress();
        if (progress == null)
            break;
        if (progress.intValue() >= 100)
            break;
        Thread.sleep(1000);
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo)

Example 3 with TaskInfo

use of com.vmware.vim25.TaskInfo in project cloudstack by apache.

the class DistributedVirtualSwitchMO method updateVMWareDVSwitchGetTask.

public TaskInfo updateVMWareDVSwitchGetTask(ManagedObjectReference dvSwitchMor, VMwareDVSConfigSpec dvsSpec) throws Exception {
    ManagedObjectReference task = _context.getService().reconfigureDvsTask(dvSwitchMor, dvsSpec);
    TaskInfo info = (TaskInfo) (_context.getVimClient().getDynamicProperty(task, "info"));
    _context.getVimClient().waitForTask(task);
    return info;
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 4 with TaskInfo

use of com.vmware.vim25.TaskInfo in project CloudStack-archive by CloudStack-extras.

the class VmwareContext method waitForTaskProgressDone.

public void waitForTaskProgressDone(ManagedObjectReference morTask) throws Exception {
    while (true) {
        TaskInfo tinfo = (TaskInfo) getServiceUtil().getDynamicProperty(morTask, "info");
        Integer progress = tinfo.getProgress();
        if (progress == null)
            break;
        if (progress.intValue() >= 100)
            break;
        Thread.sleep(1000);
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo)

Example 5 with TaskInfo

use of com.vmware.vim25.TaskInfo 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()) && 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) RemoteException(java.rmi.RemoteException) 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)

Aggregations

TaskInfo (com.vmware.vim25.TaskInfo)5 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)3 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)2 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)2 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 RemoteException (java.rmi.RemoteException)2 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)2 CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)1 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 VirtualMachine (com.cloud.vm.VirtualMachine)1