use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method copyFromNfsToS3.
protected Answer copyFromNfsToS3(CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
DataStoreTO srcDataStore = srcData.getDataStore();
NfsTO srcStore = (NfsTO) srcDataStore;
DataStoreTO destDataStore = destData.getDataStore();
final S3TO s3 = (S3TO) destDataStore;
try {
final String templatePath = determineStorageTemplatePath(srcStore.getUrl(), srcData.getPath(), _nfsVersion);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found " + srcData.getObjectType() + " from directory " + templatePath + " to upload to S3.");
}
final String bucket = s3.getBucketName();
File srcFile = findFile(templatePath);
if (srcFile == null) {
return new CopyCmdAnswer("Can't find src file:" + templatePath);
}
ImageFormat format = getTemplateFormat(srcFile.getName());
String key = destData.getPath() + S3Utils.SEPARATOR + srcFile.getName();
putFile(s3, srcFile, bucket, key).waitForCompletion();
DataTO retObj = null;
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(key);
newTemplate.setSize(getVirtualSize(srcFile, format));
newTemplate.setPhysicalSize(srcFile.length());
newTemplate.setFormat(format);
retObj = newTemplate;
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(key);
newVol.setSize(srcFile.length());
retObj = newVol;
} else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(key);
retObj = newSnapshot;
}
return new CopyCmdAnswer(retObj);
} catch (Exception e) {
s_logger.error("failed to upload" + srcData.getPath(), e);
return new CopyCmdAnswer("failed to upload" + srcData.getPath() + e.toString());
}
}
use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
public Answer execute(GetDatadisksCommand cmd) {
DataTO srcData = cmd.getData();
String configurationId = cmd.getConfigurationId();
TemplateObjectTO template = (TemplateObjectTO) srcData;
DataStoreTO srcStore = srcData.getDataStore();
if (!(srcStore instanceof NfsTO)) {
return new CreateDatadiskTemplateAnswer("Unsupported protocol");
}
NfsTO nfsImageStore = (NfsTO) srcStore;
String secondaryStorageUrl = nfsImageStore.getUrl();
assert (secondaryStorageUrl != null);
String templateUrl = secondaryStorageUrl + File.separator + srcData.getPath();
Pair<String, String> templateInfo = decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl, templateUrl, template.getName());
String templateRelativeFolderPath = templateInfo.first();
try {
String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
s_logger.info("MDOVE Secondary storage mount point: " + secondaryMountPoint);
String srcOVAFileName = getTemplateOnSecStorageFilePath(secondaryMountPoint, templateRelativeFolderPath, templateInfo.second(), ImageFormat.OVA.getFileExtension());
String ovfFilePath = getOVFFilePath(srcOVAFileName);
if (ovfFilePath == null) {
Script command = new Script("tar", 0, s_logger);
command.add("--no-same-owner");
command.add("--no-same-permissions");
command.add("-xf", srcOVAFileName);
command.setWorkDir(secondaryMountPoint + File.separator + templateRelativeFolderPath);
s_logger.info("Executing command: " + command.toString());
String result = command.execute();
if (result != null) {
String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
s_logger.error(msg);
throw new Exception(msg);
}
command = new Script("chmod", 0, s_logger);
command.add("-R");
command.add("666", secondaryMountPoint + File.separator + templateRelativeFolderPath);
result = command.execute();
if (result != null) {
s_logger.warn("Unable to set permissions for " + secondaryMountPoint + File.separator + templateRelativeFolderPath + " due to " + result);
}
}
Script command = new Script("cp", _timeout, s_logger);
command.add(ovfFilePath);
command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
String result = command.execute();
if (result != null) {
String msg = "Unable to rename original OVF, error msg: " + result;
s_logger.error(msg);
}
s_logger.debug("Reading OVF " + ovfFilePath + " to retrive the number of disks present in OVA");
OVFHelper ovfHelper = new OVFHelper();
List<DatadiskTO> disks = ovfHelper.getOVFVolumeInfoFromFile(ovfFilePath, configurationId);
return new GetDatadisksAnswer(disks);
} catch (Exception e) {
String msg = "Get Datadisk Template Count failed due to " + e.getMessage();
s_logger.error(msg, e);
return new GetDatadisksAnswer(msg);
}
}
use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
private Answer execute(ListTemplateCommand cmd) {
if (!_inSystemVM) {
return new ListTemplateAnswer(null, null);
}
DataStoreTO store = cmd.getDataStore();
if (store instanceof NfsTO) {
NfsTO nfs = (NfsTO) store;
String secUrl = nfs.getUrl();
String root = getRootDir(secUrl, cmd.getNfsVersion());
Map<String, TemplateProp> templateInfos = _dlMgr.gatherTemplateInfo(root);
return new ListTemplateAnswer(secUrl, templateInfos);
} else if (store instanceof SwiftTO) {
SwiftTO swift = (SwiftTO) store;
Map<String, TemplateProp> templateInfos = swiftListTemplate(swift);
return new ListTemplateAnswer(swift.toString(), templateInfos);
} else if (store instanceof S3TO) {
S3TO s3 = (S3TO) store;
Map<String, TemplateProp> templateInfos = s3ListTemplate(s3);
return new ListTemplateAnswer(s3.getBucketName(), templateInfos);
} else {
return new Answer(cmd, false, "Unsupported image data store: " + store);
}
}
use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method deleteTemplate.
protected Answer deleteTemplate(DeleteCommand cmd) {
DataTO obj = cmd.getData();
DataStoreTO dstore = obj.getDataStore();
if (dstore instanceof NfsTO) {
NfsTO nfs = (NfsTO) dstore;
String relativeTemplatePath = obj.getPath();
String parent = getRootDir(nfs.getUrl(), _nfsVersion);
if (relativeTemplatePath.startsWith(File.separator)) {
relativeTemplatePath = relativeTemplatePath.substring(1);
}
if (!parent.endsWith(File.separator)) {
parent += File.separator;
}
String absoluteTemplatePath = parent + relativeTemplatePath;
File tmpltPath = new File(absoluteTemplatePath);
File tmpltParent = null;
if (tmpltPath.exists() && tmpltPath.isDirectory()) {
tmpltParent = tmpltPath;
} else if (absoluteTemplatePath.endsWith(File.separator + obj.getId())) {
// the path ends with <account id>/<template id>, if upload fails
tmpltParent = tmpltPath;
} else {
tmpltParent = tmpltPath.getParentFile();
}
String details = null;
if (!tmpltParent.exists()) {
details = "template parent directory " + tmpltParent.getName() + " doesn't exist";
s_logger.debug(details);
return new Answer(cmd, true, details);
}
File[] tmpltFiles = tmpltParent.listFiles();
if (tmpltFiles == null || tmpltFiles.length == 0) {
details = "No files under template parent directory " + tmpltParent.getName();
s_logger.debug(details);
} else {
boolean found = false;
for (File f : tmpltFiles) {
if (!found && f.getName().equals(_tmpltpp)) {
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");
File[] haFiles = f.listFiles();
for (File haFile : haFiles) {
haFile.delete();
}
}
if (!f.delete()) {
return new Answer(cmd, false, "Unable to delete file " + f.getName() + " under Template path " + relativeTemplatePath);
}
}
if (!found) {
details = "Can not find template.properties under " + tmpltParent.getName();
s_logger.debug(details);
}
}
if (!tmpltParent.delete()) {
details = "Unable to delete directory " + tmpltParent.getName() + " under Template path " + relativeTemplatePath;
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 = obj.getPath();
final String bucket = s3.getBucketName();
try {
S3Utils.deleteDirectory(s3, bucket, path);
return new Answer(cmd, true, String.format("Deleted template %1$s from bucket %2$s.", path, bucket));
} catch (Exception e) {
final String errorMessage = String.format("Failed to delete template %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 swift = (SwiftTO) dstore;
String container = "T-" + obj.getId();
String object = "";
try {
String result = swiftDelete(swift, container, object);
if (result != null) {
String errMsg = "failed to delete object " + container + "/" + object + " , err=" + result;
s_logger.warn(errMsg);
return new Answer(cmd, false, errMsg);
}
return new Answer(cmd, true, "success");
} catch (Exception e) {
String errMsg = cmd + " Command failed due to " + e.toString();
s_logger.warn(errMsg, e);
return new Answer(cmd, false, errMsg);
}
} else {
return new Answer(cmd, false, "Unsupported image data store: " + dstore);
}
}
use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
private Answer execute(HandleConfigDriveIsoCommand cmd) {
if (cmd.isCreate()) {
if (cmd.getIsoData() == null) {
return new HandleConfigDriveIsoAnswer(cmd, "Invalid config drive ISO data");
}
String nfsMountPoint = getRootDir(cmd.getDestStore().getUrl(), _nfsVersion);
File isoFile = new File(nfsMountPoint, cmd.getIsoFile());
if (isoFile.exists()) {
s_logger.debug("config drive iso already exists");
}
Path tempDir = null;
try {
tempDir = java.nio.file.Files.createTempDirectory(ConfigDrive.CONFIGDRIVEDIR);
File tmpIsoFile = ConfigDriveBuilder.base64StringToFile(cmd.getIsoData(), tempDir.toAbsolutePath().toString(), cmd.getIsoFile());
copyLocalToNfs(tmpIsoFile, new File(cmd.getIsoFile()), cmd.getDestStore());
} catch (IOException | ConfigurationException e) {
return new HandleConfigDriveIsoAnswer(cmd, "Failed due to exception: " + e.getMessage());
} finally {
try {
if (tempDir != null) {
FileUtils.deleteDirectory(tempDir.toFile());
}
} catch (IOException ioe) {
s_logger.warn("Failed to delete ConfigDrive temporary directory: " + tempDir.toString(), ioe);
}
}
return new HandleConfigDriveIsoAnswer(cmd, NetworkElement.Location.SECONDARY, "Successfully saved config drive at secondary storage");
} else {
DataStoreTO dstore = cmd.getDestStore();
if (dstore instanceof NfsTO) {
NfsTO nfs = (NfsTO) dstore;
String relativeTemplatePath = new File(cmd.getIsoFile()).getPath();
String nfsMountPoint = getRootDir(nfs.getUrl(), _nfsVersion);
File tmpltPath = new File(nfsMountPoint, relativeTemplatePath);
try {
Files.deleteIfExists(tmpltPath.toPath());
} catch (IOException e) {
return new HandleConfigDriveIsoAnswer(cmd, e);
}
return new HandleConfigDriveIsoAnswer(cmd);
} else {
return new HandleConfigDriveIsoAnswer(cmd, "Not implemented yet");
}
}
}
Aggregations