Search in sources :

Example 96 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method deleteSnapshot.

protected Answer deleteSnapshot(final DeleteCommand cmd) {
    final DataTO obj = cmd.getData();
    final DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        final NfsTO nfs = (NfsTO) dstore;
        String parent = getRootDir(nfs.getUrl());
        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.
        final String fullSnapPath = parent + snapshotPath;
        final 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
        final int index = snapshotPath.lastIndexOf("/");
        final String snapshotName = snapshotPath.substring(index + 1);
        snapshotPath = snapshotPath.substring(0, index);
        final String absoluteSnapshotPath = parent + snapshotPath;
        // check if snapshot directory exists
        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 snapshot in the directory if exists
        final String lPath = absoluteSnapshotPath + "/*" + snapshotName + "*";
        final 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 {
        return new Answer(cmd, false, "Unsupported image data store: " + dstore);
    }
}
Also used : ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(com.cloud.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(com.cloud.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) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File)

Example 97 with NfsTO

use of com.cloud.agent.api.to.NfsTO 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.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(com.cloud.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(com.cloud.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) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File)

Example 98 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method execute.

private Answer execute(final ListVolumeCommand cmd) {
    if (!_inSystemVM) {
        return new ListVolumeAnswer(cmd.getSecUrl(), null);
    }
    final DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        final String root = getRootDir(cmd.getSecUrl());
        final Map<Long, TemplateProp> templateInfos = _dlMgr.gatherVolumeInfo(root);
        return new ListVolumeAnswer(cmd.getSecUrl(), 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(com.cloud.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(com.cloud.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) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 99 with NfsTO

use of com.cloud.agent.api.to.NfsTO 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.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(com.cloud.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(com.cloud.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)

Example 100 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cosmic by MissionCriticalCloud.

the class DownloadManagerImpl method handleDownloadCommand.

@Override
public DownloadAnswer handleDownloadCommand(final SecondaryStorageResource resource, final DownloadCommand cmd) {
    final ResourceType resourceType = cmd.getResourceType();
    if (cmd instanceof DownloadProgressCommand) {
        return handleDownloadProgressCmd(resource, (DownloadProgressCommand) cmd);
    }
    if (cmd.getUrl() == null) {
        return new DownloadAnswer(resourceType.toString() + " is corrupted on storage due to an invalid url , cannot download", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    if (cmd.getName() == null) {
        return new DownloadAnswer("Invalid Name", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    final DataStoreTO dstore = cmd.getDataStore();
    String installPathPrefix = cmd.getInstallPath();
    // for NFS, we need to get mounted path
    if (dstore instanceof NfsTO) {
        installPathPrefix = resource.getRootDir(((NfsTO) dstore).getUrl()) + File.separator + installPathPrefix;
    }
    String user = null;
    String password = null;
    if (cmd.getAuth() != null) {
        user = cmd.getAuth().getUserName();
        password = cmd.getAuth().getPassword();
    }
    // TO DO - Define Volume max size as well
    final long maxDownloadSizeInBytes = cmd.getMaxDownloadSizeInBytes() == null ? TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES : cmd.getMaxDownloadSizeInBytes();
    String jobId = null;
    jobId = downloadPublicTemplate(cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, cmd.getInstallPath(), user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
    sleep();
    if (jobId == null) {
        return new DownloadAnswer("Internal Error", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplateSize(jobId), getDownloadCheckSum(jobId));
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) ResourceType(com.cloud.storage.command.DownloadCommand.ResourceType) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) DownloadProgressCommand(com.cloud.storage.command.DownloadProgressCommand)

Aggregations

NfsTO (com.cloud.agent.api.to.NfsTO)142 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)79 DataTO (com.cloud.agent.api.to.DataTO)71 InternalErrorException (com.cloud.exception.InternalErrorException)58 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)52 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)39 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)38 XmlRpcException (org.apache.xmlrpc.XmlRpcException)37 XenAPIException (com.xensource.xenapi.Types.XenAPIException)36 URI (java.net.URI)36 Connection (com.xensource.xenapi.Connection)34 SR (com.xensource.xenapi.SR)34 VDI (com.xensource.xenapi.VDI)34 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)32 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)32 Answer (com.cloud.agent.api.Answer)29 IOException (java.io.IOException)28 File (java.io.File)27 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)26