Search in sources :

Example 6 with DataStoreTO

use of com.cloud.legacymodel.to.DataStoreTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method execute.

private Answer execute(final ComputeChecksumCommand cmd) {
    String relativeTemplatePath = cmd.getTemplatePath();
    final DataStoreTO store = cmd.getStore();
    if (!(store instanceof NfsTO)) {
        return new Answer(cmd, false, "can't handle non nfs data store");
    }
    final NfsTO nfsStore = (NfsTO) store;
    String parent = getRootDir(nfsStore.getUrl());
    if (relativeTemplatePath.startsWith(File.separator)) {
        relativeTemplatePath = relativeTemplatePath.substring(1);
    }
    if (!parent.endsWith(File.separator)) {
        parent += File.separator;
    }
    final String absoluteTemplatePath = parent + relativeTemplatePath;
    final MessageDigest digest;
    String checksum = null;
    final File f = new File(absoluteTemplatePath);
    InputStream is = null;
    final 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);
        }
        final byte[] md5sum = digest.digest();
        final 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 (final IOException e) {
        final String logMsg = "Unable to process file for MD5 - " + absoluteTemplatePath;
        s_logger.error(logMsg);
        return new Answer(cmd, false, checksum);
    } catch (final NoSuchAlgorithmException e) {
        return new Answer(cmd, false, checksum);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (final 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.legacymodel.to.DataStoreTO) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NfsTO(com.cloud.legacymodel.to.NfsTO) FileInputStream(java.io.FileInputStream) ListTemplateAnswer(com.cloud.legacymodel.communication.answer.ListTemplateAnswer) GetStorageStatsAnswer(com.cloud.legacymodel.communication.answer.GetStorageStatsAnswer) ReadyAnswer(com.cloud.legacymodel.communication.answer.ReadyAnswer) ListVolumeAnswer(com.cloud.legacymodel.communication.answer.ListVolumeAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) CheckHealthAnswer(com.cloud.legacymodel.communication.answer.CheckHealthAnswer) UploadStatusAnswer(com.cloud.legacymodel.communication.answer.UploadStatusAnswer) SecStorageSetupAnswer(com.cloud.legacymodel.communication.answer.SecStorageSetupAnswer) BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest) File(java.io.File)

Example 7 with DataStoreTO

use of com.cloud.legacymodel.to.DataStoreTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method deleteVolume.

protected Answer deleteVolume(final DeleteCommand cmd) {
    final DataTO obj = cmd.getData();
    final DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        final NfsTO nfs = (NfsTO) dstore;
        String relativeVolumePath = obj.getPath();
        String parent = getRootDir(nfs.getUrl());
        if (relativeVolumePath.startsWith(File.separator)) {
            relativeVolumePath = relativeVolumePath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        final String absoluteVolumePath = parent + relativeVolumePath;
        final File volPath = new File(absoluteVolumePath);
        File tmpltParent = null;
        if (volPath.exists() && volPath.isDirectory()) {
            // for vmware, absoluteVolumePath represents a directory where volume files are located.
            tmpltParent = volPath;
        } else {
            // for other hypervisors, the volume .vhd or .qcow2 file path is passed
            tmpltParent = new File(absoluteVolumePath).getParentFile();
        }
        String details = null;
        if (!tmpltParent.exists()) {
            details = "volume parent directory " + tmpltParent.getName() + " doesn't exist";
            s_logger.debug(details);
            return new Answer(cmd, true, details);
        }
        final File[] tmpltFiles = tmpltParent.listFiles();
        if (tmpltFiles == null || tmpltFiles.length == 0) {
            details = "No files under volume parent directory " + tmpltParent.getName();
            s_logger.debug(details);
        } else {
            boolean found = false;
            for (final File f : tmpltFiles) {
                if (!found && f.getName().equals("volume.properties")) {
                    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");
                    final File[] haFiles = f.listFiles();
                    for (final File haFile : haFiles) {
                        haFile.delete();
                    }
                }
                if (!f.delete()) {
                    return new Answer(cmd, false, "Unable to delete file " + f.getName() + " under Volume path " + tmpltParent.getPath());
                }
            }
            if (!found) {
                details = "Can not find volume.properties under " + tmpltParent.getName();
                s_logger.debug(details);
            }
        }
        if (!tmpltParent.delete()) {
            details = "Unable to delete directory " + tmpltParent.getName() + " under Volume path " + tmpltParent.getPath();
            s_logger.debug(details);
            return new Answer(cmd, false, details);
        }
        return new Answer(cmd, true, null);
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + dstore);
    }
}
Also used : ListTemplateAnswer(com.cloud.legacymodel.communication.answer.ListTemplateAnswer) GetStorageStatsAnswer(com.cloud.legacymodel.communication.answer.GetStorageStatsAnswer) ReadyAnswer(com.cloud.legacymodel.communication.answer.ReadyAnswer) ListVolumeAnswer(com.cloud.legacymodel.communication.answer.ListVolumeAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) CheckHealthAnswer(com.cloud.legacymodel.communication.answer.CheckHealthAnswer) UploadStatusAnswer(com.cloud.legacymodel.communication.answer.UploadStatusAnswer) SecStorageSetupAnswer(com.cloud.legacymodel.communication.answer.SecStorageSetupAnswer) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) NfsTO(com.cloud.legacymodel.to.NfsTO) File(java.io.File)

Example 8 with DataStoreTO

use of com.cloud.legacymodel.to.DataStoreTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method execute.

public Answer execute(final DeleteSnapshotsDirCommand cmd) {
    final DataStoreTO dstore = cmd.getDataStore();
    if (dstore instanceof NfsTO) {
        final NfsTO nfs = (NfsTO) dstore;
        String relativeSnapshotPath = cmd.getDirectory();
        String parent = getRootDir(nfs.getUrl());
        if (relativeSnapshotPath.startsWith(File.separator)) {
            relativeSnapshotPath = relativeSnapshotPath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        final String absoluteSnapshotPath = parent + relativeSnapshotPath;
        final 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
        final String lPath = absoluteSnapshotPath + "/*";
        final String result = deleteLocalFile(lPath);
        if (result != null) {
            final 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 {
        return new Answer(cmd, false, "Unsupported image data store: " + dstore);
    }
}
Also used : ListTemplateAnswer(com.cloud.legacymodel.communication.answer.ListTemplateAnswer) GetStorageStatsAnswer(com.cloud.legacymodel.communication.answer.GetStorageStatsAnswer) ReadyAnswer(com.cloud.legacymodel.communication.answer.ReadyAnswer) ListVolumeAnswer(com.cloud.legacymodel.communication.answer.ListVolumeAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) CheckHealthAnswer(com.cloud.legacymodel.communication.answer.CheckHealthAnswer) UploadStatusAnswer(com.cloud.legacymodel.communication.answer.UploadStatusAnswer) SecStorageSetupAnswer(com.cloud.legacymodel.communication.answer.SecStorageSetupAnswer) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) NfsTO(com.cloud.legacymodel.to.NfsTO) File(java.io.File)

Example 9 with DataStoreTO

use of com.cloud.legacymodel.to.DataStoreTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method execute.

private Answer execute(final ListTemplateCommand cmd) {
    if (!this._inSystemVM) {
        return new ListTemplateAnswer(null, null);
    }
    final DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        final NfsTO nfs = (NfsTO) store;
        final String secUrl = nfs.getUrl();
        final String root = getRootDir(secUrl);
        final Map<String, TemplateProp> templateInfos = this._dlMgr.gatherTemplateInfo(root);
        return new ListTemplateAnswer(secUrl, templateInfos);
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + store);
    }
}
Also used : TemplateProp(com.cloud.legacymodel.storage.TemplateProp) ListTemplateAnswer(com.cloud.legacymodel.communication.answer.ListTemplateAnswer) GetStorageStatsAnswer(com.cloud.legacymodel.communication.answer.GetStorageStatsAnswer) ReadyAnswer(com.cloud.legacymodel.communication.answer.ReadyAnswer) ListVolumeAnswer(com.cloud.legacymodel.communication.answer.ListVolumeAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) CheckHealthAnswer(com.cloud.legacymodel.communication.answer.CheckHealthAnswer) UploadStatusAnswer(com.cloud.legacymodel.communication.answer.UploadStatusAnswer) SecStorageSetupAnswer(com.cloud.legacymodel.communication.answer.SecStorageSetupAnswer) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) ListTemplateAnswer(com.cloud.legacymodel.communication.answer.ListTemplateAnswer) NfsTO(com.cloud.legacymodel.to.NfsTO)

Example 10 with DataStoreTO

use of com.cloud.legacymodel.to.DataStoreTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method execute.

protected GetStorageStatsAnswer execute(final GetStorageStatsCommand cmd) {
    final DataStoreTO store = cmd.getStore();
    final String rootDir = getRootDir(((NfsTO) store).getUrl());
    final long usedSize = getUsedSize(rootDir);
    final long totalSize = getTotalSize(rootDir);
    if ((usedSize == 0 && totalSize == 0) || usedSize < 0 || totalSize < 0) {
        return new GetStorageStatsAnswer(cmd, "Unable to get storage stats");
    } else {
        return new GetStorageStatsAnswer(cmd, totalSize, usedSize);
    }
}
Also used : DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) GetStorageStatsAnswer(com.cloud.legacymodel.communication.answer.GetStorageStatsAnswer)

Aggregations

DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)49 NfsTO (com.cloud.legacymodel.to.NfsTO)36 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)30 DataTO (com.cloud.legacymodel.to.DataTO)30 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)25 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)24 Answer (com.cloud.legacymodel.communication.answer.Answer)19 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)17 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)15 XenAPIException (com.xensource.xenapi.Types.XenAPIException)15 XmlRpcException (org.apache.xmlrpc.XmlRpcException)15 Connection (com.xensource.xenapi.Connection)14 SR (com.xensource.xenapi.SR)13 VDI (com.xensource.xenapi.VDI)13 URI (java.net.URI)12 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)11 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)9 GetStorageStatsAnswer (com.cloud.legacymodel.communication.answer.GetStorageStatsAnswer)9 CheckHealthAnswer (com.cloud.legacymodel.communication.answer.CheckHealthAnswer)8 ListTemplateAnswer (com.cloud.legacymodel.communication.answer.ListTemplateAnswer)8