Search in sources :

Example 1 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO 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 2 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class VmwareStorageSubsystemCommandHandler method execute.

@Override
protected Answer execute(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    DataStoreTO destDataStore = destData.getDataStore();
    //if copied between s3 and nfs cache, go to resource
    boolean needDelegation = false;
    if (destDataStore instanceof NfsTO && destDataStore.getRole() == DataStoreRole.ImageCache) {
        if (srcDataStore instanceof S3TO || srcDataStore instanceof SwiftTO) {
            needDelegation = true;
        }
    }
    if (srcDataStore.getRole() == DataStoreRole.ImageCache && destDataStore.getRole() == DataStoreRole.Image) {
        //need to take extra processing for vmware, such as packing to ova, before sending to S3
        if (srcData.getObjectType() == DataObjectType.VOLUME) {
            NfsTO cacheStore = (NfsTO) srcDataStore;
            String parentPath = storageResource.getRootDir(cacheStore.getUrl(), _nfsVersion);
            VolumeObjectTO vol = (VolumeObjectTO) srcData;
            String path = vol.getPath();
            int index = path.lastIndexOf(File.separator);
            String name = path.substring(index + 1);
            storageManager.createOva(parentPath + File.separator + path, name);
            vol.setPath(path + File.separator + name + ".ova");
        } else if (srcData.getObjectType() == DataObjectType.TEMPLATE) {
            // sync template from NFS cache to S3 in NFS migration to S3 case
            storageManager.createOvaForTemplate((TemplateObjectTO) srcData);
        } else if (srcData.getObjectType() == DataObjectType.SNAPSHOT) {
            // pack ova first
            // sync snapshot from NFS cache to S3 in NFS migration to S3 case
            String parentPath = storageResource.getRootDir(srcDataStore.getUrl(), _nfsVersion);
            SnapshotObjectTO snap = (SnapshotObjectTO) srcData;
            String path = snap.getPath();
            int index = path.lastIndexOf(File.separator);
            String name = path.substring(index + 1);
            String snapDir = path.substring(0, index);
            storageManager.createOva(parentPath + File.separator + snapDir, name);
            if (destData.getObjectType() == DataObjectType.TEMPLATE) {
                //create template from snapshot on src at first, then copy it to s3
                TemplateObjectTO cacheTemplate = (TemplateObjectTO) destData;
                cacheTemplate.setDataStore(srcDataStore);
                CopyCmdAnswer answer = (CopyCmdAnswer) processor.createTemplateFromSnapshot(cmd);
                if (!answer.getResult()) {
                    return answer;
                }
                cacheTemplate.setDataStore(destDataStore);
                TemplateObjectTO template = (TemplateObjectTO) answer.getNewData();
                template.setDataStore(srcDataStore);
                CopyCommand newCmd = new CopyCommand(template, destData, cmd.getWait(), cmd.executeInSequence());
                Answer result = storageResource.defaultAction(newCmd);
                //clean up template data on staging area
                try {
                    DeleteCommand deleteCommand = new DeleteCommand(template);
                    storageResource.defaultAction(deleteCommand);
                } catch (Exception e) {
                    s_logger.debug("Failed to clean up staging area:", e);
                }
                return result;
            }
        }
        needDelegation = true;
    }
    if (srcData.getObjectType() == DataObjectType.SNAPSHOT && srcData.getDataStore().getRole() == DataStoreRole.Primary) {
        //for back up snapshot, we need to do backup to cache, then to object store if object store is used.
        if (cmd.getCacheTO() != null) {
            cmd.setDestTO(cmd.getCacheTO());
            CopyCmdAnswer answer = (CopyCmdAnswer) processor.backupSnapshot(cmd);
            if (!answer.getResult()) {
                return answer;
            }
            NfsTO cacheStore = (NfsTO) cmd.getCacheTO().getDataStore();
            String parentPath = storageResource.getRootDir(cacheStore.getUrl(), _nfsVersion);
            SnapshotObjectTO newSnapshot = (SnapshotObjectTO) answer.getNewData();
            String path = newSnapshot.getPath();
            int index = path.lastIndexOf(File.separator);
            String name = path.substring(index + 1);
            String dir = path.substring(0, index);
            storageManager.createOva(parentPath + File.separator + dir, name);
            newSnapshot.setPath(newSnapshot.getPath() + ".ova");
            newSnapshot.setDataStore(cmd.getCacheTO().getDataStore());
            CopyCommand newCmd = new CopyCommand(newSnapshot, destData, cmd.getWait(), cmd.executeInSequence());
            Answer result = storageResource.defaultAction(newCmd);
            //clean up data on staging area
            try {
                newSnapshot.setPath(path);
                DeleteCommand deleteCommand = new DeleteCommand(newSnapshot);
                storageResource.defaultAction(deleteCommand);
            } catch (Exception e) {
                s_logger.debug("Failed to clean up staging area:", e);
            }
            return result;
        }
    }
    if (needDelegation) {
        return storageResource.defaultAction(cmd);
    } else {
        return super.execute(cmd);
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) NfsTO(com.cloud.agent.api.to.NfsTO) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) Answer(com.cloud.agent.api.Answer) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 3 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class VmwareStorageProcessor method createSnapshot.

@Override
public Answer createSnapshot(CreateObjectCommand cmd) {
    // snapshot operation (create or destroy) is handled inside BackupSnapshotCommand(), we just fake
    // a success return here
    String snapshotUUID = UUID.randomUUID().toString();
    SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
    newSnapshot.setPath(snapshotUUID);
    return new CreateObjectAnswer(newSnapshot);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer)

Example 4 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class VmwareStorageProcessor method deleteSnapshot.

@Override
public Answer deleteSnapshot(DeleteCommand cmd) {
    SnapshotObjectTO snapshot = (SnapshotObjectTO) cmd.getData();
    DataStoreTO store = snapshot.getDataStore();
    if (store.getRole() == DataStoreRole.Primary) {
        return new Answer(cmd);
    } else {
        return new Answer(cmd, false, "unsupported command");
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO)

Example 5 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO 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)

Aggregations

SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)44 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 DataTO (com.cloud.agent.api.to.DataTO)22 NfsTO (com.cloud.agent.api.to.NfsTO)21 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)20 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)19 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)16 InternalErrorException (com.cloud.exception.InternalErrorException)14 CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)14 Answer (com.cloud.agent.api.Answer)12 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)11 Connection (com.xensource.xenapi.Connection)9 XenAPIException (com.xensource.xenapi.Types.XenAPIException)9 VDI (com.xensource.xenapi.VDI)9 XmlRpcException (org.apache.xmlrpc.XmlRpcException)9 SR (com.xensource.xenapi.SR)8 URI (java.net.URI)7 ConfigurationException (javax.naming.ConfigurationException)7 S3TO (com.cloud.agent.api.to.S3TO)6