Search in sources :

Example 6 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext 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 7 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method createTemplateFromSnapshot.

@Override
public Answer createTemplateFromSnapshot(CopyCommand cmd) {
    SnapshotObjectTO snapshot = (SnapshotObjectTO) cmd.getSrcTO();
    TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
    DataStoreTO imageStore = template.getDataStore();
    String details;
    String uniqeName = UUID.randomUUID().toString();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        if (!(imageStore instanceof NfsTO)) {
            return new CopyCmdAnswer("Only support create template from snapshot, when the dest store is nfs");
        }
        NfsTO nfsSvr = (NfsTO) imageStore;
        Ternary<String, Long, Long> result = createTemplateFromSnapshot(template.getPath(), uniqeName, nfsSvr.getUrl(), snapshot.getPath(), template.getId(), (long) cmd.getWait() * 1000, _nfsVersion);
        TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(result.first());
        newTemplate.setPhysicalSize(result.second());
        newTemplate.setSize(result.third());
        newTemplate.setFormat(ImageFormat.OVA);
        newTemplate.setName(uniqeName);
        return new CopyCmdAnswer(newTemplate);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        s_logger.error("Unexpecpted exception ", e);
        details = "create template from snapshot exception: " + VmwareHelper.getExceptionMessage(e);
        return new CopyCmdAnswer(details);
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 8 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method handleDatastoreAndVmdkDetach.

public void handleDatastoreAndVmdkDetach(String datastoreName, String iqn, String storageHost, int storagePort) throws Exception {
    VmwareContext context = hostService.getServiceContext(null);
    VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
    removeVmfsDatastore(hyperHost, datastoreName, storageHost, storagePort, trimIqn(iqn));
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)

Example 9 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareSecondaryStorageContextFactory method getContext.

public static VmwareContext getContext(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
    VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName);
    if (context == null) {
        context = create(vCenterAddress, vCenterUserName, vCenterPassword);
    } else {
        // Validate current context and verify if vCenter session timeout value of the context matches the timeout value set by Admin
        if (!context.validate() || (context.getVimClient().getVcenterSessionTimeout() != s_vCenterSessionTimeout)) {
            s_logger.info("Validation of the context faild. dispose and create a new one");
            context.close();
            context = create(vCenterAddress, vCenterUserName, vCenterPassword);
        }
    }
    if (context != null) {
        context.registerStockObject("username", vCenterUserName);
        context.registerStockObject("password", vCenterPassword);
    }
    return context;
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext)

Example 10 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareContextFactory method create.

public static VmwareContext create(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception {
    assert (vCenterAddress != null);
    assert (vCenterUserName != null);
    assert (vCenterPassword != null);
    String serviceUrl = "https://" + vCenterAddress + "/sdk/vimService";
    if (s_logger.isDebugEnabled())
        s_logger.debug("initialize VmwareContext. url: " + serviceUrl + ", username: " + vCenterUserName + ", password: " + StringUtils.getMaskedPasswordForDisplay(vCenterPassword));
    VmwareClient vimClient = new VmwareClient(vCenterAddress + "-" + s_seq++);
    vimClient.setVcenterSessionTimeout(s_vmwareMgr.getVcenterSessionTimeout());
    vimClient.connect(serviceUrl, vCenterUserName, vCenterPassword);
    VmwareContext context = new VmwareContext(vimClient, vCenterAddress);
    context.registerStockObject(VmwareManager.CONTEXT_STOCK_NAME, s_vmwareMgr);
    context.registerStockObject("serviceconsole", s_vmwareMgr.getServiceConsolePortGroupName());
    context.registerStockObject("manageportgroup", s_vmwareMgr.getManagementPortGroupName());
    context.registerStockObject("noderuninfo", String.format("%d-%d", s_clusterMgr.getManagementNodeId(), s_clusterMgr.getCurrentRunId()));
    context.setPoolInfo(s_pool, VmwareContextPool.composePoolKey(vCenterAddress, vCenterUserName));
    return context;
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareClient(com.cloud.hypervisor.vmware.util.VmwareClient)

Aggregations

VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)76 RemoteException (java.rmi.RemoteException)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)47 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)35 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)32 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)28 IOException (java.io.IOException)26 CloudException (com.cloud.exception.CloudException)25 ConfigurationException (javax.naming.ConfigurationException)25 InternalErrorException (com.cloud.exception.InternalErrorException)23 ConnectException (java.net.ConnectException)23 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)16 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)14 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)13 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)11 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)10