Search in sources :

Example 91 with ManagedObjectReference

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

the class VmwareStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    TemplateObjectTO template = (TemplateObjectTO) srcData;
    DataStoreTO srcStore = srcData.getDataStore();
    if (!(srcStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) srcStore;
    DataTO destData = cmd.getDestTO();
    DataStoreTO destStore = destData.getDataStore();
    DataStoreTO primaryStore = destStore;
    String secondaryStorageUrl = nfsImageStore.getUrl();
    assert (secondaryStorageUrl != null);
    boolean managed = false;
    String storageHost = null;
    int storagePort = Integer.MIN_VALUE;
    String managedStoragePoolName = null;
    String managedStoragePoolRootVolumeName = null;
    String chapInitiatorUsername = null;
    String chapInitiatorSecret = null;
    String chapTargetUsername = null;
    String chapTargetSecret = null;
    if (destStore instanceof PrimaryDataStoreTO) {
        PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO) destStore;
        Map<String, String> details = destPrimaryDataStoreTo.getDetails();
        if (details != null) {
            managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
            if (managed) {
                storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                try {
                    storagePort = Integer.parseInt(details.get(PrimaryDataStoreTO.STORAGE_PORT));
                } catch (Exception ex) {
                    storagePort = 3260;
                }
                managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                chapTargetUsername = details.get(PrimaryDataStoreTO.CHAP_TARGET_USERNAME);
                chapTargetSecret = details.get(PrimaryDataStoreTO.CHAP_TARGET_SECRET);
            }
        }
    }
    String templateUrl = secondaryStorageUrl + "/" + srcData.getPath();
    Pair<String, String> templateInfo = VmwareStorageLayoutHelper.decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl, templateUrl, template.getName());
    VmwareContext context = hostService.getServiceContext(cmd);
    if (context == null) {
        return new CopyCmdAnswer("Failed to create a Vmware context, check the management server logs or the ssvm log for details");
    }
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        String storageUuid = managed ? managedStoragePoolName : primaryStore.getUuid();
        String templateUuidName = deriveTemplateUuidOnHost(hyperHost, storageUuid, templateInfo.second());
        DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
        VirtualMachineMO templateMo = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templateUuidName), true);
        DatastoreMO dsMo = null;
        Pair<VirtualMachineMO, Long> vmInfo = null;
        if (templateMo == null) {
            if (s_logger.isInfoEnabled()) {
                s_logger.info("Template " + templateInfo.second() + " is not setup yet. Set up template from secondary storage with uuid name: " + templateUuidName);
            }
            final ManagedObjectReference morDs;
            if (managed) {
                morDs = prepareManagedDatastore(context, hyperHost, null, managedStoragePoolName, storageHost, storagePort, chapInitiatorUsername, chapInitiatorSecret, chapTargetUsername, chapTargetSecret);
            } else {
                morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, storageUuid);
            }
            assert (morDs != null);
            dsMo = new DatastoreMO(context, morDs);
            if (managed) {
                vmInfo = copyTemplateFromSecondaryToPrimary(hyperHost, dsMo, secondaryStorageUrl, templateInfo.first(), templateInfo.second(), managedStoragePoolRootVolumeName, false, _nfsVersion);
                VirtualMachineMO vmMo = vmInfo.first();
                vmMo.unregisterVm();
                String[] vmwareLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, managedStoragePoolRootVolumeName, managedStoragePoolRootVolumeName, VmwareStorageLayoutType.VMWARE, false);
                String[] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, null, managedStoragePoolRootVolumeName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, false);
                dsMo.moveDatastoreFile(vmwareLayoutFilePair[0], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[0], dcMo.getMor(), true);
                dsMo.moveDatastoreFile(vmwareLayoutFilePair[1], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[1], dcMo.getMor(), true);
                String folderToDelete = dsMo.getDatastorePath(managedStoragePoolRootVolumeName, true);
                dsMo.deleteFolder(folderToDelete, dcMo.getMor());
            } else {
                vmInfo = copyTemplateFromSecondaryToPrimary(hyperHost, dsMo, secondaryStorageUrl, templateInfo.first(), templateInfo.second(), templateUuidName, true, _nfsVersion);
            }
        } else {
            s_logger.info("Template " + templateInfo.second() + " has already been setup, skip the template setup process in primary storage");
        }
        TemplateObjectTO newTemplate = new TemplateObjectTO();
        if (managed) {
            if (dsMo != null) {
                String path = dsMo.getDatastorePath(managedStoragePoolRootVolumeName + ".vmdk");
                newTemplate.setPath(path);
            }
        } else {
            newTemplate.setPath(templateUuidName);
        }
        newTemplate.setSize((vmInfo != null) ? vmInfo.second() : new Long(0));
        return new CopyCmdAnswer(newTemplate);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to copy template to primary storage due to exception:" + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new CopyCmdAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 92 with ManagedObjectReference

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

the class VmwareStorageProcessor method createVMLinkedClone.

private boolean createVMLinkedClone(VirtualMachineMO vmTemplate, DatacenterMO dcMo, DatastoreMO dsMo, String vmdkName, ManagedObjectReference morDatastore, ManagedObjectReference morPool) throws Exception {
    ManagedObjectReference morBaseSnapshot = vmTemplate.getSnapshotMor("cloud.template.base");
    if (morBaseSnapshot == null) {
        String msg = "Unable to find template base snapshot, invalid template";
        s_logger.error(msg);
        throw new Exception(msg);
    }
    s_logger.info("creating linked clone from template");
    if (!vmTemplate.createLinkedClone(vmdkName, morBaseSnapshot, dcMo.getVmFolder(), morPool, morDatastore)) {
        String msg = "Unable to clone from the template";
        s_logger.error(msg);
        throw new Exception(msg);
    }
    return true;
}
Also used : RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 93 with ManagedObjectReference

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

the class VmwareStorageProcessor method createVolumeFromSnapshot.

@Override
public Answer createVolumeFromSnapshot(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
    DataTO destData = cmd.getDestTO();
    DataStoreTO pool = destData.getDataStore();
    DataStoreTO imageStore = srcData.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) imageStore;
    String primaryStorageNameLabel = pool.getUuid();
    String secondaryStorageUrl = nfsImageStore.getUrl();
    String backedUpSnapshotUuid = snapshot.getPath();
    int index = backedUpSnapshotUuid.lastIndexOf(File.separator);
    String backupPath = backedUpSnapshotUuid.substring(0, index);
    backedUpSnapshotUuid = backedUpSnapshotUuid.substring(index + 1);
    String details = null;
    String newVolumeName = VmwareHelper.getVCenterSafeUuid();
    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);
        }
        // strip off the extension since restoreVolumeFromSecStorage internally will append suffix there.
        if (backedUpSnapshotUuid.endsWith(".ova")) {
            backedUpSnapshotUuid = backedUpSnapshotUuid.replace(".ova", "");
        } else if (backedUpSnapshotUuid.endsWith(".ovf")) {
            backedUpSnapshotUuid = backedUpSnapshotUuid.replace(".ovf", "");
        }
        DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs);
        restoreVolumeFromSecStorage(hyperHost, primaryDsMo, newVolumeName, secondaryStorageUrl, backupPath, backedUpSnapshotUuid, (long) cmd.getWait() * 1000, _nfsVersion);
        VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(newVolumeName);
        return new CopyCmdAnswer(newVol);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        s_logger.error("Unexpecpted exception ", e);
        details = "create volume from snapshot exception: " + VmwareHelper.getExceptionMessage(e);
    }
    return new CopyCmdAnswer(details);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 94 with ManagedObjectReference

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

the class VmwareStorageProcessor method copyVolumeFromImageCacheToPrimary.

@Override
public Answer copyVolumeFromImageCacheToPrimary(CopyCommand cmd) {
    VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        NfsTO srcStore = (NfsTO) srcVolume.getDataStore();
        DataStoreTO destStore = destVolume.getDataStore();
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        String uuid = destStore.getUuid();
        ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, uuid);
        if (morDatastore == null) {
            URI uri = new URI(destStore.getUrl());
            morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), destStore.getUuid().replace("-", ""));
            if (morDatastore == null) {
                throw new Exception("Unable to mount storage pool on host. storeUrl: " + uri.getHost() + ":/" + uri.getPath());
            }
        }
        Pair<String, String> result = copyVolumeFromSecStorage(hyperHost, srcVolume.getPath(), new DatastoreMO(context, morDatastore), srcStore.getUrl(), (long) cmd.getWait() * 1000, _nfsVersion);
        deleteVolumeDirOnSecondaryStorage(result.first(), srcStore.getUrl(), _nfsVersion);
        VolumeObjectTO newVolume = new VolumeObjectTO();
        newVolume.setPath(result.second());
        return new CopyCmdAnswer(newVolume);
    } catch (Throwable t) {
        if (t instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to execute CopyVolumeCommand due to exception";
        s_logger.error(msg, t);
        return new CopyCmdAnswer("copy volume secondary to primary failed due to exception: " + VmwareHelper.getExceptionMessage(t));
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 95 with ManagedObjectReference

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

the class VmwareStorageProcessor method addRemoveInternetScsiTargetsToAllHosts.

private void addRemoveInternetScsiTargetsToAllHosts(VmwareContext context, final boolean add, final List<HostInternetScsiHbaStaticTarget> targets, List<Pair<ManagedObjectReference, String>> hostPairs) throws Exception {
    List<HostMO> hosts = new ArrayList<>();
    for (Pair<ManagedObjectReference, String> hostPair : hostPairs) {
        HostMO host = new HostMO(context, hostPair.first());
        hosts.add(host);
    }
    addRemoveInternetScsiTargetsToAllHosts(add, targets, hosts);
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) ArrayList(java.util.ArrayList) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)236 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)62 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)57 Pair (com.cloud.utils.Pair)49 RemoteException (java.rmi.RemoteException)48 ArrayList (java.util.ArrayList)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)39 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)35 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)32 IOException (java.io.IOException)31 CloudException (com.cloud.exception.CloudException)30 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)30 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)29 ObjectContent (com.vmware.vim25.ObjectContent)28 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)27 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)25 ConfigurationException (javax.naming.ConfigurationException)24 InternalErrorException (com.cloud.exception.InternalErrorException)23 VirtualDisk (com.vmware.vim25.VirtualDisk)23 ConnectException (java.net.ConnectException)23