use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method dettachIso.
@Override
public Answer dettachIso(final DettachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final DataTO data = disk.getData();
final DataStoreTO store = data.getDataStore();
String isoURL = null;
if (store == null) {
final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
isoURL = iso.getName();
} else {
if (!(store instanceof NfsTO)) {
s_logger.debug("Can't attach a iso which is not created on nfs: ");
return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
}
final NfsTO nfsStore = (NfsTO) store;
isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
}
try {
final Connection conn = hypervisorResource.getConnection();
// Find the VM
final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
final String vmUUID = vm.getUuid(conn);
// Find the ISO VDI
final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
final SR sr = isoVDI.getSR(conn);
// Look up all VBDs for this VDI
final Set<VBD> vbds = isoVDI.getVBDs(conn);
// the ISO from it
for (final VBD vbd : vbds) {
final VM vbdVM = vbd.getVM(conn);
final String vbdVmUUID = vbdVM.getUuid(conn);
if (vbdVmUUID.equals(vmUUID)) {
// If an ISO is already inserted, eject it
if (!vbd.getEmpty(conn)) {
vbd.eject(conn);
}
break;
}
}
if (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
hypervisorResource.removeSR(conn, sr);
}
return new DettachAnswer(disk);
} catch (final XenAPIException e) {
final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + " due to " + e.toString();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
} catch (final Exception e) {
final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + " due to " + e.getMessage();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
}
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method createTemplateFromVolume.
@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
final int wait = cmd.getWait();
final String secondaryStoragePoolURL = destStore.getUrl();
final String volumeUUID = volume.getPath();
final String userSpecifiedName = template.getName();
String details = null;
SR tmpltSR = null;
boolean result = false;
String secondaryStorageMountPath = null;
String installPath = null;
Task task = null;
try {
final URI uri = new URI(secondaryStoragePoolURL);
secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
installPath = template.getPath();
if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
details = " Filed to create folder " + installPath + " in secondary storage";
s_logger.warn(details);
return new CopyCmdAnswer(details);
}
final VDI vol = getVDIbyUuid(conn, volumeUUID);
// create template SR
tmpltSR = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), installPath);
// copy volume to template SR
task = vol.copyAsync(conn, tmpltSR, null, null);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
final VDI tmpltVDI = Types.toVDI(task, conn);
// scan makes XenServer pick up VDI physicalSize
tmpltSR.scan(conn);
if (userSpecifiedName != null) {
tmpltVDI.setNameLabel(conn, userSpecifiedName);
}
final String tmpltUUID = tmpltVDI.getUuid(conn);
final String tmpltFilename = tmpltUUID + ".vhd";
final long virtualSize = tmpltVDI.getVirtualSize(conn);
final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
// create the template.properties file
final String templatePath = secondaryStorageMountPath + "/" + installPath;
result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
if (!result) {
throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
}
installPath = installPath + "/" + tmpltFilename;
hypervisorResource.removeSR(conn, tmpltSR);
tmpltSR = null;
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(installPath);
newTemplate.setFormat(Storage.ImageFormat.VHD);
newTemplate.setSize(virtualSize);
newTemplate.setPhysicalSize(physicalSize);
newTemplate.setName(tmpltUUID);
final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
return answer;
} catch (final Exception e) {
if (tmpltSR != null) {
hypervisorResource.removeSR(conn, tmpltSR);
}
if (secondaryStorageMountPath != null) {
hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
}
details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
s_logger.error(details, e);
} finally {
if (task != null) {
try {
task.destroy(conn);
} catch (final Exception e) {
s_logger.warn("unable to destroy task(" + task.toWireString() + ") due to " + e.toString());
}
}
}
return new CopyCmdAnswer(details);
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final int wait = cmd.getWait();
final DataStoreTO srcStore = srcData.getDataStore();
final Connection conn = hypervisorResource.getConnection();
SR srcSr = null;
Task task = null;
try {
if (srcStore instanceof NfsTO && srcData.getObjectType() == DataObjectType.TEMPLATE) {
final NfsTO srcImageStore = (NfsTO) srcStore;
final TemplateObjectTO srcTemplate = (TemplateObjectTO) srcData;
final String storeUrl = srcImageStore.getUrl();
final URI uri = new URI(storeUrl);
String volumePath = srcData.getPath();
volumePath = StringUtils.stripEnd(volumePath, "/");
final String[] splits = volumePath.split("/");
String volumeDirectory = volumePath;
if (splits.length > 4) {
// "template/tmpl/dcid/templateId/templatename"
final int index = volumePath.lastIndexOf("/");
volumeDirectory = volumePath.substring(0, index);
}
srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), volumeDirectory);
final Set<VDI> setVdis = srcSr.getVDIs(conn);
if (setVdis.size() != 1) {
return new CopyCmdAnswer("Expected 1 VDI template but found " + setVdis.size() + " VDI template(s) on: " + uri.getHost() + ":" + uri.getPath() + "/" + volumeDirectory);
}
final VDI srcVdi = setVdis.iterator().next();
boolean managed = false;
String storageHost = null;
String managedStoragePoolName = null;
String managedStoragePoolRootVolumeName = null;
String managedStoragePoolRootVolumeSize = null;
String chapInitiatorUsername = null;
String chapInitiatorSecret = null;
final PrimaryDataStoreTO destStore = (PrimaryDataStoreTO) destData.getDataStore();
Map<String, String> details = destStore.getDetails();
if (details != null) {
managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
if (managed) {
storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
}
}
final SR destSr;
if (managed) {
details = new HashMap<>();
details.put(DiskTO.STORAGE_HOST, storageHost);
details.put(DiskTO.IQN, managedStoragePoolName);
details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);
destSr = hypervisorResource.prepareManagedSr(conn, details);
} else {
final String srName = destStore.getUuid();
final Set<SR> srs = SR.getByNameLabel(conn, srName);
if (srs.size() != 1) {
final String msg = "There are " + srs.size() + " SRs with same name: " + srName;
s_logger.warn(msg);
return new CopyCmdAnswer(msg);
} else {
destSr = srs.iterator().next();
}
}
task = srcVdi.copyAsync(conn, destSr, null, null);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
final VDI tmplVdi = Types.toVDI(task, conn);
final String uuidToReturn;
final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
if (managed) {
uuidToReturn = tmplVdi.getUuid(conn);
tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
} else {
final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<>());
uuidToReturn = snapshotVdi.getUuid(conn);
snapshotVdi.setNameLabel(conn, "Template " + srcTemplate.getName());
tmplVdi.destroy(conn);
}
destSr.scan(conn);
try {
Thread.sleep(5000);
} catch (final Exception e) {
}
final TemplateObjectTO newVol = new TemplateObjectTO();
newVol.setUuid(uuidToReturn);
newVol.setPath(uuidToReturn);
if (physicalSize != null) {
newVol.setSize(physicalSize);
}
newVol.setFormat(Storage.ImageFormat.VHD);
return new CopyCmdAnswer(newVol);
}
} catch (final Exception e) {
final String msg = "Catch Exception " + e.getClass().getName() + " for template due to " + e.toString();
s_logger.warn(msg, e);
return new CopyCmdAnswer(msg);
} finally {
if (task != null) {
try {
task.destroy(conn);
} catch (final Exception e) {
s_logger.debug("unable to destroy task (" + task.toWireString() + ") due to " + e.toString());
}
}
if (srcSr != null) {
hypervisorResource.removeSR(conn, srcSr);
}
}
return new CopyCmdAnswer("not implemented yet");
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class NfsSecondaryStorageResource method postProcessing.
protected CopyCmdAnswer postProcessing(final File destFile, final String downloadPath, final String destPath, final DataTO srcData, final DataTO destData) throws ConfigurationException {
if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
final SnapshotObjectTO snapshot = new SnapshotObjectTO();
snapshot.setPath(destPath + File.separator + destFile.getName());
final CopyCmdAnswer answer = new CopyCmdAnswer(snapshot);
return answer;
}
// do post processing to unzip the file if it is compressed
final String scriptsDir = "scripts/storage/secondary";
final String createTmpltScr = Script.findScript(scriptsDir, "createtmplt.sh");
if (createTmpltScr == null) {
throw new ConfigurationException("Unable to find createtmplt.sh");
}
s_logger.info("createtmplt.sh found in " + createTmpltScr);
final String createVolScr = Script.findScript(scriptsDir, "createvolume.sh");
if (createVolScr == null) {
throw new ConfigurationException("Unable to find createvolume.sh");
}
s_logger.info("createvolume.sh found in " + createVolScr);
final String script = srcData.getObjectType() == DataObjectType.TEMPLATE ? createTmpltScr : createVolScr;
final int installTimeoutPerGig = 180 * 60 * 1000;
long imgSizeGigs = (long) Math.ceil(destFile.length() * 1.0d / (1024 * 1024 * 1024));
// add one just in case
imgSizeGigs++;
final long timeout = imgSizeGigs * installTimeoutPerGig;
final String origPath = destFile.getAbsolutePath();
String extension = null;
if (srcData.getObjectType() == DataObjectType.TEMPLATE) {
extension = ((TemplateObjectTO) srcData).getFormat().getFileExtension();
} else if (srcData.getObjectType() == DataObjectType.VOLUME) {
extension = ((VolumeObjectTO) srcData).getFormat().getFileExtension();
}
final String templateName = UUID.randomUUID().toString();
final String templateFilename = templateName + "." + extension;
final Script scr = new Script(script, timeout, s_logger);
// not used for now
scr.add("-s", Long.toString(imgSizeGigs));
scr.add("-n", templateFilename);
scr.add("-t", downloadPath);
// this is the temporary
scr.add("-f", origPath);
// template file downloaded
final String result;
result = scr.execute();
if (result != null) {
// script execution failure
throw new CloudRuntimeException("Failed to run script " + script);
}
final String finalFileName = templateFilename;
final String finalDownloadPath = destPath + File.separator + templateFilename;
// compute the size of
final long size = _storage.getSize(downloadPath + File.separator + templateFilename);
DataTO newDestTO = null;
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
final TemplateObjectTO newTemplTO = new TemplateObjectTO();
newTemplTO.setPath(finalDownloadPath);
newTemplTO.setName(finalFileName);
newTemplTO.setSize(size);
newTemplTO.setPhysicalSize(size);
newDestTO = newTemplTO;
} else {
final VolumeObjectTO newVolTO = new VolumeObjectTO();
newVolTO.setPath(finalDownloadPath);
newVolTO.setName(finalFileName);
newVolTO.setSize(size);
newDestTO = newVolTO;
}
return new CopyCmdAnswer(newDestTO);
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method dettachIso.
@Override
public Answer dettachIso(final DettachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final TemplateObjectTO isoTo = (TemplateObjectTO) disk.getData();
final DataStoreTO store = isoTo.getDataStore();
if (!(store instanceof NfsTO)) {
return new AttachAnswer("unsupported protocol");
}
final NfsTO nfsStore = (NfsTO) store;
try {
final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
attachOrDetachIso(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTo.getPath(), false);
} catch (final LibvirtException e) {
return new Answer(cmd, false, e.toString());
} catch (final URISyntaxException e) {
return new Answer(cmd, false, e.toString());
} catch (final InternalErrorException e) {
return new Answer(cmd, false, e.toString());
}
return new Answer(cmd);
}
Aggregations