Search in sources :

Example 11 with SwiftTO

use of com.cloud.agent.api.to.SwiftTO in project cloudstack by apache.

the class Xenserver625StorageProcessor method backupSnapshot.

@Override
public Answer backupSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final DataTO cacheData = cmd.getCacheTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWait();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) srcData.getDataStore();
    final String primaryStorageNameLabel = primaryStore.getUuid();
    String secondaryStorageUrl = null;
    NfsTO cacheStore = null;
    String destPath = null;
    if (cacheData != null) {
        cacheStore = (NfsTO) cacheData.getDataStore();
        secondaryStorageUrl = cacheStore.getUrl();
        destPath = cacheData.getPath();
    } else {
        cacheStore = (NfsTO) destData.getDataStore();
        secondaryStorageUrl = cacheStore.getUrl();
        destPath = destData.getPath();
    }
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) srcData;
    final SnapshotObjectTO snapshotOnImage = (SnapshotObjectTO) destData;
    String snapshotUuid = snapshotTO.getPath();
    final String prevBackupUuid = snapshotOnImage.getParentSnapshotPath();
    final String prevSnapshotUuid = snapshotTO.getParentSnapshotPath();
    final Map<String, String> options = cmd.getOptions();
    // By default assume failure
    String details = null;
    String snapshotBackupUuid = null;
    boolean fullbackup = Boolean.parseBoolean(options.get("fullSnapshot"));
    Long physicalSize = null;
    try {
        SR primaryStorageSR = null;
        if (primaryStore.isManaged()) {
            // currently, managed storage only supports full backup
            fullbackup = true;
            final Map<String, String> srcDetails = cmd.getOptions();
            final String iScsiName = srcDetails.get(DiskTO.IQN);
            final String storageHost = srcDetails.get(DiskTO.STORAGE_HOST);
            final String chapInitiatorUsername = srcDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
            final String chapInitiatorSecret = srcDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
            final String srType = CitrixResourceBase.SRType.LVMOISCSI.toString();
            primaryStorageSR = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false, srType, true);
            final VDI srcVdi = primaryStorageSR.getVDIs(conn).iterator().next();
            if (srcVdi == null) {
                throw new InternalErrorException("Could not Find a VDI on the SR: " + primaryStorageSR.getNameLabel(conn));
            }
            snapshotUuid = srcVdi.getUuid(conn);
        } else {
            primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
        }
        if (primaryStorageSR == null) {
            throw new InternalErrorException("Could not backup snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
        }
        // String psUuid = primaryStorageSR.getUuid(conn);
        final Boolean isISCSI = IsISCSI(primaryStorageSR.getType(conn));
        final VDI snapshotVdi = getVDIbyUuid(conn, snapshotUuid);
        final String snapshotPaUuid = snapshotVdi.getUuid(conn);
        final URI uri = new URI(secondaryStorageUrl);
        final String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
        final DataStoreTO destStore = destData.getDataStore();
        final String folder = destPath;
        String finalPath = null;
        final String localMountPoint = BaseMountPointOnHost + File.separator + UUID.nameUUIDFromBytes(secondaryStorageUrl.getBytes()).toString();
        if (fullbackup) {
            SR snapshotSr = null;
            Task task = null;
            try {
                final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(secondaryStorageMountPath.getBytes());
                mountNfs(conn, secondaryStorageMountPath, localDir);
                final boolean result = makeDirectory(conn, localDir + "/" + folder);
                if (!result) {
                    details = " Filed to create folder " + folder + " in secondary storage";
                    s_logger.warn(details);
                    return new CopyCmdAnswer(details);
                }
                snapshotSr = createFileSr(conn, secondaryStorageMountPath, folder);
                task = snapshotVdi.copyAsync(conn, snapshotSr, null, null);
                // poll every 1 seconds ,
                hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
                hypervisorResource.checkForSuccess(conn, task);
                final VDI backedVdi = Types.toVDI(task, conn);
                snapshotBackupUuid = backedVdi.getUuid(conn);
                physicalSize = backedVdi.getPhysicalUtilisation(conn);
                if (destStore instanceof SwiftTO) {
                    try {
                        final String container = "S-" + snapshotTO.getVolume().getVolumeId().toString();
                        final String destSnapshotName = swiftBackupSnapshot(conn, (SwiftTO) destStore, snapshotSr.getUuid(conn), snapshotBackupUuid, container, false, wait);
                        final String swiftPath = container + File.separator + destSnapshotName;
                        finalPath = swiftPath;
                    } finally {
                        try {
                            deleteSnapshotBackup(conn, localMountPoint, folder, secondaryStorageMountPath, snapshotBackupUuid);
                        } catch (final Exception e) {
                            s_logger.debug("Failed to delete snapshot on cache storages", e);
                        }
                    }
                } else if (destStore instanceof S3TO) {
                    try {
                        finalPath = backupSnapshotToS3(conn, (S3TO) destStore, snapshotSr.getUuid(conn), folder, snapshotBackupUuid, isISCSI, wait);
                        if (finalPath == null) {
                            throw new CloudRuntimeException("S3 upload of snapshots " + snapshotBackupUuid + " failed");
                        }
                    } finally {
                        try {
                            deleteSnapshotBackup(conn, localMountPoint, folder, secondaryStorageMountPath, snapshotBackupUuid);
                        } catch (final Exception e) {
                            s_logger.debug("Failed to delete snapshot on cache storages", e);
                        }
                    }
                // finalPath = folder + File.separator +
                // snapshotBackupUuid;
                } else {
                    finalPath = folder + File.separator + snapshotBackupUuid + ".vhd";
                }
            } finally {
                if (task != null) {
                    try {
                        task.destroy(conn);
                    } catch (final Exception e) {
                        s_logger.warn("unable to destroy task(" + task.toWireString() + ") due to " + e.toString());
                    }
                }
                if (snapshotSr != null) {
                    hypervisorResource.removeSR(conn, snapshotSr);
                }
                if (primaryStore.isManaged()) {
                    hypervisorResource.removeSR(conn, primaryStorageSR);
                }
            }
        } else {
            final String primaryStorageSRUuid = primaryStorageSR.getUuid(conn);
            if (destStore instanceof SwiftTO) {
                final String container = "S-" + snapshotTO.getVolume().getVolumeId().toString();
                snapshotBackupUuid = swiftBackupSnapshot(conn, (SwiftTO) destStore, primaryStorageSRUuid, snapshotPaUuid, "S-" + snapshotTO.getVolume().getVolumeId().toString(), isISCSI, wait);
                finalPath = container + File.separator + snapshotBackupUuid;
            } else if (destStore instanceof S3TO) {
                finalPath = backupSnapshotToS3(conn, (S3TO) destStore, primaryStorageSRUuid, folder, snapshotPaUuid, isISCSI, wait);
                if (finalPath == null) {
                    throw new CloudRuntimeException("S3 upload of snapshots " + snapshotPaUuid + " failed");
                }
            } else {
                final String result = backupSnapshot(conn, primaryStorageSRUuid, localMountPoint, folder, secondaryStorageMountPath, snapshotUuid, prevBackupUuid, prevSnapshotUuid, isISCSI, wait);
                final String[] tmp = result.split("#");
                snapshotBackupUuid = tmp[0];
                physicalSize = Long.parseLong(tmp[1]);
                finalPath = folder + File.separator + snapshotBackupUuid + ".vhd";
            }
            final String volumeUuid = snapshotTO.getVolume().getPath();
            destroySnapshotOnPrimaryStorageExceptThis(conn, volumeUuid, snapshotUuid);
        }
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(finalPath);
        newSnapshot.setPhysicalSize(physicalSize);
        if (fullbackup) {
            newSnapshot.setParentSnapshotPath(null);
        } else {
            newSnapshot.setParentSnapshotPath(prevBackupUuid);
        }
        return new CopyCmdAnswer(newSnapshot);
    } catch (final Types.XenAPIException e) {
        details = "BackupSnapshot Failed due to " + e.toString();
        s_logger.warn(details, e);
    } catch (final Exception e) {
        details = "BackupSnapshot Failed due to " + e.getMessage();
        s_logger.warn(details, e);
    }
    return new CopyCmdAnswer(details);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Types(com.xensource.xenapi.Types) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Task(com.xensource.xenapi.Task) SwiftTO(com.cloud.agent.api.to.SwiftTO) Connection(com.xensource.xenapi.Connection) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 12 with SwiftTO

use of com.cloud.agent.api.to.SwiftTO in project cloudstack by apache.

the class SwiftImageStoreDriverImpl method getStoreTO.

@Override
public DataStoreTO getStoreTO(DataStore store) {
    ImageStoreImpl imgStore = (ImageStoreImpl) store;
    Map<String, String> details = _imageStoreDetailsDao.getDetails(imgStore.getId());
    return new SwiftTO(imgStore.getId(), imgStore.getUri(), details.get(ApiConstants.ACCOUNT), details.get(ApiConstants.USERNAME), details.get(ApiConstants.KEY));
}
Also used : SwiftTO(com.cloud.agent.api.to.SwiftTO) ImageStoreImpl(org.apache.cloudstack.storage.image.store.ImageStoreImpl)

Example 13 with SwiftTO

use of com.cloud.agent.api.to.SwiftTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method copyFromNfsToImage.

protected Answer copyFromNfsToImage(CopyCommand cmd) {
    DataTO destData = cmd.getDestTO();
    DataStoreTO destDataStore = destData.getDataStore();
    if (destDataStore instanceof S3TO) {
        return copyFromNfsToS3(cmd);
    } else if (destDataStore instanceof SwiftTO) {
        return copyFromNfsToSwift(cmd);
    } else {
        return new CopyCmdAnswer("unsupported ");
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 14 with SwiftTO

use of com.cloud.agent.api.to.SwiftTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method createTemplateFromSnapshot.

protected Answer createTemplateFromSnapshot(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    DataStoreTO destDataStore = destData.getDataStore();
    if (srcDataStore.getRole() == DataStoreRole.Image || srcDataStore.getRole() == DataStoreRole.ImageCache || srcDataStore.getRole() == DataStoreRole.Primary) {
        if (!(srcDataStore instanceof NfsTO)) {
            s_logger.debug("only support nfs storage as src, when create template from snapshot");
            return Answer.createUnsupportedCommandAnswer(cmd);
        }
        if (destDataStore instanceof NfsTO) {
            return copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) destDataStore);
        } else if (destDataStore instanceof SwiftTO) {
            //create template on the same data store
            CopyCmdAnswer answer = (CopyCmdAnswer) copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) srcDataStore);
            if (!answer.getResult()) {
                return answer;
            }
            s_logger.debug("starting copy template to swift");
            TemplateObjectTO newTemplate = (TemplateObjectTO) answer.getNewData();
            newTemplate.setDataStore(srcDataStore);
            CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
            Answer result = copyFromNfsToSwift(newCpyCmd);
            cleanupStagingNfs(newTemplate);
            return result;
        } else if (destDataStore instanceof S3TO) {
            //create template on the same data store
            CopyCmdAnswer answer = (CopyCmdAnswer) copySnapshotToTemplateFromNfsToNfs(cmd, (SnapshotObjectTO) srcData, (NfsTO) srcDataStore, (TemplateObjectTO) destData, (NfsTO) srcDataStore);
            if (!answer.getResult()) {
                return answer;
            }
            TemplateObjectTO newTemplate = (TemplateObjectTO) answer.getNewData();
            newTemplate.setDataStore(srcDataStore);
            CopyCommand newCpyCmd = new CopyCommand(newTemplate, destData, cmd.getWait(), cmd.executeInSequence());
            Answer result = copyFromNfsToS3(newCpyCmd);
            cleanupStagingNfs(newTemplate);
            return result;
        }
    }
    s_logger.debug("Failed to create template from snapshot");
    return new CopyCmdAnswer("Unsupported protocol");
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(org.apache.cloudstack.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 15 with SwiftTO

use of com.cloud.agent.api.to.SwiftTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method copyFromNfsToSwift.

/**
     * Copies data from NFS and uploads it into a Swift container
     *
     * @param cmd CopyComand
     * @return CopyCmdAnswer
     */
protected Answer copyFromNfsToSwift(CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    NfsTO srcStore = (NfsTO) srcDataStore;
    DataStoreTO destDataStore = destData.getDataStore();
    File srcFile = getFile(srcData.getPath(), srcStore.getUrl(), _nfsVersion);
    SwiftTO swift = (SwiftTO) destDataStore;
    long pathId = destData.getId();
    try {
        if (destData instanceof SnapshotObjectTO) {
            pathId = ((SnapshotObjectTO) destData).getVolume().getId();
        }
        String containerName = SwiftUtil.getContainerName(destData.getObjectType().toString(), pathId);
        String swiftPath = SwiftUtil.putObject(swift, srcFile, containerName, srcFile.getName());
        DataTO retObj = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            TemplateObjectTO destTemplateData = (TemplateObjectTO) destData;
            String uniqueName = destTemplateData.getName();
            swiftUploadMetadataFile(swift, srcFile, containerName, uniqueName);
            TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(swiftPath);
            newTemplate.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
            newTemplate.setPhysicalSize(srcFile.length());
            newTemplate.setFormat(getTemplateFormat(srcFile.getName()));
            retObj = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(containerName);
            newVol.setSize(getVirtualSize(srcFile, getTemplateFormat(srcFile.getName())));
            retObj = newVol;
        } else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
            SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
            newSnapshot.setPath(containerName + File.separator + srcFile.getName());
            retObj = newSnapshot;
        }
        return new CopyCmdAnswer(retObj);
    } catch (Exception e) {
        s_logger.error("failed to upload " + srcData.getPath(), e);
        return new CopyCmdAnswer("failed to upload " + srcData.getPath() + e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Aggregations

SwiftTO (com.cloud.agent.api.to.SwiftTO)18 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)14 NfsTO (com.cloud.agent.api.to.NfsTO)13 S3TO (com.cloud.agent.api.to.S3TO)12 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)12 DataTO (com.cloud.agent.api.to.DataTO)10 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 Answer (com.cloud.agent.api.Answer)7 GetStorageStatsAnswer (com.cloud.agent.api.GetStorageStatsAnswer)7 ListTemplateAnswer (com.cloud.agent.api.storage.ListTemplateAnswer)7 InternalErrorException (com.cloud.exception.InternalErrorException)7 CheckHealthAnswer (com.cloud.agent.api.CheckHealthAnswer)6 ReadyAnswer (com.cloud.agent.api.ReadyAnswer)6 SecStorageSetupAnswer (com.cloud.agent.api.SecStorageSetupAnswer)6 ListVolumeAnswer (com.cloud.agent.api.storage.ListVolumeAnswer)6 S3Utils.putFile (com.cloud.utils.storage.S3.S3Utils.putFile)6 File (java.io.File)6 IOException (java.io.IOException)6 UploadStatusAnswer (org.apache.cloudstack.storage.command.UploadStatusAnswer)6