use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.
the class VolumeApiServiceImpl method uploadVolume.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPLOAD, eventDescription = "uploading volume for post upload", async = true)
public GetUploadParamsResponse uploadVolume(final GetUploadParamsForVolumeCmd cmd) throws ResourceAllocationException, MalformedURLException {
final Account caller = CallContext.current().getCallingAccount();
final long ownerId = cmd.getEntityOwnerId();
final Account owner = _entityMgr.findById(Account.class, ownerId);
final Long zoneId = cmd.getZoneId();
final String volumeName = cmd.getName();
final String format = cmd.getFormat();
final Long diskOfferingId = cmd.getDiskOfferingId();
final String imageStoreUuid = cmd.getImageStoreUuid();
final DataStore store = _tmpltMgr.getImageStore(imageStoreUuid, zoneId);
validateVolume(caller, ownerId, zoneId, volumeName, null, format, diskOfferingId);
return Transaction.execute(new TransactionCallbackWithException<GetUploadParamsResponse, MalformedURLException>() {
@Override
public GetUploadParamsResponse doInTransaction(final TransactionStatus status) throws MalformedURLException {
final VolumeVO volume = persistVolume(owner, zoneId, volumeName, null, cmd.getFormat(), diskOfferingId, Volume.State.NotUploaded);
final VolumeInfo vol = volFactory.getVolume(volume.getId());
final RegisterVolumePayload payload = new RegisterVolumePayload(null, cmd.getChecksum(), cmd.getFormat());
vol.addPayload(payload);
final Pair<EndPoint, DataObject> pair = volService.registerVolumeForPostUpload(vol, store);
final EndPoint ep = pair.first();
final DataObject dataObject = pair.second();
final GetUploadParamsResponse response = new GetUploadParamsResponse();
final String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
final String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, ep.getPublicAddr(), vol.getUuid());
response.setPostURL(new URL(url));
// set the post url, this is used in the monitoring thread to determine the SSVM
final VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(vol.getId());
assert volumeStore != null : "sincle volume is registered, volumestore cannot be null at this stage";
volumeStore.setExtractUrl(url);
_volumeStoreDao.persist(volumeStore);
response.setId(UUID.fromString(vol.getUuid()));
final int timeout = ImageStoreUploadMonitorImpl.getUploadOperationTimeout();
final DateTime currentDateTime = new DateTime(DateTimeZone.UTC);
final String expires = currentDateTime.plusMinutes(timeout).toString();
response.setTimeout(expires);
final String key = _configDao.getValue(Config.SSVMPSK.key());
/*
* encoded metadata using the post upload config key
*/
final TemplateOrVolumePostUploadCommand command = new TemplateOrVolumePostUploadCommand(vol.getId(), vol.getUuid(), volumeStore.getInstallPath(), cmd.getChecksum(), vol.getType().toString(), vol.getName(), vol.getFormat().toString(), dataObject.getDataStore().getUri(), dataObject.getDataStore().getRole().toString());
command.setLocalPath(volumeStore.getLocalDownloadPath());
// using the existing max upload size configuration
command.setMaxUploadSize(_configDao.getValue(Config.MaxUploadVolumeSize.key()));
command.setDefaultMaxAccountSecondaryStorage(_configDao.getValue(Config.DefaultMaxAccountSecondaryStorage.key()));
command.setAccountId(vol.getAccountId());
final Gson gson = new GsonBuilder().create();
final String metadata = EncryptionUtil.encodeData(gson.toJson(command), key);
response.setMetadata(metadata);
/*
* signature calculated on the url, expiry, metadata.
*/
response.setSignature(EncryptionUtil.generateSignature(metadata + url + expires, key));
return response;
}
});
}
use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.
the class HypervisorTemplateAdapterTest method testEmitDeleteEventUuid.
@Test
public void testEmitDeleteEventUuid() throws InterruptedException, ExecutionException {
// All the mocks required for this test to work.
final ImageStoreEntity store = mock(ImageStoreEntity.class);
when(store.getId()).thenReturn(1l);
when(store.getDataCenterId()).thenReturn(1l);
when(store.getName()).thenReturn("Test Store");
final TemplateDataStoreVO dataStoreVO = mock(TemplateDataStoreVO.class);
when(dataStoreVO.getDownloadState()).thenReturn(Status.DOWNLOADED);
final TemplateInfo info = mock(TemplateInfo.class);
when(info.getDataStore()).thenReturn(store);
final VMTemplateVO template = mock(VMTemplateVO.class);
when(template.getId()).thenReturn(1l);
when(template.getName()).thenReturn("Test Template");
when(template.getFormat()).thenReturn(ImageFormat.QCOW2);
when(template.getAccountId()).thenReturn(1l);
// TODO possibly return this from method for comparison, if things work how i want
when(template.getUuid()).thenReturn("Test UUID");
final TemplateProfile profile = mock(TemplateProfile.class);
when(profile.getTemplate()).thenReturn(template);
when(profile.getZoneId()).thenReturn(1l);
final TemplateApiResult result = mock(TemplateApiResult.class);
when(result.isSuccess()).thenReturn(true);
when(result.isFailed()).thenReturn(false);
final AsyncCallFuture<TemplateApiResult> future = mock(AsyncCallFuture.class);
when(future.get()).thenReturn(result);
final AccountVO acct = mock(AccountVO.class);
when(acct.getId()).thenReturn(1l);
when(acct.getDomainId()).thenReturn(1l);
when(_templateMgr.getImageStoreByTemplate(anyLong(), anyLong())).thenReturn(Collections.singletonList((DataStore) store));
when(_templateStoreDao.listByTemplateStore(anyLong(), anyLong())).thenReturn(Collections.singletonList(dataStoreVO));
when(_dataFactory.getTemplate(anyLong(), any(DataStore.class))).thenReturn(info);
when(_dataFactory.listTemplateOnCache(anyLong())).thenReturn(Collections.singletonList(info));
when(_templateService.deleteTemplateAsync(any(TemplateInfo.class))).thenReturn(future);
when(_accountDao.findById(anyLong())).thenReturn(acct);
when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(acct);
_adapter.delete(profile);
Assert.assertNotNull(events);
Assert.assertEquals(0, events.size());
}
use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.
the class VolumeOrchestrator method createVolumeFromSnapshot.
@DB
@Override
public VolumeInfo createVolumeFromSnapshot(final Volume volume, final Snapshot snapshot, final UserVm vm) throws StorageUnavailableException {
final Account account = _entityMgr.findById(Account.class, volume.getAccountId());
final HashSet<StoragePool> poolsToAvoid = new HashSet<>();
StoragePool pool = null;
final Set<Long> podsToAvoid = new HashSet<>();
Pair<Pod, Long> pod = null;
final DiskOffering diskOffering = _entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());
final DataCenter dc = _entityMgr.findById(DataCenter.class, volume.getDataCenterId());
final DiskProfile dskCh = new DiskProfile(volume, diskOffering, snapshot.getHypervisorType());
String msg = "There are no available storage pools to store the volume in";
if (vm != null) {
final Pod podofVM = _entityMgr.findById(Pod.class, vm.getPodIdToDeployIn());
if (podofVM != null) {
pod = new Pair<>(podofVM, podofVM.getId());
}
}
if (vm != null && pod != null) {
// if VM is running use the hostId to find the clusterID. If it is stopped, refer the cluster where the ROOT volume of the VM exists.
Long hostId = null;
Long clusterId = null;
if (vm.getState() == State.Running) {
hostId = vm.getHostId();
if (hostId != null) {
final Host vmHost = _entityMgr.findById(Host.class, hostId);
clusterId = vmHost.getClusterId();
}
} else {
final List<VolumeVO> rootVolumesOfVm = _volsDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT);
if (rootVolumesOfVm.size() != 1) {
throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state. Please contact Cloud Support.");
} else {
final VolumeVO rootVolumeOfVm = rootVolumesOfVm.get(0);
final StoragePoolVO rootDiskPool = _storagePoolDao.findById(rootVolumeOfVm.getPoolId());
clusterId = (rootDiskPool == null ? null : rootDiskPool.getClusterId());
}
}
// Determine what storage pool to store the volume in
while ((pool = findStoragePool(dskCh, dc, pod.first(), clusterId, hostId, vm, poolsToAvoid)) != null) {
break;
}
if (pool == null) {
// pool could not be found in the VM's pod/cluster.
if (s_logger.isDebugEnabled()) {
s_logger.debug("Could not find any storage pool to create Volume in the pod/cluster of the provided VM " + vm.getUuid());
}
final StringBuilder addDetails = new StringBuilder(msg);
addDetails.append(", Could not find any storage pool to create Volume in the pod/cluster of the VM ");
addDetails.append(vm.getUuid());
msg = addDetails.toString();
}
} else {
// Determine what pod to store the volume in
while ((pod = findPod(null, null, dc, account.getId(), podsToAvoid)) != null) {
podsToAvoid.add(pod.first().getId());
// Determine what storage pool to store the volume in
while ((pool = findStoragePool(dskCh, dc, pod.first(), null, null, null, poolsToAvoid)) != null) {
break;
}
if (pool != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found a suitable pool for create volume: " + pool.getId());
}
break;
}
}
}
if (pool == null) {
s_logger.info(msg);
throw new StorageUnavailableException(msg, -1);
}
final VolumeInfo vol = volFactory.getVolume(volume.getId());
final DataStore store = dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
final DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
SnapshotInfo snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
if (snapInfo == null && dataStoreRole == DataStoreRole.Image) {
// snapshot is not backed up to secondary, let's do that now.
snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Primary);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId());
}
// We need to copy the snapshot onto secondary.
final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
snapshotStrategy.backupSnapshot(snapInfo);
// Attempt to grab it again.
snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId() + " on secondary and could not create backup");
}
}
// don't try to perform a sync if the DataStoreRole of the snapshot is equal to DataStoreRole.Primary
if (!DataStoreRole.Primary.equals(dataStoreRole)) {
try {
// sync snapshot to region store if necessary
final DataStore snapStore = snapInfo.getDataStore();
final long snapVolId = snapInfo.getVolumeId();
_snapshotSrv.syncVolumeSnapshotsToRegionStore(snapVolId, snapStore);
} catch (final Exception ex) {
// log but ignore the sync error to avoid any potential S3 down issue, it should be sync next time
s_logger.warn(ex.getMessage(), ex);
}
}
// create volume on primary from snapshot
final AsyncCallFuture<VolumeService.VolumeApiResult> future = volService.createVolumeFromSnapshot(vol, store, snapInfo);
try {
final VolumeService.VolumeApiResult result = future.get();
if (result.isFailed()) {
s_logger.debug("Failed to create volume from snapshot:" + result.getResult());
throw new CloudRuntimeException("Failed to create volume from snapshot:" + result.getResult());
}
return result.getVolume();
} catch (final InterruptedException e) {
s_logger.debug("Failed to create volume from snapshot", e);
throw new CloudRuntimeException("Failed to create volume from snapshot", e);
} catch (final ExecutionException e) {
s_logger.debug("Failed to create volume from snapshot", e);
throw new CloudRuntimeException("Failed to create volume from snapshot", e);
}
}
use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.
the class TemplateServiceImpl method generateCopyUrl.
private String generateCopyUrl(final TemplateInfo srcTemplate) {
final DataStore srcStore = srcTemplate.getDataStore();
final EndPoint ep = _epSelector.select(srcTemplate);
if (ep != null) {
if (ep.getPublicAddr() == null) {
s_logger.warn("A running secondary storage vm has a null public ip?");
return null;
}
return generateCopyUrl(ep.getPublicAddr(), ((ImageStoreEntity) srcStore).getMountPoint(), srcTemplate.getInstallPath());
}
final VMTemplateVO tmplt = _templateDao.findById(srcTemplate.getId());
final HypervisorType hyperType = tmplt.getHypervisorType();
/*No secondary storage vm yet*/
if (hyperType != null && hyperType == HypervisorType.KVM) {
return "file://" + ((ImageStoreEntity) srcStore).getMountPoint() + "/" + srcTemplate.getInstallPath();
}
return null;
}
use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.
the class ImageStoreProviderManagerImpl method listImageCacheStores.
@Override
public List<DataStore> listImageCacheStores() {
final List<ImageStoreVO> stores = dataStoreDao.listImageCacheStores();
final List<DataStore> imageStores = new ArrayList<>();
for (final ImageStoreVO store : stores) {
imageStores.add(getImageStore(store.getId()));
}
return imageStores;
}
Aggregations