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