@Override public void addSystemVMTemplatesToSecondary(DataStore store) { long storeId = store.getId(); List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates(); for (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); tmpltStore.setPhysicalSize(0); // no size information for // pre-seeded system vm templates tmpltStore.setDataStoreRole(store.getRole()); _vmTemplateStoreDao.persist(tmpltStore); } } }
@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); } }
@Override public TemplateInfo getTemplate(long templateId, DataStoreRole storeRole) { TemplateDataStoreVO tmplStore = templateStoreDao.findByTemplate(templateId, storeRole); DataStore store = null; if (tmplStore != null) { store = storeMgr.getDataStore(tmplStore.getDataStoreId(), storeRole); } return this.getTemplate(templateId, store); }
/** * Persist template marking it for direct download to Primary Storage, skipping Secondary Storage */ private void persistDirectDownloadTemplate(long templateId, Long size) { TemplateDataStoreVO directDownloadEntry = templateDataStoreDao.createTemplateDirectDownloadEntry(templateId, size); templateDataStoreDao.persist(directDownloadEntry); }
@Override public Long getTemplateSize(long templateId, long zoneId) { if (_tmplStoreDao.isTemplateMarkedForDirectDownload(templateId)) { // check if template is marked for direct download return _tmplStoreDao.getReadyBypassedTemplate(templateId).getSize(); } TemplateDataStoreVO templateStoreRef = _tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, zoneId, VMTemplateStorageResourceAssoc.Status.DOWNLOADED); if (templateStoreRef == null) { // check if it is ready on image cache stores templateStoreRef = _tmplStoreDao.findByTemplateZoneStagingDownloadStatus(templateId, zoneId, VMTemplateStorageResourceAssoc.Status.DOWNLOADED); if (templateStoreRef == null) { throw new CloudRuntimeException("Template " + templateId + " has not been completely downloaded to zone " + zoneId); } } return templateStoreRef.getSize(); }
for (VMTemplateVO tmplt : allTemplates) { String uniqueName = tmplt.getUniqueName(); TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId()); if (templateInfos.containsKey(uniqueName)) { TemplateProp tmpltInfo = templateInfos.remove(uniqueName); } else { s_logger.info("Removing template_store_ref entry for corrupted template " + tmplt.getName()); _vmTemplateStoreDao.remove(tmpltStore.getId()); toBeDownloaded.add(tmplt); _vmTemplateStoreDao.update(tmpltStore.getId(), tmpltStore); } else { tmpltStore = new TemplateDataStoreVO(storeId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl()); tmpltStore.setPhysicalSize(tmpltInfo.getPhysicalSize()); tmpltStore.setDataStoreRole(store.getRole()); _vmTemplateStoreDao.persist(tmpltStore); tmpltStore.setErrorString(msg); tmpltStore.setState(State.Failed); _vmTemplateStoreDao.update(tmpltStore.getId(), tmpltStore); try { stateMachine.transitTo(tmplt, VirtualMachineTemplate.Event.OperationFailed, null, _templateDao); _vmTemplateStoreDao.remove(tmpltStore.getId()); TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId()); if (tmpltStore != null && tmpltStore.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED && tmpltStore.getState() == State.Ready
@Override public void downloadBootstrapSysTemplate(DataStore store) { Set<VMTemplateVO> toBeDownloaded = new HashSet(); List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates(); for (VMTemplateVO rtngTmplt : rtngTmplts) { toBeDownloaded.add(rtngTmplt); } 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); } /* Baremetal need not to download any template */ availHypers.remove(HypervisorType.BareMetal); availHypers.add(HypervisorType.None); // bug 9809: resume ISO // download. for (VMTemplateVO template : toBeDownloaded) { if (availHypers.contains(template.getHypervisorType())) { // only download sys template applicable for current hypervisor TemplateDataStoreVO tmpltHost = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId()); if (tmpltHost == null || tmpltHost.getState() != ObjectInDataStoreStateMachine.State.Ready) { TemplateInfo tmplt = _templateFactory.getTemplate(template.getId(), DataStoreRole.Image); createTemplateAsync(tmplt, store, null); } } } }
DataStore store = obj.getDataStore(); TemplateDataStoreVO tmpltStoreVO = _templateStoreDao.findByStoreTemplate(store.getId(), obj.getId()); if (tmpltStoreVO != null) { if (tmpltStoreVO.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) { TemplateDataStoreVO updateBuilder = _templateStoreDao.createForUpdate(); updateBuilder.setDownloadPercent(answer.getDownloadPct()); updateBuilder.setDownloadState(answer.getDownloadStatus()); updateBuilder.setSize(answer.getTemplateSize()); updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize()); _templateStoreDao.update(tmpltStoreVO.getId(), updateBuilder);
TemplateDataStoreVO templateStore = _tmplStoreDao.findByTemplate(firstCommand.getEntityId(), DataStoreRole.getRole(firstCommand.getDataToRole())); if (templateStore != null) { templateStore.setExtractUrl(url); _tmplStoreDao.persist(templateStore);
Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId()); String eventType = EventTypes.EVENT_TEMPLATE_DELETE; List<TemplateDataStoreVO> templateHostVOs = this._tmpltStoreDao.listByTemplate(templateId); lock = _tmpltStoreDao.acquireInLockTable(vo.getId()); if (lock == null) { s_logger.debug("Failed to acquire lock when deleting templateDataStoreVO with ID: " + vo.getId()); _tmpltStoreDao.update(vo.getId(), vo); _tmpltStoreDao.releaseFromLockTable(lock.getId()); if (success && (_tmpltStoreDao.listByTemplate(templateId).size() == 0)) { long accountId = template.getAccountId();
TemplateDataStoreVO destTmpltStore = templateDataStoreDao.findByStoreTemplate(dataStore.getId(), objId); if (destTmpltStore != null) { return templateDataStoreDao.remove(destTmpltStore.getId()); } else { s_logger.warn("Template " + objId + " is not found on image store " + dataStore.getId() + ", so no need to delete");
@Override public VMTemplateVO create(TemplateProfile profile) { VMTemplateVO template = persistTemplate(profile, State.Active); List<Long> zones = profile.getZoneIdList(); // create an entry at template_store_ref with store_id = null to represent that this template is ready for use. TemplateDataStoreVO vmTemplateHost = new TemplateDataStoreVO(null, template.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, null, template.getUrl()); this._tmpltStoreDao.persist(vmTemplateHost); if (zones == null) { List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved(); if (dcs != null && dcs.size() > 0) { templateCreateUsage(template, dcs.get(0).getId()); } } else { for (Long zoneId: zones) { templateCreateUsage(template, zoneId); } } _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template); return template; }
/** * Perform basic checkings to make sure restore is possible. If not, #InvalidParameterValueException is thrown. * * @param vm vm * @param template template * @throws InvalidParameterValueException if restore is not possible */ private void checkRestoreVmFromTemplate(UserVmVO vm, VMTemplateVO template) { TemplateDataStoreVO tmplStore; if (!template.isDirectDownload()) { tmplStore = _templateStoreDao.findByTemplateZoneReady(template.getId(), vm.getDataCenterId()); if (tmplStore == null) { throw new InvalidParameterValueException("Cannot restore the vm as the template " + template.getUuid() + " isn't available in the zone"); } } else { tmplStore = _templateStoreDao.findByTemplate(template.getId(), DataStoreRole.Image); if (tmplStore == null || (tmplStore != null && !tmplStore.getDownloadState().equals(VMTemplateStorageResourceAssoc.Status.BYPASSED))) { throw new InvalidParameterValueException("Cannot restore the vm as the bypassed template " + template.getUuid() + " isn't available in the zone"); } } }
List<TemplateDataStoreVO> templatesOnImageStoreList = _templateStoreDao.listTemplateDownloadUrls(); for (TemplateDataStoreVO templateOnImageStore : templatesOnImageStoreList) { _templateStoreDao.update(templateOnImageStore.getId(), templateOnImageStore); } catch (Throwable th) { s_logger.warn("caught exception while deleting download url " + templateOnImageStore.getExtractUrl() + " for template id " + templateOnImageStore.getTemplateId(), th);
@Override public void doInTransactionWithoutResult(TransactionStatus status) { VMTemplateVO tmpTemplate = _templateDao.findById(template.getId()); TemplateDataStoreVO tmpTemplateDataStore = _templateDataStoreDao.findById(templateDataStore.getId()); boolean sendAlert = false; String msg = null; _templateDataStoreDao.update(tmpTemplateDataStore.getId(), tmpTemplateDataStore); } catch (NoTransitionException e) { s_logger.error("Unexpected error " + e.getMessage());
@Override public DataStore getImageStore(long zoneId, long tmpltId) { TemplateDataStoreVO tmpltStore = _tmplStoreDao.findByTemplateZoneDownloadStatus(tmpltId, zoneId, VMTemplateStorageResourceAssoc.Status.DOWNLOADED); if (tmpltStore != null) { return _dataStoreMgr.getDataStore(tmpltStore.getDataStoreId(), DataStoreRole.Image); } return null; }
try { long storeId = store.getId(); List<TemplateDataStoreVO> destroyedTemplateStoreVOs = _templateStoreDao.listDestroyed(storeId); s_logger.debug("Secondary storage garbage collector found " + destroyedTemplateStoreVOs.size() + " templates to cleanup on template_store_ref for store: " + store.getName()); for (TemplateDataStoreVO destroyedTemplateStoreVO : destroyedTemplateStoreVOs) { s_logger.debug("Deleting template store DB entry: " + destroyedTemplateStoreVO); _templateStoreDao.remove(destroyedTemplateStoreVO.getId());
TemplateDataStoreVO srcTmpltStore = _tmplStoreDao.findByStoreTemplate(srcSecStore.getId(), tmpltId); TemplateDataStoreVO dstTmpltStore = _tmplStoreDao.findByStoreTemplate(dstSecStore.getId(), tmpltId); if (dstTmpltStore != null && dstTmpltStore.getDownloadState() == Status.DOWNLOADED) { return true; // already downloaded on this image store _tmplStoreDao.removeByTemplateStore(tmpltId, dstSecStore.getId());
ResourceCountSearch.done(); templateSizeSearch = _vmTemplateStoreDao.createSearchBuilder(SumCount.class); templateSizeSearch.select("sum", Func.SUM, templateSizeSearch.entity().getSize()); templateSizeSearch.and("downloadState", templateSizeSearch.entity().getDownloadState(), Op.EQ);
@Override public TemplateInfo getReadyTemplateOnImageStore(long templateId, Long zoneId) { TemplateDataStoreVO tmplStore = templateStoreDao.findByTemplateZoneReady(templateId, zoneId); if (tmplStore != null) { DataStore store = storeMgr.getDataStore(tmplStore.getDataStoreId(), DataStoreRole.Image); return this.getTemplate(templateId, store); } else { return null; } }