Search in sources :

Example 86 with DataStoreTO

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

the class NfsSecondaryStorageResource method execute.

private Answer execute(ComputeChecksumCommand cmd) {
    String relativeTemplatePath = cmd.getTemplatePath();
    DataStoreTO store = cmd.getStore();
    if (!(store instanceof NfsTO)) {
        return new Answer(cmd, false, "can't handle non nfs data store");
    }
    NfsTO nfsStore = (NfsTO) store;
    String parent = getRootDir(nfsStore.getUrl(), _nfsVersion);
    if (relativeTemplatePath.startsWith(File.separator)) {
        relativeTemplatePath = relativeTemplatePath.substring(1);
    }
    if (!parent.endsWith(File.separator)) {
        parent += File.separator;
    }
    String absoluteTemplatePath = parent + relativeTemplatePath;
    MessageDigest digest;
    String checksum = null;
    File f = new File(absoluteTemplatePath);
    InputStream is = null;
    byte[] buffer = new byte[8192];
    int read = 0;
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("parent path " + parent + " relative template path " + relativeTemplatePath);
    }
    try {
        digest = MessageDigest.getInstance("MD5");
        is = new FileInputStream(f);
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        checksum = bigInt.toString(16);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Successfully calculated checksum for file " + absoluteTemplatePath + " - " + checksum);
        }
    } catch (IOException e) {
        String logMsg = "Unable to process file for MD5 - " + absoluteTemplatePath;
        s_logger.error(logMsg);
        return new Answer(cmd, false, checksum);
    } catch (NoSuchAlgorithmException e) {
        return new Answer(cmd, false, checksum);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Could not close the file " + absoluteTemplatePath);
            }
            return new Answer(cmd, false, checksum);
        }
    }
    return new Answer(cmd, true, checksum);
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NfsTO(com.cloud.agent.api.to.NfsTO) FileInputStream(java.io.FileInputStream) 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) BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile)

Example 87 with DataStoreTO

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

the class NfsSecondaryStorageResource method execute.

public Answer execute(DeleteSnapshotsDirCommand cmd) {
    DataStoreTO dstore = cmd.getDataStore();
    if (dstore instanceof NfsTO) {
        NfsTO nfs = (NfsTO) dstore;
        String relativeSnapshotPath = cmd.getDirectory();
        String parent = getRootDir(nfs.getUrl(), _nfsVersion);
        if (relativeSnapshotPath.startsWith(File.separator)) {
            relativeSnapshotPath = relativeSnapshotPath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        String absoluteSnapshotPath = parent + relativeSnapshotPath;
        File snapshotDir = new File(absoluteSnapshotPath);
        String details = null;
        if (!snapshotDir.exists()) {
            details = "snapshot directory " + snapshotDir.getName() + " doesn't exist";
            s_logger.debug(details);
            return new Answer(cmd, true, details);
        }
        // delete all files in the directory
        String lPath = absoluteSnapshotPath + "/*";
        String result = deleteLocalFile(lPath);
        if (result != null) {
            String errMsg = "failed to delete all snapshots " + lPath + " , err=" + result;
            s_logger.warn(errMsg);
            return new Answer(cmd, false, errMsg);
        }
        // delete the directory
        if (!snapshotDir.delete()) {
            details = "Unable to delete directory " + snapshotDir.getName() + " under snapshot path " + relativeSnapshotPath;
            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 = cmd.getDirectory();
        final String bucket = s3.getBucketName();
        try {
            S3Utils.deleteDirectory(s3, bucket, path);
            return new Answer(cmd, true, String.format("Deleted snapshot %1%s from bucket %2$s.", path, bucket));
        } catch (Exception e) {
            final String errorMessage = String.format("Failed to delete snapshot %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) {
        String path = cmd.getDirectory();
        // assuming
        String volumeId = StringUtils.substringAfterLast(path, "/");
        // that
        // the
        // filename
        // is
        // the
        // last
        // section
        // in
        // the
        // path
        String result = swiftDelete((SwiftTO) dstore, "V-" + volumeId.toString(), "");
        if (result != null) {
            String errMsg = "failed to delete snapshot for volume " + volumeId + " , err=" + result;
            s_logger.warn(errMsg);
            return new Answer(cmd, false, errMsg);
        }
        return new Answer(cmd, true, "Deleted snapshot " + path + " from swift");
    } 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) 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 88 with DataStoreTO

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

the class NfsSecondaryStorageResource method deleteSnapshot.

protected Answer deleteSnapshot(final DeleteCommand cmd) {
    DataTO obj = cmd.getData();
    DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        NfsTO nfs = (NfsTO) dstore;
        String parent = getRootDir(nfs.getUrl(), _nfsVersion);
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        String snapshotPath = obj.getPath();
        if (snapshotPath.startsWith(File.separator)) {
            snapshotPath = snapshotPath.substring(1);
        }
        // check if the passed snapshot path is a directory or not. For ImageCache, path is stored as a directory instead of
        // snapshot file name. If so, since backupSnapshot process has already deleted snapshot in cache, so we just do nothing
        // and return true.
        String fullSnapPath = parent + snapshotPath;
        File snapDir = new File(fullSnapPath);
        if (snapDir.exists() && snapDir.isDirectory()) {
            s_logger.debug("snapshot path " + snapshotPath + " is a directory, already deleted during backup snapshot, so no need to delete");
            return new Answer(cmd, true, null);
        }
        // passed snapshot path is a snapshot file path, then get snapshot directory first
        int index = snapshotPath.lastIndexOf("/");
        String snapshotName = snapshotPath.substring(index + 1);
        snapshotPath = snapshotPath.substring(0, index);
        String absoluteSnapshotPath = parent + snapshotPath;
        // check if snapshot directory exists
        File snapshotDir = new File(absoluteSnapshotPath);
        String details = null;
        if (!snapshotDir.exists()) {
            details = "snapshot directory " + snapshotDir.getName() + " doesn't exist";
            s_logger.debug(details);
            return new Answer(cmd, true, details);
        }
        // delete snapshot in the directory if exists
        String lPath = absoluteSnapshotPath + "/*" + snapshotName + "*";
        String result = deleteLocalFile(lPath);
        if (result != null) {
            details = "failed to delete snapshot " + lPath + " , err=" + result;
            s_logger.warn(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.deleteObject(s3, bucket, path);
            return new Answer(cmd, true, String.format("Deleted snapshot %1%s from bucket %2$s.", path, bucket));
        } catch (Exception e) {
            final String errorMessage = String.format("Failed to delete snapshot %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 swiftTO = (SwiftTO) dstore;
        String path = obj.getPath();
        SwiftUtil.deleteObject(swiftTO, path);
        return new Answer(cmd, true, "Deleted snapshot " + path + " from swift");
    } 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 89 with DataStoreTO

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

the class VmwareStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
    VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    String vmName = srcVolume.getVmName();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        DataStoreTO primaryStorage = srcVolume.getDataStore();
        NfsTO destStore = (NfsTO) destVolume.getDataStore();
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        Pair<String, String> result;
        result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(), destVolume.getPath(), destStore.getUrl(), hostService.getWorkerName(context, cmd, 0));
        VolumeObjectTO newVolume = new VolumeObjectTO();
        newVolume.setPath(result.first() + File.separator + result.second());
        return new CopyCmdAnswer(newVolume);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to execute CopyVolumeCommand due to exception";
        s_logger.error(msg, e);
        return new CopyCmdAnswer("copy volume from primary to secondary failed due to exception: " + VmwareHelper.getExceptionMessage(e));
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 90 with DataStoreTO

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

the class VmwareResource method examineStorageSubSystemCommandFullCloneFlagForVmware.

/**
     * Examine StorageSubSystem command to get full clone flag, if provided
     * @param cmd command to execute
     * @param params params
     * @return copy of params including new values, if suitable
     */
protected EnumMap<VmwareStorageProcessorConfigurableFields, Object> examineStorageSubSystemCommandFullCloneFlagForVmware(CopyCommand cmd, EnumMap<VmwareStorageProcessorConfigurableFields, Object> params) {
    EnumMap<VmwareStorageProcessorConfigurableFields, Object> paramsCopy = new EnumMap<VmwareStorageProcessorConfigurableFields, Object>(params);
    HypervisorType hypervisor = cmd.getDestTO().getHypervisorType();
    if (hypervisor != null && hypervisor.equals(HypervisorType.VMware)) {
        DataStoreTO destDataStore = cmd.getDestTO().getDataStore();
        if (destDataStore instanceof PrimaryDataStoreTO) {
            PrimaryDataStoreTO dest = (PrimaryDataStoreTO) destDataStore;
            if (dest.isFullCloneFlag() != null) {
                paramsCopy.put(VmwareStorageProcessorConfigurableFields.FULL_CLONE_FLAG, dest.isFullCloneFlag().booleanValue());
            }
        }
    }
    return paramsCopy;
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) VmwareStorageProcessorConfigurableFields(com.cloud.storage.resource.VmwareStorageProcessor.VmwareStorageProcessorConfigurableFields) EnumMap(java.util.EnumMap)

Aggregations

DataStoreTO (com.cloud.agent.api.to.DataStoreTO)91 NfsTO (com.cloud.agent.api.to.NfsTO)66 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)54 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)50 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)47 DataTO (com.cloud.agent.api.to.DataTO)46 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)36 InternalErrorException (com.cloud.exception.InternalErrorException)30 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)28 Answer (com.cloud.agent.api.Answer)26 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)19 IOException (java.io.IOException)17 S3TO (com.cloud.agent.api.to.S3TO)16 ConfigurationException (javax.naming.ConfigurationException)16 XmlRpcException (org.apache.xmlrpc.XmlRpcException)16 XenAPIException (com.xensource.xenapi.Types.XenAPIException)15 RemoteException (java.rmi.RemoteException)15 SwiftTO (com.cloud.agent.api.to.SwiftTO)14 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)14