Search in sources :

Example 6 with SwiftTO

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

the class NfsSecondaryStorageResource method deleteTemplate.

protected Answer deleteTemplate(DeleteCommand cmd) {
    DataTO obj = cmd.getData();
    DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        NfsTO nfs = (NfsTO) dstore;
        String relativeTemplatePath = obj.getPath();
        String parent = getRootDir(nfs.getUrl(), _nfsVersion);
        if (relativeTemplatePath.startsWith(File.separator)) {
            relativeTemplatePath = relativeTemplatePath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        String absoluteTemplatePath = parent + relativeTemplatePath;
        File tmpltPath = new File(absoluteTemplatePath);
        File tmpltParent = null;
        if (tmpltPath.exists() && tmpltPath.isDirectory()) {
            tmpltParent = tmpltPath;
        } else {
            tmpltParent = tmpltPath.getParentFile();
        }
        String details = null;
        if (!tmpltParent.exists()) {
            details = "template parent directory " + tmpltParent.getName() + " doesn't exist";
            s_logger.debug(details);
            return new Answer(cmd, true, details);
        }
        File[] tmpltFiles = tmpltParent.listFiles();
        if (tmpltFiles == null || tmpltFiles.length == 0) {
            details = "No files under template parent directory " + tmpltParent.getName();
            s_logger.debug(details);
        } else {
            boolean found = false;
            for (File f : tmpltFiles) {
                if (!found && f.getName().equals(_tmpltpp)) {
                    found = true;
                }
                // Don't let this stop us from cleaning up the template
                if (f.isDirectory() && f.getName().equals("KVMHA")) {
                    s_logger.debug("Deleting KVMHA directory contents from template location");
                    File[] haFiles = f.listFiles();
                    for (File haFile : haFiles) {
                        haFile.delete();
                    }
                }
                if (!f.delete()) {
                    return new Answer(cmd, false, "Unable to delete file " + f.getName() + " under Template path " + relativeTemplatePath);
                }
            }
            if (!found) {
                details = "Can not find template.properties under " + tmpltParent.getName();
                s_logger.debug(details);
            }
        }
        if (!tmpltParent.delete()) {
            details = "Unable to delete directory " + tmpltParent.getName() + " under Template path " + relativeTemplatePath;
            s_logger.debug(details);
            return new Answer(cmd, false, details);
        }
        return new Answer(cmd, true, null);
    } else if (dstore instanceof S3TO) {
        final S3TO s3 = (S3TO) dstore;
        final String path = obj.getPath();
        final String bucket = s3.getBucketName();
        try {
            S3Utils.deleteDirectory(s3, bucket, path);
            return new Answer(cmd, true, String.format("Deleted template %1$s from bucket %2$s.", path, bucket));
        } catch (Exception e) {
            final String errorMessage = String.format("Failed to delete template %1$s from bucket %2$s due to the following error: %3$s", path, bucket, e.getMessage());
            s_logger.error(errorMessage, e);
            return new Answer(cmd, false, errorMessage);
        }
    } else if (dstore instanceof SwiftTO) {
        SwiftTO swift = (SwiftTO) dstore;
        String container = "T-" + obj.getId();
        String object = "";
        try {
            String result = swiftDelete(swift, container, object);
            if (result != null) {
                String errMsg = "failed to delete object " + container + "/" + object + " , err=" + result;
                s_logger.warn(errMsg);
                return new Answer(cmd, false, errMsg);
            }
            return new Answer(cmd, true, "success");
        } catch (Exception e) {
            String errMsg = cmd + " Command failed due to " + e.toString();
            s_logger.warn(errMsg, e);
            return new Answer(cmd, false, errMsg);
        }
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + dstore);
    }
}
Also used : 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) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) S3TO(com.cloud.agent.api.to.S3TO) 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)

Example 7 with SwiftTO

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

the class NfsSecondaryStorageResource method execute.

protected GetStorageStatsAnswer execute(final GetStorageStatsCommand cmd) {
    DataStoreTO store = cmd.getStore();
    if (store instanceof S3TO || store instanceof SwiftTO) {
        long infinity = Integer.MAX_VALUE;
        return new GetStorageStatsAnswer(cmd, infinity, 0L);
    }
    String rootDir = getRootDir(((NfsTO) store).getUrl(), cmd.getNfsVersion());
    final long usedSize = getUsedSize(rootDir);
    final long totalSize = getTotalSize(rootDir);
    if (usedSize == -1 || totalSize == -1) {
        return new GetStorageStatsAnswer(cmd, "Unable to get storage stats");
    } else {
        return new GetStorageStatsAnswer(cmd, totalSize, usedSize);
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) SwiftTO(com.cloud.agent.api.to.SwiftTO) S3TO(com.cloud.agent.api.to.S3TO)

Example 8 with SwiftTO

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

the class NfsSecondaryStorageResource method execute.

private Answer execute(ListTemplateCommand cmd) {
    if (!_inSystemVM) {
        return new ListTemplateAnswer(null, null);
    }
    DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        NfsTO nfs = (NfsTO) store;
        String secUrl = nfs.getUrl();
        String root = getRootDir(secUrl, cmd.getNfsVersion());
        Map<String, TemplateProp> templateInfos = _dlMgr.gatherTemplateInfo(root);
        return new ListTemplateAnswer(secUrl, templateInfos);
    } else if (store instanceof SwiftTO) {
        SwiftTO swift = (SwiftTO) store;
        Map<String, TemplateProp> templateInfos = swiftListTemplate(swift);
        return new ListTemplateAnswer(swift.toString(), templateInfos);
    } else if (store instanceof S3TO) {
        S3TO s3 = (S3TO) store;
        Map<String, TemplateProp> templateInfos = s3ListTemplate(s3);
        return new ListTemplateAnswer(s3.getBucketName(), templateInfos);
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + store);
    }
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) 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) SwiftTO(com.cloud.agent.api.to.SwiftTO) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) Map(java.util.Map) HashMap(java.util.HashMap) S3TO(com.cloud.agent.api.to.S3TO)

Example 9 with SwiftTO

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

the class NfsSecondaryStorageResource method execute.

protected Answer execute(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    DataStoreTO destDataStore = destData.getDataStore();
    if (srcData.getObjectType() == DataObjectType.SNAPSHOT && destData.getObjectType() == DataObjectType.TEMPLATE) {
        return createTemplateFromSnapshot(cmd);
    }
    if (destDataStore instanceof NfsTO && destDataStore.getRole() == DataStoreRole.ImageCache) {
        NfsTO destImageStore = (NfsTO) destDataStore;
        if (srcDataStore instanceof S3TO) {
            S3TO s3 = (S3TO) srcDataStore;
            return copyFromS3ToNfs(cmd, srcData, s3, destData, destImageStore);
        } else if (srcDataStore instanceof SwiftTO) {
            return copyFromSwiftToNfs(cmd, srcData, (SwiftTO) srcDataStore, destData, destImageStore);
        }
    }
    if (srcDataStore.getRole() == DataStoreRole.ImageCache && destDataStore.getRole() == DataStoreRole.Image) {
        return copyFromNfsToImage(cmd);
    }
    return Answer.createUnsupportedCommandAnswer(cmd);
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO)

Example 10 with SwiftTO

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

the class XenServerStorageProcessor 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 String primaryStorageNameLabel = srcData.getDataStore().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;
    final String snapshotUuid = snapshotTO.getPath();
    final String volumeUuid = snapshotTO.getVolume().getPath();
    final String prevBackupUuid = snapshotOnImage.getParentSnapshotPath();
    final String prevSnapshotUuid = snapshotTO.getParentSnapshotPath();
    // By default assume failure
    String details = null;
    String snapshotBackupUuid = null;
    Long physicalSize = null;
    final Map<String, String> options = cmd.getOptions();
    boolean fullbackup = Boolean.parseBoolean(options.get("fullSnapshot"));
    boolean result = false;
    try {
        final SR 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);
        }
        final String psUuid = primaryStorageSR.getUuid(conn);
        final Boolean isISCSI = IsISCSI(primaryStorageSR.getType(conn));
        final VDI snapshotVdi = getVDIbyUuid(conn, snapshotUuid);
        String snapshotPaUuid = null;
        if (prevSnapshotUuid != null && !fullbackup) {
            try {
                snapshotPaUuid = getVhdParent(conn, psUuid, snapshotUuid, isISCSI);
                if (snapshotPaUuid != null) {
                    final String snashotPaPaPaUuid = getVhdParent(conn, psUuid, snapshotPaUuid, isISCSI);
                    final String prevSnashotPaUuid = getVhdParent(conn, psUuid, prevSnapshotUuid, isISCSI);
                    if (snashotPaPaPaUuid != null && prevSnashotPaUuid != null && prevSnashotPaUuid.equals(snashotPaPaPaUuid)) {
                        fullbackup = false;
                    } else {
                        fullbackup = true;
                    }
                }
            } catch (final Exception e) {
                s_logger.debug("Failed to get parent snapshots, take full snapshot", e);
                fullbackup = true;
            }
        }
        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) {
            if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, folder)) {
                details = " Filed to create folder " + folder + " in secondary storage";
                s_logger.warn(details);
                return new CopyCmdAnswer(details);
            }
            final String snapshotMountpoint = secondaryStorageUrl + "/" + folder;
            SR snapshotSr = null;
            try {
                snapshotSr = hypervisorResource.createNfsSRbyURI(conn, new URI(snapshotMountpoint), false);
                final VDI backedVdi = hypervisorResource.cloudVDIcopy(conn, snapshotVdi, snapshotSr, wait);
                snapshotBackupUuid = backedVdi.getUuid(conn);
                final String primarySRuuid = snapshotSr.getUuid(conn);
                physicalSize = getSnapshotSize(conn, primarySRuuid, snapshotBackupUuid, isISCSI, wait);
                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 + cacheStore.getPathSeparator() + snapshotBackupUuid;
                }
            } finally {
                if (snapshotSr != null) {
                    hypervisorResource.removeSR(conn, snapshotSr);
                }
            }
        } 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 results = backupSnapshot(conn, primaryStorageSRUuid, localMountPoint, folder, secondaryStorageMountPath, snapshotUuid, prevBackupUuid, isISCSI, wait);
                final String[] tmp = results.split("#");
                snapshotBackupUuid = tmp[1];
                physicalSize = Long.parseLong(tmp[2]);
                finalPath = folder + cacheStore.getPathSeparator() + snapshotBackupUuid;
            }
        }
        // delete primary snapshots with only the last one left
        destroySnapshotOnPrimaryStorageExceptThis(conn, volumeUuid, snapshotUuid);
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(finalPath);
        newSnapshot.setPhysicalSize(physicalSize);
        if (fullbackup) {
            newSnapshot.setParentSnapshotPath(null);
        } else {
            newSnapshot.setParentSnapshotPath(prevBackupUuid);
        }
        result = true;
        return new CopyCmdAnswer(newSnapshot);
    } catch (final 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);
    } finally {
        if (!result) {
            // remove last bad primary snapshot when exception happens
            try {
                destroySnapshotOnPrimaryStorage(conn, snapshotUuid);
            } catch (final Exception e) {
                s_logger.debug("clean up snapshot failed", 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) SwiftTO(com.cloud.agent.api.to.SwiftTO) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) 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) DataTO(com.cloud.agent.api.to.DataTO) 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)

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