Search in sources :

Example 21 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class HypervisorTemplateAdapter method delete.

@Override
@DB
public boolean delete(TemplateProfile profile) {
    boolean success = true;
    VMTemplateVO template = profile.getTemplate();
    // find all eligible image stores for this template
    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 (DataStore store : imageStores) {
            long storeId = store.getId();
            List<TemplateDataStoreVO> templateStores = _tmpltStoreDao.listByTemplateStore(template.getId(), storeId);
            for (TemplateDataStoreVO templateStore : templateStores) {
                if (templateStore.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
                    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);
                }
            }
        }
        String eventType = "";
        if (template.getFormat().equals(ImageFormat.ISO)) {
            eventType = EventTypes.EVENT_ISO_DELETE;
        } else {
            eventType = EventTypes.EVENT_TEMPLATE_DELETE;
        }
        for (DataStore imageStore : imageStores) {
            // publish zone-wide usage event
            Long sZoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
            if (sZoneId != null) {
                UsageEventUtils.publishUsageEvent(eventType, template.getAccountId(), sZoneId, template.getId(), null, VirtualMachineTemplate.class.getName(), template.getUuid());
            }
            s_logger.info("Delete template from image store: " + imageStore.getName());
            AsyncCallFuture<TemplateApiResult> future = imageService.deleteTemplateAsync(imageFactory.getTemplate(template.getId(), imageStore));
            try {
                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
                List<VMTemplateZoneVO> templateZones = templateZoneDao.listByZoneTemplate(sZoneId, template.getId());
                if (templateZones != null) {
                    for (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 (InterruptedException e) {
                s_logger.debug("delete template Failed", e);
                throw new CloudRuntimeException("delete template Failed", e);
            } catch (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
        List<TemplateInfo> cacheTmpls = imageFactory.listTemplateOnCache(template.getId());
        for (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
        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
            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
        Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(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(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) ExecutionException(java.util.concurrent.ExecutionException) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 22 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class StorageCacheReplacementAlgorithmLRUTest method testSelectObject.

@Test
public void testSelectObject() {
    cacheReplacementAlgorithm.setUnusedTimeInterval(1);
    try {
        VMTemplateVO template = new VMTemplateVO();
        template.setTemplateType(Storage.TemplateType.USER);
        template.setUrl(UUID.randomUUID().toString());
        template.setUniqueName(UUID.randomUUID().toString());
        template.setName(UUID.randomUUID().toString());
        template.setPublicTemplate(true);
        template.setFeatured(true);
        template.setRequiresHvm(true);
        template.setBits(64);
        template.setFormat(Storage.ImageFormat.VHD);
        template.setEnablePassword(true);
        template.setEnableSshKey(true);
        template.setGuestOSId(1);
        template.setBootable(true);
        template.setPrepopulate(true);
        template.setCrossZones(true);
        template.setExtractable(true);
        template = templateDao.persist(template);
        VMTemplateVO template2 = new VMTemplateVO();
        template2.setTemplateType(Storage.TemplateType.USER);
        template2.setUrl(UUID.randomUUID().toString());
        template2.setUniqueName(UUID.randomUUID().toString());
        template2.setName(UUID.randomUUID().toString());
        template2.setPublicTemplate(true);
        template2.setFeatured(true);
        template2.setRequiresHvm(true);
        template2.setBits(64);
        template2.setFormat(Storage.ImageFormat.VHD);
        template2.setEnablePassword(true);
        template2.setEnableSshKey(true);
        template2.setGuestOSId(1);
        template2.setBootable(true);
        template2.setPrepopulate(true);
        template2.setCrossZones(true);
        template2.setExtractable(true);
        template2 = templateDao.persist(template2);
        ImageStoreVO imageStoreVO = new ImageStoreVO();
        imageStoreVO.setRole(DataStoreRole.ImageCache);
        imageStoreVO.setName(UUID.randomUUID().toString());
        imageStoreVO.setProviderName(DataStoreProvider.NFS_IMAGE);
        imageStoreVO.setProtocol("nfs");
        imageStoreVO.setUrl(UUID.randomUUID().toString());
        imageStoreVO = imageStoreDao.persist(imageStoreVO);
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtil.now());
        cal.add(Calendar.DAY_OF_MONTH, -2);
        Date date = cal.getTime();
        TemplateDataStoreVO templateStoreVO1 = new TemplateDataStoreVO();
        templateStoreVO1.setLastUpdated(date);
        templateStoreVO1.setDataStoreRole(DataStoreRole.ImageCache);
        templateStoreVO1.setDataStoreId(imageStoreVO.getId());
        templateStoreVO1.setState(ObjectInDataStoreStateMachine.State.Ready);
        templateStoreVO1.setCopy(true);
        templateStoreVO1.setTemplateId(template.getId());
        templateDataStoreDao.persist(templateStoreVO1);
        TemplateDataStoreVO templateStoreVO2 = new TemplateDataStoreVO();
        templateStoreVO2.setLastUpdated(date);
        templateStoreVO2.setDataStoreRole(DataStoreRole.ImageCache);
        templateStoreVO2.setDataStoreId(imageStoreVO.getId());
        templateStoreVO2.setState(ObjectInDataStoreStateMachine.State.Ready);
        templateStoreVO2.setCopy(true);
        templateStoreVO2.setTemplateId(template2.getId());
        templateDataStoreDao.persist(templateStoreVO2);
        DataStore store = dataStoreManager.getDataStore(imageStoreVO.getId(), DataStoreRole.ImageCache);
        Assert.assertNotNull(cacheReplacementAlgorithm.chooseOneToBeReplaced(store));
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : Calendar(java.util.Calendar) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) Date(java.util.Date) Test(org.junit.Test)

Example 23 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class TemplateObject method processEvent.

@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
    try {
        if (getDataStore().getRole() == DataStoreRole.Primary) {
            if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
                VMTemplateStoragePoolVO templatePoolRef = templatePoolDao.findByPoolTemplate(getDataStore().getId(), getId());
                templatePoolRef.setDownloadPercent(100);
                setTemplateSizeIfNeeded(newTemplate, templatePoolRef);
                templatePoolRef.setDownloadState(Status.DOWNLOADED);
                setDownloadPathIfNeeded(newTemplate, templatePoolRef);
                setInstallPathIfNeeded(newTemplate, templatePoolRef);
                templatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
            }
        } else if (getDataStore().getRole() == DataStoreRole.Image || getDataStore().getRole() == DataStoreRole.ImageCache) {
            if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
                TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), getId());
                templateStoreRef.setInstallPath(newTemplate.getPath());
                templateStoreRef.setDownloadPercent(100);
                templateStoreRef.setDownloadState(Status.DOWNLOADED);
                templateStoreRef.setSize(newTemplate.getSize());
                if (newTemplate.getPhysicalSize() != null) {
                    templateStoreRef.setPhysicalSize(newTemplate.getPhysicalSize());
                }
                templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
                if (getDataStore().getRole() == DataStoreRole.Image) {
                    VMTemplateVO templateVO = imageDao.findById(getId());
                    if (newTemplate.getFormat() != null) {
                        templateVO.setFormat(newTemplate.getFormat());
                    }
                    if (newTemplate.getName() != null) {
                        // For template created from snapshot, template name is determine by resource code.
                        templateVO.setUniqueName(newTemplate.getName());
                    }
                    if (newTemplate.getHypervisorType() != null) {
                        templateVO.setHypervisorType(newTemplate.getHypervisorType());
                    }
                    templateVO.setSize(newTemplate.getSize());
                    imageDao.update(templateVO.getId(), templateVO);
                }
            }
        }
        objectInStoreMgr.update(this, event);
    } catch (NoTransitionException e) {
        s_logger.debug("failed to update state", e);
        throw new CloudRuntimeException("Failed to update state" + e.toString());
    } catch (Exception ex) {
        s_logger.debug("failed to process event and answer", ex);
        objectInStoreMgr.delete(this);
        throw new CloudRuntimeException("Failed to process event", ex);
    } finally {
        // in case of OperationFailed, expunge the entry
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
    }
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Example 24 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class TemplateObject method decRefCount.

@Override
public void decRefCount() {
    if (dataStore == null) {
        return;
    }
    if (dataStore.getRole() == DataStoreRole.Image || dataStore.getRole() == DataStoreRole.ImageCache) {
        TemplateDataStoreVO store = templateStoreDao.findByStoreTemplate(dataStore.getId(), getId());
        store.decrRefCnt();
        store.setLastUpdated(new Date());
        templateStoreDao.update(store.getId(), store);
    }
}
Also used : TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) Date(java.util.Date)

Example 25 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class TemplateDataFactoryImpl method listTemplateOnCache.

@Override
public List<TemplateInfo> listTemplateOnCache(long templateId) {
    List<TemplateDataStoreVO> cacheTmpls = templateStoreDao.listOnCache(templateId);
    List<TemplateInfo> tmplObjs = new ArrayList<TemplateInfo>();
    for (TemplateDataStoreVO cacheTmpl : cacheTmpls) {
        long storeId = cacheTmpl.getDataStoreId();
        DataStore store = storeMgr.getDataStore(storeId, DataStoreRole.ImageCache);
        TemplateInfo tmplObj = getTemplate(templateId, store);
        tmplObjs.add(tmplObj);
    }
    return tmplObjs;
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ArrayList(java.util.ArrayList) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Aggregations

TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)46 VMTemplateVO (com.cloud.storage.VMTemplateVO)22 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)22 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 Date (java.util.Date)13 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)7 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)7 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)7 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)6 DB (com.cloud.utils.db.DB)6 ExecutionException (java.util.concurrent.ExecutionException)6 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)6 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 ConfigurationException (javax.naming.ConfigurationException)5 ImageStoreEntity (org.apache.cloudstack.storage.image.datastore.ImageStoreEntity)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4