use of com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected PrimaryStorageDownloadAnswer execute(final PrimaryStorageDownloadCommand cmd) {
String tmplturl = cmd.getUrl();
int index = tmplturl.lastIndexOf("/");
String mountpoint = tmplturl.substring(0, index);
String tmpltname = null;
if (index < tmplturl.length() - 1) {
tmpltname = tmplturl.substring(index + 1);
}
KVMPhysicalDisk tmplVol = null;
KVMStoragePool secondaryPool = null;
try {
secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
/* Get template vol */
if (tmpltname == null) {
secondaryPool.refresh();
List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
}
for (KVMPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
tmplVol = disk;
break;
}
}
if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
}
} else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
}
/* Copy volume to primary storage */
KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPoolUuid());
KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool);
return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
} catch (CloudRuntimeException e) {
return new PrimaryStorageDownloadAnswer(e.toString());
} finally {
if (secondaryPool != null) {
secondaryPool.delete();
}
}
}
use of com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer in project cloudstack by apache.
the class KVMStorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final TemplateObjectTO template = (TemplateObjectTO) srcData;
final DataStoreTO imageStore = template.getDataStore();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
final int index = tmplturl.lastIndexOf("/");
final String mountpoint = tmplturl.substring(0, index);
String tmpltname = null;
if (index < tmplturl.length() - 1) {
tmpltname = tmplturl.substring(index + 1);
}
KVMPhysicalDisk tmplVol = null;
KVMStoragePool secondaryPool = null;
try {
secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint);
/* Get template vol */
if (tmpltname == null) {
secondaryPool.refresh();
final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
}
for (final KVMPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
tmplVol = disk;
break;
}
}
} else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
}
if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
}
/* Copy volume to primary storage */
s_logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
KVMPhysicalDisk primaryVol = null;
if (destData instanceof VolumeObjectTO) {
final VolumeObjectTO volume = (VolumeObjectTO) destData;
// pass along volume's target size if it's bigger than template's size, for storage types that copy template rather than cloning on deploy
if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
s_logger.debug("Using configured size of " + volume.getSize());
tmplVol.setSize(volume.getSize());
tmplVol.setVirtualSize(volume.getSize());
} else {
s_logger.debug("Using template's size of " + tmplVol.getVirtualSize());
}
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else if (destData instanceof TemplateObjectTO) {
final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else {
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
}
DataTO data = null;
/**
* Force the ImageFormat for RBD templates to RAW
*
*/
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(primaryVol.getName());
newTemplate.setSize(primaryVol.getSize());
if (primaryPool.getType() == StoragePoolType.RBD) {
newTemplate.setFormat(ImageFormat.RAW);
} else {
newTemplate.setFormat(ImageFormat.QCOW2);
}
data = newTemplate;
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
final VolumeObjectTO volumeObjectTO = new VolumeObjectTO();
volumeObjectTO.setPath(primaryVol.getName());
volumeObjectTO.setSize(primaryVol.getSize());
if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
volumeObjectTO.setFormat(ImageFormat.RAW);
} else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
volumeObjectTO.setFormat(ImageFormat.QCOW2);
}
data = volumeObjectTO;
}
return new CopyCmdAnswer(data);
} catch (final CloudRuntimeException e) {
return new CopyCmdAnswer(e.toString());
} finally {
try {
if (secondaryPool != null) {
secondaryPool.delete();
}
} catch (final Exception e) {
s_logger.debug("Failed to clean up secondary storage", e);
}
}
}
use of com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer in project cloudstack by apache.
the class HypervDirectConnectResourceTest method corePrimaryStorageDownloadCommandTestCycle.
private void corePrimaryStorageDownloadCommandTestCycle(final PrimaryStorageDownloadCommand cmd) {
PrimaryStorageDownloadAnswer ans = (PrimaryStorageDownloadAnswer) s_hypervresource.executeRequest(cmd);
if (!ans.getResult()) {
s_logger.error(ans.getDetails());
} else {
s_logger.debug(ans.getDetails());
}
Assert.assertTrue(ans.getDetails(), ans.getResult());
// Test that returned URL works.
// CreateCommand createCmd = CreateCommandSample();
// CreateCommand testCreateCmd = new
// CreateCommand(createCmd.getDiskCharacteristics(),
// ans.getInstallPath(), createCmd.getPool());
// CreateAnswer ans2 =
// (CreateAnswer)s_hypervresource.executeRequest(testCreateCmd);
// Assert.assertTrue(ans2.getDetails(), ans2.getResult());
}
use of com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer in project cloudstack by apache.
the class LibvirtPrimaryStorageDownloadCommandWrapper method execute.
@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String tmplturl = command.getUrl();
final int index = tmplturl.lastIndexOf("/");
final String mountpoint = tmplturl.substring(0, index);
String tmpltname = null;
if (index < tmplturl.length() - 1) {
tmpltname = tmplturl.substring(index + 1);
}
KVMPhysicalDisk tmplVol = null;
KVMStoragePool secondaryPool = null;
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
try {
secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint);
/* Get template vol */
if (tmpltname == null) {
secondaryPool.refresh();
final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
}
for (final KVMPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
tmplVol = disk;
break;
}
}
if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
}
} else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
}
/* Copy volume to primary storage */
final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid());
final KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0);
return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
} catch (final CloudRuntimeException e) {
return new PrimaryStorageDownloadAnswer(e.toString());
} finally {
if (secondaryPool != null) {
storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
}
}
}
use of com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer in project cloudstack by apache.
the class MockStorageManagerImpl method primaryStorageDownload.
@Override
public PrimaryStorageDownloadAnswer primaryStorageDownload(PrimaryStorageDownloadCommand cmd) {
MockVolumeVO template = findVolumeFromSecondary(cmd.getUrl(), cmd.getSecondaryStorageUrl(), MockVolumeType.TEMPLATE);
if (template == null) {
return new PrimaryStorageDownloadAnswer("Can't find primary storage");
}
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
MockStoragePoolVO primaryStorage = null;
try {
txn.start();
primaryStorage = _mockStoragePoolDao.findByUuid(cmd.getPoolUuid());
txn.commit();
if (primaryStorage == null) {
return new PrimaryStorageDownloadAnswer("Can't find primary storage");
}
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when finding primary storagee " + cmd.getPoolUuid(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
String volumeName = UUID.randomUUID().toString();
MockVolumeVO newVolume = new MockVolumeVO();
newVolume.setName(volumeName);
newVolume.setPath(primaryStorage.getMountPoint() + volumeName);
newVolume.setPoolId(primaryStorage.getId());
newVolume.setSize(template.getSize());
newVolume.setType(MockVolumeType.VOLUME);
txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
_mockVolumeDao.persist(newVolume);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when saving volume " + newVolume, ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
return new PrimaryStorageDownloadAnswer(newVolume.getPath(), newVolume.getSize());
}
Aggregations