use of com.cloud.storage.template.Processor in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
String secondaryStorageURL = cmd.getSecondaryStorageUrl();
KVMStoragePool secondaryStorage = null;
try {
Connect conn = LibvirtConnection.getConnection();
String templateFolder = cmd.getAccountId() + File.separator + cmd.getTemplateId() + File.separator;
String templateInstallFolder = "/template/tmpl/" + templateFolder;
secondaryStorage = _storagePoolMgr.getStoragePoolByURI(secondaryStorageURL);
KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
_storage.mkdirs(tmpltPath);
Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
command.add("-f", disk.getPath());
command.add("-t", tmpltPath);
command.add("-n", cmd.getUniqueName() + ".qcow2");
String result = command.execute();
if (result != null) {
s_logger.debug("failed to create template: " + result);
return new CreatePrivateTemplateAnswer(cmd, false, result);
}
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
Processor qcow2Processor = new QCOW2Processor();
qcow2Processor.configure("QCOW2 Processor", params);
FormatInfo info = qcow2Processor.process(tmpltPath, null, cmd.getUniqueName());
TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
loc.create(1, true, cmd.getUniqueName());
loc.addFormat(info);
loc.save();
return new CreatePrivateTemplateAnswer(cmd, true, null, templateInstallFolder + cmd.getUniqueName() + ".qcow2", info.virtualSize, info.size, cmd.getUniqueName(), ImageFormat.QCOW2);
} catch (LibvirtException e) {
s_logger.debug("Failed to get secondary storage pool: " + e.toString());
return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
} catch (InternalErrorException e) {
return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
} catch (IOException e) {
return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
} catch (ConfigurationException e) {
return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
} catch (CloudRuntimeException e) {
return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
} finally {
if (secondaryStorage != null) {
secondaryStorage.delete();
}
}
}
use of com.cloud.storage.template.Processor in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method postUpload.
public String postUpload(final String uuid, final String filename) {
final UploadEntity uploadEntity = uploadEntityStateMap.get(uuid);
final int installTimeoutPerGig = 180 * 60 * 1000;
final String resourcePath = uploadEntity.getInstallPathPrefix();
// template download
final String finalResourcePath = uploadEntity.getTmpltPath();
final UploadEntity.ResourceType resourceType = uploadEntity.getResourceType();
final String fileSavedTempLocation = uploadEntity.getInstallPathPrefix() + "/" + filename;
final String uploadedFileExtension = FilenameUtils.getExtension(filename);
String userSelectedFormat = uploadEntity.getFormat().toString();
if (uploadedFileExtension.equals("zip") || uploadedFileExtension.equals("bz2") || uploadedFileExtension.equals("gz")) {
userSelectedFormat += "." + uploadedFileExtension;
}
final String formatError = ImageStoreUtil.checkTemplateFormat(fileSavedTempLocation, userSelectedFormat);
if (StringUtils.isNotBlank(formatError)) {
final String errorString = "File type mismatch between uploaded file and selected format. Selected file format: " + userSelectedFormat + ". Received: " + formatError;
s_logger.error(errorString);
return errorString;
}
int imgSizeGigs = getSizeInGB(_storage.getSize(fileSavedTempLocation));
final int maxSize = uploadEntity.getMaxSizeInGB();
if (imgSizeGigs > maxSize) {
final String errorMessage = "Maximum file upload size exceeded. Physical file size: " + imgSizeGigs + "GB. Maximum allowed size: " + maxSize + "GB.";
s_logger.error(errorMessage);
return errorMessage;
}
// add one just in case
imgSizeGigs++;
final long timeout = (long) imgSizeGigs * installTimeoutPerGig;
final Script scr = new Script(getScriptLocation(resourceType), timeout, s_logger);
scr.add("-s", Integer.toString(imgSizeGigs));
scr.add("-S", Long.toString(UploadEntity.s_maxTemplateSize));
if (uploadEntity.getDescription() != null && uploadEntity.getDescription().length() > 1) {
scr.add("-d", uploadEntity.getDescription());
}
if (uploadEntity.isHvm()) {
scr.add("-h");
}
final String checkSum = uploadEntity.getChksum();
if (StringUtils.isNotBlank(checkSum)) {
scr.add("-c", checkSum);
}
// add options common to ISO and template
final String extension = uploadEntity.getFormat().getFileExtension();
String templateName = "";
if (extension.equals("iso")) {
templateName = uploadEntity.getUuid().trim().replace(" ", "_");
} else {
try {
templateName = UUID.nameUUIDFromBytes((uploadEntity.getFilename() + System.currentTimeMillis()).getBytes("UTF-8")).toString();
} catch (final UnsupportedEncodingException e) {
templateName = uploadEntity.getUuid().trim().replace(" ", "_");
}
}
// run script to mv the temporary template file to the final template
// file
final String templateFilename = templateName + "." + extension;
uploadEntity.setTemplatePath(finalResourcePath + "/" + templateFilename);
scr.add("-n", templateFilename);
scr.add("-t", resourcePath);
// this is the temporary
scr.add("-f", fileSavedTempLocation);
// template file downloaded
if (uploadEntity.getChksum() != null && uploadEntity.getChksum().length() > 1) {
scr.add("-c", uploadEntity.getChksum());
}
// cleanup
scr.add("-u");
final String result;
result = scr.execute();
if (result != null) {
return result;
}
// Set permissions for the downloaded template
final File downloadedTemplate = new File(resourcePath + "/" + templateFilename);
_storage.setWorldReadableAndWriteable(downloadedTemplate);
// Set permissions for template/volume.properties
String propertiesFile = resourcePath;
if (resourceType == UploadEntity.ResourceType.TEMPLATE) {
propertiesFile += "/template.properties";
} else {
propertiesFile += "/volume.properties";
}
final File templateProperties = new File(propertiesFile);
_storage.setWorldReadableAndWriteable(templateProperties);
final TemplateLocation loc = new TemplateLocation(_storage, resourcePath);
try {
loc.create(uploadEntity.getEntityId(), true, uploadEntity.getFilename());
} catch (final IOException e) {
s_logger.warn("Something is wrong with template location " + resourcePath, e);
loc.purge();
return "Unable to upload due to " + e.getMessage();
}
final Map<String, Processor> processors = _dlMgr.getProcessors();
for (final Processor processor : processors.values()) {
FormatInfo info = null;
try {
info = processor.process(resourcePath, null, templateName);
} catch (final InternalErrorException e) {
s_logger.error("Template process exception ", e);
return e.toString();
}
if (info != null) {
loc.addFormat(info);
uploadEntity.setVirtualSize(info.virtualSize);
uploadEntity.setPhysicalSize(info.size);
break;
}
}
if (!loc.save()) {
s_logger.warn("Cleaning up because we're unable to save the formats");
loc.purge();
}
uploadEntity.setStatus(UploadEntity.Status.COMPLETED);
uploadEntityStateMap.put(uploadEntity.getUuid(), uploadEntity);
return null;
}
use of com.cloud.storage.template.Processor in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method getVirtualSize.
protected long getVirtualSize(final File file, final ImageFormat format) {
Processor processor = null;
try {
if (format == null) {
return file.length();
} else if (format == ImageFormat.QCOW2) {
processor = new QCOW2Processor();
} else if (format == ImageFormat.VHD) {
processor = new VhdProcessor();
} else if (format == ImageFormat.RAW) {
processor = new RawImageProcessor();
}
if (format == ImageFormat.TAR) {
processor = new TARProcessor();
}
if (processor == null) {
return file.length();
}
final Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("template processor", params);
return processor.getVirtualSize(file);
} catch (final Exception e) {
s_logger.warn("Failed to get virtual size of file " + file.getPath() + ", returning file size instead: ", e);
return file.length();
}
}
use of com.cloud.storage.template.Processor in project cosmic by MissionCriticalCloud.
the class LibvirtUtilitiesHelper method buildQcow2Processor.
public Processor buildQcow2Processor(final StorageLayer storage) throws ConfigurationException {
final Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, storage);
final Processor qcow2Processor = new QCOW2Processor();
qcow2Processor.configure("QCOW2 Processor", params);
return qcow2Processor;
}
use of com.cloud.storage.template.Processor in project cosmic by MissionCriticalCloud.
the class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper method execute.
@Override
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
final String templateInstallFolder = "template/tmpl/" + templateFolder;
final String tmplName = libvirtUtilitiesHelper.generateUuidName();
final String tmplFileName = tmplName + ".qcow2";
KvmStoragePool secondaryPool = null;
KvmStoragePool snapshotPool = null;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
try {
String snapshotPath = command.getSnapshotUuid();
final int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
snapshotPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
secondaryPool = storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl());
final KvmPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName());
final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
final StorageLayer storage = libvirtComputingResource.getStorage();
storage.mkdirs(templatePath);
final String tmplPath = templateInstallFolder + File.separator + tmplFileName;
final String createTmplPath = libvirtComputingResource.createTmplPath();
final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
scriptCommand.add("-t", templatePath);
scriptCommand.add("-n", tmplFileName);
scriptCommand.add("-f", snapshot.getPath());
scriptCommand.execute();
final Processor qcow2Processor = libvirtUtilitiesHelper.buildQcow2Processor(storage);
final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath);
loc.create(1, true, tmplName);
loc.addFormat(info);
loc.save();
return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
} catch (final ConfigurationException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} catch (final InternalErrorException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} catch (final IOException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} catch (final CloudRuntimeException e) {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
} finally {
if (secondaryPool != null) {
storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
}
if (snapshotPool != null) {
storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid());
}
}
}
Aggregations