Search in sources :

Example 1 with ImageStoreEntity

use of com.cloud.storage.image.datastore.ImageStoreEntity in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method extract.

private String extract(final Account caller, final Long templateId, final String url, final Long zoneId, final String mode, final Long eventId, final boolean isISO) {
    String desc = Upload.Type.TEMPLATE.toString();
    if (isISO) {
        desc = Upload.Type.ISO.toString();
    }
    if (!_accountMgr.isRootAdmin(caller.getId()) && _disableExtraction) {
        throw new PermissionDeniedException("Extraction has been disabled by admin");
    }
    final VMTemplateVO template = _tmpltDao.findById(templateId);
    if (template == null || template.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find " + desc + " with id " + templateId);
    }
    if (template.getTemplateType() == Storage.TemplateType.SYSTEM) {
        throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it is a default System template");
    } else if (template.getTemplateType() == Storage.TemplateType.PERHOST) {
        throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it resides on host and not on SSVM");
    }
    if (isISO) {
        if (template.getFormat() != ImageFormat.ISO) {
            throw new InvalidParameterValueException("Unsupported format, could not extract the ISO");
        }
    } else {
        if (template.getFormat() == ImageFormat.ISO) {
            throw new InvalidParameterValueException("Unsupported format, could not extract the template");
        }
    }
    if (zoneId != null && _dcDao.findById(zoneId) == null) {
        throw new IllegalArgumentException("Please specify a valid zone.");
    }
    if (!_accountMgr.isRootAdmin(caller.getId()) && !template.isExtractable()) {
        throw new InvalidParameterValueException("Unable to extract template id=" + templateId + " as it's not extractable");
    }
    _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template);
    final List<DataStore> ssStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(null));
    TemplateDataStoreVO tmpltStoreRef = null;
    ImageStoreEntity tmpltStore = null;
    if (ssStores != null) {
        for (final DataStore store : ssStores) {
            tmpltStoreRef = _tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
            if (tmpltStoreRef != null) {
                if (tmpltStoreRef.getDownloadState() == com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
                    tmpltStore = (ImageStoreEntity) store;
                    break;
                }
            }
        }
    }
    if (tmpltStore == null) {
        throw new InvalidParameterValueException("The " + desc + " has not been downloaded ");
    }
    // Check if the url already exists
    if (tmpltStoreRef.getExtractUrl() != null) {
        return tmpltStoreRef.getExtractUrl();
    }
    // Handle NFS to S3 object store migration case, we trigger template sync from NFS to S3 during extract template or copy template
    _tmpltSvr.syncTemplateToRegionStore(templateId, tmpltStore);
    final TemplateInfo templateObject = _tmplFactory.getTemplate(templateId, tmpltStore);
    final String extractUrl = tmpltStore.createEntityExtractUrl(templateObject.getInstallPath(), template.getFormat(), templateObject);
    tmpltStoreRef.setExtractUrl(extractUrl);
    tmpltStoreRef.setExtractUrlCreated(DateUtil.now());
    _tmplStoreDao.update(tmpltStoreRef.getId(), tmpltStoreRef);
    return extractUrl;
}
Also used : ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO)

Example 2 with ImageStoreEntity

use of com.cloud.storage.image.datastore.ImageStoreEntity in project cosmic by MissionCriticalCloud.

the class HypervisorTemplateAdapter method delete.

@Override
@DB
public boolean delete(final TemplateProfile profile) {
    boolean success = true;
    final VMTemplateVO template = profile.getTemplate();
    // find all eligible image stores for this template
    final List<DataStore> imageStores = templateMgr.getImageStoreByTemplate(template.getId(), profile.getZoneId());
    if (imageStores == null || imageStores.size() == 0) {
        // already destroyed on image stores
        s_logger.info("Unable to find image store still having template: " + template.getName() + ", so just mark the template removed");
    } else {
        // Make sure the template is downloaded to all found image stores
        for (final DataStore store : imageStores) {
            final long storeId = store.getId();
            final List<TemplateDataStoreVO> templateStores = _tmpltStoreDao.listByTemplateStore(template.getId(), storeId);
            for (final TemplateDataStoreVO templateStore : templateStores) {
                if (templateStore.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
                    final String errorMsg = "Please specify a template that is not currently being downloaded.";
                    s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + store.getName() + "; cant' delete it.");
                    throw new CloudRuntimeException(errorMsg);
                }
            }
        }
        for (final DataStore imageStore : imageStores) {
            // publish zone-wide usage event
            final Long sZoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
            s_logger.info("Delete template from image store: " + imageStore.getName());
            final AsyncCallFuture<TemplateApiResult> future = imageService.deleteTemplateAsync(imageFactory.getTemplate(template.getId(), imageStore));
            try {
                final TemplateApiResult result = future.get();
                success = result.isSuccess();
                if (!success) {
                    s_logger.warn("Failed to delete the template " + template + " from the image store: " + imageStore.getName() + " due to: " + result.getResult());
                    break;
                }
                // remove from template_zone_ref
                final List<VMTemplateZoneVO> templateZones = templateZoneDao.listByZoneTemplate(sZoneId, template.getId());
                if (templateZones != null) {
                    for (final VMTemplateZoneVO templateZone : templateZones) {
                        templateZoneDao.remove(templateZone.getId());
                    }
                }
                // mark all the occurrences of this template in the given store as destroyed.
                templateDataStoreDao.removeByTemplateStore(template.getId(), imageStore.getId());
            } catch (final InterruptedException | ExecutionException e) {
                s_logger.debug("delete template Failed", e);
                throw new CloudRuntimeException("delete template Failed", e);
            }
        }
    }
    if (success) {
        if ((imageStores.size() > 1) && (profile.getZoneId() != null)) {
            // if template is stored in more than one image stores, and the zone id is not null, then don't delete other templates.
            return success;
        }
        // delete all cache entries for this template
        final List<TemplateInfo> cacheTmpls = imageFactory.listTemplateOnCache(template.getId());
        for (final TemplateInfo tmplOnCache : cacheTmpls) {
            s_logger.info("Delete template from image cache store: " + tmplOnCache.getDataStore().getName());
            tmplOnCache.delete();
        }
        // find all eligible image stores for this template
        final List<DataStore> iStores = templateMgr.getImageStoreByTemplate(template.getId(), null);
        if (iStores == null || iStores.size() == 0) {
            // Mark template as Inactive.
            template.setState(VirtualMachineTemplate.State.Inactive);
            _tmpltDao.update(template.getId(), template);
            // Decrement the number of templates and total secondary storage
            // space used by the account
            final Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
            _resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
            _resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
        }
        // remove its related ACL permission
        final Pair<Class<?>, Long> tmplt = new Pair<>(VirtualMachineTemplate.class, template.getId());
        _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
    }
    return success;
}
Also used : Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) ExecutionException(java.util.concurrent.ExecutionException) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 3 with ImageStoreEntity

use of com.cloud.storage.image.datastore.ImageStoreEntity 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());
}
Also used : TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) AccountVO(com.cloud.user.AccountVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateProfile(com.cloud.storage.TemplateProfile) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ImageStoreEntity

use of com.cloud.storage.image.datastore.ImageStoreEntity in project cosmic by MissionCriticalCloud.

the class ImageStoreProviderManagerImpl method getImageStore.

@Override
public ImageStoreEntity getImageStore(final long dataStoreId) {
    final ImageStoreVO dataStore = dataStoreDao.findById(dataStoreId);
    final String providerName = dataStore.getProviderName();
    final ImageStoreProvider provider = (ImageStoreProvider) providerManager.getDataStoreProvider(providerName);
    final ImageStoreEntity imgStore = ImageStoreImpl.getDataStore(dataStore, driverMaps.get(provider.getName()), provider);
    return imgStore;
}
Also used : ImageStoreProvider(com.cloud.engine.subsystem.api.storage.ImageStoreProvider) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO)

Example 5 with ImageStoreEntity

use of com.cloud.storage.image.datastore.ImageStoreEntity in project cosmic by MissionCriticalCloud.

the class AncientDataMotionStrategy method copyVolumeBetweenPools.

protected Answer copyVolumeBetweenPools(final DataObject srcData, final DataObject destData) {
    final String value = configDao.getValue(Config.CopyVolumeWait.key());
    final int _copyvolumewait = NumbersUtil.parseInt(value, Integer.parseInt(Config.CopyVolumeWait.getDefaultValue()));
    final Scope destScope = getZoneScope(destData.getDataStore().getScope());
    final DataStore cacheStore = cacheMgr.getCacheStorage(destScope);
    if (cacheStore == null) {
        // need to find a nfs or cifs image store, assuming that can't copy volume
        final ImageStoreEntity imageStore = (ImageStoreEntity) dataStoreMgr.getImageStore(destScope.getScopeId());
        if (!imageStore.getProtocol().equalsIgnoreCase("nfs") && !imageStore.getProtocol().equalsIgnoreCase("cifs")) {
            s_logger.debug("can't find a nfs (or cifs) image store to satisfy the need for a staging store");
            return null;
        }
        final DataObject objOnImageStore = imageStore.create(srcData);
        objOnImageStore.processEvent(Event.CreateOnlyRequested);
        Answer answer = copyObject(srcData, objOnImageStore);
        if (answer == null || !answer.getResult()) {
            if (answer != null) {
                s_logger.debug("copy to image store failed: " + answer.getDetails());
            }
            objOnImageStore.processEvent(Event.OperationFailed);
            imageStore.delete(objOnImageStore);
            return answer;
        }
        objOnImageStore.processEvent(Event.OperationSuccessed, answer);
        objOnImageStore.processEvent(Event.CopyingRequested);
        final CopyCommand cmd = new CopyCommand(objOnImageStore.getTO(), destData.getTO(), _copyvolumewait, VirtualMachineManager.ExecuteInSequence.value());
        final EndPoint ep = selector.select(objOnImageStore, destData);
        if (ep == null) {
            final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
            s_logger.error(errMsg);
            answer = new Answer(cmd, false, errMsg);
        } else {
            answer = ep.sendMessage(cmd);
        }
        if (answer == null || !answer.getResult()) {
            if (answer != null) {
                s_logger.debug("copy to primary store failed: " + answer.getDetails());
            }
            objOnImageStore.processEvent(Event.OperationFailed);
            imageStore.delete(objOnImageStore);
            return answer;
        }
        objOnImageStore.processEvent(Event.OperationSuccessed);
        imageStore.delete(objOnImageStore);
        return answer;
    } else {
        final DataObject cacheData = cacheMgr.createCacheObject(srcData, destScope);
        final CopyCommand cmd = new CopyCommand(cacheData.getTO(), destData.getTO(), _copyvolumewait, VirtualMachineManager.ExecuteInSequence.value());
        final EndPoint ep = selector.select(cacheData, destData);
        Answer answer = null;
        if (ep == null) {
            final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
            s_logger.error(errMsg);
            answer = new Answer(cmd, false, errMsg);
        } else {
            answer = ep.sendMessage(cmd);
        }
        // delete volume on cache store
        if (cacheData != null) {
            cacheMgr.deleteCacheObject(cacheData);
        }
        return answer;
    }
}
Also used : MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) Answer(com.cloud.agent.api.Answer) DataObject(com.cloud.engine.subsystem.api.storage.DataObject) ClusterScope(com.cloud.engine.subsystem.api.storage.ClusterScope) Scope(com.cloud.engine.subsystem.api.storage.Scope) ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) HostScope(com.cloud.engine.subsystem.api.storage.HostScope) CopyCommand(com.cloud.storage.command.CopyCommand) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) RemoteHostEndPoint(com.cloud.storage.RemoteHostEndPoint) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(com.cloud.storage.RemoteHostEndPoint) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint)

Aggregations

ImageStoreEntity (com.cloud.storage.image.datastore.ImageStoreEntity)7 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)4 TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)4 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)3 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)2 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)2 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Answer (com.cloud.agent.api.Answer)1 MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)1 ClusterScope (com.cloud.engine.subsystem.api.storage.ClusterScope)1 DataObject (com.cloud.engine.subsystem.api.storage.DataObject)1 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)1 HostScope (com.cloud.engine.subsystem.api.storage.HostScope)1 ImageStoreProvider (com.cloud.engine.subsystem.api.storage.ImageStoreProvider)1 Scope (com.cloud.engine.subsystem.api.storage.Scope)1 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)1