Search in sources :

Example 56 with VmwareHypervisorHost

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

the class VmwareStorageManagerImpl method execute.

@Override
public Answer execute(VmwareHostService hostService, CreateVolumeFromSnapshotCommand cmd) {
    String primaryStorageNameLabel = cmd.getPrimaryStoragePoolNameLabel();
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
    String backedUpSnapshotUuid = cmd.getSnapshotUuid();
    String details = null;
    boolean success = false;
    String newVolumeName = UUID.randomUUID().toString().replace("-", "");
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        ManagedObjectReference morPrimaryDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStorageNameLabel);
        if (morPrimaryDs == null) {
            String msg = "Unable to find datastore: " + primaryStorageNameLabel;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs);
        details = createVolumeFromSnapshot(hyperHost, primaryDsMo, newVolumeName, accountId, volumeId, secondaryStorageUrl, backedUpSnapshotUuid, cmd.getNfsVersion());
        if (details == null) {
            success = true;
        }
    } catch (Throwable e) {
        details = hostService.createLogMessageException(e, cmd);
    }
    return new CreateVolumeFromSnapshotAnswer(cmd, success, details, newVolumeName);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)

Example 57 with VmwareHypervisorHost

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

use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost 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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 59 with VmwareHypervisorHost

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

the class VmwareStorageManagerImpl method execute.

@Override
public Answer execute(VmwareHostService hostService, CopyVolumeCommand cmd) {
    Long volumeId = cmd.getVolumeId();
    String volumePath = cmd.getVolumePath();
    String secondaryStorageURL = cmd.getSecondaryStorageURL();
    String vmName = cmd.getVmName();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        Pair<String, String> result;
        if (cmd.toSecondaryStorage()) {
            result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, volumeId, cmd.getPool().getUuid(), volumePath, secondaryStorageURL, hostService.getWorkerName(context, cmd, 0, null), cmd.getNfsVersion());
        } else {
            StorageFilerTO poolTO = cmd.getPool();
            ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolTO.getUuid());
            if (morDatastore == null) {
                morDatastore = hyperHost.mountDatastore(false, poolTO.getHost(), 0, poolTO.getPath(), poolTO.getUuid().replace("-", ""), true);
                if (morDatastore == null) {
                    throw new Exception("Unable to mount storage pool on host. storeUrl: " + poolTO.getHost() + ":/" + poolTO.getPath());
                }
            }
            result = copyVolumeFromSecStorage(hyperHost, volumeId, new DatastoreMO(context, morDatastore), secondaryStorageURL, volumePath, cmd.getNfsVersion());
            deleteVolumeDirOnSecondaryStorage(volumeId, secondaryStorageURL, cmd.getNfsVersion());
        }
        return new CopyVolumeAnswer(cmd, true, null, result.first(), result.second());
    } catch (Throwable e) {
        return new CopyVolumeAnswer(cmd, false, hostService.createLogMessageException(e, cmd), null, null);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 60 with VmwareHypervisorHost

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

the class VmwareStorageManagerImpl method execute.

@Override
@Deprecated
public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd) {
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
    // not null: Precondition.
    String snapshotUuid = cmd.getSnapshotUuid();
    String prevSnapshotUuid = cmd.getPrevSnapshotUuid();
    String prevBackupUuid = cmd.getPrevBackupUuid();
    String searchExcludedFolders = cmd.getContextParam("searchexludefolders");
    VirtualMachineMO workerVm = null;
    String workerVMName = null;
    String volumePath = cmd.getVolumePath();
    ManagedObjectReference morDs = null;
    DatastoreMO dsMo = null;
    // By default assume failure
    String details = null;
    boolean success = false;
    String snapshotBackupUuid = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    VirtualMachineMO vmMo = null;
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getPool().getUuid());
        try {
            vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
            if (vmMo == null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
                }
                vmMo = hyperHost.findVmOnPeerHyperHost(cmd.getVmName());
                if (vmMo == null) {
                    dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
                    workerVMName = hostService.getWorkerName(context, cmd, 0, dsMo);
                    vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName, null);
                    if (vmMo == null) {
                        throw new Exception("Failed to find the newly create or relocated VM. vmName: " + workerVMName);
                    }
                    workerVm = vmMo;
                    // attach volume to worker VM
                    String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk", searchExcludedFolders);
                    vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
                }
            }
            if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + cmd.getSnapshotName(), false, false)) {
                throw new Exception("Failed to take snapshot " + cmd.getSnapshotName() + " on vm: " + cmd.getVmName());
            }
            snapshotBackupUuid = backupSnapshotToSecondaryStorage(vmMo, accountId, volumeId, cmd.getVolumePath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1, dsMo), cmd.getNfsVersion());
            success = (snapshotBackupUuid != null);
            if (success) {
                details = "Successfully backedUp the snapshotUuid: " + snapshotUuid + " to secondary storage.";
            }
        } finally {
            if (vmMo != null) {
                ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
                if (snapshotMor != null) {
                    vmMo.removeSnapshot(snapshotUuid, false);
                }
            }
            try {
                if (workerVm != null) {
                    workerVm.detachAllDisksAndDestroy();
                }
            } catch (Throwable e) {
                s_logger.warn(String.format("Failed to destroy worker VM [%s] due to: [%s].", workerVMName, e.getMessage()), e);
            }
        }
    } catch (Throwable e) {
        return new BackupSnapshotAnswer(cmd, false, hostService.createLogMessageException(e, cmd), snapshotBackupUuid, true);
    }
    return new BackupSnapshotAnswer(cmd, success, details, snapshotBackupUuid, true);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)85 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)65 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)65 RemoteException (java.rmi.RemoteException)59 UnsupportedEncodingException (java.io.UnsupportedEncodingException)57 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)48 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)46 CloudException (com.cloud.exception.CloudException)40 InternalErrorException (com.cloud.exception.InternalErrorException)40 IOException (java.io.IOException)40 ConnectException (java.net.ConnectException)40 ConfigurationException (javax.naming.ConfigurationException)40 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)31 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)27 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)23 Pair (com.cloud.utils.Pair)15 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 Answer (com.cloud.agent.api.Answer)11 AttachIsoAnswer (com.cloud.agent.api.AttachIsoAnswer)10 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)10