Search in sources :

Example 41 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.

the class VmwareStorageManagerImpl method copyVolumeToSecStorage.

private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyVolumeCommand cmd, String vmName, long volumeId, String poolId, String volumePath, String secStorageUrl, String workerVmName, Integer nfsVersion) throws Exception {
    String volumeFolder = String.valueOf(volumeId) + "/";
    VirtualMachineMO workerVm = null;
    VirtualMachineMO vmMo = null;
    String exportName = UUID.randomUUID().toString();
    try {
        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
        if (morDs == null) {
            String msg = "Unable to find volumes's storage pool for copy volume operation";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            // create a dummy worker vm for attaching the volume
            DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
            workerVm = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName);
            if (workerVm == null) {
                String msg = "Unable to create worker VM to execute CopyVolumeCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            }
            //attach volume to worker VM
            String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk");
            workerVm.attachDisk(new String[] { datastoreVolumePath }, morDs);
            vmMo = workerVm;
        }
        vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false);
        exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, "volumes/" + volumeFolder, exportName, hostService.getWorkerName(hyperHost.getContext(), cmd, 1), nfsVersion);
        return new Pair<String, String>(volumeFolder, exportName);
    } finally {
        vmMo.removeSnapshot(exportName, false);
        if (workerVm != null) {
            //detach volume and destroy worker vm
            workerVm.detachAllDisks();
            workerVm.destroy();
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 42 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO 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 43 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.

the class VmwareCleanupMaid method gcLeftOverVMs.

public static synchronized void gcLeftOverVMs(VmwareContext context) {
    List<VmwareCleanupMaid> l = s_leftoverDummyVMs.get(context.getServerAddress());
    VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
    assert (mgr != null);
    if (l != null && l.size() > 0) {
        for (VmwareCleanupMaid cleanupMaid : l) {
            try {
                VirtualMachineMO vmMo = null;
                if (cleanupMaid.getDatacenterMorValue() != null) {
                    DatacenterMO dcMo = new DatacenterMO(context, "Datacenter", cleanupMaid.getDatacenterMorValue());
                    vmMo = dcMo.findVm(cleanupMaid.getVmName());
                } else {
                    assert (cleanupMaid.getHostMorValue() != null);
                    HostMO hostMo = new HostMO(context, "HostSystem", cleanupMaid.getHostMorValue());
                    ClusterMO clusterMo = new ClusterMO(context, hostMo.getHyperHostCluster());
                    vmMo = clusterMo.findVmOnHyperHost(cleanupMaid.getVmName());
                }
                if (vmMo != null) {
                    s_logger.info("Found left over dummy VM " + cleanupMaid.getVmName() + ", destroy it");
                    vmMo.destroy();
                }
            } catch (Throwable e) {
                s_logger.warn("Unable to destroy left over dummy VM " + cleanupMaid.getVmName());
            } finally {
            // FIXME                    mgr.popCleanupCheckpoint(cleanupMaid.getCheckPoint());
            }
        }
        l.clear();
    }
}
Also used : VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 44 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO 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)

Example 45 with VirtualMachineMO

use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.

the class VmwareHelper method pickOneVmOnRunningHost.

public static VirtualMachineMO pickOneVmOnRunningHost(List<VirtualMachineMO> vmList, boolean bFirstFit) throws Exception {
    List<VirtualMachineMO> candidates = new ArrayList<VirtualMachineMO>();
    for (VirtualMachineMO vmMo : vmList) {
        HostMO hostMo = vmMo.getRunningHost();
        if (hostMo.isHyperHostConnected())
            candidates.add(vmMo);
    }
    if (candidates.size() == 0)
        return null;
    if (bFirstFit)
        return candidates.get(0);
    Random random = new Random();
    return candidates.get(random.nextInt(candidates.size()));
}
Also used : Random(java.util.Random) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ArrayList(java.util.ArrayList)

Aggregations

VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)51 RemoteException (java.rmi.RemoteException)46 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)41 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)35 UnsupportedEncodingException (java.io.UnsupportedEncodingException)31 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)28 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)25 CloudException (com.cloud.exception.CloudException)17 InternalErrorException (com.cloud.exception.InternalErrorException)17 IOException (java.io.IOException)17 ConnectException (java.net.ConnectException)17 ConfigurationException (javax.naming.ConfigurationException)17 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)16 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)14 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)11 VirtualDisk (com.vmware.vim25.VirtualDisk)9 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)8 Script (com.cloud.utils.script.Script)8 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)7