Search in sources :

Example 1 with TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO 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 TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO 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 TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO 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 TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateServiceImpl method addSystemVMTemplatesToSecondary.

@Override
public void addSystemVMTemplatesToSecondary(final DataStore store) {
    final long storeId = store.getId();
    final List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
    for (final VMTemplateVO tmplt : rtngTmplts) {
        TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId());
        if (tmpltStore == null) {
            tmpltStore = new TemplateDataStoreVO(storeId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, TemplateConstants.DEFAULT_SYSTEM_VM_TEMPLATE_PATH + tmplt.getId() + '/', tmplt.getUrl());
            tmpltStore.setSize(0L);
            // no size information for
            tmpltStore.setPhysicalSize(0);
            // pre-seeded system vm templates
            tmpltStore.setDataStoreRole(store.getRole());
            _vmTemplateStoreDao.persist(tmpltStore);
        }
    }
}
Also used : VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) Date(java.util.Date)

Example 5 with TemplateDataStoreVO

use of com.cloud.storage.datastore.db.TemplateDataStoreVO in project cosmic by MissionCriticalCloud.

the class TemplateServiceImpl method downloadBootstrapSysTemplate.

@Override
public void downloadBootstrapSysTemplate(final DataStore store) {
    final Set<VMTemplateVO> toBeDownloaded = new HashSet();
    final List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
    for (final VMTemplateVO rtngTmplt : rtngTmplts) {
        toBeDownloaded.add(rtngTmplt);
    }
    final List<HypervisorType> availHypers = _clusterDao.getAvailableHypervisorInZone(store.getScope().getScopeId());
    if (availHypers.isEmpty()) {
        /*
             * This is for cloudzone, local secondary storage resource started
             * before cluster created
             */
        availHypers.add(HypervisorType.KVM);
    }
    // bug 9809: resume ISO
    availHypers.add(HypervisorType.None);
    for (final VMTemplateVO template : toBeDownloaded) {
        if (availHypers.contains(template.getHypervisorType())) {
            // only download sys template applicable for current hypervisor
            final TemplateDataStoreVO tmpltHost = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
            if (tmpltHost == null || tmpltHost.getState() != ObjectInDataStoreStateMachine.State.Ready) {
                final TemplateInfo tmplt = _templateFactory.getTemplate(template.getId(), DataStoreRole.Image);
                createTemplateAsync(tmplt, store, null);
            }
        }
    }
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) HashSet(java.util.HashSet)

Aggregations

TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)39 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)17 VMTemplateVO (com.cloud.storage.VMTemplateVO)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)11 Date (java.util.Date)9 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)7 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)6 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)6 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)5 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)5 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 URISyntaxException (java.net.URISyntaxException)5 ExecutionException (java.util.concurrent.ExecutionException)5 ConfigurationException (javax.naming.ConfigurationException)5 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)4 CreateCmdResult (com.cloud.engine.subsystem.api.storage.CreateCmdResult)3