Search in sources :

Example 56 with Answer

use of com.cloud.legacymodel.communication.answer.Answer in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method execute.

protected Answer execute(final DeleteCommand cmd) {
    final DataTO obj = cmd.getData();
    final DataObjectType objType = obj.getObjectType();
    if (obj.getPath() == null) {
        // account for those fake entries for NFS migration to object store
        return new Answer(cmd, true, "Object with null install path does not exist on image store , no need to delete");
    }
    switch(objType) {
        case TEMPLATE:
            return deleteTemplate(cmd);
        case VOLUME:
            return deleteVolume(cmd);
        case SNAPSHOT:
            return deleteSnapshot(cmd);
    }
    return Answer.createUnsupportedCommandAnswer(cmd);
}
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) DataTO(com.cloud.legacymodel.to.DataTO) DataObjectType(com.cloud.model.enumeration.DataObjectType)

Example 57 with Answer

use of com.cloud.legacymodel.communication.answer.Answer 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 58 with Answer

use of com.cloud.legacymodel.communication.answer.Answer 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.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 59 with Answer

use of com.cloud.legacymodel.communication.answer.Answer 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 60 with Answer

use of com.cloud.legacymodel.communication.answer.Answer 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)

Aggregations

Answer (com.cloud.legacymodel.communication.answer.Answer)342 Test (org.junit.Test)201 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)173 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)116 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)110 RebootAnswer (com.cloud.legacymodel.communication.answer.RebootAnswer)63 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)61 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)58 CreateAnswer (com.cloud.legacymodel.communication.answer.CreateAnswer)54 LibvirtException (org.libvirt.LibvirtException)53 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)53 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)48 Connection (com.xensource.xenapi.Connection)48 ArrayList (java.util.ArrayList)45 KvmStoragePool (com.cloud.agent.resource.kvm.storage.KvmStoragePool)39 StoragePool (com.cloud.legacymodel.storage.StoragePool)38 Connect (org.libvirt.Connect)38 NfsStoragePool (com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool)29 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)29 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)27