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);
}
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);
}
}
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);
}
}
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);
}
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);
}
}
Aggregations