Search in sources :

Example 6 with CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer 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 CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer 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 CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.

the class SnapshotObject method processEvent.

@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
    try {
        SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findByStoreSnapshot(getDataStore().getRole(), getDataStore().getId(), getId());
        if (answer instanceof CreateObjectAnswer) {
            SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CreateObjectAnswer) answer).getData();
            snapshotStore.setInstallPath(snapshotTO.getPath());
            snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
        } else if (answer instanceof CopyCmdAnswer) {
            SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CopyCmdAnswer) answer).getNewData();
            snapshotStore.setInstallPath(snapshotTO.getPath());
            if (snapshotTO.getPhysicalSize() != null) {
                // For S3 delta snapshot, physical size is currently not set
                snapshotStore.setPhysicalSize(snapshotTO.getPhysicalSize());
            }
            if (snapshotTO.getParentSnapshotPath() == null) {
                snapshotStore.setParentSnapshotId(0L);
            }
            snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
            // update side-effect of snapshot operation
            if (snapshotTO.getVolume() != null && snapshotTO.getVolume().getPath() != null) {
                VolumeVO vol = volumeDao.findByUuid(snapshotTO.getVolume().getUuid());
                if (vol != null) {
                    s_logger.info("Update volume path change due to snapshot operation, volume " + vol.getId() + " path: " + vol.getPath() + "->" + snapshotTO.getVolume().getPath());
                    vol.setPath(snapshotTO.getVolume().getPath());
                    volumeDao.update(vol.getId(), vol);
                } else {
                    s_logger.error("Cound't find the original volume with uuid: " + snapshotTO.getVolume().getUuid());
                }
            }
        } else {
            throw new CloudRuntimeException("Unknown answer: " + answer.getClass());
        }
    } catch (RuntimeException ex) {
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
        throw ex;
    }
    this.processEvent(event);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 9 with CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.

the class SnapshotServiceImpl method copySnapshotAsyncCallback.

protected Void copySnapshotAsyncCallback(AsyncCallbackDispatcher<SnapshotServiceImpl, CopyCommandResult> callback, CopySnapshotContext<CommandResult> context) {
    CopyCommandResult result = callback.getResult();
    SnapshotInfo destSnapshot = context.destSnapshot;
    SnapshotObject srcSnapshot = (SnapshotObject) context.srcSnapshot;
    AsyncCallFuture<SnapshotResult> future = context.future;
    SnapshotResult snapResult = new SnapshotResult(destSnapshot, result.getAnswer());
    if (result.isFailed()) {
        try {
            destSnapshot.processEvent(Event.OperationFailed);
            //if backup snapshot failed, mark srcSnapshot in snapshot_store_ref as failed also
            srcSnapshot.processEvent(Event.DestroyRequested);
            srcSnapshot.processEvent(Event.OperationSuccessed);
            srcSnapshot.processEvent(Snapshot.Event.OperationFailed);
            _snapshotDao.remove(srcSnapshot.getId());
        } catch (NoTransitionException e) {
            s_logger.debug("Failed to update state: " + e.toString());
        }
        snapResult.setResult(result.getResult());
        future.complete(snapResult);
        return null;
    }
    try {
        CopyCmdAnswer copyCmdAnswer = (CopyCmdAnswer) result.getAnswer();
        destSnapshot.processEvent(Event.OperationSuccessed, copyCmdAnswer);
        srcSnapshot.processEvent(Snapshot.Event.OperationSucceeded);
        snapResult = new SnapshotResult(_snapshotFactory.getSnapshot(destSnapshot.getId(), destSnapshot.getDataStore()), copyCmdAnswer);
        future.complete(snapResult);
    } catch (Exception e) {
        s_logger.debug("Failed to update snapshot state", e);
        snapResult.setResult(e.toString());
        future.complete(snapResult);
    }
    return null;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.

the class SnapshotTest method setUp.

@Test(priority = -1)
public void setUp() {
    ComponentContext.initComponentsLifeCycle();
    host = hostDao.findByGuid(this.getHostGuid());
    if (host != null) {
        dcId = host.getDataCenterId();
        clusterId = host.getClusterId();
        podId = host.getPodId();
        imageStore = this.imageStoreDao.findByName(imageStoreName);
    } else {
        // create data center
        DataCenterVO dc = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null, NetworkType.Basic, null, null, true, true, null, null);
        dc = dcDao.persist(dc);
        dcId = dc.getId();
        // create pod
        HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), dc.getId(), this.getHostGateway(), this.getHostCidr(), 8, "test");
        pod = podDao.persist(pod);
        podId = pod.getId();
        // create xenserver cluster
        ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
        cluster.setHypervisorType(this.getHypervisor().toString());
        cluster.setClusterType(ClusterType.CloudManaged);
        cluster.setManagedState(ManagedState.Managed);
        cluster = clusterDao.persist(cluster);
        clusterId = cluster.getId();
        // create xenserver host
        host = new HostVO(this.getHostGuid());
        host.setName("devcloud xenserver host");
        host.setType(Host.Type.Routing);
        host.setPrivateIpAddress(this.getHostIp());
        host.setDataCenterId(dc.getId());
        host.setVersion("6.0.1");
        host.setAvailable(true);
        host.setSetup(true);
        host.setPodId(podId);
        host.setLastPinged(0);
        host.setResourceState(ResourceState.Enabled);
        host.setHypervisorType(this.getHypervisor());
        host.setClusterId(cluster.getId());
        host = hostDao.persist(host);
        imageStore = new ImageStoreVO();
        imageStore.setName(imageStoreName);
        imageStore.setDataCenterId(dcId);
        imageStore.setProviderName(DataStoreProvider.NFS_IMAGE);
        imageStore.setRole(DataStoreRole.Image);
        imageStore.setUrl(this.getSecondaryStorage());
        imageStore.setUuid(UUID.randomUUID().toString());
        imageStore.setProtocol("nfs");
        imageStore = imageStoreDao.persist(imageStore);
    }
    image = new VMTemplateVO();
    image.setTemplateType(TemplateType.USER);
    image.setUrl(this.getTemplateUrl());
    image.setUniqueName(UUID.randomUUID().toString());
    image.setName(UUID.randomUUID().toString());
    image.setPublicTemplate(true);
    image.setFeatured(true);
    image.setRequiresHvm(true);
    image.setBits(64);
    image.setFormat(Storage.ImageFormat.VHD);
    image.setEnablePassword(true);
    image.setEnableSshKey(true);
    image.setGuestOSId(1);
    image.setBootable(true);
    image.setPrepopulate(true);
    image.setCrossZones(true);
    image.setExtractable(true);
    image = imageDataDao.persist(image);
    /*
         * TemplateDataStoreVO templateStore = new TemplateDataStoreVO();
         *
         * templateStore.setDataStoreId(imageStore.getId());
         * templateStore.setDownloadPercent(100);
         * templateStore.setDownloadState(Status.DOWNLOADED);
         * templateStore.setDownloadUrl(imageStore.getUrl());
         * templateStore.setInstallPath(this.getImageInstallPath());
         * templateStore.setTemplateId(image.getId());
         * templateStoreDao.persist(templateStore);
         */
    DataStore store = this.dataStoreMgr.getDataStore(imageStore.getId(), DataStoreRole.Image);
    TemplateInfo template = templateFactory.getTemplate(image.getId(), DataStoreRole.Image);
    DataObject templateOnStore = store.create(template);
    TemplateObjectTO to = new TemplateObjectTO();
    to.setPath(this.getImageInstallPath());
    to.setFormat(ImageFormat.VHD);
    to.setSize(1000L);
    CopyCmdAnswer answer = new CopyCmdAnswer(to);
    templateOnStore.processEvent(Event.CreateOnlyRequested);
    templateOnStore.processEvent(Event.OperationSuccessed, answer);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) ClusterVO(com.cloud.dc.ClusterVO) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) Test(org.testng.annotations.Test)

Aggregations

CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)78 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)49 NfsTO (com.cloud.agent.api.to.NfsTO)40 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)38 DataTO (com.cloud.agent.api.to.DataTO)37 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)37 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)34 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)30 InternalErrorException (com.cloud.exception.InternalErrorException)27 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)25 Connection (com.xensource.xenapi.Connection)18 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 XmlRpcException (org.apache.xmlrpc.XmlRpcException)18 SR (com.xensource.xenapi.SR)17 VDI (com.xensource.xenapi.VDI)17 URI (java.net.URI)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ConfigurationException (javax.naming.ConfigurationException)10 IOException (java.io.IOException)9 CopyCommand (org.apache.cloudstack.storage.command.CopyCommand)9