use of com.cloud.agent.api.storage.OVFHelper in project cloudstack by apache.
the class NfsSecondaryStorageResource method execute.
public Answer execute(CreateDatadiskTemplateCommand cmd) {
TemplateObjectTO diskTemplate = new TemplateObjectTO();
TemplateObjectTO dataDiskTemplate = (TemplateObjectTO) cmd.getDataDiskTemplate();
DataStoreTO dataStore = dataDiskTemplate.getDataStore();
if (!(dataStore instanceof NfsTO)) {
return new CreateDatadiskTemplateAnswer("Unsupported protocol");
}
NfsTO nfsImageStore = (NfsTO) dataStore;
String secondaryStorageUrl = nfsImageStore.getUrl();
assert (secondaryStorageUrl != null);
try {
String secondaryMountPoint = getRootDir(secondaryStorageUrl, _nfsVersion);
long templateId = dataDiskTemplate.getId();
String templateUniqueName = dataDiskTemplate.getUniqueName();
String origDisk = cmd.getPath();
long virtualSize = dataDiskTemplate.getSize();
String diskName = origDisk.substring((origDisk.lastIndexOf(File.separator)) + 1);
long physicalSize = new File(origDisk).length();
String newTmplDir = getTemplateRelativeDirInSecStorage(dataDiskTemplate.getAccountId(), dataDiskTemplate.getId());
String newTmplDirAbsolute = secondaryMountPoint + File.separator + newTmplDir;
String ovfFilePath = getOVFFilePath(origDisk);
if (!cmd.getBootable()) {
// Create folder to hold datadisk template
synchronized (newTmplDir.intern()) {
Script command = new Script("mkdir", _timeout, s_logger);
command.add("-p");
command.add(newTmplDirAbsolute);
String result = command.execute();
if (result != null) {
String msg = "Unable to prepare template directory: " + newTmplDir + ", storage: " + secondaryStorageUrl + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
// Move Datadisk VMDK from parent template folder to Datadisk template folder
synchronized (origDisk.intern()) {
Script command = new Script("mv", _timeout, s_logger);
command.add(origDisk);
command.add(newTmplDirAbsolute);
String result = command.execute();
if (result != null) {
String msg = "Unable to copy VMDK from parent template folder to datadisk template folder" + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
command = new Script("cp", _timeout, s_logger);
command.add(ovfFilePath + ORIGINAL_FILE_EXTENSION);
command.add(newTmplDirAbsolute);
result = command.execute();
if (result != null) {
String msg = "Unable to copy VMDK from parent template folder to datadisk template folder" + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
}
// Create OVF for the disk
String newOvfFilePath = newTmplDirAbsolute + File.separator + ovfFilePath.substring(ovfFilePath.lastIndexOf(File.separator) + 1);
OVFHelper ovfHelper = new OVFHelper();
ovfHelper.rewriteOVFFileForSingleDisk(ovfFilePath + ORIGINAL_FILE_EXTENSION, newOvfFilePath, diskName);
postCreatePrivateTemplate(newTmplDirAbsolute, templateId, templateUniqueName, physicalSize, virtualSize);
writeMetaOvaForTemplate(newTmplDirAbsolute, ovfFilePath.substring(ovfFilePath.lastIndexOf(File.separator) + 1), diskName, templateUniqueName, physicalSize);
diskTemplate.setId(templateId);
if (diskName.endsWith("iso")) {
diskTemplate.setPath(newTmplDir + File.separator + diskName);
} else {
diskTemplate.setPath(newTmplDir + File.separator + templateUniqueName + ".ova");
}
diskTemplate.setSize(virtualSize);
diskTemplate.setPhysicalSize(physicalSize);
} catch (Exception e) {
String msg = "Create Datadisk template failed due to " + e.getMessage();
s_logger.error(msg, e);
return new CreateDatadiskTemplateAnswer(msg);
}
return new CreateDatadiskTemplateAnswer(diskTemplate);
}
use of com.cloud.agent.api.storage.OVFHelper 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.storage.OVFHelper in project cloudstack by apache.
the class OVAProcessor method validateOva.
/**
* side effect; properties are added to the info
*
* @throws InternalErrorException on an invalid ova contents
*/
private void validateOva(String templateFileFullPath, FormatInfo info) throws InternalErrorException {
String ovfFilePath = getOVFFilePath(templateFileFullPath);
OVFHelper ovfHelper = new OVFHelper();
Document doc = ovfHelper.getOvfParser().parseOVFFile(ovfFilePath);
OVFInformationTO ovfInformationTO = createOvfInformationTO(ovfHelper, doc, ovfFilePath);
info.ovfInformationTO = ovfInformationTO;
}
use of com.cloud.agent.api.storage.OVFHelper in project cloudstack by apache.
the class OVAProcessor method getTemplateVirtualSize.
/**
* gets the virtual size from the OVF file meta data.
*
* @return the accumulative virtual size of the disk definitions in the OVF
* @throws InternalErrorException
*/
public long getTemplateVirtualSize(String templatePath, String templateName) throws InternalErrorException {
long virtualSize = 0;
String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator;
templateFileFullPath += templateName.endsWith(ImageFormat.OVA.getFileExtension()) ? templateName : templateName + "." + ImageFormat.OVA.getFileExtension();
String ovfFileName = getOVFFilePath(templateFileFullPath);
OVFHelper ovfHelper = new OVFHelper();
if (ovfFileName == null) {
String msg = "Unable to locate OVF file in template package directory: " + templatePath;
LOGGER.error(msg);
throw new InternalErrorException(msg);
}
try {
Document ovfDoc = ovfHelper.getOvfParser().parseOVFFile(ovfFileName);
NodeList diskElements = ovfHelper.getOvfParser().getElementsFromOVFDocument(ovfDoc, "Disk");
for (int i = 0; i < diskElements.getLength(); i++) {
Element disk = (Element) diskElements.item(i);
String diskSizeValue = disk.getAttribute("ovf:capacity");
long diskSize = 1;
try {
diskSize = Long.parseLong(diskSizeValue);
} catch (NumberFormatException e) {
// ASSUMEably the diskSize contains a property for replacement
LOGGER.warn(String.format("the disksize for disk %s is not a valid number: %s", disk.getAttribute("diskId"), diskSizeValue));
// TODO parse the property to get any value can not be done at registration time
// and will have to be done at deploytime, so for orchestration purposes
// we now assume, a value of one
}
String allocationUnits = disk.getAttribute("ovf:capacityAllocationUnits");
diskSize = OVFHelper.getDiskVirtualSize(diskSize, allocationUnits, ovfFileName);
virtualSize += diskSize;
}
return virtualSize;
} catch (InternalErrorException | NumberFormatException e) {
String msg = "getTemplateVirtualSize: Unable to parse OVF XML document " + templatePath + " to get the virtual disk " + templateName + " size due to " + e;
LOGGER.error(msg);
throw new InternalErrorException(msg);
}
}
Aggregations